88 lines
1.6 KiB
Go
Raw Normal View History

2018-04-04 16:59:20 +00:00
package main
import (
"flag"
"fmt"
"net/http"
"runtime"
"strconv"
"time"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"github.com/aerogo/http/client"
2019-04-23 05:45:17 +00:00
"github.com/akyoto/color"
2019-06-03 09:32:43 +00:00
"github.com/animenotifier/notify.moe/arn"
2018-04-04 16:59:20 +00:00
)
var ticker = time.NewTicker(50 * time.Millisecond)
// Shell parameters
var from int
var to int
// Shell flags
func init() {
flag.IntVar(&from, "from", 0, "From index")
flag.IntVar(&to, "to", 0, "To index")
flag.Parse()
}
func main() {
color.Yellow("Downloading anime images")
defer color.Green("Finished.")
defer arn.Node.Close()
if from < 0 {
from = 0
}
allAnime := arn.FilterAnime(func(anime *arn.Anime) bool {
id, _ := strconv.Atoi(anime.GetMapping("kitsu/anime"))
return id >= from && id <= to
})
for index, anime := range allAnime {
fmt.Printf("%d / %d\n", index+1, len(allAnime))
2019-06-05 06:45:54 +00:00
err := work(anime)
if err != nil {
color.Red(err.Error())
}
2018-04-04 16:59:20 +00:00
}
// Give file buffers some time, just to be safe
time.Sleep(time.Second)
}
func work(anime *arn.Anime) error {
<-ticker.C
kitsuOriginal := fmt.Sprintf("https://media.kitsu.io/anime/poster_images/%s/original%s", anime.GetMapping("kitsu/anime"), anime.Image.Extension)
// Download kitsu image
response, err := client.Get(kitsuOriginal).End()
if err != nil {
2019-06-05 06:45:54 +00:00
return fmt.Errorf("%s (%s)", err.Error(), kitsuOriginal)
2018-04-04 16:59:20 +00:00
}
if response.StatusCode() != http.StatusOK {
2019-06-05 06:45:54 +00:00
return fmt.Errorf("Status %d (%s)", response.StatusCode(), kitsuOriginal)
2018-04-04 16:59:20 +00:00
}
2019-06-05 06:45:54 +00:00
err = anime.SetImageBytes(response.Bytes())
if err != nil {
return err
}
2018-04-04 16:59:20 +00:00
// Try to free up some memory
runtime.GC()
return nil
}