104 lines
1.8 KiB
Go
Raw Normal View History

2018-03-08 23:07:48 +00:00
package main
import (
"encoding/json"
"errors"
"fmt"
"os"
2018-04-12 17:05:05 +00:00
"path"
2018-03-08 23:07:48 +00:00
"path/filepath"
"strings"
2018-04-12 09:05:55 +00:00
"github.com/animenotifier/mal"
2018-03-09 03:32:31 +00:00
"github.com/animenotifier/arn"
2018-03-08 23:07:48 +00:00
"github.com/animenotifier/mal/parser"
2018-03-16 23:28:36 +00:00
"github.com/fatih/color"
2018-03-08 23:07:48 +00:00
)
func main() {
2018-04-12 11:39:58 +00:00
color.Yellow("Parsing MAL files")
2018-03-16 23:28:36 +00:00
defer color.Green("Finished.")
2018-03-21 04:15:03 +00:00
defer arn.Node.Close()
2018-03-08 23:07:48 +00:00
2018-04-12 17:05:05 +00:00
// Invoke via parameters
if InvokeShellArgs() {
return
}
2018-03-08 23:07:48 +00:00
2018-04-12 17:05:05 +00:00
filepath.Walk(path.Join(arn.Root, "jobs/mal-download/files"), func(name string, info os.FileInfo, err error) error {
2018-03-08 23:07:48 +00:00
if err != nil {
fmt.Println(err)
return err
}
if info.IsDir() {
return nil
}
if !strings.HasSuffix(name, ".html") {
return nil
}
return readFile(name)
})
}
func readFile(name string) error {
file, err := os.Open(name)
defer file.Close()
if err != nil {
fmt.Println(err)
return err
}
2018-04-12 09:05:55 +00:00
anime, characters, err := malparser.ParseAnime(file)
2018-03-08 23:07:48 +00:00
if err != nil {
fmt.Println(err)
return err
}
if anime.ID == "" {
return errors.New("Empty ID")
}
2018-04-12 09:05:55 +00:00
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.ImagePath != character.ImagePath {
existing.ImagePath = character.ImagePath
modified = true
}
if modified {
arn.MAL.Set("Character", existing.ID, existing)
}
}
2018-04-12 09:11:02 +00:00
fmt.Println(anime.ID, anime.Title)
2018-03-09 03:32:31 +00:00
arn.MAL.Set("Anime", anime.ID, anime)
2018-03-08 23:07:48 +00:00
return nil
}
// prettyPrint prints the object as indented JSON data on the console.
func prettyPrint(obj interface{}) {
pretty, _ := json.MarshalIndent(obj, "", "\t")
fmt.Println(string(pretty))
}