Added anime popularity

This commit is contained in:
2017-09-30 16:04:13 +02:00
parent 3caad5cb0d
commit 831c6118d9
6 changed files with 99 additions and 7 deletions

View File

@ -7,7 +7,12 @@ import (
"github.com/fatih/color"
)
const currentlyAiringBonus = 4.0
const (
currentlyAiringBonus = 4.0
popularityThreshold = 5
popularityPenalty = 4.0
watchingPopularityWeight = 0.1
)
func main() {
color.Yellow("Caching airing anime")
@ -21,17 +26,30 @@ func main() {
}
sort.Slice(animeList, func(i, j int) bool {
scoreA := animeList[i].Rating.Overall
scoreB := animeList[j].Rating.Overall
a := animeList[i]
b := animeList[j]
scoreA := a.Rating.Overall
scoreB := b.Rating.Overall
if animeList[i].Status == "current" {
if a.Status == "current" {
scoreA += currentlyAiringBonus
}
if animeList[j].Status == "current" {
if b.Status == "current" {
scoreB += currentlyAiringBonus
}
if a.Popularity.Total() < popularityThreshold {
scoreA -= popularityPenalty
}
if b.Popularity.Total() < popularityThreshold {
scoreB -= popularityPenalty
}
scoreA += float64(a.Popularity.Watching) * watchingPopularityWeight
scoreB += float64(b.Popularity.Watching) * watchingPopularityWeight
return scoreA > scoreB
})

View File

@ -7,6 +7,7 @@ import (
var ratings = map[string][]*arn.AnimeRating{}
var finalRating = map[string]*arn.AnimeRating{}
var popularity = map[string]*arn.AnimePopularity{}
// Note this is using the airing-anime as a template with modfications
// made to it.
@ -18,9 +19,10 @@ func main() {
for _, animeList := range allAnimeLists {
extractRatings(animeList)
extractPopularity(animeList)
}
// Calculate
// Calculate rating
for animeID := range finalRating {
overall := []float64{}
story := []float64{}
@ -59,6 +61,14 @@ func main() {
arn.PanicOnError(anime.Save())
}
// Save popularity
for animeID := range popularity {
anime, err := arn.GetAnime(animeID)
arn.PanicOnError(err)
anime.Popularity = popularity[animeID]
arn.PanicOnError(anime.Save())
}
color.Green("Finished.")
}
@ -92,3 +102,28 @@ func extractRatings(animeList *arn.AnimeList) {
ratings[item.AnimeID] = append(ratings[item.AnimeID], item.Rating)
}
}
func extractPopularity(animeList *arn.AnimeList) {
for _, item := range animeList.Items {
_, found := popularity[item.AnimeID]
if !found {
popularity[item.AnimeID] = &arn.AnimePopularity{}
}
counter := popularity[item.AnimeID]
switch item.Status {
case arn.AnimeListStatusWatching:
counter.Watching++
case arn.AnimeListStatusCompleted:
counter.Completed++
case arn.AnimeListStatusPlanned:
counter.Planned++
case arn.AnimeListStatusHold:
counter.Hold++
case arn.AnimeListStatusDropped:
counter.Dropped++
}
}
}

View File

@ -111,6 +111,11 @@ func sync(data *kitsu.Anime) *arn.Anime {
anime.Rating.Reset()
}
// Popularity
if anime.Popularity == nil {
anime.Popularity = &arn.AnimePopularity{}
}
// Trailers
anime.Trailers = []*arn.ExternalMedia{}