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"
|
|
|
|
)
|
|
|
|
|
|
|
|
const maxCompareMALEntries = 10
|
|
|
|
|
|
|
|
// 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:12:22 +00:00
|
|
|
// Compare canonical titles
|
2018-03-09 03:58:36 +00:00
|
|
|
if anime.Title.Canonical != malAnime.Title {
|
2018-03-09 04:12:22 +00:00
|
|
|
differences = append(differences, &animediff.CanonicalTitle{
|
2018-03-09 03:58:36 +00:00
|
|
|
TitleA: anime.Title.Canonical,
|
|
|
|
TitleB: malAnime.Title,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-09 04:12:22 +00:00
|
|
|
if anime.Title.Japanese != malAnime.JapaneseTitle {
|
|
|
|
differences = append(differences, &animediff.JapaneseTitle{
|
|
|
|
TitleA: anime.Title.Japanese,
|
|
|
|
TitleB: malAnime.JapaneseTitle,
|
|
|
|
})
|
2018-03-09 03:03:31 +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 {
|
|
|
|
differences = append(differences, &animediff.Genres{
|
2018-03-09 03:03:31 +00:00
|
|
|
GenresA: anime.Genres,
|
|
|
|
GenresB: malAnime.Genres,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.HTML(components.CompareMAL(comparisons, user))
|
|
|
|
}
|