Improved import

This commit is contained in:
Eduard Urbach 2017-07-02 01:44:10 +02:00
parent 3309e5fb63
commit f9bbf7e6db
3 changed files with 52 additions and 73 deletions

View File

@ -0,0 +1,42 @@
package main
import (
"fmt"
"github.com/animenotifier/arn"
"github.com/fatih/color"
)
var userName = "Akyoto"
var allAnime []*arn.Anime
func init() {
allAnime, _ = arn.AllAnime()
}
func main() {
arn.PanicOnError(arn.AniList.Authorize())
println(arn.AniList.AccessToken)
user, _ := arn.GetUserByNick(userName)
animeList, err := arn.AniList.GetAnimeList(user)
arn.PanicOnError(err)
importList(animeList.Lists.Watching)
importList(animeList.Lists.Completed)
}
func importList(animeListItems []*arn.AniListAnimeListItem) {
imported := []*arn.Anime{}
for _, item := range animeListItems {
anime := arn.FindAniListAnime(item.Anime, allAnime)
if anime != nil {
fmt.Println(item.Anime.TitleRomaji, "=>", anime.Title.Romaji)
imported = append(imported, anime)
}
}
color.Green("%d / %d", len(imported), len(animeListItems))
}

View File

@ -1,92 +1,29 @@
package main package main
import ( import (
"strconv" "fmt"
"strings"
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
"github.com/fatih/color" "github.com/fatih/color"
) )
var allAnime []*arn.Anime
func init() {
allAnime, _ = arn.AllAnime()
}
func main() { func main() {
arn.PanicOnError(arn.AniList.Authorize()) arn.PanicOnError(arn.AniList.Authorize())
println(arn.AniList.AccessToken) color.Green(arn.AniList.AccessToken)
user, _ := arn.GetUserByNick("Boltasar") allAnime, err := arn.AllAnime()
animeList, err := arn.AniList.GetAnimeList(user)
arn.PanicOnError(err) arn.PanicOnError(err)
importList(animeList.Lists.Watching) count := 0
importList(animeList.Lists.Completed) stream := arn.AniList.StreamAnime()
}
func importList(animeListItems []*arn.AniListAnimeListItem) { for aniListAnime := range stream {
imported := []*arn.Anime{} anime := arn.FindAniListAnime(aniListAnime, allAnime)
for _, item := range animeListItems {
anime := findAniListAnime(item.Anime)
if anime != nil { if anime != nil {
// fmt.Println(item.Anime.TitleRomaji, "=>", anime.Title.Romaji) fmt.Println(aniListAnime.TitleRomaji, "=>", anime.Title.Canonical)
imported = append(imported, anime)
}
} }
color.Green("%d / %d", len(imported), len(animeListItems)) count++
} }
func findAniListAnime(search *arn.AniListAnime) *arn.Anime {
match, err := arn.GetAniListToAnime(strconv.Itoa(search.ID))
if err == nil {
anime, _ := arn.GetAnime(match.AnimeID)
return anime
}
var mostSimilar *arn.Anime
var similarity float64
for _, anime := range allAnime {
anime.Title.Japanese = strings.Replace(anime.Title.Japanese, "2ndシーズン", "2", 1)
anime.Title.Romaji = strings.Replace(anime.Title.Romaji, " 2nd Season", " 2", 1)
search.TitleJapanese = strings.TrimSpace(strings.Replace(search.TitleJapanese, "2ndシーズン", "2", 1))
search.TitleRomaji = strings.TrimSpace(strings.Replace(search.TitleRomaji, " 2nd Season", " 2", 1))
titleSimilarity := arn.StringSimilarity(anime.Title.Romaji, search.TitleRomaji)
if strings.ToLower(anime.Title.Japanese) == strings.ToLower(search.TitleJapanese) {
titleSimilarity += 1.0
}
if strings.ToLower(anime.Title.Romaji) == strings.ToLower(search.TitleRomaji) {
titleSimilarity += 1.0
}
if strings.ToLower(anime.Title.English) == strings.ToLower(search.TitleEnglish) {
titleSimilarity += 1.0
}
if titleSimilarity > similarity {
mostSimilar = anime
similarity = titleSimilarity
}
}
if mostSimilar.EpisodeCount != search.TotalEpisodes {
similarity -= 0.02
}
if similarity >= 0.92 {
// fmt.Printf("MATCH: %s => %s (%.2f)\n", search.TitleRomaji, mostSimilar.Title.Romaji, similarity)
return mostSimilar
}
color.Red("MISMATCH: %s => %s (%.2f)", search.TitleRomaji, mostSimilar.Title.Romaji, similarity)
return nil
} }