132 lines
2.8 KiB
Go
Raw Normal View History

2017-07-12 06:24:26 +00:00
package listimportkitsu
import (
"net/http"
2018-04-03 20:53:53 +00:00
"strings"
2017-07-12 06:24:26 +00:00
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/kitsu"
"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 {
return ctx.Error(http.StatusBadRequest, "Not logged in", nil)
}
matches, response := getMatches(ctx)
if response != "" {
return response
}
return ctx.HTML(components.ImportKitsu(user, matches))
}
// Finish ...
func Finish(ctx *aero.Context) string {
user := utils.GetUser(ctx)
if user == nil {
return ctx.Error(http.StatusBadRequest, "Not logged in", nil)
}
matches, response := getMatches(ctx)
if response != "" {
return response
}
animeList := user.AnimeList()
for _, match := range matches {
if match.ARNAnime == nil || match.KitsuItem == nil {
continue
}
rating := match.KitsuItem.Attributes.RatingTwenty
2018-03-15 23:28:43 +00:00
if rating < 0 {
rating = 0
2017-07-12 06:24:26 +00:00
}
if rating > 20 {
rating = 20
}
// Convert rating
2018-03-15 23:28:43 +00:00
convertedRating := (float64(rating) / 20.0) * 10.0
2017-07-12 06:24:26 +00:00
item := &arn.AnimeListItem{
AnimeID: match.ARNAnime.ID,
Status: arn.KitsuStatusToARNStatus(match.KitsuItem.Attributes.Status),
Episodes: match.KitsuItem.Attributes.Progress,
Notes: match.KitsuItem.Attributes.Notes,
2018-03-27 06:19:01 +00:00
Rating: arn.AnimeListItemRating{
2017-07-12 06:24:26 +00:00
Overall: convertedRating,
},
RewatchCount: match.KitsuItem.Attributes.ReconsumeCount,
Created: arn.DateTimeUTC(),
Edited: arn.DateTimeUTC(),
}
animeList.Import(item)
}
2017-10-27 07:11:56 +00:00
animeList.Save()
2017-07-12 06:24:26 +00:00
2018-04-03 20:53:53 +00:00
// Redirect
prefix := "/"
if strings.HasPrefix(ctx.URI(), "/_") {
prefix = "/_/"
}
return ctx.Redirect(prefix + "+" + user.Nick + "/animelist/watching")
2017-07-12 06:24:26 +00:00
}
// getMatches finds and returns all matches for the logged in user.
func getMatches(ctx *aero.Context) ([]*arn.KitsuMatch, string) {
user := utils.GetUser(ctx)
if user == nil {
return nil, ctx.Error(http.StatusBadRequest, "Not logged in", nil)
}
kitsuUser, err := kitsu.GetUser(user.Accounts.Kitsu.Nick)
if err != nil {
return nil, ctx.Error(http.StatusBadRequest, "Couldn't load your user info from Kitsu", err)
}
library := kitsuUser.StreamLibraryEntries()
matches := findAllMatches(library)
return matches, ""
}
// findAllMatches returns all matches for the anime inside an anilist anime list.
func findAllMatches(library chan *kitsu.LibraryEntry) []*arn.KitsuMatch {
2018-04-04 13:21:52 +00:00
finder := arn.NewAnimeFinder("kitsu/anime")
2017-07-12 06:24:26 +00:00
matches := []*arn.KitsuMatch{}
for item := range library {
// Ignore non-anime entries
if item.Anime == nil {
continue
}
matches = append(matches, &arn.KitsuMatch{
KitsuItem: item,
2018-04-04 13:08:18 +00:00
ARNAnime: finder.GetAnime(item.Anime.ID),
2017-07-12 06:24:26 +00:00
})
}
return matches
}