mirror of
https://github.com/imjasonh/go-marvel
synced 2026-07-20 21:28:23 +00:00
skip image_not_available images
This commit is contained in:
parent
13f16dda48
commit
7edb63b950
1 changed files with 16 additions and 12 deletions
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"image/jpeg"
|
"image/jpeg"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
marvel "github.com/ImJasonH/go-marvel"
|
marvel "github.com/ImJasonH/go-marvel"
|
||||||
)
|
)
|
||||||
|
|
@ -34,14 +35,13 @@ func main() {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
for _, iss := range r.Data.Results {
|
for _, iss := range r.Data.Results {
|
||||||
img, err := fetchImage(iss.Thumbnail.URL(marvel.PortraitIncredible))
|
url := iss.Thumbnail.URL(marvel.PortraitIncredible)
|
||||||
if err != nil {
|
img := fetchImage(url)
|
||||||
fmt.Printf("error: %v", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if img != nil {
|
if img != nil {
|
||||||
imgs = append(imgs, img)
|
imgs = append(imgs, img)
|
||||||
fmt.Printf("fetched %v - %s\n", *iss.IssueNumber, iss.Thumbnail.URL(marvel.PortraitIncredible))
|
fmt.Printf("fetched %v - %s\n", *iss.IssueNumber, url)
|
||||||
|
} else {
|
||||||
|
fmt.Printf("skipped %s\n", url)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if len(r.Data.Results) < limit {
|
if len(r.Data.Results) < limit {
|
||||||
|
|
@ -55,26 +55,30 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func fetchImage(url string) (image.Image, error) {
|
func fetchImage(url string) image.Image {
|
||||||
|
if strings.Contains(url, "image_not_available") {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
resp, err := http.Get(url)
|
resp, err := http.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil
|
||||||
}
|
}
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
return nil, fmt.Errorf("error: %s -> %d\n", url, resp.StatusCode)
|
return nil
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
b := bufio.NewReaderSize(resp.Body, 1)
|
b := bufio.NewReaderSize(resp.Body, 1)
|
||||||
if _, err := b.Peek(1); err == bufio.ErrBufferFull {
|
if _, err := b.Peek(1); err == bufio.ErrBufferFull {
|
||||||
return nil, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
img, err := jpeg.Decode(b)
|
img, err := jpeg.Decode(b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil
|
return nil
|
||||||
}
|
}
|
||||||
return img, err
|
return img
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeGIF(filename string, imgs []image.Image) error {
|
func writeGIF(filename string, imgs []image.Image) error {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue