Reduced MAL sync complexity

This commit is contained in:
Eduard Urbach 2018-04-26 20:41:41 +02:00
parent 63c3ab894f
commit fec35059ae

View File

@ -55,36 +55,27 @@ func sync(anime *arn.Anime, malID string) {
// Log title
fmt.Printf("%s %s\n", color.CyanString(anime.Title.Canonical), malID)
if len(anime.Genres) == 0 && len(malAnime.Genres) > 0 {
fmt.Println("Genres:", malAnime.Genres)
anime.Genres = malAnime.Genres
}
if anime.EpisodeCount == 0 && malAnime.EpisodeCount != 0 {
fmt.Println("EpisodeCount:", malAnime.EpisodeCount)
anime.EpisodeCount = malAnime.EpisodeCount
}
if anime.EpisodeLength == 0 && malAnime.EpisodeLength != 0 {
fmt.Println("EpisodeLength:", malAnime.EpisodeLength)
anime.EpisodeLength = malAnime.EpisodeLength
}
if anime.StartDate == "" && malAnime.StartDate != "" {
fmt.Println("StartDate:", malAnime.StartDate)
anime.StartDate = malAnime.StartDate
}
if anime.EndDate == "" && malAnime.EndDate != "" {
fmt.Println("EndDate:", malAnime.EndDate)
anime.EndDate = malAnime.EndDate
}
if anime.Source == "" && malAnime.Source != "" {
fmt.Println("Source:", malAnime.Source)
anime.Source = malAnime.Source
// Titles
syncTitles(anime, malAnime)
// Dates
syncDates(anime, malAnime)
// Episodes
syncEpisodes(anime, malAnime)
// Others
syncOthers(anime, malAnime)
// Characters
syncCharacters(anime, malAnime)
// Save in database
anime.Save()
}
// Sync titles
func syncTitles(anime *arn.Anime, malAnime *mal.Anime) {
if anime.Title.Japanese == "" && malAnime.JapaneseTitle != "" {
fmt.Println("JapaneseTitle:", malAnime.JapaneseTitle)
anime.Title.Japanese = malAnime.JapaneseTitle
@ -94,8 +85,53 @@ func sync(anime *arn.Anime, malID string) {
fmt.Println("EnglishTitle:", malAnime.EnglishTitle)
anime.Title.English = malAnime.EnglishTitle
}
}
// Sync dates
func syncDates(anime *arn.Anime, malAnime *mal.Anime) {
if anime.StartDate == "" && malAnime.StartDate != "" {
fmt.Println("StartDate:", malAnime.StartDate)
anime.StartDate = malAnime.StartDate
}
if anime.EndDate == "" && malAnime.EndDate != "" {
fmt.Println("EndDate:", malAnime.EndDate)
anime.EndDate = malAnime.EndDate
}
}
// Sync episodes
func syncEpisodes(anime *arn.Anime, malAnime *mal.Anime) {
if anime.EpisodeCount == 0 && malAnime.EpisodeCount != 0 {
fmt.Println("EpisodeCount:", malAnime.EpisodeCount)
anime.EpisodeCount = malAnime.EpisodeCount
}
if anime.EpisodeLength == 0 && malAnime.EpisodeLength != 0 {
fmt.Println("EpisodeLength:", malAnime.EpisodeLength)
anime.EpisodeLength = malAnime.EpisodeLength
}
}
// Sync others
func syncOthers(anime *arn.Anime, malAnime *mal.Anime) {
if len(anime.Genres) == 0 && len(malAnime.Genres) > 0 {
fmt.Println("Genres:", malAnime.Genres)
anime.Genres = malAnime.Genres
}
if anime.Source == "" && malAnime.Source != "" {
fmt.Println("Source:", malAnime.Source)
anime.Source = malAnime.Source
}
}
// Sync image
func syncImage(anime *arn.Anime, malAnime *mal.Anime) {
if (anime.HasImage() && anime.Image.Width > imageWidthThreshold) || malAnime.Image == "" {
return
}
if (!anime.HasImage() || anime.Image.Width <= imageWidthThreshold) && malAnime.Image != "" {
fmt.Println("Downloading image:", malAnime.Image)
response, err := client.Get(malAnime.Image).End()
@ -106,13 +142,7 @@ func sync(anime *arn.Anime, malID string) {
}
}
// Characters
syncCharacters(anime, malAnime)
// Save in database
anime.Save()
}
// Sync characters
func syncCharacters(anime *arn.Anime, malAnime *mal.Anime) {
// Check for existence of characters
animeCharacters := anime.Characters()