Updated MAL sync
This commit is contained in:
parent
3ccf470cf0
commit
9d57aaf12a
72
jobs/mal-parse/anime.go
Normal file
72
jobs/mal-parse/anime.go
Normal 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
|
||||
}
|
45
jobs/mal-parse/character.go
Normal file
45
jobs/mal-parse/character.go
Normal 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
|
||||
}
|
@ -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
|
||||
}
|
||||
|
@ -26,7 +26,8 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
// Sync the most important ones first
|
||||
// Sync anime
|
||||
if objectType == "all" || objectType == "anime" {
|
||||
allAnime := arn.FilterAnime(func(anime *arn.Anime) bool {
|
||||
return anime.GetMapping("myanimelist/anime") != ""
|
||||
})
|
||||
@ -37,8 +38,10 @@ func main() {
|
||||
for _, anime := range allAnime {
|
||||
syncAnime(anime, anime.GetMapping("myanimelist/anime"))
|
||||
}
|
||||
}
|
||||
|
||||
// Sync the most important ones first
|
||||
// Sync characters
|
||||
if objectType == "all" || objectType == "character" {
|
||||
allCharacters := arn.FilterCharacters(func(character *arn.Character) bool {
|
||||
return character.GetMapping("myanimelist/character") != ""
|
||||
})
|
||||
@ -50,6 +53,7 @@ func main() {
|
||||
syncCharacter(character, character.GetMapping("myanimelist/character"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func syncAnime(anime *arn.Anime, malID string) {
|
||||
obj, err := malDB.Get("Anime", malID)
|
||||
@ -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
|
||||
|
@ -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 err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if objectID != "" {
|
||||
switch objectType {
|
||||
case "anime":
|
||||
anime, err := arn.GetAnime(objectID)
|
||||
arn.PanicOnError(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"))
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user