Added character sync
This commit is contained in:
parent
c899cea62f
commit
880cd5bd53
@ -43,7 +43,7 @@ func sync(anime *arn.Anime, malID string) {
|
||||
var err error
|
||||
|
||||
for try := 1; try <= maxRetries; try++ {
|
||||
time.Sleep(1 * time.Second)
|
||||
time.Sleep(time.Second)
|
||||
anime, err = jikan.GetAnime(malID)
|
||||
|
||||
if err == nil {
|
||||
@ -52,6 +52,9 @@ func sync(anime *arn.Anime, malID string) {
|
||||
}
|
||||
|
||||
fmt.Printf("Error fetching %s on try %d: %v", malID, try, err)
|
||||
|
||||
// Wait an additional second
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
71
jobs/sync-jikan-characters/sync-jikan-characters.go
Normal file
71
jobs/sync-jikan-characters/sync-jikan-characters.go
Normal file
@ -0,0 +1,71 @@
|
||||
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 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)
|
||||
|
||||
if !jikanDB.Exists("Character", malCharacterID) {
|
||||
var character *jikan.Character
|
||||
var err error
|
||||
|
||||
for try := 1; try <= maxRetries; try++ {
|
||||
time.Sleep(time.Second)
|
||||
character, err = jikan.GetCharacter(malCharacterID)
|
||||
|
||||
if err == nil {
|
||||
jikanDB.Set("Character", malCharacterID, character)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Error fetching %s on try %d: %v", malCharacterID, try, err)
|
||||
|
||||
// Wait an additional second
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user