Added anime grid for genres (up to 100 for now)

This commit is contained in:
Eduard Urbach 2017-11-30 01:47:12 +01:00
parent 1c2cd2cee7
commit 91b6ae7efd
7 changed files with 46 additions and 86 deletions

View File

@ -66,7 +66,14 @@ func AnimeList(ctx *aero.Context) string {
} }
sort.Slice(comparisons, func(i, j int) bool { sort.Slice(comparisons, func(i, j int) bool {
return comparisons[i].Anime.Popularity.Total() > comparisons[j].Anime.Popularity.Total() aPopularity := comparisons[i].Anime.Popularity.Total()
bPopularity := comparisons[j].Anime.Popularity.Total()
if aPopularity == bPopularity {
return comparisons[i].Anime.Title.Canonical < comparisons[j].Anime.Title.Canonical
}
return aPopularity > bPopularity
}) })
return ctx.HTML(components.CompareAnimeList(a, b, countA, countB, comparisons, user)) return ctx.HTML(components.CompareAnimeList(a, b, countA, countB, comparisons, user))

View File

@ -1,26 +1,12 @@
package explore package explore
import ( import (
"sort"
"time"
"github.com/aerogo/aero" "github.com/aerogo/aero"
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components" "github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils" "github.com/animenotifier/notify.moe/utils"
) )
const (
currentlyAiringBonus = 5.0
popularityThreshold = 5
popularityPenalty = 8.0
watchingPopularityWeight = 0.3
completedPopularityWeight = 0.3
plannedPopularityWeight = 0.2
agePenalty = 11.0
ageThreshold = 6 * 30 * 24 * time.Hour
)
// Get ... // Get ...
func Get(ctx *aero.Context) string { func Get(ctx *aero.Context) string {
year := "2017" year := "2017"
@ -67,54 +53,6 @@ func filterAnime(year, status, typ string) []*arn.Anime {
results = append(results, anime) results = append(results, anime)
} }
sortAnime(results, status) arn.SortAnimeByQuality(results, status)
return results return results
} }
func sortAnime(animeList []*arn.Anime, filterStatus string) {
sort.Slice(animeList, func(i, j int) bool {
a := animeList[i]
b := animeList[j]
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
}
// 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
}
}
scoreA += float64(a.Popularity.Watching) * watchingPopularityWeight
scoreB += float64(b.Popularity.Watching) * watchingPopularityWeight
scoreA += float64(a.Popularity.Planned) * plannedPopularityWeight
scoreB += float64(b.Popularity.Planned) * plannedPopularityWeight
scoreA += float64(a.Popularity.Completed) * completedPopularityWeight
scoreB += float64(b.Popularity.Completed) * completedPopularityWeight
return scoreA > scoreB
})
}

View File

@ -1,21 +1,44 @@
package genre package genre
import ( import (
"strings"
"github.com/aerogo/aero" "github.com/aerogo/aero"
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components" "github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils" "github.com/animenotifier/notify.moe/utils"
) )
const animePerPage = 100
// Get ... // Get ...
func Get(ctx *aero.Context) string { func Get(ctx *aero.Context) string {
user := utils.GetUser(ctx) user := utils.GetUser(ctx)
genreName := ctx.Get("name") genreName := ctx.Get("name")
genre, err := arn.GetGenre(genreName) animes := []*arn.Anime{}
if err != nil { for anime := range arn.StreamAnime() {
return ctx.Error(404, "Genre not found", err) if containsLowerCase(anime.Genres, genreName) {
animes = append(animes, anime)
}
} }
return ctx.HTML(components.Genre(genre, user)) arn.SortAnimeByQuality(animes, "")
if len(animes) > animePerPage {
animes = animes[:animePerPage]
}
return ctx.HTML(components.Genre(genreName, animes, user))
}
// 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
}
}
return false
} }

View File

@ -1,3 +1,3 @@
component Genre(genre *arn.Genre, user *arn.User) component Genre(genre string, animes []*arn.Anime, user *arn.User)
h2(title=toString(len(genre.AnimeList)) + " anime")= arn.Capitalize(genre.ID) h1(title=toString(len(animes)) + " anime")= strings.Title(genre)
AnimeGrid(genre.AnimeList, user) AnimeGrid(animes, user)

View File

@ -1,8 +1,6 @@
package genres package genres
import ( import (
"fmt"
"github.com/aerogo/aero" "github.com/aerogo/aero"
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components" "github.com/animenotifier/notify.moe/components"
@ -10,22 +8,15 @@ import (
// Get ... // Get ...
func Get(ctx *aero.Context) string { func Get(ctx *aero.Context) string {
var genres []*arn.Genre var genres []string
for _, genreName := range arn.Genres { for _, genre := range arn.Genres {
// Skip this genre because it doesn't get processed in the background jobs // Skip this genre because it doesn't get processed in the background jobs
if genreName == "Hentai" { if genreName == "Hentai" {
continue continue
} }
genre, err := arn.GetGenre(arn.GetGenreIDByName(genreName)) genres = append(genres, genre)
if err == nil {
genre.Name = genreName
genres = append(genres, genre)
} else {
fmt.Println(err)
}
} }
return ctx.HTML(components.Genres(genres)) return ctx.HTML(components.Genres(genres))

View File

@ -1,4 +1,4 @@
component Genres(genres []*arn.Genre) component Genres(genres []string)
//- h1.page-title Genres //- h1.page-title Genres
//- .genres //- .genres

View File

@ -23,6 +23,7 @@ import (
"github.com/animenotifier/notify.moe/pages/embed" "github.com/animenotifier/notify.moe/pages/embed"
"github.com/animenotifier/notify.moe/pages/explore" "github.com/animenotifier/notify.moe/pages/explore"
"github.com/animenotifier/notify.moe/pages/forum" "github.com/animenotifier/notify.moe/pages/forum"
"github.com/animenotifier/notify.moe/pages/genre"
"github.com/animenotifier/notify.moe/pages/group" "github.com/animenotifier/notify.moe/pages/group"
"github.com/animenotifier/notify.moe/pages/groups" "github.com/animenotifier/notify.moe/pages/groups"
"github.com/animenotifier/notify.moe/pages/home" "github.com/animenotifier/notify.moe/pages/home"
@ -201,6 +202,6 @@ func Configure(app *aero.Application) {
app.Post("/api/paypal/payment/create", paypal.CreatePayment) app.Post("/api/paypal/payment/create", paypal.CreatePayment)
// Genres // Genres
// l.Ajax("/genres", genres.Get) // l.Page("/genres", genres.Get)
// l.Ajax("/genres/:name", genre.Get) l.Page("/genre/:name", genre.Get)
} }