111 lines
2.5 KiB
Go
Raw Normal View History

2017-07-07 23:18:21 +00:00
package listimportmyanimelist
import (
"net/http"
2017-07-08 00:41:13 +00:00
"strconv"
2017-07-07 23:18:21 +00:00
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/mal"
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils"
)
// Preview shows an import preview.
func Preview(ctx *aero.Context) string {
user := utils.GetUser(ctx)
if user == nil {
2018-07-07 03:42:00 +00:00
return ctx.Error(http.StatusBadRequest, "Not logged in")
2017-07-07 23:18:21 +00:00
}
matches, response := getMatches(ctx)
if response != "" {
return response
}
return ctx.HTML(components.ImportMyAnimeList(user, matches))
}
// Finish ...
func Finish(ctx *aero.Context) string {
user := utils.GetUser(ctx)
if user == nil {
2018-07-07 03:42:00 +00:00
return ctx.Error(http.StatusBadRequest, "Not logged in")
2017-07-07 23:18:21 +00:00
}
2017-07-08 00:41:13 +00:00
matches, response := getMatches(ctx)
if response != "" {
return response
}
animeList := user.AnimeList()
for _, match := range matches {
if match.ARNAnime == nil || match.MyAnimeListItem == nil {
continue
}
2018-11-06 05:43:36 +00:00
rewatchCount := 0
2017-07-08 00:41:13 +00:00
2018-11-06 05:43:36 +00:00
if match.MyAnimeListItem.IsRewatching {
rewatchCount = 1
2017-07-08 00:41:13 +00:00
}
item := &arn.AnimeListItem{
AnimeID: match.ARNAnime.ID,
2018-11-06 05:43:36 +00:00
Status: arn.MyAnimeListStatusToARNStatus(match.MyAnimeListItem.Status),
Episodes: match.MyAnimeListItem.NumWatchedEpisodes,
2017-07-08 00:41:13 +00:00
Notes: "",
2018-03-27 06:19:01 +00:00
Rating: arn.AnimeListItemRating{
2018-11-06 05:43:36 +00:00
Overall: float64(match.MyAnimeListItem.Score),
2017-07-08 00:41:13 +00:00
},
RewatchCount: rewatchCount,
Created: arn.DateTimeUTC(),
Edited: arn.DateTimeUTC(),
}
animeList.Import(item)
}
2017-10-27 07:11:56 +00:00
animeList.Save()
2018-11-15 03:42:10 +00:00
return ctx.HTML(components.ImportFinished(user))
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.MyAnimeListMatch, string) {
user := utils.GetUser(ctx)
if user == nil {
2018-07-07 03:42:00 +00:00
return nil, ctx.Error(http.StatusBadRequest, "Not logged in")
2017-07-07 23:18:21 +00:00
}
malAnimeList, err := mal.GetAnimeList(user.Accounts.MyAnimeList.Nick)
if err != nil {
return nil, ctx.Error(http.StatusBadRequest, "Couldn't load your anime list from MyAnimeList", err)
}
matches := findAllMatches(malAnimeList)
return matches, ""
}
// findAllMatches returns all matches for the anime inside an anilist anime list.
2018-11-06 05:43:36 +00:00
func findAllMatches(animeList mal.AnimeList) []*arn.MyAnimeListMatch {
2018-04-04 13:21:52 +00:00
finder := arn.NewAnimeFinder("myanimelist/anime")
2017-07-07 23:18:21 +00:00
matches := []*arn.MyAnimeListMatch{}
2018-11-06 05:43:36 +00:00
for _, item := range animeList {
2017-07-07 23:18:21 +00:00
matches = append(matches, &arn.MyAnimeListMatch{
MyAnimeListItem: item,
2018-11-06 05:43:36 +00:00
ARNAnime: finder.GetAnime(strconv.Itoa(item.AnimeID)),
2017-07-07 23:18:21 +00:00
})
}
return matches
}