132 lines
3.1 KiB
Go
Raw Normal View History

2018-03-09 03:03:31 +00:00
package editor
import (
"sort"
2018-03-09 04:12:22 +00:00
"github.com/animenotifier/notify.moe/utils/animediff"
2018-03-09 03:03:31 +00:00
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/mal"
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils"
)
2018-03-09 13:48:00 +00:00
const maxCompareMALEntries = 10
2018-03-09 03:03:31 +00:00
// CompareMAL ...
func CompareMAL(ctx *aero.Context) string {
user := utils.GetUser(ctx)
year, _ := ctx.GetInt("year")
animeType := ctx.Get("type")
animes := arn.FilterAnime(func(anime *arn.Anime) bool {
if year != 0 && year != anime.StartDateTime().Year() {
return false
}
if animeType != "" && anime.Type != animeType {
return false
}
return anime.GetMapping("myanimelist/anime") != ""
})
sort.Slice(animes, func(i, j int) bool {
a := animes[i]
b := animes[j]
aPop := a.Popularity.Total()
bPop := b.Popularity.Total()
if aPop == bPop {
return a.Title.Canonical < b.Title.Canonical
}
return aPop > bPop
})
comparisons := []*utils.MALComparison{}
malAnimeCollection := arn.MAL.Collection("Anime")
for _, anime := range animes {
malID := anime.GetMapping("myanimelist/anime")
obj, err := malAnimeCollection.Get(malID)
if err != nil {
continue
}
malAnime := obj.(*mal.Anime)
2018-03-09 04:12:22 +00:00
var differences []animediff.Difference
2018-03-09 03:03:31 +00:00
2018-03-09 04:26:34 +00:00
// Canonical title
2018-03-09 03:58:36 +00:00
if anime.Title.Canonical != malAnime.Title {
2018-03-09 12:42:45 +00:00
hash := utils.HashString(malAnime.Title)
if !arn.IsAnimeDifferenceIgnored(anime.ID, "mal", malAnime.ID, "CanonicalTitle", hash) {
differences = append(differences, &animediff.CanonicalTitle{
2018-03-09 13:48:00 +00:00
TitleA: anime.Title.Canonical,
TitleB: malAnime.Title,
NumericHash: hash,
2018-03-09 12:42:45 +00:00
})
}
2018-03-09 03:58:36 +00:00
}
2018-03-09 04:26:34 +00:00
// Japanese title
2018-03-09 04:12:22 +00:00
if anime.Title.Japanese != malAnime.JapaneseTitle {
2018-03-09 12:42:45 +00:00
hash := utils.HashString(malAnime.JapaneseTitle)
if !arn.IsAnimeDifferenceIgnored(anime.ID, "mal", malAnime.ID, "JapaneseTitle", hash) {
differences = append(differences, &animediff.JapaneseTitle{
2018-03-09 13:48:00 +00:00
TitleA: anime.Title.Japanese,
TitleB: malAnime.JapaneseTitle,
NumericHash: hash,
2018-03-09 12:42:45 +00:00
})
}
2018-03-09 03:03:31 +00:00
}
2018-03-09 04:26:34 +00:00
// Synopsis
if len(anime.Summary) < len(malAnime.Synopsis) {
2018-03-09 12:42:45 +00:00
hash := utils.HashString(malAnime.Synopsis)
if !arn.IsAnimeDifferenceIgnored(anime.ID, "mal", malAnime.ID, "Synopsis", hash) {
differences = append(differences, &animediff.Synopsis{
2018-03-09 13:48:00 +00:00
SynopsisA: anime.Summary,
SynopsisB: malAnime.Synopsis,
NumericHash: hash,
2018-03-09 12:42:45 +00:00
})
}
2018-03-09 04:26:34 +00:00
}
2018-03-09 04:12:22 +00:00
// Compare genres
hashA := utils.HashStringsNoOrder(anime.Genres)
hashB := utils.HashStringsNoOrder(malAnime.Genres)
2018-03-09 03:03:31 +00:00
2018-03-09 04:12:22 +00:00
if hashA != hashB {
2018-03-09 12:42:45 +00:00
if !arn.IsAnimeDifferenceIgnored(anime.ID, "mal", malAnime.ID, "Genres", hashB) {
differences = append(differences, &animediff.Genres{
2018-03-09 13:48:00 +00:00
GenresA: anime.Genres,
GenresB: malAnime.Genres,
NumericHash: hashB,
2018-03-09 12:42:45 +00:00
})
}
2018-03-09 03:03:31 +00:00
}
2018-03-09 03:58:36 +00:00
// Add if there were any differences
2018-03-09 03:03:31 +00:00
if len(differences) > 0 {
comparisons = append(comparisons, &utils.MALComparison{
Anime: anime,
MALAnime: malAnime,
Differences: differences,
})
if len(comparisons) >= maxCompareMALEntries {
break
}
}
}
2018-03-09 11:38:46 +00:00
return ctx.HTML(components.CompareMAL(comparisons, ctx.URI(), user))
2018-03-09 03:03:31 +00:00
}