mirror of
https://github.com/imjasonh/go-marvel
synced 2026-07-11 07:30:11 +00:00
add example -- pulling all issue covers of a series
This commit is contained in:
parent
b1236026ea
commit
420e08fac8
2 changed files with 64 additions and 1 deletions
63
example/main.go
Normal file
63
example/main.go
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
marvel "github.com/ImJasonH/go-marvel"
|
||||
)
|
||||
|
||||
var (
|
||||
seriesID = flag.Int64("series", 2258, "Series ID (default: Uncanny X-Men)")
|
||||
apiKey = flag.String("pub", "", "Public API key")
|
||||
secret = flag.String("priv", "", "Private API secret")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
c := marvel.NewClient(*apiKey, *secret)
|
||||
|
||||
offset := 0
|
||||
limit := 100
|
||||
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("%d - %s\n", iss.IssueNumber, iss.Thumbnail.URL(marvel.PortraitIncredible))
|
||||
}
|
||||
if len(r.Data.Results) < limit {
|
||||
return
|
||||
}
|
||||
offset += limit
|
||||
}
|
||||
}
|
||||
|
||||
func fetchImage(num int, url string) {
|
||||
f, err := os.Create(fmt.Sprintf("%d.jpg", num))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
fmt.Printf("error: %s -> %d\n", url, resp.StatusCode)
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
_, err = io.Copy(f, resp.Body)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
|
@ -122,7 +122,7 @@ func (c Client) Series(id int64, params CommonParams) (resp struct {
|
|||
commonResponse
|
||||
Data struct {
|
||||
commonList
|
||||
Results []Series
|
||||
Results []Comic
|
||||
}
|
||||
}, err error) {
|
||||
u := c.baseURL(fmt.Sprintf("series/%d/comics", id), params)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue