From ca90d40ca9112c823d10d9cd505d243d0a67de53 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Mon, 26 Mar 2018 23:38:15 +0200 Subject: [PATCH] Show sequels of completed anime --- mixins/AnimeGrid.pixy | 25 ++++++-- pages/explore/explore.pixy | 10 +++- pages/explore/explorerelations/relations.pixy | 8 +++ pages/explore/explorerelations/sequels.go | 57 +++++++++++++++++++ pages/index.go | 2 + utils/AnimeWithRelatedAnime.go | 9 +++ 6 files changed, 104 insertions(+), 7 deletions(-) create mode 100644 pages/explore/explorerelations/relations.pixy create mode 100644 pages/explore/explorerelations/sequels.go create mode 100644 utils/AnimeWithRelatedAnime.go diff --git a/mixins/AnimeGrid.pixy b/mixins/AnimeGrid.pixy index a47a9736..41c6785d 100644 --- a/mixins/AnimeGrid.pixy +++ b/mixins/AnimeGrid.pixy @@ -2,14 +2,31 @@ component AnimeGrid(animes []*arn.Anime, user *arn.User) #load-more-target.anime-grid AnimeGridScrollable(animes, user) +component AnimeGridWithRelation(entries []*utils.AnimeWithRelatedAnime, user *arn.User) + #load-more-target.anime-grid + AnimeGridWithRelationScrollable(entries, user) + component AnimeGridScrollable(animes []*arn.Anime, user *arn.User) each anime in animes .anime-grid-cell(data-added=(user != nil && user.AnimeList().Contains(anime.ID))) - a(href="/anime/" + toString(anime.ID)) + a(href="/anime/" + anime.ID) img.anime-grid-image.lazy(data-src=anime.ImageLink("medium"), data-webp="true", data-color=anime.AverageColor(), alt=anime.Title.Romaji) .anime-grid-title .anime-grid-title-text= anime.Title.ByUser(user) - if user != nil && !user.AnimeList().Contains(anime.ID) - button.anime-grid-add-button.action(data-action="addAnimeToCollection", data-trigger="click", data-api="/api/animelist/" + user.ID, data-anime-id=anime.ID) - RawIcon("plus") \ No newline at end of file + AnimeGridButton(anime, user) + +component AnimeGridWithRelationScrollable(entries []*utils.AnimeWithRelatedAnime, user *arn.User) + each entry in entries + .anime-grid-cell(data-added=(user != nil && user.AnimeList().Contains(entry.Anime.ID))) + a(href="/anime/" + entry.Anime.ID) + img.anime-grid-image.lazy(data-src=entry.Anime.ImageLink("medium"), data-webp="true", data-color=entry.Anime.AverageColor(), alt=entry.Anime.Title.Romaji) + .anime-grid-title + .anime-grid-title-text= entry.Anime.Title.ByUser(user) + + AnimeGridButton(entry.Anime, user) + +component AnimeGridButton(anime *arn.Anime, user *arn.User) + if user != nil && !user.AnimeList().Contains(anime.ID) + button.anime-grid-add-button.action(data-action="addAnimeToCollection", data-trigger="click", data-api="/api/animelist/" + user.ID, data-anime-id=anime.ID) + RawIcon("plus") \ No newline at end of file diff --git a/pages/explore/explore.pixy b/pages/explore/explore.pixy index c355f8e6..598c15b1 100644 --- a/pages/explore/explore.pixy +++ b/pages/explore/explore.pixy @@ -1,4 +1,4 @@ -component ExploreAnime(animeList []*arn.Anime, year string, status string, typ string, user *arn.User) +component ExploreAnime(animes []*arn.Anime, year string, status string, typ string, user *arn.User) #filter-root(data-url="/explore/anime") ExploreFilters(year, status, typ, false) @@ -7,6 +7,10 @@ component ExploreAnime(animeList []*arn.Anime, year string, status string, typ s button.action(data-trigger="click", data-action="hideAddedAnime", title="Hide anime in my collection") RawIcon("eye-slash") + if user != nil + a.button(href="/explore/sequels", title="View sequels of my completed anime") + RawIcon("forward") + a.button(href="/explore/color/any/anime", title="View colors") RawIcon("paint-brush") @@ -16,10 +20,10 @@ component ExploreAnime(animeList []*arn.Anime, year string, status string, typ s h1.page-title Explore .explore-anime - if len(animeList) == 0 + if len(animes) == 0 p.no-data.mountable No anime found using the above filters. else - AnimeGrid(animeList, user) + AnimeGrid(animes, user) component ExploreFilters(year string, status string, typ string, advancedFilters bool) .explore-filters diff --git a/pages/explore/explorerelations/relations.pixy b/pages/explore/explorerelations/relations.pixy new file mode 100644 index 00000000..1faf32b3 --- /dev/null +++ b/pages/explore/explorerelations/relations.pixy @@ -0,0 +1,8 @@ +component ExploreAnimeSequels(entries []*utils.AnimeWithRelatedAnime, user *arn.User) + h1 Sequels of my completed anime + + .explore-anime + if len(entries) == 0 + p.no-data.mountable No sequels found for the anime you completed. + else + AnimeGridWithRelation(entries, user) \ No newline at end of file diff --git a/pages/explore/explorerelations/sequels.go b/pages/explore/explorerelations/sequels.go new file mode 100644 index 00000000..36bad102 --- /dev/null +++ b/pages/explore/explorerelations/sequels.go @@ -0,0 +1,57 @@ +package explorerelations + +import ( + "net/http" + "sort" + + "github.com/animenotifier/arn" + + "github.com/aerogo/aero" + "github.com/animenotifier/notify.moe/components" + "github.com/animenotifier/notify.moe/utils" +) + +// Sequels ... +func Sequels(ctx *aero.Context) string { + user := utils.GetUser(ctx) + + if user == nil { + return ctx.Error(http.StatusUnauthorized, "Not logged in", nil) + } + + animeList := user.AnimeList() + sequels := []*utils.AnimeWithRelatedAnime{} + + for anime := range arn.StreamAnime() { + if animeList.Contains(anime.ID) { + continue + } + + prequels := anime.Prequels() + + for _, prequel := range prequels { + item := animeList.Find(prequel.ID) + + if item != nil && item.Status == arn.AnimeListStatusCompleted { + sequels = append(sequels, &utils.AnimeWithRelatedAnime{ + Anime: anime, + Related: prequel, + }) + break + } + } + } + + sort.Slice(sequels, func(i, j int) bool { + aScore := sequels[i].Anime.Score() + bScore := sequels[j].Anime.Score() + + if aScore == bScore { + return sequels[i].Anime.Title.Canonical < sequels[j].Anime.Title.Canonical + } + + return aScore > bScore + }) + + return ctx.HTML(components.ExploreAnimeSequels(sequels, user)) +} diff --git a/pages/index.go b/pages/index.go index d398c2cb..e8249482 100644 --- a/pages/index.go +++ b/pages/index.go @@ -28,6 +28,7 @@ import ( "github.com/animenotifier/notify.moe/pages/episode" "github.com/animenotifier/notify.moe/pages/explore" "github.com/animenotifier/notify.moe/pages/explore/explorecolor" + "github.com/animenotifier/notify.moe/pages/explore/explorerelations" "github.com/animenotifier/notify.moe/pages/forum" "github.com/animenotifier/notify.moe/pages/genre" "github.com/animenotifier/notify.moe/pages/genres" @@ -79,6 +80,7 @@ func Configure(app *aero.Application) { l.Page("/explore/anime/:year/:status/:type", explore.Filter) l.Page("/explore/color/:color/anime", explorecolor.AnimeByAverageColor) l.Page("/explore/color/:color/anime/from/:index", explorecolor.AnimeByAverageColor) + l.Page("/explore/sequels", explorerelations.Sequels) l.Page("/login", login.Get) l.Page("/api", apiview.Get) // l.Ajax("/dashboard", dashboard.Get) diff --git a/utils/AnimeWithRelatedAnime.go b/utils/AnimeWithRelatedAnime.go new file mode 100644 index 00000000..e6a9b568 --- /dev/null +++ b/utils/AnimeWithRelatedAnime.go @@ -0,0 +1,9 @@ +package utils + +import "github.com/animenotifier/arn" + +// AnimeWithRelatedAnime ... +type AnimeWithRelatedAnime struct { + Anime *arn.Anime + Related *arn.Anime +}