From 420e08fac8cb9102d281444b91ced65bd408e435 Mon Sep 17 00:00:00 2001 From: Jason Hall Date: Sat, 26 Apr 2014 20:31:05 -0400 Subject: [PATCH] add example -- pulling all issue covers of a series --- example/main.go | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ marvel.go | 2 +- 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 example/main.go diff --git a/example/main.go b/example/main.go new file mode 100644 index 0000000..b85a723 --- /dev/null +++ b/example/main.go @@ -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) + } +} diff --git a/marvel.go b/marvel.go index 7a2d202..af45d6c 100644 --- a/marvel.go +++ b/marvel.go @@ -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)