diff --git a/jobs/airing-anime.go b/jobs/airing-anime.go new file mode 100644 index 00000000..de10c05a --- /dev/null +++ b/jobs/airing-anime.go @@ -0,0 +1,36 @@ +package jobs + +import ( + "sort" + + "github.com/animenotifier/arn" + "github.com/fatih/color" +) + +// AiringAnimeCache ... +type AiringAnimeCache struct { + Anime []*arn.Anime `json:"anime"` +} + +// AiringAnime ... +func AiringAnime() { + animeList, err := arn.GetAiringAnime() + + if err != nil { + color.Red("Failed fetching airing anime") + color.Red(err.Error()) + return + } + + sort.Sort(arn.AnimeByPopularity(animeList)) + + saveErr := arn.SetObject("Cache", "airingAnime", &AiringAnimeCache{ + Anime: animeList, + }) + + if saveErr != nil { + color.Red("Error saving airing anime") + color.Red(saveErr.Error()) + return + } +} diff --git a/main.go b/main.go index f1ec23dc..08bafaeb 100644 --- a/main.go +++ b/main.go @@ -4,8 +4,8 @@ import ( "io/ioutil" "github.com/aerogo/aero" - "github.com/animenotifier/arn" "github.com/animenotifier/notify.moe/components" + "github.com/animenotifier/notify.moe/jobs" "github.com/animenotifier/notify.moe/pages/airing" "github.com/animenotifier/notify.moe/pages/anime" "github.com/animenotifier/notify.moe/pages/dashboard" @@ -24,11 +24,14 @@ var app = aero.New() func main() { app.SetStyle(components.BundledCSS) - user, _ := arn.GetUserByNick("Akyoto") - user.CoverImage.URL = "https://www.pixelstalk.net/wp-content/uploads/2016/10/Hanyijie-sky-scenery-ship-anime-art-1920x1080.jpg" - user.CoverImage.Position.X = "50%" - user.CoverImage.Position.Y = "0%" - user.Save() + // user, _ := arn.GetUserByNick("Akyoto") + // user.CoverImage.URL = "https://www.pixelstalk.net/wp-content/uploads/2016/10/Hanyijie-sky-scenery-ship-anime-art-1920x1080.jpg" + // user.CoverImage.Position.X = "50%" + // user.CoverImage.Position.Y = "0%" + // user.Save() + + // Background jobs + go jobs.AiringAnime() scripts, _ := ioutil.ReadFile("temp/scripts.js") js := string(scripts) diff --git a/pages/airing/airing.go b/pages/airing/airing.go index f644766d..706706c5 100644 --- a/pages/airing/airing.go +++ b/pages/airing/airing.go @@ -1,21 +1,15 @@ package airing import ( - "sort" - "github.com/aerogo/aero" "github.com/animenotifier/arn" "github.com/animenotifier/notify.moe/components" + "github.com/animenotifier/notify.moe/jobs" ) // Get ... func Get(ctx *aero.Context) string { - animeList, err := arn.GetAiringAnime() - - if err != nil { - return ctx.Error(500, "Failed fetching airing anime", err) - } - - sort.Sort(arn.AnimeByPopularity(animeList)) - return ctx.HTML(components.Airing(animeList)) + var airingAnimeCache jobs.AiringAnimeCache + arn.GetObject("Cache", "airingAnime", &airingAnimeCache) + return ctx.HTML(components.Airing(airingAnimeCache.Anime)) }