Added API for popular anime titles

This commit is contained in:
Eduard Urbach 2017-11-16 16:38:49 +01:00
parent a3b553d8de
commit 0d0ae75bd4
2 changed files with 38 additions and 0 deletions

View File

@ -33,6 +33,7 @@ import (
"github.com/animenotifier/notify.moe/pages/newthread"
"github.com/animenotifier/notify.moe/pages/notifications"
"github.com/animenotifier/notify.moe/pages/paypal"
"github.com/animenotifier/notify.moe/pages/popular"
"github.com/animenotifier/notify.moe/pages/posts"
"github.com/animenotifier/notify.moe/pages/profile"
"github.com/animenotifier/notify.moe/pages/search"
@ -175,6 +176,7 @@ func Configure(app *aero.Application) {
// API
app.Get("/api/me", me.Get)
app.Get("/api/popular/anime/titles/:count", popular.AnimeTitles)
app.Get("/api/test/notification", notifications.Test)
// PayPal

36
pages/popular/anime.go Normal file
View File

@ -0,0 +1,36 @@
package popular
import (
"net/http"
"strings"
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
)
// AnimeTitles returns a list of the 500 most popular anime titles.
func AnimeTitles(ctx *aero.Context) string {
maxLength, err := ctx.GetInt("count")
if err != nil {
return ctx.Error(http.StatusBadRequest, "Invalid value for count parameter", err)
}
popularAnimeTitles := []string{}
popularAnime := arn.AllAnime()
arn.SortAnimeByPopularity(popularAnime)
if len(popularAnime) > maxLength {
popularAnime = popularAnime[:maxLength]
}
for _, anime := range popularAnime {
popularAnimeTitles = append(popularAnimeTitles, strings.ToLower(anime.Title.Canonical))
if arn.ContainsUnicodeLetters(anime.Title.Japanese) {
popularAnimeTitles = append(popularAnimeTitles, anime.Title.Japanese)
}
}
return ctx.JSON(popularAnimeTitles)
}