144 lines
3.1 KiB
Go
Raw Normal View History

2018-04-12 11:39:58 +00:00
package main
import (
"fmt"
2018-11-08 22:54:02 +00:00
"strings"
2018-04-12 11:39:58 +00:00
"github.com/animenotifier/arn"
"github.com/animenotifier/mal"
"github.com/blitzprog/color"
2018-04-12 11:39:58 +00:00
)
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
}
2018-10-31 16:05:18 +00:00
malCharacter := obj.(*mal.Character)
if character.Name.Japanese == "" && malCharacter.JapaneseName != "" {
character.Name.Japanese = malCharacter.JapaneseName
}
2018-11-13 09:51:51 +00:00
if character.Name.Canonical == "" && malCharacter.Name != "" {
2018-11-08 22:54:02 +00:00
character.Name.Canonical = malCharacter.Name
2018-11-13 09:51:51 +00:00
}
allowUpdating := character.CreatedBy == "" && character.EditedBy == ""
description, attributes := parseCharacterDescription(malCharacter.Description)
2018-11-13 10:13:30 +00:00
if strings.Contains(character.Description, "No biography written.") {
character.Description = ""
}
2018-11-13 09:51:51 +00:00
if (allowUpdating || character.Description == "") && description != "" {
2018-11-08 22:54:02 +00:00
character.Description = description
2018-11-13 09:51:51 +00:00
}
if allowUpdating {
2018-11-08 22:54:02 +00:00
character.Attributes = attributes
character.Spoilers = []arn.Spoiler{}
for _, spoilerText := range malCharacter.Spoilers {
if !strings.Contains(strings.TrimSpace(spoilerText), "\n\n") {
character.Spoilers = append(character.Spoilers, arn.Spoiler{
Text: spoilerText,
})
continue
}
paragraphs := strings.Split(spoilerText, "\n\n")
for _, paragraph := range paragraphs {
character.Spoilers = append(character.Spoilers, arn.Spoiler{
Text: paragraph,
})
}
}
}
2018-10-31 16:05:18 +00:00
// Save in database
character.Save()
}