64 lines
1.1 KiB
Go
Raw Normal View History

2017-11-13 19:42:53 +00:00
package main
import (
"fmt"
"time"
"github.com/animenotifier/jikan"
"github.com/animenotifier/arn"
"github.com/fatih/color"
)
var jikanDB = arn.Node.Namespace("jikan")
func main() {
color.Yellow("Syncing characters with Jikan API")
defer arn.Node.Close()
allAnime := jikanDB.All("Anime")
count := 0
for animeObj := range allAnime {
anime := animeObj.(*jikan.Anime)
if len(anime.Character) == 0 {
continue
}
fmt.Println(anime.Title)
for _, character := range anime.Character {
characterID := jikan.GetCharacterIDFromURL(character.URL)
if characterID == "" {
fmt.Println("Invalid character ID")
continue
}
fetchCharacter(characterID)
}
}
color.Green("Finished syncing %d characters.", count)
}
func fetchCharacter(malCharacterID string) {
fmt.Printf("Fetching character ID %s\n", malCharacterID)
2017-11-13 20:38:16 +00:00
if jikanDB.Exists("Character", malCharacterID) {
return
}
2017-11-13 19:42:53 +00:00
2017-11-13 20:38:16 +00:00
time.Sleep(time.Second)
character, err := jikan.GetCharacter(malCharacterID)
2017-11-13 19:42:53 +00:00
2017-11-13 20:38:16 +00:00
if err == nil {
jikanDB.Set("Character", malCharacterID, character)
return
2017-11-13 19:42:53 +00:00
}
2017-11-13 20:38:16 +00:00
2017-11-13 21:17:26 +00:00
fmt.Printf("Error fetching %s: %v\n", malCharacterID, err)
2017-11-13 19:42:53 +00:00
}