diff --git a/jobs/popular-anime/main.go b/jobs/popular-anime/main.go new file mode 100644 index 00000000..ec003b54 --- /dev/null +++ b/jobs/popular-anime/main.go @@ -0,0 +1,53 @@ +package main + +import ( + "sort" + + "github.com/animenotifier/arn" + "github.com/fatih/color" +) + +// Note this is using the airing-anime as a template with modfications +// made to it. +func main() { + color.Yellow("Caching popular anime") + + animeChan, err := arn.AllAnime() + + if err != nil { + color.Red("Failed fetching anime channel") + color.Red(err.Error()) + return + } + + var animeList []*arn.Anime + + for anime := range animeChan { + animeList = append(animeList, anime) + } + + sort.Slice(animeList, func(i, j int) bool { + return animeList[i].Rating.Overall > animeList[j].Rating.Overall + }) + // Change size of anime list to 10 + animeList = animeList[:10] + + // Convert to small anime list + cache := &arn.ListOfIDs{} + + for _, anime := range animeList { + cache.IDList = append(cache.IDList, anime.ID) + } + + println(len(cache.IDList)) + + saveErr := arn.DB.Set("Cache", "popular anime", cache) + + if saveErr != nil { + color.Red("Error saving popular anime") + color.Red(saveErr.Error()) + return + } + + color.Green("Finished.") +} diff --git a/pages/popularanime/popular.go b/pages/popularanime/popular.go index 5d141f59..0d35d971 100644 --- a/pages/popularanime/popular.go +++ b/pages/popularanime/popular.go @@ -2,6 +2,8 @@ package popularanime import ( "github.com/aerogo/aero" + "github.com/animenotifier/arn" + "github.com/animenotifier/notify.moe/components" ) // Get search page. @@ -25,5 +27,11 @@ func Get(ctx *aero.Context) string { // popular, _ := arn.GetPopularCache() // return ctx.HTML(components.Search(popular.Anime, titleCount, animeCount)) - return ctx.HTML("Coming soon.") + animeList, err := arn.GetPopularAnimeCached() + + if err != nil { + return ctx.HTML("There was a problem listing anime!") + } + + return ctx.HTML(components.AnimeGrid(animeList)) }