129 lines
3.2 KiB
Go
Raw Normal View History

2017-07-03 17:33:52 +00:00
package listimportanilist
import (
"net/http"
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils"
)
2017-07-04 22:40:03 +00:00
func getMatches(ctx *aero.Context) ([]*arn.AniListMatch, string) {
2017-07-03 17:33:52 +00:00
user := utils.GetUser(ctx)
if user == nil {
2017-07-04 22:40:03 +00:00
return nil, ctx.Error(http.StatusBadRequest, "Not logged in", nil)
2017-07-03 17:33:52 +00:00
}
authErr := arn.AniList.Authorize()
if authErr != nil {
2017-07-04 22:40:03 +00:00
return nil, ctx.Error(http.StatusBadRequest, "Couldn't authorize the Anime Notifier app on AniList", authErr)
2017-07-03 17:33:52 +00:00
}
allAnime, allErr := arn.AllAnime()
if allErr != nil {
2017-07-04 22:40:03 +00:00
return nil, ctx.Error(http.StatusBadRequest, "Couldn't load notify.moe list of all anime", allErr)
2017-07-03 17:33:52 +00:00
}
2017-07-04 22:40:03 +00:00
anilistAnimeList, err := arn.AniList.GetAnimeList(user)
2017-07-03 17:33:52 +00:00
if err != nil {
2017-07-04 22:40:03 +00:00
return nil, ctx.Error(http.StatusBadRequest, "Couldn't load your anime list from AniList", err)
2017-07-03 17:33:52 +00:00
}
2017-07-04 22:40:03 +00:00
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
}
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)
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")
}
2017-07-04 08:26:18 +00:00
// findAllMatches returns all matches for the anime inside an anilist anime list.
func findAllMatches(allAnime []*arn.Anime, animeList *arn.AniListAnimeList) []*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-04 14:24:46 +00:00
custom, ok := animeList.CustomLists.(map[string][]*arn.AniListAnimeListItem)
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-03 17:33:52 +00:00
func importList(matches []*arn.AniListMatch, allAnime []*arn.Anime, animeListItems []*arn.AniListAnimeListItem) []*arn.AniListMatch {
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
}