Created animediff package

This commit is contained in:
Eduard Urbach 2018-03-09 05:12:22 +01:00
parent 3ac77b889b
commit 1910b3c2d8
8 changed files with 93 additions and 59 deletions

View File

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

View File

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

17
utils/HashStrings.go Normal file
View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,8 @@
package animediff
// Difference describes a difference between two anime.
type Difference interface {
String() string
DetailsA() string
DetailsB() string
}

View File

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