149 lines
3.3 KiB
Go
Raw Normal View History

2017-06-02 14:17:58 +00:00
package main
import (
"fmt"
"strings"
"github.com/animenotifier/arn"
"github.com/animenotifier/kitsu"
"github.com/fatih/color"
)
func main() {
color.Yellow("Syncing Anime")
2017-11-01 09:28:40 +00:00
defer arn.Node.Close()
2017-06-02 14:17:58 +00:00
// In case we refresh only one anime
if InvokeShellArgs() {
color.Green("Finished.")
return
}
2017-06-02 14:17:58 +00:00
// Get a stream of all anime
2017-07-07 13:44:06 +00:00
allAnime := kitsu.StreamAnimeWithMappings()
2017-06-02 14:17:58 +00:00
// Iterate over the stream
for anime := range allAnime {
sync(anime)
}
2017-06-04 20:06:14 +00:00
2017-06-27 22:16:45 +00:00
color.Green("Finished.")
2017-06-02 14:17:58 +00:00
}
func sync(data *kitsu.Anime) *arn.Anime {
2017-06-28 15:55:08 +00:00
anime, err := arn.GetAnime(data.ID)
if err != nil {
if strings.Contains(err.Error(), "not found") {
2017-07-05 21:25:39 +00:00
anime = &arn.Anime{
Title: &arn.AnimeTitle{},
Image: &arn.AnimeImageTypes{},
}
2017-06-28 15:55:08 +00:00
} else {
panic(err)
}
}
2017-06-04 20:06:14 +00:00
attr := data.Attributes
2017-06-02 14:17:58 +00:00
2017-06-08 09:51:34 +00:00
// General data
2017-06-04 20:06:14 +00: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 20:08:43 +00:00
anime.Status = attr.Status
2017-06-04 20:06:14 +00:00
anime.Summary = arn.FixAnimeDescription(attr.Synopsis)
2017-06-02 14:17:58 +00:00
2017-06-28 15:55:08 +00:00
if anime.Mappings == nil {
anime.Mappings = []*arn.Mapping{}
}
2017-06-29 05:20:55 +00: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-07-07 13:44:06 +00:00
// Import mappings
for _, mapping := range data.Mappings {
switch mapping.Attributes.ExternalSite {
case "myanimelist/anime":
anime.AddMapping("myanimelist/anime", mapping.Attributes.ExternalID, "")
case "anidb":
anime.AddMapping("anidb/anime", mapping.Attributes.ExternalID, "")
case "thetvdb/series":
anime.AddMapping("thetvdb/anime", mapping.Attributes.ExternalID, "")
case "thetvdb/season":
// Ignore
default:
color.Yellow("Unknown mapping: %s %s", mapping.Attributes.ExternalSite, mapping.Attributes.ExternalID)
}
}
2017-06-27 14:23:57 +00:00
// NSFW
if attr.Nsfw {
anime.NSFW = 1
} else {
anime.NSFW = 0
}
2017-06-08 09:51:34 +00:00
// Rating
2017-06-30 18:00:56 +00:00
if anime.Rating == nil {
anime.Rating = &arn.AnimeRating{}
2017-06-08 09:51:34 +00:00
}
2017-06-30 18:00:56 +00:00
if anime.Rating.IsNotRated() {
anime.Rating.Reset()
}
2017-06-08 09:51:34 +00:00
2017-09-30 14:04:13 +00:00
// Popularity
if anime.Popularity == nil {
anime.Popularity = &arn.AnimePopularity{}
}
2017-06-08 09:51:34 +00:00
// Trailers
2017-06-27 14:23:57 +00:00
anime.Trailers = []*arn.ExternalMedia{}
2017-06-21 18:10:12 +00:00
2017-06-06 20:08:43 +00:00
if attr.YoutubeVideoID != "" {
2017-06-27 14:23:57 +00:00
anime.Trailers = append(anime.Trailers, &arn.ExternalMedia{
2017-06-27 11:46:29 +00:00
Service: "Youtube",
ServiceID: attr.YoutubeVideoID,
2017-06-02 14:17:58 +00:00
})
}
2017-06-08 09:51:34 +00:00
// Save in database
2017-11-01 09:28:40 +00:00
anime.Save()
2017-06-02 14:17:58 +00:00
2017-07-10 22:56:18 +00:00
// Episodes
episodes, err := arn.GetAnimeEpisodes(anime.ID)
if err != nil || episodes == nil {
2017-11-07 11:35:32 +00:00
episodes := &arn.AnimeEpisodes{
AnimeID: anime.ID,
Items: []*arn.AnimeEpisode{},
}
arn.DB.Set("AnimeEpisodes", anime.ID, episodes)
2017-07-10 22:56:18 +00:00
}
2017-06-08 09:51:34 +00:00
// Log
2017-11-01 09:28:40 +00:00
fmt.Println(color.GreenString("✔"), anime.ID, anime.Title.Canonical)
return anime
2017-06-02 14:17:58 +00:00
}