12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package main
- import (
- "fmt"
- "image/gif"
- "io"
- "net/http"
- "os"
- )
- func GrabPascoGif() error {
- resp, err := http.Get("https://cdn.tegna-media.com/wtsp/weather/radar/880x495/pasco30.gif")
- if err != nil {
- return fmt.Errorf("http.Get('%s') => %v", "https://cdn.tegna-media.com/wtsp/weather/radar/880x495/pasco30.gif", err)
- }
- defer resp.Body.Close()
- file, err := os.Create("pasco.gif")
- if err != nil {
- return fmt.Errorf("os.Create('pasco.gif') => %v", err)
- }
- defer file.Close()
- _, err = io.Copy(file, resp.Body)
- if err != nil {
- return fmt.Errorf("io.Copy => %v", err)
- }
- return nil
- }
- func GrabCentralFloridaGif() error {
- resp, err := http.Get("https://cdn.tegna-media.com/wtsp/weather/radar/880x495/central_florida.gif")
- if err != nil {
- return fmt.Errorf("http.Get('%s') => %v", "https://cdn.tegna-media.com/wtsp/weather/radar/880x495/central_florida.gif", err)
- }
- defer resp.Body.Close()
- file, err := os.Create("central_florida.gif")
- if err != nil {
- return fmt.Errorf("os.Create('central_florida.gif') => %v", err)
- }
- defer file.Close()
- _, err = io.Copy(file, resp.Body)
- if err != nil {
- return fmt.Errorf("io.Copy => %v", err)
- }
- return nil
- }
- func DecodeGif(filename string) (*gif.GIF, error) {
- file, err := os.Open(filename)
- if err != nil {
- return nil, fmt.Errorf("os.Open('%s') => %v", filename, err)
- }
- defer file.Close()
- g, err := gif.DecodeAll(file)
- if err != nil {
- return nil, fmt.Errorf("gif.DecodeAll => %v", err)
- }
- return g, nil
- }
- func EncodeGif(filename string, g *gif.GIF, separate ...bool) error {
- sep := false
- if len(separate) != 0 {
- sep = separate[0]
- }
- if !sep {
- file, err := os.Create(filename + ".gif")
- if err != nil {
- return fmt.Errorf("os.Create('%s') => %v", filename, err)
- }
- defer file.Close()
- err = gif.EncodeAll(file, g)
- if err != nil {
- return fmt.Errorf("gif.EncodeAll => %v", err)
- }
- return nil
- }
- for idx, frame := range g.Image {
- file, err := os.Create(fmt.Sprintf("%s-%d.png", filename, idx))
- if err != nil {
- return fmt.Errorf("os.Create('%s-%d.png') => %v", filename, idx, err)
- }
- defer file.Close()
- err = gif.Encode(file, frame, &gif.Options{
- NumColors: 256,
- })
- if err != nil {
- return fmt.Errorf("gif.Encode(%d) => %v", idx, err)
- }
- }
- return nil
- }
|