60 lines
1.1 KiB
Go
Raw Normal View History

2017-10-21 03:28:52 +00:00
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")
2017-11-01 19:12:38 +00:00
defer arn.Node.Close()
2017-10-21 03:28:52 +00:00
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)
2017-10-23 23:32:12 +00:00
if !strings.HasPrefix(anime.Image.Original, "//media.kitsu.io/anime/") {
2017-10-21 03:28:52 +00:00
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
}