93 lines
2.3 KiB
Go
Raw Normal View History

2017-07-01 16:33:20 +00:00
package main
import (
2017-07-01 22:27:58 +00:00
"strconv"
2017-07-01 19:29:33 +00:00
"strings"
2017-07-01 16:33:20 +00:00
"github.com/animenotifier/arn"
2017-07-01 19:29:33 +00:00
"github.com/fatih/color"
2017-07-01 16:33:20 +00:00
)
2017-07-01 19:29:33 +00:00
var allAnime []*arn.Anime
func init() {
allAnime, _ = arn.AllAnime()
}
2017-07-01 16:33:20 +00:00
func main() {
arn.PanicOnError(arn.AniList.Authorize())
2017-07-01 19:29:33 +00:00
println(arn.AniList.AccessToken)
user, _ := arn.GetUserByNick("Boltasar")
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 {
2017-07-01 22:27:58 +00:00
anime := findAniListAnime(item.Anime)
2017-07-01 19:29:33 +00:00
if anime != nil {
// fmt.Println(item.Anime.TitleRomaji, "=>", anime.Title.Romaji)
imported = append(imported, anime)
}
}
color.Green("%d / %d", len(imported), len(animeListItems))
}
2017-07-01 22:27:58 +00:00
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
}
2017-07-01 19:29:33 +00:00
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 {
2017-07-01 22:27:58 +00:00
// fmt.Printf("MATCH: %s => %s (%.2f)\n", search.TitleRomaji, mostSimilar.Title.Romaji, similarity)
2017-07-01 19:29:33 +00:00
return mostSimilar
}
color.Red("MISMATCH: %s => %s (%.2f)", search.TitleRomaji, mostSimilar.Title.Romaji, similarity)
return nil
2017-07-01 16:33:20 +00:00
}