mirror of
https://github.com/imjasonh/go-marvel
synced 2026-07-16 12:31: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/)
|
**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 (
|
import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"image"
|
||||||
|
"image/gif"
|
||||||
|
"image/jpeg"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
|
@ -23,41 +25,55 @@ func main() {
|
||||||
|
|
||||||
offset := 0
|
offset := 0
|
||||||
limit := 100
|
limit := 100
|
||||||
|
imgs := []image.Image{}
|
||||||
for {
|
for {
|
||||||
r, err := c.Series(*seriesID, marvel.CommonParams{offset, limit})
|
r, err := c.Series(*seriesID, marvel.CommonParams{offset, limit})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
for _, iss := range r.Data.Results {
|
for _, iss := range r.Data.Results {
|
||||||
fetchImage(*iss.IssueNumber, iss.Thumbnail.URL(marvel.PortraitIncredible))
|
img, err := fetchImage(iss.Thumbnail.URL(marvel.PortraitIncredible))
|
||||||
fmt.Printf("%v - %s\n", *iss.IssueNumber, 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 {
|
if len(r.Data.Results) < limit {
|
||||||
return
|
break
|
||||||
}
|
}
|
||||||
offset += limit
|
offset += limit
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := writeGIF(fmt.Sprintf("%d.gif", *seriesID), imgs); err != nil {
|
||||||
|
fmt.Printf("error: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchImage(num float64, url string) {
|
func fetchImage(url string) (image.Image, error) {
|
||||||
f, err := os.Create(fmt.Sprintf("%v.jpg", num))
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
resp, err := http.Get(url)
|
resp, err := http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return nil, err
|
||||||
}
|
}
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
fmt.Printf("error: %s -> %d\n", url, resp.StatusCode)
|
return nil, fmt.Errorf("error: %s -> %d\n", url, resp.StatusCode)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
_, err = io.Copy(f, resp.Body)
|
img, err := jpeg.Decode(resp.Body)
|
||||||
if err != nil {
|
return img, err
|
||||||
panic(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