244 lines
5.9 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 23:25:37 +00:00
const maxCompareMALEntries = 15
2018-03-09 03:03:31 +00:00
// CompareMAL ...
func CompareMAL(ctx *aero.Context) string {
user := utils.GetUser(ctx)
2018-03-23 00:43:45 +00:00
year := ctx.Get("year")
2018-03-10 09:15:05 +00:00
status := ctx.Get("status")
2018-04-13 15:55:52 +00:00
season := ctx.Get("season")
2018-03-23 00:43:45 +00:00
typ := ctx.Get("type")
if year == "any" {
year = ""
}
2018-04-13 15:55:52 +00:00
if season == "any" {
season = ""
}
2018-03-23 00:43:45 +00:00
if status == "any" {
status = ""
}
if typ == "any" {
typ = ""
}
settings := user.Settings()
settings.Editor.Filter.Year = year
2018-04-13 15:55:52 +00:00
settings.Editor.Filter.Season = season
2018-03-23 00:43:45 +00:00
settings.Editor.Filter.Status = status
settings.Editor.Filter.Type = typ
settings.Save()
2018-03-09 03:03:31 +00:00
animes := arn.FilterAnime(func(anime *arn.Anime) bool {
2018-03-23 00:43:45 +00:00
if year != "" && (len(anime.StartDate) < 4 || anime.StartDate[:4] != year) {
2018-03-10 09:15:05 +00:00
return false
}
2018-03-23 00:43:45 +00:00
if status != "" && anime.Status != status {
2018-03-09 03:03:31 +00:00
return false
}
2018-04-13 15:55:52 +00:00
if season != "" && anime.Season() != season {
return false
}
2018-03-23 00:43:45 +00:00
if typ != "" && anime.Type != typ {
2018-03-09 03:03:31 +00:00
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
})
2018-04-25 23:46:26 +00:00
comparisons := compare(animes)
return ctx.HTML(components.CompareMAL(comparisons, year, status, typ, "/editor/mal/diff/anime", user))
}
// compare builds the comparisons to MAL anime entries.
func compare(animes []*arn.Anime) []*utils.MALComparison {
2018-03-09 03:03:31 +00:00
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-04-26 17:39:43 +00:00
differences := diff(anime, malAnime)
2018-03-09 03:58:36 +00:00
2018-04-26 17:39:43 +00:00
// Add if there were any differences
if len(differences) > 0 {
comparisons = append(comparisons, &utils.MALComparison{
Anime: anime,
MALAnime: malAnime,
Differences: differences,
})
2018-03-09 12:42:45 +00:00
2018-04-26 17:39:43 +00:00
if len(comparisons) >= maxCompareMALEntries {
break
2018-03-09 12:42:45 +00:00
}
2018-03-09 03:03:31 +00:00
}
2018-04-26 17:39:43 +00:00
}
return comparisons
}
2018-03-09 03:03:31 +00:00
2018-04-26 17:39:43 +00:00
// diff returns all the differences between an anime and its MAL counterpart.
func diff(anime *arn.Anime, malAnime *mal.Anime) []animediff.Difference {
var differences []animediff.Difference
2018-03-09 23:25:37 +00:00
2018-04-26 17:39:43 +00:00
// Canonical title
if anime.Title.Canonical != malAnime.Title {
hash := utils.HashString(malAnime.Title)
if !arn.IsAnimeDifferenceIgnored(anime.ID, "mal", malAnime.ID, "CanonicalTitle", hash) {
differences = append(differences, &animediff.CanonicalTitle{
TitleA: anime.Title.Canonical,
TitleB: malAnime.Title,
NumericHash: hash,
})
2018-03-09 23:25:37 +00:00
}
2018-04-26 17:39:43 +00:00
}
2018-03-09 23:25:37 +00:00
2018-04-26 17:39:43 +00:00
// Japanese title
if anime.Title.Japanese != malAnime.JapaneseTitle {
hash := utils.HashString(malAnime.JapaneseTitle)
2018-03-09 23:25:37 +00:00
2018-04-26 17:39:43 +00:00
if !arn.IsAnimeDifferenceIgnored(anime.ID, "mal", malAnime.ID, "JapaneseTitle", hash) {
differences = append(differences, &animediff.JapaneseTitle{
TitleA: anime.Title.Japanese,
TitleB: malAnime.JapaneseTitle,
NumericHash: hash,
})
2018-03-09 23:25:37 +00:00
}
2018-04-26 17:39:43 +00:00
}
2018-03-09 23:25:37 +00:00
2018-04-26 17:39:43 +00:00
// Romaji title
if anime.Title.Romaji != malAnime.Title {
hash := utils.HashString(malAnime.Title)
2018-03-09 23:25:37 +00:00
2018-04-26 17:39:43 +00:00
if !arn.IsAnimeDifferenceIgnored(anime.ID, "mal", malAnime.ID, "RomajiTitle", hash) {
differences = append(differences, &animediff.RomajiTitle{
TitleA: anime.Title.Romaji,
TitleB: malAnime.Title,
NumericHash: hash,
})
2018-03-09 23:25:37 +00:00
}
2018-04-26 17:39:43 +00:00
}
2018-03-09 23:25:37 +00:00
2018-04-26 17:39:43 +00:00
// Airing start date
if anime.StartDate != malAnime.StartDate && malAnime.StartDate != "" {
hash := utils.HashString(malAnime.StartDate)
2018-03-09 23:25:37 +00:00
2018-04-26 17:39:43 +00:00
if !arn.IsAnimeDifferenceIgnored(anime.ID, "mal", malAnime.ID, "StartDate", hash) {
differences = append(differences, &animediff.StartDate{
DateA: anime.StartDate,
DateB: malAnime.StartDate,
NumericHash: hash,
})
2018-03-09 23:25:37 +00:00
}
2018-04-26 17:39:43 +00:00
}
2018-03-09 23:25:37 +00:00
2018-04-26 17:39:43 +00:00
// Airing end date
if anime.EndDate != malAnime.EndDate && malAnime.EndDate != "" {
hash := utils.HashString(malAnime.EndDate)
2018-03-09 23:25:37 +00:00
2018-04-26 17:39:43 +00:00
if !arn.IsAnimeDifferenceIgnored(anime.ID, "mal", malAnime.ID, "EndDate", hash) {
differences = append(differences, &animediff.EndDate{
DateA: anime.EndDate,
DateB: malAnime.EndDate,
NumericHash: hash,
})
2018-03-09 23:25:37 +00:00
}
2018-04-26 17:39:43 +00:00
}
2018-03-09 23:25:37 +00:00
2018-04-26 17:39:43 +00:00
// Status
if anime.Status != malAnime.Status {
hash := utils.HashString(malAnime.Status)
2018-03-09 12:42:45 +00:00
2018-04-26 17:39:43 +00:00
if !arn.IsAnimeDifferenceIgnored(anime.ID, "mal", malAnime.ID, "Status", hash) {
differences = append(differences, &animediff.Status{
StatusA: anime.Status,
StatusB: malAnime.Status,
NumericHash: hash,
})
2018-03-09 04:26:34 +00:00
}
2018-04-26 17:39:43 +00:00
}
2018-03-09 04:26:34 +00:00
2018-04-26 17:39:43 +00:00
// EpisodeCount
if malAnime.EpisodeCount != 0 && anime.EpisodeCount != malAnime.EpisodeCount {
hash := uint64(malAnime.EpisodeCount)
2018-03-09 03:03:31 +00:00
2018-04-26 17:39:43 +00:00
if !arn.IsAnimeDifferenceIgnored(anime.ID, "mal", malAnime.ID, "EpisodeCount", hash) {
differences = append(differences, &animediff.EpisodeCount{
EpisodesA: anime.EpisodeCount,
EpisodesB: malAnime.EpisodeCount,
NumericHash: hash,
})
2018-03-09 03:03:31 +00:00
}
2018-04-26 17:39:43 +00:00
}
2018-03-09 03:03:31 +00:00
2018-04-26 17:39:43 +00:00
// Synopsis
if len(anime.Summary) < 300 && len(anime.Summary)+50 < len(malAnime.Synopsis) {
hash := utils.HashString(malAnime.Synopsis)
if !arn.IsAnimeDifferenceIgnored(anime.ID, "mal", malAnime.ID, "Synopsis", hash) {
differences = append(differences, &animediff.Synopsis{
SynopsisA: anime.Summary,
SynopsisB: malAnime.Synopsis,
NumericHash: hash,
2018-03-09 03:03:31 +00:00
})
2018-04-26 17:39:43 +00:00
}
}
2018-03-09 03:03:31 +00:00
2018-04-26 17:39:43 +00:00
// Compare genres
hashA := utils.HashStringsNoOrder(anime.Genres)
hashB := utils.HashStringsNoOrder(malAnime.Genres)
if hashA != hashB {
if !arn.IsAnimeDifferenceIgnored(anime.ID, "mal", malAnime.ID, "Genres", hashB) {
differences = append(differences, &animediff.Genres{
GenresA: anime.Genres,
GenresB: malAnime.Genres,
NumericHash: hashB,
})
2018-03-09 03:03:31 +00:00
}
}
2018-04-26 17:39:43 +00:00
return differences
2018-03-09 03:03:31 +00:00
}