From 98c9278e2467035d0386bd3fe593c2946c4d6491 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 9 Mar 2018 04:03:31 +0100 Subject: [PATCH] Added MAL diffs --- pages/editor/editor.pixy | 5 +- pages/editor/mal.go | 101 +++++++++++++++++++++++++++++++++++++++ pages/editor/mal.pixy | 17 +++++++ pages/editor/mal.scarlet | 21 ++++++++ pages/index.go | 1 + utils/MALComparison.go | 37 ++++++++++++++ 6 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 pages/editor/mal.go create mode 100644 pages/editor/mal.pixy create mode 100644 pages/editor/mal.scarlet create mode 100644 utils/MALComparison.go diff --git a/pages/editor/editor.pixy b/pages/editor/editor.pixy index 8bdc1928..1fb7f5b6 100644 --- a/pages/editor/editor.pixy +++ b/pages/editor/editor.pixy @@ -1,8 +1,6 @@ component Editor - h1.page-title Editor Panel - EditorTabs - + h1.page-title Editor Panel p.text-center.mountable Welcome to the Editor Panel! component EditorTabs @@ -11,6 +9,7 @@ component EditorTabs Tab("Shoboi", "list", "/editor/shoboi") Tab("AniList", "list", "/editor/anilist") Tab("Genres", "list", "/editor/genres") + Tab("MAL", "list", "/editor/mal") Tab("Search", "search", "/database") //- a.tab.ajax(href="/admin", aria-label="Admin") diff --git a/pages/editor/mal.go b/pages/editor/mal.go new file mode 100644 index 00000000..b7428e38 --- /dev/null +++ b/pages/editor/mal.go @@ -0,0 +1,101 @@ +package editor + +import ( + "sort" + + "github.com/OneOfOne/xxhash" + "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) + var differences []utils.AnimeDiff + + sumA := uint64(0) + + for _, genre := range anime.Genres { + h := xxhash.NewS64(0) + h.Write([]byte(genre)) + numHash := h.Sum64() + sumA += numHash + } + + sumB := uint64(0) + + for _, genre := range malAnime.Genres { + h := xxhash.NewS64(0) + h.Write([]byte(genre)) + numHash := h.Sum64() + sumB += numHash + } + + if sumA != sumB { + differences = append(differences, &utils.AnimeGenresDiff{ + GenresA: anime.Genres, + GenresB: malAnime.Genres, + }) + } + + 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)) +} diff --git a/pages/editor/mal.pixy b/pages/editor/mal.pixy new file mode 100644 index 00000000..ac737b64 --- /dev/null +++ b/pages/editor/mal.pixy @@ -0,0 +1,17 @@ +component CompareMAL(comparisons []*utils.MALComparison, user *arn.User) + EditorTabs + h1.mountable MAL Comparison + + .mal-comparisons + each comparison in comparisons + .mal-comparison.mountable + a(href=comparison.Anime.Link(), target="_blank")= comparison.Anime.Title.ByUser(user) + a(href=comparison.MALAnime.URL, target="_blank")= comparison.MALAnime.Title + + each difference in comparison.Differences + .comparison-difference + div= difference.String() + + .comparison-difference-details + .comparison-difference-detail= difference.DetailsA() + .comparison-difference-detail= difference.DetailsB() diff --git a/pages/editor/mal.scarlet b/pages/editor/mal.scarlet new file mode 100644 index 00000000..cf41a261 --- /dev/null +++ b/pages/editor/mal.scarlet @@ -0,0 +1,21 @@ +.mal-comparisons + vertical + width 100% + max-width 1100px + margin 0 auto + +.mal-comparison + vertical + ui-element + padding 0.5rem 1rem + margin 0.5rem + +.comparison-difference + vertical + font-weight bold + +.comparison-difference-details + horizontal + +.comparison-difference-detail + flex 1 \ No newline at end of file diff --git a/pages/index.go b/pages/index.go index 6824a456..16963798 100644 --- a/pages/index.go +++ b/pages/index.go @@ -208,6 +208,7 @@ func Configure(app *aero.Application) { l.Page("/editor/genres", editor.Genres) l.Page("/editor/genres/:year", editor.Genres) l.Page("/editor/genres/:year/:type", editor.Genres) + l.Page("/editor/mal", editor.CompareMAL) // Mixed l.Page("/database", database.Get) diff --git a/utils/MALComparison.go b/utils/MALComparison.go new file mode 100644 index 00000000..40af36bf --- /dev/null +++ b/utils/MALComparison.go @@ -0,0 +1,37 @@ +package utils + +import ( + "strings" + + "github.com/animenotifier/arn" + "github.com/animenotifier/mal" +) + +type AnimeDiff interface { + String() string + DetailsA() string + DetailsB() string +} + +type AnimeGenresDiff struct { + GenresA []string + GenresB []string +} + +func (diff *AnimeGenresDiff) String() string { + return "Genres are different" +} + +func (diff *AnimeGenresDiff) DetailsA() string { + return strings.Join(diff.GenresA, ", ") +} + +func (diff *AnimeGenresDiff) DetailsB() string { + return strings.Join(diff.GenresB, ", ") +} + +type MALComparison struct { + Anime *arn.Anime + MALAnime *mal.Anime + Differences []AnimeDiff +}