121 lines
2.5 KiB
Go
Raw Normal View History

2017-06-02 16:17:58 +02:00
package main
import (
2017-06-21 20:10:12 +02:00
"encoding/json"
2017-06-02 16:17:58 +02:00
"fmt"
"strings"
"github.com/animenotifier/arn"
"github.com/animenotifier/kitsu"
"github.com/fatih/color"
)
func main() {
color.Yellow("Syncing Anime")
// Get a stream of all anime
2017-06-27 12:39:41 +02:00
allAnime := kitsu.StreamAnime()
2017-06-02 16:17:58 +02:00
// Iterate over the stream
for anime := range allAnime {
sync(anime)
}
2017-06-04 22:06:14 +02:00
2017-06-28 00:16:45 +02:00
color.Green("Finished.")
2017-06-02 16:17:58 +02:00
}
func sync(data *kitsu.Anime) {
2017-06-28 17:55:08 +02:00
anime, err := arn.GetAnime(data.ID)
if err != nil {
if strings.Contains(err.Error(), "not found") {
anime = &arn.Anime{}
} else {
panic(err)
}
}
2017-06-04 22:06:14 +02:00
attr := data.Attributes
2017-06-02 16:17:58 +02:00
2017-06-08 11:51:34 +02:00
// General data
2017-06-04 22:06:14 +02:00
anime.ID = data.ID
anime.Type = strings.ToLower(attr.ShowType)
anime.Title.Canonical = attr.CanonicalTitle
anime.Title.English = attr.Titles.En
anime.Title.Romaji = attr.Titles.EnJp
anime.Title.Synonyms = attr.AbbreviatedTitles
anime.Image.Tiny = kitsu.FixImageURL(attr.PosterImage.Tiny)
anime.Image.Small = kitsu.FixImageURL(attr.PosterImage.Small)
anime.Image.Large = kitsu.FixImageURL(attr.PosterImage.Large)
anime.Image.Original = kitsu.FixImageURL(attr.PosterImage.Original)
anime.StartDate = attr.StartDate
anime.EndDate = attr.EndDate
anime.EpisodeCount = attr.EpisodeCount
anime.EpisodeLength = attr.EpisodeLength
2017-06-06 22:08:43 +02:00
anime.Status = attr.Status
2017-06-04 22:06:14 +02:00
anime.Summary = arn.FixAnimeDescription(attr.Synopsis)
2017-06-02 16:17:58 +02:00
2017-06-28 17:55:08 +02:00
if anime.Mappings == nil {
anime.Mappings = []*arn.Mapping{}
}
2017-06-28 19:30:09 +02:00
if anime.Episodes == nil {
anime.Episodes = []*arn.AnimeEpisode{}
}
2017-06-29 07:20:55 +02:00
// Prefer Shoboi Japanese titles over Kitsu JP titles
if anime.GetMapping("shoboi/anime") != "" {
// Only take Kitsu title when our JP title is empty
if anime.Title.Japanese == "" {
anime.Title.Japanese = attr.Titles.JaJp
}
} else {
// Update JP title with Kitsu JP title
anime.Title.Japanese = attr.Titles.JaJp
}
2017-06-27 16:23:57 +02:00
// NSFW
if attr.Nsfw {
anime.NSFW = 1
} else {
anime.NSFW = 0
}
2017-06-08 11:51:34 +02:00
// Rating
2017-06-30 20:00:56 +02:00
if anime.Rating == nil {
anime.Rating = &arn.AnimeRating{}
2017-06-08 11:51:34 +02:00
}
2017-06-30 20:00:56 +02:00
if anime.Rating.IsNotRated() {
anime.Rating.Reset()
}
2017-06-08 11:51:34 +02:00
// Trailers
2017-06-27 16:23:57 +02:00
anime.Trailers = []*arn.ExternalMedia{}
2017-06-21 20:10:12 +02:00
2017-06-06 22:08:43 +02:00
if attr.YoutubeVideoID != "" {
2017-06-27 16:23:57 +02:00
anime.Trailers = append(anime.Trailers, &arn.ExternalMedia{
2017-06-27 13:46:29 +02:00
Service: "Youtube",
ServiceID: attr.YoutubeVideoID,
2017-06-02 16:17:58 +02:00
})
}
2017-06-08 11:51:34 +02:00
// Save in database
2017-06-28 17:55:08 +02:00
err = anime.Save()
2017-06-02 16:17:58 +02:00
status := ""
if err == nil {
status = color.GreenString("✔")
} else {
2017-06-21 20:10:12 +02:00
color.Red(err.Error())
data, _ := json.MarshalIndent(anime, "", "\t")
fmt.Println(string(data))
2017-06-02 16:17:58 +02:00
status = color.RedString("✘")
}
2017-06-08 11:51:34 +02:00
// Log
2017-06-02 16:17:58 +02:00
fmt.Println(status, anime.ID, anime.Title.Canonical)
}