2017-07-12 06:24:26 +00:00
|
|
|
package listimportkitsu
|
|
|
|
|
|
|
|
import (
|
2019-06-01 04:55:49 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-07-12 06:24:26 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/aerogo/aero"
|
|
|
|
"github.com/animenotifier/kitsu"
|
2019-06-07 01:03:16 +00:00
|
|
|
"github.com/animenotifier/notify.moe/arn"
|
2017-07-12 06:24:26 +00:00
|
|
|
"github.com/animenotifier/notify.moe/components"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Preview shows an import preview.
|
2019-06-01 04:55:49 +00:00
|
|
|
func Preview(ctx aero.Context) error {
|
2019-11-17 07:59:34 +00:00
|
|
|
user := arn.GetUserFromContext(ctx)
|
2017-07-12 06:24:26 +00:00
|
|
|
|
|
|
|
if user == nil {
|
2018-07-07 03:42:00 +00:00
|
|
|
return ctx.Error(http.StatusBadRequest, "Not logged in")
|
2017-07-12 06:24:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-01 04:55:49 +00:00
|
|
|
matches, err := getMatches(ctx)
|
2017-07-12 06:24:26 +00:00
|
|
|
|
2019-06-01 04:55:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return ctx.Error(http.StatusBadRequest, err)
|
2017-07-12 06:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.HTML(components.ImportKitsu(user, matches))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finish ...
|
2019-06-01 04:55:49 +00:00
|
|
|
func Finish(ctx aero.Context) error {
|
2019-11-17 07:59:34 +00:00
|
|
|
user := arn.GetUserFromContext(ctx)
|
2017-07-12 06:24:26 +00:00
|
|
|
|
|
|
|
if user == nil {
|
2018-07-07 03:42:00 +00:00
|
|
|
return ctx.Error(http.StatusBadRequest, "Not logged in")
|
2017-07-12 06:24:26 +00:00
|
|
|
}
|
|
|
|
|
2019-06-01 04:55:49 +00:00
|
|
|
matches, err := getMatches(ctx)
|
2017-07-12 06:24:26 +00:00
|
|
|
|
2019-06-01 04:55:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return ctx.Error(http.StatusBadRequest, err)
|
2017-07-12 06:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2018-11-15 03:42:10 +00:00
|
|
|
return ctx.HTML(components.ImportFinished(user))
|
2017-07-12 06:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getMatches finds and returns all matches for the logged in user.
|
2019-06-01 04:55:49 +00:00
|
|
|
func getMatches(ctx aero.Context) ([]*arn.KitsuMatch, error) {
|
2019-11-17 07:59:34 +00:00
|
|
|
user := arn.GetUserFromContext(ctx)
|
2017-07-12 06:24:26 +00:00
|
|
|
|
|
|
|
if user == nil {
|
2019-06-01 04:55:49 +00:00
|
|
|
return nil, errors.New("Not logged in")
|
2017-07-12 06:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
kitsuUser, err := kitsu.GetUser(user.Accounts.Kitsu.Nick)
|
|
|
|
|
|
|
|
if err != nil {
|
2019-06-01 04:55:49 +00:00
|
|
|
return nil, fmt.Errorf("Couldn't load your user info from Kitsu: %s", err.Error())
|
2017-07-12 06:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
library := kitsuUser.StreamLibraryEntries()
|
|
|
|
matches := findAllMatches(library)
|
|
|
|
|
2019-06-01 04:55:49 +00:00
|
|
|
return matches, nil
|
2017-07-12 06:24:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|