From 5774c51ced1c07355be8a4395ddfc97999fd9d1b Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 5 Jul 2017 00:40:03 +0200 Subject: [PATCH] Finished anilist importer --- main.go | 3 +- pages/listimport/listimportanilist/anilist.go | 72 ++++++++++++++++--- .../listimport/listimportanilist/anilist.pixy | 6 +- pages/profile/profile.go | 22 ------ 4 files changed, 67 insertions(+), 36 deletions(-) diff --git a/main.go b/main.go index 83e5dd83..dd2b9390 100644 --- a/main.go +++ b/main.go @@ -82,7 +82,8 @@ func configure(app *aero.Application) *aero.Application { app.Ajax("/settings", settings.Get) app.Ajax("/music", music.Get) app.Ajax("/import", listimport.Get) - app.Ajax("/import/anilist/animelist", listimportanilist.Get) + app.Ajax("/import/anilist/animelist", listimportanilist.Preview) + app.Ajax("/import/anilist/animelist/finish", listimportanilist.Finish) app.Ajax("/admin", admin.Get) app.Ajax("/search", search.Get) app.Ajax("/search/:term", search.Get) diff --git a/pages/listimport/listimportanilist/anilist.go b/pages/listimport/listimportanilist/anilist.go index f5372148..e3adb193 100644 --- a/pages/listimport/listimportanilist/anilist.go +++ b/pages/listimport/listimportanilist/anilist.go @@ -9,37 +9,89 @@ import ( "github.com/animenotifier/notify.moe/utils" ) -// Get ... -func Get(ctx *aero.Context) string { +func getMatches(ctx *aero.Context) ([]*arn.AniListMatch, string) { user := utils.GetUser(ctx) if user == nil { - return ctx.Error(http.StatusBadRequest, "Not logged in", nil) + return nil, ctx.Error(http.StatusBadRequest, "Not logged in", nil) } authErr := arn.AniList.Authorize() if authErr != nil { - return ctx.Error(http.StatusBadRequest, "Couldn't authorize the Anime Notifier app on AniList", authErr) + return nil, ctx.Error(http.StatusBadRequest, "Couldn't authorize the Anime Notifier app on AniList", authErr) } allAnime, allErr := arn.AllAnime() if allErr != nil { - return ctx.Error(http.StatusBadRequest, "Couldn't load notify.moe list of all anime", allErr) + return nil, ctx.Error(http.StatusBadRequest, "Couldn't load notify.moe list of all anime", allErr) } - animeList, err := arn.AniList.GetAnimeList(user) + anilistAnimeList, err := arn.AniList.GetAnimeList(user) if err != nil { - return ctx.Error(http.StatusBadRequest, "Couldn't load your anime list from AniList", err) + return nil, ctx.Error(http.StatusBadRequest, "Couldn't load your anime list from AniList", err) } - matches := findAllMatches(allAnime, animeList) + matches := findAllMatches(allAnime, anilistAnimeList) + + return matches, "" +} + +// Preview ... +func Preview(ctx *aero.Context) string { + user := utils.GetUser(ctx) + matches, response := getMatches(ctx) + + if response != "" { + return response + } return ctx.HTML(components.ImportAnilist(user, matches)) } +// Finish ... +func Finish(ctx *aero.Context) string { + user := utils.GetUser(ctx) + matches, response := getMatches(ctx) + + if response != "" { + return response + } + + animeList := user.AnimeList() + + for _, match := range matches { + if match.ARNAnime == nil || match.AniListItem == nil { + continue + } + + item := &arn.AnimeListItem{ + AnimeID: match.ARNAnime.ID, + Status: match.AniListItem.AnimeListStatus(), + Episodes: match.AniListItem.EpisodesWatched, + Notes: match.AniListItem.Notes, + Rating: &arn.AnimeRating{ + Overall: float64(match.AniListItem.ScoreRaw) / 10.0, + }, + RewatchCount: match.AniListItem.Rewatched, + Created: arn.DateTimeUTC(), + Edited: arn.DateTimeUTC(), + } + + animeList.Import(item) + } + + err := animeList.Save() + + if err != nil { + return ctx.Error(http.StatusInternalServerError, "Error saving your anime list", err) + } + + return ctx.Redirect("/+" + user.Nick + "/animelist") +} + // 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{} @@ -67,8 +119,8 @@ func findAllMatches(allAnime []*arn.Anime, animeList *arn.AniListAnimeList) []*a func importList(matches []*arn.AniListMatch, allAnime []*arn.Anime, animeListItems []*arn.AniListAnimeListItem) []*arn.AniListMatch { for _, item := range animeListItems { matches = append(matches, &arn.AniListMatch{ - AniListAnime: item.Anime, - ARNAnime: arn.FindAniListAnime(item.Anime, allAnime), + AniListItem: item, + ARNAnime: arn.FindAniListAnime(item.Anime, allAnime), }) } diff --git a/pages/listimport/listimportanilist/anilist.pixy b/pages/listimport/listimportanilist/anilist.pixy index 51562268..c8e4772f 100644 --- a/pages/listimport/listimportanilist/anilist.pixy +++ b/pages/listimport/listimportanilist/anilist.pixy @@ -10,14 +10,14 @@ component ImportAnilist(user *arn.User, matches []*arn.AniListMatch) each match in matches tr td - a(href=match.AniListAnime.Link(), target="_blank", rel="noopener")= match.AniListAnime.TitleRomaji + a(href=match.AniListItem.Anime.Link(), target="_blank", rel="noopener")= match.AniListItem.Anime.TitleRomaji 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 - .button.mountable.action(data-action="soon", data-trigger="click") + a.button.mountable(href="/import/anilist/animelist/finish") Icon("refresh") span Import \ No newline at end of file diff --git a/pages/profile/profile.go b/pages/profile/profile.go index 16117f39..e54912cd 100644 --- a/pages/profile/profile.go +++ b/pages/profile/profile.go @@ -35,28 +35,6 @@ func Profile(ctx *aero.Context, viewUser *arn.User) string { user = utils.GetUser(ctx) }, func() { animeList = viewUser.AnimeList() - }, func() { - // threads = viewUser.Threads() - - // arn.SortThreadsLatestFirst(threads) - - // if len(threads) > maxPosts { - // threads = threads[:maxPosts] - // } - }, func() { - // posts = viewUser.Posts() - // arn.SortPostsLatestFirst(posts) - - // if len(posts) > maxPosts { - // posts = posts[:maxPosts] - // } - }, func() { - // tracks = viewUser.SoundTracks() - // arn.SortSoundTracksLatestFirst(tracks) - - // if len(tracks) > maxTracks { - // tracks = tracks[:maxTracks] - // } }) return ctx.HTML(components.Profile(viewUser, user, animeList, threads, posts, tracks))