166 lines
3.6 KiB
Go
Raw Normal View History

2017-06-02 16:17:58 +02:00
package main
import (
"fmt"
2017-11-09 18:10:10 +01:00
"path/filepath"
2017-06-02 16:17:58 +02:00
"strings"
"github.com/animenotifier/arn"
"github.com/animenotifier/kitsu"
"github.com/fatih/color"
)
func main() {
color.Yellow("Syncing Anime")
2017-11-11 12:45:17 +01:00
2017-11-01 10:28:40 +01:00
defer arn.Node.Close()
2017-11-11 12:45:17 +01:00
defer color.Green("Finished.")
2017-06-02 16:17:58 +02:00
// In case we refresh only one anime
if InvokeShellArgs() {
return
}
2017-06-02 16:17:58 +02:00
// Get a stream of all anime
2017-07-07 15:44:06 +02:00
allAnime := kitsu.StreamAnimeWithMappings()
2017-06-02 16:17:58 +02:00
// Iterate over the stream
for anime := range allAnime {
sync(anime)
}
}
2018-03-09 16:27:40 +01:00
func sync(data *kitsu.Anime) *arn.Anime {
2017-06-28 17:55:08 +02:00
anime, err := arn.GetAnime(data.ID)
2018-03-09 05:36:43 +01:00
// This stops overwriting existing data
2018-03-09 16:27:40 +01:00
if anime != nil {
return anime
2018-03-09 05:36:43 +01:00
}
2017-06-28 17:55:08 +02:00
if err != nil {
if strings.Contains(err.Error(), "not found") {
2017-07-05 23:25:39 +02:00
anime = &arn.Anime{
Title: &arn.AnimeTitle{},
}
2017-06-28 17:55:08 +02:00
} 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.StartDate = attr.StartDate
anime.EndDate = attr.EndDate
anime.EpisodeCount = attr.EpisodeCount
anime.EpisodeLength = attr.EpisodeLength
anime.Status = attr.Status
2017-11-19 13:03:12 +01:00
anime.ImageExtension = filepath.Ext(kitsu.FixImageURL(attr.PosterImage.Original))
2017-11-11 12:45:17 +01:00
// Status "unreleased" means the same as "upcoming" so we should normalize it
if anime.Status == "unreleased" {
anime.Status = "upcoming"
}
2017-11-19 13:03:12 +01:00
// Normalize image extension to .jpg if .jpeg is used
if anime.ImageExtension == ".jpeg" {
anime.ImageExtension = ".jpg"
}
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-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-07-07 15:44:06 +02: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, "")
2017-11-09 20:03:25 +01:00
case "thetvdb", "thetvdb/series":
2017-07-07 15:44:06 +02:00
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-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
2017-09-30 16:04:13 +02:00
// Popularity
if anime.Popularity == nil {
anime.Popularity = &arn.AnimePopularity{}
}
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-11-01 10:28:40 +01:00
anime.Save()
2017-06-02 16:17:58 +02:00
2017-07-11 00:56:18 +02:00
// Episodes
episodes, err := arn.GetAnimeEpisodes(anime.ID)
if err != nil || episodes == nil {
2017-11-07 12:35:32 +01:00
episodes := &arn.AnimeEpisodes{
AnimeID: anime.ID,
Items: []*arn.AnimeEpisode{},
}
arn.DB.Set("AnimeEpisodes", anime.ID, episodes)
2017-07-11 00:56:18 +02:00
}
2018-03-09 16:27:40 +01:00
// Relations
relations, _ := arn.GetAnimeRelations(anime.ID)
if relations == nil {
relations := &arn.AnimeRelations{
AnimeID: anime.ID,
Items: []*arn.AnimeRelation{},
}
arn.DB.Set("AnimeRelations", anime.ID, relations)
}
2017-06-08 11:51:34 +02:00
// Log
2017-11-01 10:28:40 +01:00
fmt.Println(color.GreenString("✔"), anime.ID, anime.Title.Canonical)
2018-03-09 16:27:40 +01:00
return anime
2017-06-02 16:17:58 +02:00
}