From 9d57aaf12a46e149c3f7f149a911db5369448ef7 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Thu, 1 Nov 2018 08:23:41 +0900 Subject: [PATCH] Updated MAL sync --- jobs/mal-parse/anime.go | 72 +++++++++++++++++++++++++++ jobs/mal-parse/character.go | 45 +++++++++++++++++ jobs/mal-parse/mal-parse.go | 98 +------------------------------------ jobs/mal-sync/mal-sync.go | 43 ++++++++++------ jobs/mal-sync/shell.go | 35 ++++++++----- 5 files changed, 169 insertions(+), 124 deletions(-) create mode 100644 jobs/mal-parse/anime.go create mode 100644 jobs/mal-parse/character.go diff --git a/jobs/mal-parse/anime.go b/jobs/mal-parse/anime.go new file mode 100644 index 00000000..9e7036a2 --- /dev/null +++ b/jobs/mal-parse/anime.go @@ -0,0 +1,72 @@ +package main + +import ( + "compress/gzip" + "errors" + "os" + + "github.com/animenotifier/arn" + "github.com/animenotifier/mal" + "github.com/animenotifier/mal/parser" + "github.com/fatih/color" +) + +// Read anime file +func readAnimeFile(name string) error { + file, err := os.Open(name) + + if err != nil { + color.Red(err.Error()) + return err + } + + defer file.Close() + + reader, err := gzip.NewReader(file) + + if err != nil { + color.Red(err.Error()) + return err + } + + anime, characters, err := malparser.ParseAnime(reader) + + if err != nil { + color.Red(err.Error()) + return err + } + + if anime.ID == "" { + return errors.New("Empty ID") + } + + for _, character := range characters { + obj, err := arn.MAL.Get("Character", character.ID) + + if err != nil { + arn.MAL.Set("Character", character.ID, character) + continue + } + + existing := obj.(*mal.Character) + modified := false + + if existing.Name != character.Name { + existing.Name = character.Name + modified = true + } + + if existing.Image != character.Image { + existing.Image = character.Image + modified = true + } + + if modified { + arn.MAL.Set("Character", existing.ID, existing) + } + } + + // fmt.Println(anime.ID, anime.Title) + arn.MAL.Set("Anime", anime.ID, anime) + return nil +} diff --git a/jobs/mal-parse/character.go b/jobs/mal-parse/character.go new file mode 100644 index 00000000..c292f6e6 --- /dev/null +++ b/jobs/mal-parse/character.go @@ -0,0 +1,45 @@ +package main + +import ( + "compress/gzip" + "errors" + "os" + + "github.com/animenotifier/arn" + "github.com/animenotifier/mal/parser" + "github.com/fatih/color" +) + +// Read character file +func readCharacterFile(name string) error { + file, err := os.Open(name) + + if err != nil { + color.Red(err.Error()) + return err + } + + defer file.Close() + + reader, err := gzip.NewReader(file) + + if err != nil { + color.Red(err.Error()) + return err + } + + character, err := malparser.ParseCharacter(reader) + + if err != nil { + color.Red(err.Error()) + return err + } + + if character.ID == "" { + return errors.New("Empty ID") + } + + // fmt.Println(character.ID, character.Name) + arn.MAL.Set("Character", character.ID, character) + return nil +} diff --git a/jobs/mal-parse/mal-parse.go b/jobs/mal-parse/mal-parse.go index aa6df8bd..094a052e 100644 --- a/jobs/mal-parse/mal-parse.go +++ b/jobs/mal-parse/mal-parse.go @@ -1,17 +1,12 @@ package main import ( - "compress/gzip" - "errors" "os" "path" "path/filepath" "strings" - "github.com/animenotifier/mal" - "github.com/animenotifier/arn" - "github.com/animenotifier/mal/parser" "github.com/fatih/color" ) @@ -35,6 +30,7 @@ func main() { } } +// Read files in a given directory and apply a function on them func readFiles(root string, onFile func(string) error) { count := 0 @@ -65,95 +61,3 @@ func readFiles(root string, onFile func(string) error) { color.Cyan("%d files found", count) } - -func readAnimeFile(name string) error { - file, err := os.Open(name) - - if err != nil { - color.Red(err.Error()) - return err - } - - defer file.Close() - - reader, err := gzip.NewReader(file) - - if err != nil { - color.Red(err.Error()) - return err - } - - anime, characters, err := malparser.ParseAnime(reader) - - if err != nil { - color.Red(err.Error()) - return err - } - - if anime.ID == "" { - return errors.New("Empty ID") - } - - for _, character := range characters { - obj, err := arn.MAL.Get("Character", character.ID) - - if err != nil { - arn.MAL.Set("Character", character.ID, character) - continue - } - - existing := obj.(*mal.Character) - modified := false - - if existing.Name != character.Name { - existing.Name = character.Name - modified = true - } - - if existing.Image != character.Image { - existing.Image = character.Image - modified = true - } - - if modified { - arn.MAL.Set("Character", existing.ID, existing) - } - } - - // fmt.Println(anime.ID, anime.Title) - arn.MAL.Set("Anime", anime.ID, anime) - return nil -} - -func readCharacterFile(name string) error { - file, err := os.Open(name) - - if err != nil { - color.Red(err.Error()) - return err - } - - defer file.Close() - - reader, err := gzip.NewReader(file) - - if err != nil { - color.Red(err.Error()) - return err - } - - character, err := malparser.ParseCharacter(reader) - - if err != nil { - color.Red(err.Error()) - return err - } - - if character.ID == "" { - return errors.New("Empty ID") - } - - // fmt.Println(character.ID, character.Name) - arn.MAL.Set("Character", character.ID, character) - return nil -} diff --git a/jobs/mal-sync/mal-sync.go b/jobs/mal-sync/mal-sync.go index 509e0bb1..28979fdf 100644 --- a/jobs/mal-sync/mal-sync.go +++ b/jobs/mal-sync/mal-sync.go @@ -26,28 +26,32 @@ func main() { return } - // Sync the most important ones first - allAnime := arn.FilterAnime(func(anime *arn.Anime) bool { - return anime.GetMapping("myanimelist/anime") != "" - }) + // 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)) + arn.SortAnimeByQuality(allAnime) + color.Yellow("%d anime found", len(allAnime)) - for _, anime := range allAnime { - syncAnime(anime, anime.GetMapping("myanimelist/anime")) + for _, anime := range allAnime { + syncAnime(anime, anime.GetMapping("myanimelist/anime")) + } } - // Sync the most important ones first - allCharacters := arn.FilterCharacters(func(character *arn.Character) bool { - return character.GetMapping("myanimelist/character") != "" - }) + // Sync characters + if objectType == "all" || objectType == "character" { + allCharacters := arn.FilterCharacters(func(character *arn.Character) bool { + return character.GetMapping("myanimelist/character") != "" + }) - arn.SortCharactersByLikes(allCharacters) - color.Yellow("%d characters found", len(allCharacters)) + arn.SortCharactersByLikes(allCharacters) + color.Yellow("%d characters found", len(allCharacters)) - for _, character := range allCharacters { - syncCharacter(character, character.GetMapping("myanimelist/character")) + for _, character := range allCharacters { + syncCharacter(character, character.GetMapping("myanimelist/character")) + } } } @@ -99,6 +103,13 @@ func syncCharacter(character *arn.Character, malID string) { description, attributes := parseCharacterDescription(malCharacter.Description) character.Description = description character.Attributes = attributes + character.Spoilers = []arn.Spoiler{} + + for _, spoilerText := range malCharacter.Spoilers { + character.Spoilers = append(character.Spoilers, arn.Spoiler{ + Text: spoilerText, + }) + } if character.Name.Japanese == "" && malCharacter.JapaneseName != "" { character.Name.Japanese = malCharacter.JapaneseName diff --git a/jobs/mal-sync/shell.go b/jobs/mal-sync/shell.go index ec5bcef1..9ce12215 100644 --- a/jobs/mal-sync/shell.go +++ b/jobs/mal-sync/shell.go @@ -7,28 +7,41 @@ import ( ) // Shell parameters -var animeID string +var objectType string +var objectID string // Shell flags func init() { - flag.StringVar(&animeID, "id", "", "ID of the notify.moe anime you want to refresh") + flag.StringVar(&objectType, "type", "all", "all | anime | character") + flag.StringVar(&objectID, "id", "", "ID of the notify.moe anime/character you want to refresh") flag.Parse() } // InvokeShellArgs ... func InvokeShellArgs() bool { - if animeID != "" { - anime, err := arn.GetAnime(animeID) + if objectID != "" { + switch objectType { + case "anime": + anime, err := arn.GetAnime(objectID) + arn.PanicOnError(err) - if err != nil { - panic(err) + if anime.GetMapping("myanimelist/anime") == "" { + panic("No MAL ID") + } + + syncAnime(anime, anime.GetMapping("myanimelist/anime")) + + case "character": + character, err := arn.GetCharacter(objectID) + arn.PanicOnError(err) + + if character.GetMapping("myanimelist/character") == "" { + panic("No MAL ID") + } + + syncCharacter(character, character.GetMapping("myanimelist/character")) } - if anime.GetMapping("myanimelist/anime") == "" { - panic("No MAL ID") - } - - syncAnime(anime, anime.GetMapping("myanimelist/anime")) return true }