141 lines
3.5 KiB
Go
Raw Normal View History

2017-07-03 17:33:52 +00:00
package listimportanilist
import (
"net/http"
"github.com/aerogo/aero"
2017-07-09 13:56:40 +00:00
"github.com/animenotifier/anilist"
2017-07-03 17:33:52 +00:00
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils"
)
2017-07-07 23:18:21 +00:00
// Preview shows an import preview.
func Preview(ctx *aero.Context) string {
2017-07-03 17:33:52 +00:00
user := utils.GetUser(ctx)
if user == nil {
2017-07-07 23:18:21 +00:00
return ctx.Error(http.StatusBadRequest, "Not logged in", nil)
2017-07-03 17:33:52 +00:00
}
2017-07-04 22:40:03 +00:00
matches, response := getMatches(ctx)
if response != "" {
return response
}
2017-07-04 08:26:18 +00:00
return ctx.HTML(components.ImportAnilist(user, matches))
}
2017-07-04 22:40:03 +00:00
// Finish ...
func Finish(ctx *aero.Context) string {
user := utils.GetUser(ctx)
2017-07-07 23:18:21 +00:00
if user == nil {
return ctx.Error(http.StatusBadRequest, "Not logged in", nil)
}
2017-07-04 22:40:03 +00:00
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,
2017-07-09 13:56:40 +00:00
Status: arn.AniListAnimeListStatus(match.AniListItem),
2017-07-04 22:40:03 +00:00
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")
}
2017-07-07 23:18:21 +00:00
// 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)
}
2017-07-09 13:56:40 +00:00
authErr := anilist.Authorize()
2017-07-07 23:18:21 +00:00
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)
}
2017-07-09 13:56:40 +00:00
anilistAnimeList, err := anilist.GetAnimeList(user.Accounts.AniList.Nick)
2017-07-07 23:18:21 +00:00
if err != nil {
return nil, ctx.Error(http.StatusBadRequest, "Couldn't load your anime list from AniList", err)
}
matches := findAllMatches(allAnime, anilistAnimeList)
return matches, ""
}
2017-07-04 08:26:18 +00:00
// findAllMatches returns all matches for the anime inside an anilist anime list.
2017-07-09 13:56:40 +00:00
func findAllMatches(allAnime []*arn.Anime, animeList *anilist.AnimeList) []*arn.AniListMatch {
2017-07-03 17:33:52 +00:00
matches := []*arn.AniListMatch{}
matches = importList(matches, allAnime, animeList.Lists.Watching)
matches = importList(matches, allAnime, animeList.Lists.Completed)
matches = importList(matches, allAnime, animeList.Lists.PlanToWatch)
matches = importList(matches, allAnime, animeList.Lists.OnHold)
matches = importList(matches, allAnime, animeList.Lists.Dropped)
2017-07-09 13:56:40 +00:00
custom, ok := animeList.CustomLists.(map[string][]*anilist.AnimeListItem)
2017-07-04 14:24:46 +00:00
if !ok {
return matches
}
for _, list := range custom {
2017-07-03 17:33:52 +00:00
matches = importList(matches, allAnime, list)
}
2017-07-04 08:26:18 +00:00
return matches
2017-07-03 17:33:52 +00:00
}
2017-07-04 08:26:18 +00:00
// importList imports a single list inside an anilist anime list collection.
2017-07-09 13:56:40 +00:00
func importList(matches []*arn.AniListMatch, allAnime []*arn.Anime, animeListItems []*anilist.AnimeListItem) []*arn.AniListMatch {
2017-07-03 17:33:52 +00:00
for _, item := range animeListItems {
matches = append(matches, &arn.AniListMatch{
2017-07-04 22:40:03 +00:00
AniListItem: item,
ARNAnime: arn.FindAniListAnime(item.Anime, allAnime),
2017-07-03 17:33:52 +00:00
})
}
return matches
}