mirror of
https://github.com/imjasonh/go-marvel
synced 2026-07-08 19:24:57 +00:00
update example to construct a GIF
This commit is contained in:
parent
0e6a6ec153
commit
bac15e00ae
2 changed files with 35 additions and 19 deletions
|
|
@ -1,3 +1,3 @@
|
|||
**go-marvel** is a [Go](https://golang.org) client library for the [Marvel REST API](https://developer.marvel.com/)
|
||||
|
||||
See [`example/main.go`](https://github.com/ImJasonH/go-marvel/blob/master/example/main.go), which downloads all issue covers of a series, by series ID (default is "Uncanny X-Men")
|
||||
See [`example/main.go`](https://github.com/ImJasonH/go-marvel/blob/master/example/main.go), which downloads all issue covers of a series, by series ID (default is "Uncanny X-Men"), and begins to construct a GIF image of them. (TODO)
|
||||
|
|
|
|||
|
|
@ -3,7 +3,9 @@ package main
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"image"
|
||||
"image/gif"
|
||||
"image/jpeg"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
|
|
@ -23,41 +25,55 @@ func main() {
|
|||
|
||||
offset := 0
|
||||
limit := 100
|
||||
imgs := []image.Image{}
|
||||
for {
|
||||
r, err := c.Series(*seriesID, marvel.CommonParams{offset, limit})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for _, iss := range r.Data.Results {
|
||||
fetchImage(*iss.IssueNumber, iss.Thumbnail.URL(marvel.PortraitIncredible))
|
||||
fmt.Printf("%v - %s\n", *iss.IssueNumber, iss.Thumbnail.URL(marvel.PortraitIncredible))
|
||||
img, err := fetchImage(iss.Thumbnail.URL(marvel.PortraitIncredible))
|
||||
if err != nil {
|
||||
fmt.Printf("error: %v", err)
|
||||
return
|
||||
}
|
||||
imgs = append(imgs, img)
|
||||
fmt.Printf("fetched %v - %s\n", *iss.IssueNumber, iss.Thumbnail.URL(marvel.PortraitIncredible))
|
||||
}
|
||||
if len(r.Data.Results) < limit {
|
||||
return
|
||||
break
|
||||
}
|
||||
offset += limit
|
||||
}
|
||||
|
||||
if err := writeGIF(fmt.Sprintf("%d.gif", *seriesID), imgs); err != nil {
|
||||
fmt.Printf("error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func fetchImage(num float64, url string) {
|
||||
f, err := os.Create(fmt.Sprintf("%v.jpg", num))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
func fetchImage(url string) (image.Image, error) {
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
fmt.Printf("error: %s -> %d\n", url, resp.StatusCode)
|
||||
return
|
||||
return nil, fmt.Errorf("error: %s -> %d\n", url, resp.StatusCode)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
_, err = io.Copy(f, resp.Body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
img, err := jpeg.Decode(resp.Body)
|
||||
return img, err
|
||||
}
|
||||
|
||||
func writeGIF(filename string, imgs []image.Image) error {
|
||||
f, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
pimgs := []*image.Paletted{}
|
||||
// TODO: convert imgs into pimgs
|
||||
|
||||
return gif.EncodeAll(f, &gif.GIF{Image:pimgs})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue