2018-02-24 12:19:11 +00:00
|
|
|
package recommended
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/aerogo/aero"
|
|
|
|
"github.com/animenotifier/arn"
|
|
|
|
"github.com/animenotifier/notify.moe/components"
|
2018-02-24 14:35:34 +00:00
|
|
|
"github.com/animenotifier/notify.moe/utils"
|
2018-02-24 12:19:11 +00:00
|
|
|
)
|
|
|
|
|
2018-02-24 13:51:47 +00:00
|
|
|
const (
|
|
|
|
maxRecommendations = 20
|
|
|
|
worstGenreCount = 5
|
|
|
|
)
|
2018-02-24 12:19:11 +00:00
|
|
|
|
|
|
|
// Anime shows a list of recommended anime.
|
|
|
|
func Anime(ctx *aero.Context) string {
|
2018-02-24 14:35:34 +00:00
|
|
|
user := utils.GetUser(ctx)
|
2018-02-24 12:19:11 +00:00
|
|
|
nick := ctx.Get("nick")
|
2018-02-24 14:35:34 +00:00
|
|
|
viewUser, err := arn.GetUserByNick(nick)
|
2018-02-24 12:19:11 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Error(http.StatusUnauthorized, "Not logged in", err)
|
|
|
|
}
|
|
|
|
|
2018-02-24 14:35:34 +00:00
|
|
|
animeList := viewUser.AnimeList()
|
2018-02-24 12:19:11 +00:00
|
|
|
genreItems := animeList.Genres()
|
|
|
|
genreAffinity := map[string]float64{}
|
2018-02-24 13:51:47 +00:00
|
|
|
worstGenres := []string{}
|
2018-02-24 12:19:11 +00:00
|
|
|
|
|
|
|
for genre, animeListItems := range genreItems {
|
|
|
|
affinity := 0.0
|
|
|
|
|
|
|
|
for _, item := range animeListItems {
|
2018-02-24 13:51:47 +00:00
|
|
|
// if item.Status == arn.AnimeListStatusDropped {
|
|
|
|
// affinity -= 5.0
|
|
|
|
// continue
|
|
|
|
// }
|
2018-02-24 12:19:11 +00:00
|
|
|
|
|
|
|
if item.Rating.Overall != 0 {
|
|
|
|
affinity += item.Rating.Overall
|
|
|
|
} else {
|
|
|
|
affinity += 5.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
genreAffinity[genre] = affinity
|
2018-02-24 13:51:47 +00:00
|
|
|
worstGenres = append(worstGenres, genre)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(worstGenres, func(i, j int) bool {
|
|
|
|
return genreAffinity[worstGenres[i]] < genreAffinity[worstGenres[j]]
|
|
|
|
})
|
|
|
|
|
|
|
|
if len(worstGenres) > worstGenreCount {
|
|
|
|
worstGenres = worstGenres[:worstGenreCount]
|
2018-02-24 12:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get all anime
|
|
|
|
recommendations := arn.AllAnime()
|
|
|
|
|
|
|
|
// Affinity maps an anime ID to a number that indicates how likely a user is going to enjoy that anime.
|
|
|
|
affinity := map[string]float64{}
|
|
|
|
|
|
|
|
// Calculate affinity for each anime
|
|
|
|
for _, anime := range recommendations {
|
2018-03-04 00:02:30 +00:00
|
|
|
// Skip anime that are upcoming or tba
|
|
|
|
if anime.Status == "upcoming" || anime.Status == "tba" {
|
2018-02-24 13:51:47 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-02-24 12:19:11 +00:00
|
|
|
// Skip anime from my list (except planned anime)
|
|
|
|
existing := animeList.Find(anime.ID)
|
|
|
|
|
|
|
|
if existing != nil && existing.Status != arn.AnimeListStatusPlanned {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-02-24 13:51:47 +00:00
|
|
|
// Skip anime that don't have one of the top genres for that user
|
|
|
|
worstGenreFound := false
|
2018-02-24 12:19:11 +00:00
|
|
|
|
2018-02-24 13:51:47 +00:00
|
|
|
for _, genre := range anime.Genres {
|
|
|
|
if arn.Contains(worstGenres, genre) {
|
|
|
|
worstGenreFound = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2018-02-24 12:19:11 +00:00
|
|
|
|
2018-02-24 13:51:47 +00:00
|
|
|
if worstGenreFound {
|
|
|
|
continue
|
|
|
|
}
|
2018-02-24 12:19:11 +00:00
|
|
|
|
2018-02-24 13:51:47 +00:00
|
|
|
animeAffinity := 0.0
|
2018-02-24 12:19:11 +00:00
|
|
|
|
2018-02-24 13:51:47 +00:00
|
|
|
// Planned anime go higher
|
|
|
|
if existing != nil && existing.Status == arn.AnimeListStatusPlanned {
|
|
|
|
animeAffinity += 75.0
|
|
|
|
}
|
|
|
|
|
|
|
|
animeAffinity += float64(anime.Popularity.Total())
|
2018-03-16 14:53:20 +00:00
|
|
|
animeAffinity += anime.Rating.Overall * 80
|
2018-02-24 13:51:47 +00:00
|
|
|
affinity[anime.ID] = animeAffinity
|
2018-02-24 12:19:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sort
|
|
|
|
sort.Slice(recommendations, func(i, j int) bool {
|
2018-02-24 13:51:47 +00:00
|
|
|
affinityA := affinity[recommendations[i].ID]
|
|
|
|
affinityB := affinity[recommendations[j].ID]
|
|
|
|
|
|
|
|
if affinityA == affinityB {
|
|
|
|
return recommendations[i].Title.Canonical < recommendations[j].Title.Canonical
|
|
|
|
}
|
|
|
|
|
|
|
|
return affinityA > affinityB
|
2018-02-24 12:19:11 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Take the top 10
|
|
|
|
if len(recommendations) > maxRecommendations {
|
|
|
|
recommendations = recommendations[:maxRecommendations]
|
|
|
|
}
|
|
|
|
|
2018-02-24 14:35:34 +00:00
|
|
|
return ctx.HTML(components.RecommendedAnime(recommendations, worstGenres, viewUser, user))
|
2018-02-24 12:19:11 +00:00
|
|
|
}
|