Added shoboi sync

This commit is contained in:
Eduard Urbach 2017-06-29 07:02:09 +02:00
parent d8828cf8a3
commit 2b8503f255

80
jobs/sync-shoboi/main.go Normal file
View File

@ -0,0 +1,80 @@
package main
import (
"os"
"time"
"github.com/animenotifier/arn"
"github.com/animenotifier/shoboi"
"github.com/fatih/color"
)
func main() {
color.Yellow("Syncing Shoboi Anime")
// Get a slice of all anime
allAnime, _ := arn.AllAnime()
// Iterate over the slice
count := 0
for _, anime := range allAnime {
if sync(anime) {
count++
}
}
// Log
color.Green("Successfully added Shoboi IDs for %d anime", count)
// This is a lazy hack: Wait 5 minutes for goroutines to finish their remaining work.
time.Sleep(5 * time.Minute)
color.Green("Finished.")
}
func sync(anime *arn.Anime) bool {
// If we already have the ID, nothing to do here
if anime.GetMapping("shoboi/anime") != "" {
return false
}
// Log ID and title
os.Stdout.Write([]byte(anime.ID + " | [JP] " + anime.Title.Japanese + " | [EN] " + anime.Title.English))
// Search Japanese title
if anime.GetMapping("shoboi/anime") == "" && anime.Title.Japanese != "" {
search(anime, anime.Title.Japanese)
}
// Search English title
if anime.GetMapping("shoboi/anime") == "" && anime.Title.English != "" {
search(anime, anime.Title.English)
}
// Did we get the ID?
if anime.GetMapping("shoboi/anime") != "" {
println(color.GreenString("✔"))
time.Sleep(2 * time.Second)
return true
}
println(color.RedString("✘"))
return false
}
// Search for a specific title
func search(anime *arn.Anime, title string) {
tid, err := shoboi.SearchAnime(title)
if err != nil {
color.Red(err.Error())
return
}
if tid == "" {
return
}
// This will start a goroutine that saves the anime
anime.AddMapping("shoboi/anime", tid, "")
}