diff --git a/pages/editor/mal.go b/pages/editor/mal.go index 3e079a1a..6b49cfa5 100644 --- a/pages/editor/mal.go +++ b/pages/editor/mal.go @@ -3,7 +3,8 @@ package editor import ( "sort" - "github.com/OneOfOne/xxhash" + "github.com/animenotifier/notify.moe/utils/animediff" + "github.com/aerogo/aero" "github.com/animenotifier/arn" "github.com/animenotifier/mal" @@ -57,37 +58,29 @@ func CompareMAL(ctx *aero.Context) string { } malAnime := obj.(*mal.Anime) - var differences []utils.AnimeDiff + var differences []animediff.Difference - // Compare titles + // Compare canonical titles if anime.Title.Canonical != malAnime.Title { - differences = append(differences, &utils.AnimeTitleDiff{ + differences = append(differences, &animediff.CanonicalTitle{ TitleA: anime.Title.Canonical, TitleB: malAnime.Title, }) } + if anime.Title.Japanese != malAnime.JapaneseTitle { + differences = append(differences, &animediff.JapaneseTitle{ + TitleA: anime.Title.Japanese, + TitleB: malAnime.JapaneseTitle, + }) + } + // Compare genres - sumA := uint64(0) + hashA := utils.HashStringsNoOrder(anime.Genres) + hashB := utils.HashStringsNoOrder(malAnime.Genres) - 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{ + if hashA != hashB { + differences = append(differences, &animediff.Genres{ GenresA: anime.Genres, GenresB: malAnime.Genres, }) diff --git a/utils/AnimeTitleDiff.go b/utils/AnimeTitleDiff.go deleted file mode 100644 index 4e5e740b..00000000 --- a/utils/AnimeTitleDiff.go +++ /dev/null @@ -1,22 +0,0 @@ -package utils - -// AnimeTitleDiff describes differing titles. -type AnimeTitleDiff struct { - TitleA string - TitleB string -} - -// String returns the description. -func (diff *AnimeTitleDiff) String() string { - return "Titles are different" -} - -// DetailsA shows the details for the first anime. -func (diff *AnimeTitleDiff) DetailsA() string { - return diff.TitleA -} - -// DetailsB shows the details for the second anime. -func (diff *AnimeTitleDiff) DetailsB() string { - return diff.TitleB -} diff --git a/utils/HashStrings.go b/utils/HashStrings.go new file mode 100644 index 00000000..38a958b3 --- /dev/null +++ b/utils/HashStrings.go @@ -0,0 +1,17 @@ +package utils + +import "github.com/OneOfOne/xxhash" + +// HashStringsNoOrder returns a hash of the string slice contents, ignoring order. +func HashStringsNoOrder(items []string) uint64 { + sum := uint64(0) + + for _, item := range items { + h := xxhash.NewS64(0) + h.Write([]byte(item)) + numHash := h.Sum64() + sum += numHash + } + + return sum +} diff --git a/utils/MALComparison.go b/utils/MALComparison.go index f9e2ecc9..170507d5 100644 --- a/utils/MALComparison.go +++ b/utils/MALComparison.go @@ -3,18 +3,12 @@ package utils import ( "github.com/animenotifier/arn" "github.com/animenotifier/mal" + "github.com/animenotifier/notify.moe/utils/animediff" ) -// AnimeDiff describes a difference between two anime. -type AnimeDiff interface { - String() string - DetailsA() string - DetailsB() string -} - // MALComparison encapsulates the difference between an ARN anime and a MAL anime. type MALComparison struct { Anime *arn.Anime MALAnime *mal.Anime - Differences []AnimeDiff + Differences []animediff.Difference } diff --git a/utils/animediff/CanonicalTitle.go b/utils/animediff/CanonicalTitle.go new file mode 100644 index 00000000..0a1bdc04 --- /dev/null +++ b/utils/animediff/CanonicalTitle.go @@ -0,0 +1,22 @@ +package animediff + +// CanonicalTitle describes differing titles. +type CanonicalTitle struct { + TitleA string + TitleB string +} + +// String returns the description. +func (diff *CanonicalTitle) String() string { + return "Canonical titles are different" +} + +// DetailsA shows the details for the first anime. +func (diff *CanonicalTitle) DetailsA() string { + return diff.TitleA +} + +// DetailsB shows the details for the second anime. +func (diff *CanonicalTitle) DetailsB() string { + return diff.TitleB +} diff --git a/utils/AnimeGenresDiff.go b/utils/animediff/Genres.go similarity index 57% rename from utils/AnimeGenresDiff.go rename to utils/animediff/Genres.go index a4799dbe..8c5eef1e 100644 --- a/utils/AnimeGenresDiff.go +++ b/utils/animediff/Genres.go @@ -1,24 +1,24 @@ -package utils +package animediff import "strings" -// AnimeGenresDiff describes differing genres. -type AnimeGenresDiff struct { +// Genres describes differing genres. +type Genres struct { GenresA []string GenresB []string } // String returns the description. -func (diff *AnimeGenresDiff) String() string { +func (diff *Genres) String() string { return "Genres are different" } // DetailsA shows the details for the first anime. -func (diff *AnimeGenresDiff) DetailsA() string { +func (diff *Genres) DetailsA() string { return strings.Join(diff.GenresA, ", ") } // DetailsB shows the details for the second anime. -func (diff *AnimeGenresDiff) DetailsB() string { +func (diff *Genres) DetailsB() string { return strings.Join(diff.GenresB, ", ") } diff --git a/utils/animediff/Interface.go b/utils/animediff/Interface.go new file mode 100644 index 00000000..0c884955 --- /dev/null +++ b/utils/animediff/Interface.go @@ -0,0 +1,8 @@ +package animediff + +// Difference describes a difference between two anime. +type Difference interface { + String() string + DetailsA() string + DetailsB() string +} diff --git a/utils/animediff/JapaneseTitle.go b/utils/animediff/JapaneseTitle.go new file mode 100644 index 00000000..4b439db5 --- /dev/null +++ b/utils/animediff/JapaneseTitle.go @@ -0,0 +1,22 @@ +package animediff + +// JapaneseTitle describes differing Japanese titles. +type JapaneseTitle struct { + TitleA string + TitleB string +} + +// String returns the description. +func (diff *JapaneseTitle) String() string { + return "Japanese titles are different" +} + +// DetailsA shows the details for the first anime. +func (diff *JapaneseTitle) DetailsA() string { + return diff.TitleA +} + +// DetailsB shows the details for the second anime. +func (diff *JapaneseTitle) DetailsB() string { + return diff.TitleB +}