146 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/mal"
2019-06-07 01:03:16 +00:00
"github.com/animenotifier/notify.moe/arn"
2018-03-09 03:03:31 +00:00
"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
2018-04-26 18:54:39 +00:00
// diffFunction is the signature of a diff function.
type diffFunction func(*arn.Anime, *mal.Anime) []animediff.Difference
2018-03-09 03:03:31 +00:00
// CompareMAL ...
2019-06-01 04:55:49 +00:00
func CompareMAL(ctx aero.Context) error {
2018-03-09 03:03:31 +00:00
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 {
2019-06-07 01:03:16 +00:00
// Prealloc linter would complain, but this is best left as nil by default.
// nolint:prealloc
2018-04-26 17:39:43 +00:00
var differences []animediff.Difference
2018-03-09 23:25:37 +00:00
2018-04-26 18:54:39 +00:00
// We'll use the following diffs
diffFunctions := []diffFunction{
diffTitles,
diffDates,
diffEpisodes,
diffStatus,
diffSynopsis,
diffGenres,
2018-04-26 17:39:43 +00:00
}
2018-03-09 23:25:37 +00:00
2018-04-26 18:54:39 +00:00
for _, diffFunction := range diffFunctions {
diffs := diffFunction(anime, malAnime)
differences = append(differences, diffs...)
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
}