Added MAL diffs

This commit is contained in:
Eduard Urbach 2018-03-09 04:03:31 +01:00
parent c7fc3d2173
commit 98c9278e24
6 changed files with 179 additions and 3 deletions

View File

@ -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")

101
pages/editor/mal.go Normal file
View File

@ -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))
}

17
pages/editor/mal.pixy Normal file
View File

@ -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()

21
pages/editor/mal.scarlet Normal file
View File

@ -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

View File

@ -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)

37
utils/MALComparison.go Normal file
View File

@ -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
}