121 lines
2.5 KiB
Go
Raw Normal View History

2017-06-29 05:02:09 +00:00
package main
import (
"time"
2019-04-23 05:52:55 +00:00
"github.com/akyoto/color"
2019-06-03 09:32:43 +00:00
"github.com/animenotifier/notify.moe/arn"
2017-06-29 05:02:09 +00:00
"github.com/animenotifier/shoboi"
)
func main() {
color.Yellow("Syncing Shoboi Anime")
2018-03-28 22:26:19 +00:00
defer color.Green("Finished.")
2017-11-01 09:28:40 +00:00
defer arn.Node.Close()
2017-06-29 05:02:09 +00:00
2017-10-02 14:39:23 +00:00
// Priority queues
highPriority := []*arn.Anime{}
mediumPriority := []*arn.Anime{}
lowPriority := []*arn.Anime{}
2017-06-29 05:02:09 +00:00
2017-11-01 09:28:40 +00:00
for anime := range arn.StreamAnime() {
2017-10-02 14:39:23 +00:00
if anime.GetMapping("shoboi/anime") != "" {
continue
2017-06-29 05:02:09 +00:00
}
2017-09-24 03:01:15 +00:00
2017-10-02 14:39:23 +00:00
switch anime.Status {
case "current":
highPriority = append(highPriority, anime)
case "upcoming":
mediumPriority = append(mediumPriority, anime)
default:
lowPriority = append(lowPriority, anime)
}
2017-06-29 05:02:09 +00:00
}
2017-10-02 14:39:23 +00:00
color.Cyan("High priority queue (%d):", len(highPriority))
refreshQueue(highPriority)
color.Cyan("Medium priority queue (%d):", len(mediumPriority))
refreshQueue(mediumPriority)
color.Cyan("Low priority queue (%d):", len(lowPriority))
refreshQueue(lowPriority)
2017-06-29 05:02:09 +00:00
// This is a lazy hack: Wait 5 minutes for goroutines to finish their remaining work.
time.Sleep(5 * time.Minute)
}
2017-10-02 14:39:23 +00:00
func refreshQueue(queue []*arn.Anime) {
count := 0
for _, anime := range queue {
if sync(anime) {
2017-11-01 09:28:40 +00:00
anime.Save()
2017-10-02 14:39:23 +00:00
count++
}
}
color.Green("Added Shoboi IDs for %d anime", count)
}
2017-06-29 05:02:09 +00:00
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
2017-07-02 12:03:56 +00:00
print(anime.ID + " | [JP] " + anime.Title.Japanese + " | [EN] " + anime.Title.Canonical)
2017-06-29 05:02:09 +00:00
// 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("✔"))
return true
}
println(color.RedString("✘"))
return false
}
// Search for a specific title
func search(anime *arn.Anime, title string) {
2017-06-29 05:09:29 +00:00
shoboi, err := shoboi.SearchAnime(title)
2017-06-29 05:02:09 +00:00
if err != nil {
color.Red(err.Error())
return
}
2017-06-29 05:09:29 +00:00
if shoboi == nil {
2017-06-29 05:02:09 +00:00
return
}
2017-06-29 05:09:29 +00:00
// Copy titles
if shoboi.TitleJapanese != "" {
anime.Title.Japanese = shoboi.TitleJapanese
}
if shoboi.TitleHiragana != "" {
anime.Title.Hiragana = shoboi.TitleHiragana
}
if shoboi.FirstChannel != "" {
anime.FirstChannel = shoboi.FirstChannel
}
2017-06-29 05:02:09 +00:00
// This will start a goroutine that saves the anime
2018-03-21 04:15:03 +00:00
anime.SetMapping("shoboi/anime", shoboi.TID)
2017-06-29 05:02:09 +00:00
}