diff --git a/pages/index.go b/pages/index.go index 4d8cee5b..07da208e 100644 --- a/pages/index.go +++ b/pages/index.go @@ -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 diff --git a/pages/popular/anime.go b/pages/popular/anime.go new file mode 100644 index 00000000..4f8660c2 --- /dev/null +++ b/pages/popular/anime.go @@ -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) +}