121 lines
2.6 KiB
Go
Raw Normal View History

2017-06-30 23:16:25 +00:00
package explore
2017-06-20 20:54:45 +00:00
import (
2017-10-28 01:53:12 +00:00
"sort"
2017-11-10 10:46:05 +00:00
"time"
2017-10-28 01:53:12 +00:00
2017-06-20 20:54:45 +00:00
"github.com/aerogo/aero"
2017-10-28 01:53:12 +00:00
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
2017-11-12 10:18:16 +00:00
"github.com/animenotifier/notify.moe/utils"
2017-10-28 01:53:12 +00:00
)
const (
2017-11-07 16:46:39 +00:00
currentlyAiringBonus = 5.0
popularityThreshold = 5
popularityPenalty = 8.0
watchingPopularityWeight = 0.3
completedPopularityWeight = 0.3
plannedPopularityWeight = 0.2
2017-11-10 10:46:05 +00:00
agePenalty = 11.0
ageThreshold = 6 * 30 * 24 * time.Hour
2017-06-20 20:54:45 +00:00
)
2017-07-01 00:14:14 +00:00
// Get ...
2017-06-20 20:54:45 +00:00
func Get(ctx *aero.Context) string {
2017-11-07 16:46:39 +00:00
year := "2017"
status := "current"
typ := "tv"
results := filterAnime(year, status, typ)
2017-11-12 10:18:16 +00:00
user := utils.GetUser(ctx)
2017-10-28 01:53:12 +00:00
2017-11-12 10:18:16 +00:00
return ctx.HTML(components.ExploreAnime(results, year, status, typ, user))
2017-11-07 16:46:39 +00:00
}
// Filter ...
func Filter(ctx *aero.Context) string {
year := ctx.Get("year")
status := ctx.Get("status")
typ := ctx.Get("type")
2017-11-12 10:18:16 +00:00
user := utils.GetUser(ctx)
2017-11-07 16:46:39 +00:00
results := filterAnime(year, status, typ)
2017-11-12 10:18:16 +00:00
return ctx.HTML(components.ExploreAnime(results, year, status, typ, user))
2017-11-07 16:46:39 +00:00
}
func filterAnime(year, status, typ string) []*arn.Anime {
var results []*arn.Anime
for anime := range arn.StreamAnime() {
if len(anime.StartDate) < 4 {
continue
}
if anime.StartDate[:4] != year {
continue
}
if anime.Status != status {
continue
}
if anime.Type != typ {
continue
}
results = append(results, anime)
}
2017-11-10 10:46:05 +00:00
sortAnime(results, status)
2017-11-07 16:46:39 +00:00
return results
}
2017-11-10 10:46:05 +00:00
func sortAnime(animeList []*arn.Anime, filterStatus string) {
2017-10-28 01:53:12 +00:00
sort.Slice(animeList, func(i, j int) bool {
a := animeList[i]
b := animeList[j]
2017-11-07 16:46:39 +00:00
2017-10-28 01:53:12 +00:00
scoreA := a.Rating.Overall
scoreB := b.Rating.Overall
if a.Status == "current" {
scoreA += currentlyAiringBonus
}
if b.Status == "current" {
scoreB += currentlyAiringBonus
}
if a.Popularity.Total() < popularityThreshold {
scoreA -= popularityPenalty
}
if b.Popularity.Total() < popularityThreshold {
scoreB -= popularityPenalty
}
2017-11-10 10:46:05 +00:00
// If we show current shows, rank old shows a bit lower
if filterStatus == "current" {
if a.StartDate != "" && time.Since(a.StartDateTime()) > ageThreshold {
scoreA -= agePenalty
}
if b.StartDate != "" && time.Since(b.StartDateTime()) > ageThreshold {
scoreB -= agePenalty
}
}
2017-10-28 01:53:12 +00:00
scoreA += float64(a.Popularity.Watching) * watchingPopularityWeight
scoreB += float64(b.Popularity.Watching) * watchingPopularityWeight
2017-07-01 00:14:14 +00:00
2017-10-28 01:53:12 +00:00
scoreA += float64(a.Popularity.Planned) * plannedPopularityWeight
scoreB += float64(b.Popularity.Planned) * plannedPopularityWeight
2017-06-23 22:12:05 +00:00
2017-11-07 16:46:39 +00:00
scoreA += float64(a.Popularity.Completed) * completedPopularityWeight
scoreB += float64(b.Popularity.Completed) * completedPopularityWeight
2017-10-28 01:53:12 +00:00
return scoreA > scoreB
})
2017-11-07 16:00:09 +00:00
}