Added anime popularity

This commit is contained in:
Eduard Urbach 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{}

View File

@ -2,4 +2,4 @@ component AnimeGrid(animeList []*arn.Anime)
.anime-grid
each anime in animeList
a.anime-grid-cell.ajax(href="/anime/" + toString(anime.ID))
img.anime-grid-image.lazy(data-src=anime.Image.Small, alt=anime.Title.Romaji, title=anime.Title.Romaji + " (" + toString(anime.Rating.Overall) + ")")
img.anime-grid-image.lazy(data-src=anime.Image.Small, alt=anime.Title.Romaji, title=anime.Title.Romaji)

View File

@ -169,6 +169,24 @@ component Anime(anime *arn.Anime, friends []*arn.User, listItems map[*arn.User]*
if character.Character() != nil
Character(character.Character())
h3.anime-section-name Popularity
.anime-rating-categories
.anime-rating-category
.anime-rating-category-name Watching
.anime-rating= anime.Popularity.Watching
.anime-rating-category
.anime-rating-category-name Completed
.anime-rating= anime.Popularity.Completed
.anime-rating-category
.anime-rating-category-name Planned
.anime-rating= anime.Popularity.Planned
.anime-rating-category
.anime-rating-category-name Hold
.anime-rating= anime.Popularity.Hold
.anime-rating-category
.anime-rating-category-name Dropped
.anime-rating= anime.Popularity.Dropped
if len(anime.Episodes().Items) > 0
if episodesReversed
h3.anime-section-name Latest episodes

View File

@ -0,0 +1,16 @@
package main
import (
"github.com/animenotifier/arn"
)
func main() {
for anime := range arn.MustStreamAnime() {
if anime.Popularity != nil {
continue
}
anime.Popularity = &arn.AnimePopularity{}
arn.PanicOnError(anime.Save())
}
}