MAL import preview

This commit is contained in:
2017-07-08 01:18:21 +02:00
parent 93cfb471d9
commit f90522203d
6 changed files with 158 additions and 34 deletions

View File

@ -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
}

View File

@ -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