73 lines
1.2 KiB
Go
Raw Normal View History

2018-10-31 23:23:41 +00:00
package main
import (
"compress/gzip"
"errors"
"os"
2019-04-23 05:52:55 +00:00
"github.com/akyoto/color"
2018-10-31 23:23:41 +00:00
"github.com/animenotifier/arn"
"github.com/animenotifier/mal"
"github.com/animenotifier/mal/parser"
)
// 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
}