From 14116309ddcad71d38f645ab5c797444b4216ff4 Mon Sep 17 00:00:00 2001 From: m3philis Date: Sun, 10 Dec 2023 00:13:38 +0100 Subject: [PATCH] add parameter to set path to save to --- createepub/createepub.go | 34 +++++++++++++++------------------- main.go | 24 +++++++++++++----------- 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/createepub/createepub.go b/createepub/createepub.go index 9b325cc..0f5cc03 100644 --- a/createepub/createepub.go +++ b/createepub/createepub.go @@ -8,8 +8,7 @@ import ( "mangacrawler/mangacrawler" "net/http" "os" - "os/exec" - "os/user" + "os/exec" "regexp" "strings" @@ -43,15 +42,11 @@ type MangaDesc struct { En string `json:"en"` } -func CreateEpub(mangaPath string, mangaTitle string, mangaId string) { +func CreateEpub(mangaPath string, epubPath string, mangaTitle string, mangaId string) { var author MangaPlus url := "https://api.mangadex.org/manga/" + mangaId + "?includes[]=author&includes[]=cover_art" data := mangacrawler.GetJson(url) - homepath, err := user.Current() - if err != nil { - panic(err) - } if err := json.Unmarshal(data, &author); err != nil { panic(err) @@ -69,7 +64,7 @@ func CreateEpub(mangaPath string, mangaTitle string, mangaId string) { book := epub.NewEpub(mangaTitle) book.SetAuthor(author.Data.Rels[0].Attributes.Name) book.SetDescription(author.Data.Attr.Desc.En) - bookCss, _ := book.AddCSS(strings.Join([]string{homepath.HomeDir, "mangas/EPUB", "epub.css"}, "/"), "") + bookCss, _ := book.AddCSS(strings.Join([]string{epubPath, "epub.css"}, "/"), "") bookCover, _ := book.AddImage(coverPath, coverFile) book.SetCover(bookCover, "") @@ -79,21 +74,22 @@ func CreateEpub(mangaPath string, mangaTitle string, mangaId string) { addPages(book, mangaPath, bookCss) fmt.Println("Writing EPUB to disk...") - err = book.Write(strings.Join([]string{homepath.HomeDir, "mangas/EPUB", mangaTitle + ".epub"}, "/")) + err := book.Write(strings.Join([]string{epubPath, mangaTitle + ".epub"}, "/")) if err != nil { log.Fatal(err) } - fmt.Println("Adding EPUB to calibre DB") - cmd := exec.Command("calibredb", "add", "--automerge", "overwrite", strings.Join([]string{homepath.HomeDir, "mangas/EPUB", mangaTitle + ".epub"}, "/")) - var out strings.Builder - cmd.Stdout = &out - fmt.Println(cmd) - err = cmd.Run() - if err != nil { - log.Fatal(err) - } - fmt.Printf(out.String()) + // TODO add check to skip calibre call if calibre not found + fmt.Println("Adding EPUB to calibre DB") + cmd := exec.Command("calibredb", "add", "--automerge", "overwrite", strings.Join([]string{epubPath, mangaTitle + ".epub"}, "/")) + var out strings.Builder + cmd.Stdout = &out + fmt.Println(cmd) + err = cmd.Run() + if err != nil { + log.Fatal(err) + } + fmt.Printf(out.String()) } diff --git a/main.go b/main.go index 96b743e..16adab7 100644 --- a/main.go +++ b/main.go @@ -15,12 +15,19 @@ import ( func main() { // get infos for the manga we want to download + var path string var file string var forceDl bool var forceEpub bool var mangas []mangacrawler.MangaYaml var skipDl bool + homepath, err := os.UserHomeDir() + if err != nil { + log.Fatal(err) + } + + flag.StringVar(&path, "path", homepath+"/mangas", "Path where to download mangas and create EPUBs in. (Default is ~/mangas)") flag.BoolVar(&forceEpub, "force-epub", false, "Flag for creating an EPUB from the manga") flag.BoolVar(&skipDl, "skip-download", false, "Flag for not downloading the manga") flag.BoolVar(&forceDl, "force-download", false, "Download already downloaded chapters") @@ -47,29 +54,24 @@ func main() { } } - homepath, err := os.UserHomeDir() - if err != nil { - log.Fatal(err) - } - for i, manga := range mangas { manga.Name, manga.Completed = mangacrawler.GetMangaInfo(manga) var newChapter bool - mangapath := strings.Join([]string{homepath, "mangas/MangaDex", manga.Name}, "/") - os.MkdirAll(mangapath, 0755) + mangaPath := strings.Join([]string{path, "MangaDex", manga.Name}, "/") + os.MkdirAll(mangaPath, 0755) if (!manga.Completed && !skipDl) || forceDl { - manga, newChapter = mangacrawler.GetManga(manga, mangapath, forceDl) + manga, newChapter = mangacrawler.GetManga(manga, mangaPath, forceDl) } else if manga.Completed { fmt.Print(" Manga already completed!\n\n") } - if _, err := os.Stat(strings.Join([]string{homepath, "mangas/EPUB", manga.Name + ".epub"}, "/")); err != nil || forceEpub || newChapter { - epubPath := strings.Join([]string{homepath, "mangas/EPUB"}, "/") + if _, err := os.Stat(strings.Join([]string{path, "EPUB", manga.Name + ".epub"}, "/")); err != nil || forceEpub || newChapter { + epubPath := strings.Join([]string{path, "EPUB"}, "/") os.MkdirAll(epubPath, 0755) fmt.Println("Generating EPUB") - createepub.CreateEpub(mangapath, manga.Name, manga.ID) + createepub.CreateEpub(mangaPath, epubPath, manga.Name, manga.ID) fmt.Printf("EPUB created and saved under: %s\n\n", epubPath) } else { fmt.Print("No update on manga, skipping epub creation!\n\n")