add parameter to set path to save to

This commit is contained in:
m3philis
2023-12-10 00:13:38 +01:00
parent 0997659112
commit 14116309dd
2 changed files with 28 additions and 30 deletions

View File

@@ -9,7 +9,6 @@ import (
"net/http"
"os"
"os/exec"
"os/user"
"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,13 +74,14 @@ 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)
}
// 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{homepath.HomeDir, "mangas/EPUB", mangaTitle + ".epub"}, "/"))
cmd := exec.Command("calibredb", "add", "--automerge", "overwrite", strings.Join([]string{epubPath, mangaTitle + ".epub"}, "/"))
var out strings.Builder
cmd.Stdout = &out
fmt.Println(cmd)

24
main.go
View File

@@ -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")