73 lines
1.4 KiB
Go
Raw Normal View History

2016-11-19 14:54:31 +00:00
package genre
import (
"strings"
2016-11-19 14:54:31 +00:00
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"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
)
const animePerPage = 100
2016-11-19 14:54:31 +00:00
// Get ...
func Get(ctx *aero.Context) string {
2017-11-12 10:18:16 +00:00
user := utils.GetUser(ctx)
2016-11-19 14:54:31 +00:00
genreName := ctx.Get("name")
animes := []*arn.Anime{}
for anime := range arn.StreamAnime() {
if containsLowerCase(anime.Genres, genreName) {
animes = append(animes, anime)
}
}
userScore := averageScore(user, animes)
2018-03-14 00:02:41 +00:00
arn.SortAnimeByQuality(animes)
if len(animes) > animePerPage {
animes = animes[:animePerPage]
}
return ctx.HTML(components.Genre(genreName, animes, user, userScore))
}
2016-11-19 14:54:31 +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
}
return false
2016-11-19 14:54:31 +00:00
}
// Counts users average score in genre
func averageScore(user *arn.User, animes []*arn.Anime) float64 {
if user == nil {
return 0
}
count := 0.0
scores := 0.0
animeList := user.AnimeList()
for _, anime := range animes {
userAnime := animeList.Find(anime.ID)
if userAnime != nil && !userAnime.Rating.IsNotRated() {
scores += userAnime.Rating.Overall
count += 1
}
}
if count == 0.0 || scores == 0.0 {
return 0
}
return scores / count
}