Finished anilist importer

This commit is contained in:
Eduard Urbach 2017-07-05 00:40:03 +02:00
parent 788adcaf2d
commit 5774c51ced
4 changed files with 67 additions and 36 deletions

View File

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

View File

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

View File

@ -10,7 +10,7 @@ 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
@ -18,6 +18,6 @@ component ImportAnilist(user *arn.User, matches []*arn.AniListMatch)
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

View File

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