2016-11-19 14:54:31 +00:00
|
|
|
package genre
|
|
|
|
|
|
|
|
import (
|
2017-11-30 00:47:12 +00:00
|
|
|
"strings"
|
|
|
|
|
2016-11-19 14:54:31 +00:00
|
|
|
"github.com/aerogo/aero"
|
2019-06-03 09:32:43 +00:00
|
|
|
"github.com/animenotifier/notify.moe/arn"
|
2016-11-19 14:54:31 +00:00
|
|
|
"github.com/animenotifier/notify.moe/components"
|
2017-11-12 10:18:16 +00:00
|
|
|
"github.com/animenotifier/notify.moe/utils"
|
2016-11-19 14:54:31 +00:00
|
|
|
)
|
|
|
|
|
2017-11-30 00:47:12 +00:00
|
|
|
const animePerPage = 100
|
2018-10-10 09:37:31 +00:00
|
|
|
const animeRatingCountThreshold = 5
|
2017-11-30 00:47:12 +00:00
|
|
|
|
2018-10-10 09:37:31 +00:00
|
|
|
// Get renders the genre page.
|
2019-06-01 04:55:49 +00:00
|
|
|
func Get(ctx aero.Context) error {
|
2017-11-12 10:18:16 +00:00
|
|
|
user := utils.GetUser(ctx)
|
2016-11-19 14:54:31 +00:00
|
|
|
genreName := ctx.Get("name")
|
2017-11-30 00:47:12 +00:00
|
|
|
animes := []*arn.Anime{}
|
|
|
|
|
|
|
|
for anime := range arn.StreamAnime() {
|
|
|
|
if containsLowerCase(anime.Genres, genreName) {
|
|
|
|
animes = append(animes, anime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-30 12:06:31 +00:00
|
|
|
userScore := averageUserScore(user, animes)
|
2018-07-26 16:06:47 +00:00
|
|
|
userCompleted := totalCompleted(user, animes)
|
2018-07-30 12:06:31 +00:00
|
|
|
globalScore := averageGlobalScore(animes)
|
2018-07-02 17:57:44 +00:00
|
|
|
|
2018-03-14 00:02:41 +00:00
|
|
|
arn.SortAnimeByQuality(animes)
|
2017-11-30 00:47:12 +00:00
|
|
|
|
|
|
|
if len(animes) > animePerPage {
|
|
|
|
animes = animes[:animePerPage]
|
|
|
|
}
|
|
|
|
|
2018-07-30 12:06:31 +00:00
|
|
|
return ctx.HTML(components.Genre(genreName, animes, user, userScore, userCompleted, globalScore))
|
2017-11-30 00:47:12 +00:00
|
|
|
}
|
2016-11-19 14:54:31 +00:00
|
|
|
|
2017-11-30 00:47:12 +00:00
|
|
|
// containsLowerCase tells you whether the given element exists when all elements are lowercased.
|
|
|
|
func containsLowerCase(array []string, search string) bool {
|
|
|
|
for _, item := range array {
|
|
|
|
if strings.ToLower(item) == search {
|
|
|
|
return true
|
|
|
|
}
|
2016-11-19 14:54:31 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 00:47:12 +00:00
|
|
|
return false
|
2016-11-19 14:54:31 +00:00
|
|
|
}
|
2018-07-02 17:57:44 +00:00
|
|
|
|
2018-10-10 09:37:31 +00:00
|
|
|
// averageUserScore counts the user's average score for a list of anime.
|
2018-07-30 12:06:31 +00:00
|
|
|
func averageUserScore(user *arn.User, animes []*arn.Anime) float64 {
|
2018-07-02 17:57:44 +00:00
|
|
|
if user == nil {
|
2018-07-03 10:13:38 +00:00
|
|
|
return 0
|
2018-07-02 17:57:44 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 10:13:38 +00:00
|
|
|
count := 0.0
|
2018-07-02 17:57:44 +00:00
|
|
|
scores := 0.0
|
|
|
|
|
2018-07-03 10:13:38 +00:00
|
|
|
animeList := user.AnimeList()
|
2018-07-02 17:57:44 +00:00
|
|
|
|
2018-07-03 10:13:38 +00:00
|
|
|
for _, anime := range animes {
|
|
|
|
userAnime := animeList.Find(anime.ID)
|
2018-07-03 13:33:35 +00:00
|
|
|
|
2018-07-03 10:13:38 +00:00
|
|
|
if userAnime != nil && !userAnime.Rating.IsNotRated() {
|
|
|
|
scores += userAnime.Rating.Overall
|
2018-07-03 10:31:49 +00:00
|
|
|
count++
|
2018-07-02 17:57:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-03 13:33:35 +00:00
|
|
|
if count == 0.0 {
|
2018-07-03 10:13:38 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return scores / count
|
2018-07-02 17:57:44 +00:00
|
|
|
}
|
2018-07-26 16:06:47 +00:00
|
|
|
|
2018-10-10 09:37:31 +00:00
|
|
|
// averageGlobalScore returns the average overall score for the given anime.
|
2018-07-30 12:06:31 +00:00
|
|
|
func averageGlobalScore(animes []*arn.Anime) float64 {
|
|
|
|
sum := 0.0
|
|
|
|
count := 0
|
|
|
|
|
|
|
|
for _, anime := range animes {
|
2018-10-10 09:37:31 +00:00
|
|
|
if anime.Rating.Count.Overall >= animeRatingCountThreshold {
|
2018-07-30 12:06:31 +00:00
|
|
|
sum += anime.Rating.Overall
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sum / float64(count)
|
|
|
|
}
|
|
|
|
|
2018-10-10 09:37:31 +00:00
|
|
|
// totalCompleted counts the number of anime the user has completed from a given list of anime.
|
2018-07-26 16:06:47 +00:00
|
|
|
func totalCompleted(user *arn.User, animes []*arn.Anime) int {
|
|
|
|
if user == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
count := 0
|
|
|
|
|
|
|
|
completedList := user.AnimeList().FilterStatus(arn.AnimeListStatusCompleted)
|
|
|
|
|
|
|
|
for _, anime := range animes {
|
2018-07-28 14:47:25 +00:00
|
|
|
if completedList.Contains(anime.ID) {
|
|
|
|
count++
|
2018-07-26 16:06:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-28 14:47:25 +00:00
|
|
|
return count
|
2018-07-26 16:06:47 +00:00
|
|
|
}
|