New Anilist importer

This commit is contained in:
Eduard Urbach 2018-04-05 22:36:59 +02:00
parent 8acdcaeb38
commit a4bbd5cbbc
4 changed files with 37 additions and 53 deletions

View File

@ -2,6 +2,7 @@ package listimportanilist
import (
"net/http"
"strconv"
"github.com/aerogo/aero"
"github.com/animenotifier/anilist"
@ -51,12 +52,13 @@ func Finish(ctx *aero.Context) string {
item := &arn.AnimeListItem{
AnimeID: match.ARNAnime.ID,
Status: arn.AniListAnimeListStatus(match.AniListItem),
Episodes: match.AniListItem.EpisodesWatched,
Episodes: match.AniListItem.Progress,
Notes: match.AniListItem.Notes,
Rating: arn.AnimeListItemRating{
Overall: float64(match.AniListItem.ScoreRaw) / 10.0,
},
RewatchCount: match.AniListItem.Rewatched,
RewatchCount: match.AniListItem.Repeat,
Private: match.AniListItem.Private,
Created: arn.DateTimeUTC(),
Edited: arn.DateTimeUTC(),
}
@ -77,53 +79,44 @@ func getMatches(ctx *aero.Context) ([]*arn.AniListMatch, string) {
return nil, ctx.Error(http.StatusBadRequest, "Not logged in", nil)
}
authErr := anilist.Authorize()
// Get user
anilistUser, err := anilist.GetUser(user.Accounts.AniList.Nick)
if authErr != nil {
return nil, ctx.Error(http.StatusBadRequest, "Couldn't authorize the Anime Notifier app on AniList", authErr)
if err != nil {
return nil, ctx.Error(http.StatusBadRequest, "User doesn't exist on AniList", err)
}
allAnime := arn.AllAnime()
anilistAnimeList, err := anilist.GetAnimeList(user.Accounts.AniList.Nick)
// Get anime list
anilistAnimeList, err := anilist.GetAnimeList(anilistUser.ID)
if err != nil {
return nil, ctx.Error(http.StatusBadRequest, "Couldn't load your anime list from AniList", err)
}
matches := findAllMatches(allAnime, anilistAnimeList)
// Find matches
matches := findAllMatches(anilistAnimeList)
return matches, ""
}
// findAllMatches returns all matches for the anime inside an anilist anime list.
func findAllMatches(allAnime []*arn.Anime, animeList *anilist.AnimeList) []*arn.AniListMatch {
func findAllMatches(animeList *anilist.AnimeList) []*arn.AniListMatch {
finder := arn.NewAniListAnimeFinder()
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)
custom, ok := animeList.CustomLists.(map[string][]*anilist.AnimeListItem)
if !ok {
return matches
}
for _, list := range custom {
matches = importList(matches, allAnime, list)
for _, list := range animeList.Lists {
matches = importList(matches, finder, list.Entries)
}
return matches
}
// importList imports a single list inside an anilist anime list collection.
func importList(matches []*arn.AniListMatch, allAnime []*arn.Anime, animeListItems []*anilist.AnimeListItem) []*arn.AniListMatch {
func importList(matches []*arn.AniListMatch, finder *arn.AniListAnimeFinder, animeListItems []*anilist.AnimeListItem) []*arn.AniListMatch {
for _, item := range animeListItems {
matches = append(matches, &arn.AniListMatch{
AniListItem: item,
ARNAnime: arn.FindAniListAnime(item.Anime, allAnime),
ARNAnime: finder.GetAnime(strconv.Itoa(item.Anime.ID), strconv.Itoa(item.Anime.MALID)),
})
}

View File

@ -12,7 +12,7 @@ component ImportAnilist(user *arn.User, matches []*arn.AniListMatch)
each match in matches
tr
td
a(href=match.AniListItem.Anime.Link(), target="_blank", rel="noopener")= match.AniListItem.Anime.TitleRomaji
a(href=match.AniListItem.Anime.Link(), target="_blank", rel="noopener")= match.AniListItem.Anime.Title.Romaji
td
if match.ARNAnime == nil
span.import-error Not found on notify.moe

View File

@ -16,9 +16,6 @@ func init() {
}
func main() {
arn.PanicOnError(anilist.Authorize())
println(anilist.AccessToken)
user, _ := arn.GetUserByNick(userName)
animeList, err := anilist.GetAnimeList(user.Accounts.AniList.Nick)
arn.PanicOnError(err)

View File

@ -1,34 +1,28 @@
package main
import (
"github.com/animenotifier/anilist"
"github.com/animenotifier/arn"
"github.com/fatih/color"
)
func main() {
color.Yellow("Iterating through AniList anime to generate new mappings")
defer arn.Node.Close()
// color.Yellow("Iterating through AniList anime to generate new mappings")
// defer arn.Node.Close()
err := anilist.Authorize()
arn.PanicOnError(err)
color.Green(anilist.AccessToken)
// err := anilist.Authorize()
// arn.PanicOnError(err)
// color.Green(anilist.AccessToken)
allAnime := arn.AllAnime()
count := 0
// allAnime := arn.AllAnime()
// count := 0
for aniListAnime := range anilist.StreamAnime() {
println(aniListAnime.TitleRomaji)
// for aniListAnime := range anilist.StreamAnime() {
// println(aniListAnime.TitleRomaji)
anime := arn.FindAniListAnime(aniListAnime, allAnime)
// anime := arn.FindAniListAnime(aniListAnime, allAnime)
if anime != nil {
color.Green("%s %s", anime.ID, anime)
count++
} else {
color.Red("Not found")
}
}
// if anime != nil {
// color.Green("%s %s", anime.ID, anime)
// count++
// } else {
// color.Red("Not found")
// }
// }
color.Green("%d anime are connected with AniList", count)
// color.Green("%d anime are connected with AniList", count)
}