Started working on anime images
This commit is contained in:
parent
6393e91cc2
commit
26422408ce
2
images/anime/.gitignore
vendored
Normal file
2
images/anime/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
*
|
||||||
|
!.gitignore
|
57
jobs/anime-images/anime-images.go
Normal file
57
jobs/anime-images/anime-images.go
Normal file
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user