From f90522203d4ca55371dbe1a3be8bb08d5abadfbc Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Sat, 8 Jul 2017 01:18:21 +0200 Subject: [PATCH] MAL import preview --- main.go | 3 + pages/listimport/listimport.pixy | 17 ++-- .../anilist.scarlet => listimport.scarlet} | 3 + pages/listimport/listimportanilist/anilist.go | 67 +++++++++------- .../listimportmyanimelist/myanimelist.go | 79 +++++++++++++++++++ .../listimportmyanimelist/myanimelist.pixy | 23 ++++++ 6 files changed, 158 insertions(+), 34 deletions(-) rename pages/listimport/{listimportanilist/anilist.scarlet => listimport.scarlet} (64%) create mode 100644 pages/listimport/listimportmyanimelist/myanimelist.go create mode 100644 pages/listimport/listimportmyanimelist/myanimelist.pixy diff --git a/main.go b/main.go index e810fd01..31b25a84 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,7 @@ import ( "github.com/animenotifier/notify.moe/pages/forums" "github.com/animenotifier/notify.moe/pages/listimport" "github.com/animenotifier/notify.moe/pages/listimport/listimportanilist" + "github.com/animenotifier/notify.moe/pages/listimport/listimportmyanimelist" "github.com/animenotifier/notify.moe/pages/login" "github.com/animenotifier/notify.moe/pages/music" "github.com/animenotifier/notify.moe/pages/newsoundtrack" @@ -91,6 +92,8 @@ func configure(app *aero.Application) *aero.Application { app.Ajax("/import", listimport.Get) app.Ajax("/import/anilist/animelist", listimportanilist.Preview) app.Ajax("/import/anilist/animelist/finish", listimportanilist.Finish) + app.Ajax("/import/myanimelist/animelist", listimportmyanimelist.Preview) + app.Ajax("/import/myanimelist/animelist/finish", listimportmyanimelist.Finish) app.Ajax("/admin", admin.Get) app.Ajax("/statistics", statistics.Get) app.Ajax("/statistics/anime", statistics.Anime) diff --git a/pages/listimport/listimport.pixy b/pages/listimport/listimport.pixy index 3a8e7d57..e237e2a7 100644 --- a/pages/listimport/listimport.pixy +++ b/pages/listimport/listimport.pixy @@ -1,8 +1,13 @@ component ImportLists(user *arn.User) - .buttons + .buttons.buttons-vertical if user.Accounts.AniList.Nick != "" - a.button.mountable.ajax(href="/import/anilist/animelist") - Icon("refresh") - span Import AniList Anime List - else - p No imports available. \ No newline at end of file + .widget-input + a.button.mountable.ajax(href="/import/anilist/animelist") + Icon("download") + span Import AniList + + if user.Accounts.MyAnimeList.Nick != "" + .widget-input + a.button.mountable.ajax(href="/import/myanimelist/animelist") + Icon("download") + span Import MyAnimeList \ No newline at end of file diff --git a/pages/listimport/listimportanilist/anilist.scarlet b/pages/listimport/listimport.scarlet similarity index 64% rename from pages/listimport/listimportanilist/anilist.scarlet rename to pages/listimport/listimport.scarlet index 34b92482..63b8d829 100644 --- a/pages/listimport/listimportanilist/anilist.scarlet +++ b/pages/listimport/listimport.scarlet @@ -1,3 +1,6 @@ +.buttons-vertical + width 100% + .import-list max-width table-width-normal margin 0 auto \ No newline at end of file diff --git a/pages/listimport/listimportanilist/anilist.go b/pages/listimport/listimportanilist/anilist.go index e3adb193..12f2e8dc 100644 --- a/pages/listimport/listimportanilist/anilist.go +++ b/pages/listimport/listimportanilist/anilist.go @@ -9,39 +9,14 @@ import ( "github.com/animenotifier/notify.moe/utils" ) -func getMatches(ctx *aero.Context) ([]*arn.AniListMatch, string) { +// Preview shows an import preview. +func Preview(ctx *aero.Context) string { user := utils.GetUser(ctx) if user == nil { - return nil, ctx.Error(http.StatusBadRequest, "Not logged in", nil) + return ctx.Error(http.StatusBadRequest, "Not logged in", nil) } - authErr := arn.AniList.Authorize() - - if authErr != nil { - return nil, ctx.Error(http.StatusBadRequest, "Couldn't authorize the Anime Notifier app on AniList", authErr) - } - - allAnime, allErr := arn.AllAnime() - - if allErr != nil { - return nil, ctx.Error(http.StatusBadRequest, "Couldn't load notify.moe list of all anime", allErr) - } - - anilistAnimeList, err := arn.AniList.GetAnimeList(user) - - if err != nil { - return nil, ctx.Error(http.StatusBadRequest, "Couldn't load your anime list from AniList", err) - } - - matches := findAllMatches(allAnime, anilistAnimeList) - - return matches, "" -} - -// Preview ... -func Preview(ctx *aero.Context) string { - user := utils.GetUser(ctx) matches, response := getMatches(ctx) if response != "" { @@ -54,6 +29,11 @@ func Preview(ctx *aero.Context) string { // Finish ... func Finish(ctx *aero.Context) string { user := utils.GetUser(ctx) + + if user == nil { + return ctx.Error(http.StatusBadRequest, "Not logged in", nil) + } + matches, response := getMatches(ctx) if response != "" { @@ -92,6 +72,37 @@ func Finish(ctx *aero.Context) string { return ctx.Redirect("/+" + user.Nick + "/animelist") } +// getMatches finds and returns all matches for the logged in user. +func getMatches(ctx *aero.Context) ([]*arn.AniListMatch, string) { + user := utils.GetUser(ctx) + + if user == nil { + return nil, ctx.Error(http.StatusBadRequest, "Not logged in", nil) + } + + authErr := arn.AniList.Authorize() + + if authErr != nil { + return nil, ctx.Error(http.StatusBadRequest, "Couldn't authorize the Anime Notifier app on AniList", authErr) + } + + allAnime, allErr := arn.AllAnime() + + if allErr != nil { + return nil, ctx.Error(http.StatusBadRequest, "Couldn't load notify.moe list of all anime", allErr) + } + + anilistAnimeList, err := arn.AniList.GetAnimeList(user) + + if err != nil { + return nil, ctx.Error(http.StatusBadRequest, "Couldn't load your anime list from AniList", err) + } + + matches := findAllMatches(allAnime, anilistAnimeList) + + return matches, "" +} + // findAllMatches returns all matches for the anime inside an anilist anime list. func findAllMatches(allAnime []*arn.Anime, animeList *arn.AniListAnimeList) []*arn.AniListMatch { matches := []*arn.AniListMatch{} diff --git a/pages/listimport/listimportmyanimelist/myanimelist.go b/pages/listimport/listimportmyanimelist/myanimelist.go new file mode 100644 index 00000000..20050750 --- /dev/null +++ b/pages/listimport/listimportmyanimelist/myanimelist.go @@ -0,0 +1,79 @@ +package listimportmyanimelist + +import ( + "net/http" + + "github.com/aerogo/aero" + "github.com/animenotifier/arn" + "github.com/animenotifier/mal" + "github.com/animenotifier/notify.moe/components" + "github.com/animenotifier/notify.moe/utils" +) + +// Preview shows an import preview. +func Preview(ctx *aero.Context) string { + user := utils.GetUser(ctx) + + if user == nil { + return ctx.Error(http.StatusBadRequest, "Not logged in", nil) + } + + matches, response := getMatches(ctx) + + if response != "" { + return response + } + + return ctx.HTML(components.ImportMyAnimeList(user, matches)) +} + +// Finish ... +func Finish(ctx *aero.Context) string { + user := utils.GetUser(ctx) + + if user == nil { + return ctx.Error(http.StatusBadRequest, "Not logged in", nil) + } + + return ctx.Redirect("/+" + user.Nick + "/animelist") +} + +// getMatches finds and returns all matches for the logged in user. +func getMatches(ctx *aero.Context) ([]*arn.MyAnimeListMatch, string) { + user := utils.GetUser(ctx) + + if user == nil { + return nil, ctx.Error(http.StatusBadRequest, "Not logged in", nil) + } + + malAnimeList, err := mal.GetAnimeList(user.Accounts.MyAnimeList.Nick) + + if err != nil { + return nil, ctx.Error(http.StatusBadRequest, "Couldn't load your anime list from MyAnimeList", err) + } + + matches := findAllMatches(malAnimeList) + + return matches, "" +} + +// findAllMatches returns all matches for the anime inside an anilist anime list. +func findAllMatches(animeList *mal.AnimeList) []*arn.MyAnimeListMatch { + matches := []*arn.MyAnimeListMatch{} + + for _, item := range animeList.Items { + var anime *arn.Anime + connection, err := arn.GetMyAnimeListToAnime(item.AnimeID) + + if err == nil { + anime, _ = arn.GetAnime(connection.AnimeID) + } + + matches = append(matches, &arn.MyAnimeListMatch{ + MyAnimeListItem: item, + ARNAnime: anime, + }) + } + + return matches +} diff --git a/pages/listimport/listimportmyanimelist/myanimelist.pixy b/pages/listimport/listimportmyanimelist/myanimelist.pixy new file mode 100644 index 00000000..2908e126 --- /dev/null +++ b/pages/listimport/listimportmyanimelist/myanimelist.pixy @@ -0,0 +1,23 @@ +component ImportMyAnimeList(user *arn.User, matches []*arn.MyAnimeListMatch) + h1= "myanimelist.net Import (" + user.Accounts.MyAnimeList.Nick + ", " + toString(len(matches)) + " anime)" + + table.import-list + thead + tr + th myanimelist.net + th notify.moe + tbody + each match in matches + tr + td + a(href=match.MyAnimeListItem.AnimeLink(), target="_blank", rel="noopener")= match.MyAnimeListItem.AnimeTitle + td + if match.ARNAnime == nil + span.import-error Not found on notify.moe + else + a(href=match.ARNAnime.Link(), target="_blank", rel="noopener")= match.ARNAnime.Title.Canonical + + .buttons + a.button.mountable(href="/import/myanimelist/animelist/finish") + Icon("refresh") + span Import \ No newline at end of file