Updated MAL sync

This commit is contained in:
Eduard Urbach 2018-11-01 08:23:41 +09:00
parent 3ccf470cf0
commit 9d57aaf12a
5 changed files with 169 additions and 124 deletions

72
jobs/mal-parse/anime.go Normal file
View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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

View File

@ -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
}