diff --git a/images/anime/.gitignore b/images/anime/.gitignore new file mode 100644 index 00000000..c96a04f0 --- /dev/null +++ b/images/anime/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file diff --git a/jobs/anime-images/anime-images.go b/jobs/anime-images/anime-images.go new file mode 100644 index 00000000..f92c4c84 --- /dev/null +++ b/jobs/anime-images/anime-images.go @@ -0,0 +1,57 @@ +package main + +import ( + "fmt" + "io/ioutil" + "net/http" + "strings" + "time" + + "github.com/aerogo/flow/jobqueue" + "github.com/animenotifier/arn" + "github.com/fatih/color" + "github.com/parnurzeal/gorequest" +) + +var ticker = time.NewTicker(50 * time.Millisecond) + +func main() { + color.Yellow("Downloading anime images") + jobs := jobqueue.New(work) + allAnime, _ := arn.AllAnime() + + for _, anime := range allAnime { + jobs.Queue(anime) + } + + results := jobs.Wait() + color.Green("Finished downloading %d anime images.", len(results)) +} + +func work(job interface{}) interface{} { + anime := job.(*arn.Anime) + + if !strings.HasPrefix(anime.Image.Original, "https://media.kitsu.io/anime/") { + return nil + } + + <-ticker.C + resp, body, errs := gorequest.New().Get(anime.Image.Original).End() + + if len(errs) > 0 { + color.Red(errs[0].Error()) + return errs[0] + } + + if resp.StatusCode != http.StatusOK { + color.Red("Status %d", resp.StatusCode) + } + + extension := anime.Image.Original[strings.LastIndex(anime.Image.Original, "."):] + fileName := "anime/" + anime.ID + extension + fmt.Println(fileName) + + ioutil.WriteFile(fileName, []byte(body), 0644) + + return nil +}