Added jikan sync job

This commit is contained in:
Eduard Urbach 2017-11-12 17:40:25 +01:00
parent aadd4187a0
commit 6f945febf7

View File

@ -0,0 +1,57 @@
package main
import (
"fmt"
"time"
"github.com/animenotifier/jikan"
"github.com/animenotifier/arn"
"github.com/fatih/color"
)
const maxRetries = 3
var jikanDB = arn.Node.Namespace("jikan")
func main() {
color.Yellow("Syncing with Jikan API")
defer arn.Node.Close()
count := 0
for anime := range arn.StreamAnime() {
malID := anime.GetMapping("myanimelist/anime")
if malID != "" {
sync(anime, malID)
count++
}
}
color.Green("Finished syncing %d anime.", count)
// Give OS some time to write buffers, just to be safe
time.Sleep(10 * time.Second)
}
func sync(anime *arn.Anime, malID string) {
fmt.Printf("%s %s (MAL: %s)\n", anime.ID, anime.Title.Canonical, malID)
if !jikanDB.Exists("Anime", malID) {
var anime *jikan.Anime
var err error
for try := 1; try <= maxRetries; try++ {
time.Sleep(1 * time.Second)
anime, err = jikan.GetAnime(malID)
if err == nil {
jikanDB.Set("Anime", malID, anime)
return
}
fmt.Printf("Error fetching %s on try %d: %v", malID, try, err)
}
}
}