121 lines
2.5 KiB
Go
Raw Normal View History

2018-04-12 11:39:58 +00:00
package main
import (
"fmt"
"github.com/animenotifier/arn"
"github.com/animenotifier/mal"
"github.com/fatih/color"
)
2018-04-17 13:33:19 +00:00
const imageWidthThreshold = 225
2018-04-17 13:28:57 +00:00
2018-04-12 11:39:58 +00:00
var (
malDB = arn.Node.Namespace("mal").RegisterTypes((*mal.Anime)(nil))
characterFinder = arn.NewCharacterFinder("myanimelist/character")
)
func main() {
color.Yellow("Syncing with MAL DB")
defer color.Green("Finished.")
defer arn.Node.Close()
2018-04-12 17:05:05 +00:00
// Invoke via parameters
if InvokeShellArgs() {
return
}
2018-10-31 23:23:41 +00:00
// Sync anime
if objectType == "all" || objectType == "anime" {
allAnime := arn.FilterAnime(func(anime *arn.Anime) bool {
return anime.GetMapping("myanimelist/anime") != ""
})
arn.SortAnimeByQuality(allAnime)
color.Yellow("%d anime found", len(allAnime))
for _, anime := range allAnime {
syncAnime(anime, anime.GetMapping("myanimelist/anime"))
}
2018-10-31 16:05:18 +00:00
}
2018-10-31 23:23:41 +00:00
// Sync characters
if objectType == "all" || objectType == "character" {
allCharacters := arn.FilterCharacters(func(character *arn.Character) bool {
return character.GetMapping("myanimelist/character") != ""
})
2018-10-31 23:23:41 +00:00
arn.SortCharactersByLikes(allCharacters)
color.Yellow("%d characters found", len(allCharacters))
2018-10-31 16:05:18 +00:00
2018-10-31 23:23:41 +00:00
for _, character := range allCharacters {
syncCharacter(character, character.GetMapping("myanimelist/character"))
}
2018-04-12 11:39:58 +00:00
}
}
2018-10-31 16:05:18 +00:00
func syncAnime(anime *arn.Anime, malID string) {
2018-04-12 11:39:58 +00:00
obj, err := malDB.Get("Anime", malID)
if err != nil {
return
}
malAnime := obj.(*mal.Anime)
// Log title
fmt.Printf("%s %s\n", color.CyanString(anime.Title.Canonical), malID)
2018-04-26 19:36:26 +00:00
type syncFunction func(*arn.Anime, *mal.Anime)
2018-04-12 11:39:58 +00:00
2018-04-26 19:36:26 +00:00
syncFunctions := []syncFunction{
syncTitles,
syncDates,
syncEpisodes,
syncOthers,
syncImage,
syncCharacters,
2018-04-26 18:41:41 +00:00
}
2018-04-17 13:28:57 +00:00
2018-04-26 19:36:26 +00:00
for _, syncFunction := range syncFunctions {
syncFunction(anime, malAnime)
2018-04-17 13:28:57 +00:00
}
2018-04-26 19:36:26 +00:00
// Save in database
anime.Save()
2018-04-12 11:39:58 +00:00
}
2018-10-31 16:05:18 +00:00
func syncCharacter(character *arn.Character, malID string) {
obj, err := malDB.Get("Character", malID)
if err != nil {
return
}
// Skip manually created characters
if character.CreatedBy != "" {
2018-10-31 16:05:18 +00:00
return
}
malCharacter := obj.(*mal.Character)
description, attributes := parseCharacterDescription(malCharacter.Description)
character.Description = description
character.Attributes = attributes
2018-10-31 23:23:41 +00:00
character.Spoilers = []arn.Spoiler{}
for _, spoilerText := range malCharacter.Spoilers {
character.Spoilers = append(character.Spoilers, arn.Spoiler{
Text: spoilerText,
})
}
2018-10-31 16:05:18 +00:00
if character.Name.Japanese == "" && malCharacter.JapaneseName != "" {
character.Name.Japanese = malCharacter.JapaneseName
}
// Save in database
character.Save()
}