95 lines
2.0 KiB
Go
Raw Normal View History

2017-06-02 14:17:58 +00:00
package main
import (
2017-06-21 18:10:12 +00:00
"encoding/json"
2017-06-02 14:17:58 +00:00
"fmt"
2017-06-08 09:51:34 +00:00
"strconv"
2017-06-02 14:17:58 +00:00
"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 10:39:41 +00:00
allAnime := kitsu.StreamAnime()
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
println("Finished.")
2017-06-02 14:17:58 +00:00
}
func sync(data *kitsu.Anime) {
anime := arn.Anime{}
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.Japanese = attr.Titles.JaJp
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-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
overall, convertError := strconv.ParseFloat(attr.AverageRating, 64)
if convertError != nil {
overall = 0
}
anime.Rating.Overall = overall
// 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-06-02 14:17:58 +00:00
err := anime.Save()
status := ""
if err == nil {
status = color.GreenString("✔")
} else {
2017-06-21 18:10:12 +00:00
color.Red(err.Error())
data, _ := json.MarshalIndent(anime, "", "\t")
fmt.Println(string(data))
2017-06-02 14:17:58 +00:00
status = color.RedString("✘")
}
2017-06-08 09:51:34 +00:00
// Log
2017-06-02 14:17:58 +00:00
fmt.Println(status, anime.ID, anime.Title.Canonical)
}