125 lines
3.0 KiB
Go
Raw Normal View History

2017-07-03 17:33:52 +00:00
package listimportanilist
import (
"net/http"
2018-04-05 20:36:59 +00:00
"strconv"
2017-07-03 17:33:52 +00:00
"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),
2018-04-05 20:36:59 +00:00
Episodes: match.AniListItem.Progress,
2017-07-04 22:40:03 +00:00
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,
},
2018-04-05 20:36:59 +00:00
RewatchCount: match.AniListItem.Repeat,
Private: match.AniListItem.Private,
2017-07-04 22:40:03 +00:00
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)
}
2018-04-05 20:36:59 +00:00
// Get user
anilistUser, err := anilist.GetUser(user.Accounts.AniList.Nick)
2017-07-07 23:18:21 +00:00
2018-04-05 20:36:59 +00:00
if err != nil {
return nil, ctx.Error(http.StatusBadRequest, "User doesn't exist on AniList", err)
2017-07-07 23:18:21 +00:00
}
2018-04-05 20:36:59 +00:00
// Get anime list
anilistAnimeList, err := anilist.GetAnimeList(anilistUser.ID)
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)
}
2018-04-05 20:36:59 +00:00
// Find matches
matches := findAllMatches(anilistAnimeList)
2017-07-07 23:18:21 +00:00
return matches, ""
}
2017-07-04 08:26:18 +00:00
// findAllMatches returns all matches for the anime inside an anilist anime list.
2018-04-05 20:36:59 +00:00
func findAllMatches(animeList *anilist.AnimeList) []*arn.AniListMatch {
finder := arn.NewAniListAnimeFinder()
2017-07-03 17:33:52 +00:00
matches := []*arn.AniListMatch{}
2018-04-05 20:36:59 +00:00
for _, list := range animeList.Lists {
matches = importList(matches, finder, list.Entries)
2017-07-03 17:33:52 +00:00
}
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.
2018-04-05 20:36:59 +00:00
func importList(matches []*arn.AniListMatch, finder *arn.AniListAnimeFinder, 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,
2018-04-05 20:36:59 +00:00
ARNAnime: finder.GetAnime(strconv.Itoa(item.Anime.ID), strconv.Itoa(item.Anime.MALID)),
2017-07-03 17:33:52 +00:00
})
}
return matches
}