From 6038648e3d7b1327ea69ada414be58ff10e8b591 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 9 Mar 2018 00:07:48 +0100 Subject: [PATCH] Changed job names --- .../crawler.go => mal-crawler/mal-crawler.go} | 2 +- jobs/mal-import/mal-import.go | 72 +++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) rename jobs/{crawler/crawler.go => mal-crawler/mal-crawler.go} (96%) create mode 100644 jobs/mal-import/mal-import.go diff --git a/jobs/crawler/crawler.go b/jobs/mal-crawler/mal-crawler.go similarity index 96% rename from jobs/crawler/crawler.go rename to jobs/mal-crawler/mal-crawler.go index a464ce34..2c68eddb 100644 --- a/jobs/crawler/crawler.go +++ b/jobs/mal-crawler/mal-crawler.go @@ -54,7 +54,7 @@ func main() { for _, anime := range animes { malID := anime.GetMapping("myanimelist/anime") url := "https://myanimelist.net/anime/" + malID - filePath := fmt.Sprintf("mal/anime-%s.html", malID) + filePath := fmt.Sprintf("files/anime-%s.html", malID) fileInfo, err := os.Stat(filePath) if err == nil && time.Since(fileInfo.ModTime()) <= maxAge { diff --git a/jobs/mal-import/mal-import.go b/jobs/mal-import/mal-import.go new file mode 100644 index 00000000..4f657ffd --- /dev/null +++ b/jobs/mal-import/mal-import.go @@ -0,0 +1,72 @@ +package main + +import ( + "encoding/json" + "errors" + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/animenotifier/mal" + + "github.com/aerogo/nano" + "github.com/animenotifier/mal/parser" +) + +var node = nano.New(5000) +var db = node.Namespace("mal").RegisterTypes((*mal.Anime)(nil)) + +func main() { + defer node.Close() + + // readFile("../mal-crawler/files/anime-31240.html") + + filepath.Walk("../mal-crawler/files", func(name string, info os.FileInfo, err error) error { + 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 + } + + anime, err := malparser.ParseAnime(file) + + if err != nil { + fmt.Println(err) + return err + } + + if anime.ID == "" { + return errors.New("Empty ID") + } + + fmt.Println(anime.ID, anime.Title) + // db.Set("Anime", anime.ID, anime) + 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)) +}