132 lines
3.3 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,
2018-03-27 06:19:01 +00:00
Rating: arn.AnimeListItemRating{
2017-07-04 22:40:03 +00:00
Overall: float64(match.AniListItem.ScoreRaw) / 10.0,
},
RewatchCount: match.AniListItem.Rewatched,
Created: arn.DateTimeUTC(),
Edited: arn.DateTimeUTC(),
}
animeList.Import(item)
}
2017-10-27 07:11:56 +00:00
animeList.Save()
2017-07-04 22:40:03 +00:00
return utils.SmartRedirect(ctx, "/+"+user.Nick+"/animelist/watching")
2017-07-04 22:40:03 +00:00
}
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)
}
2017-11-09 17:31:32 +00:00
allAnime := arn.AllAnime()
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
}