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

@@ -8,8 +8,7 @@ import (
"mangacrawler/mangacrawler" "mangacrawler/mangacrawler"
"net/http" "net/http"
"os" "os"
"os/exec" "os/exec"
"os/user"
"regexp" "regexp"
"strings" "strings"
@@ -43,15 +42,11 @@ type MangaDesc struct {
En string `json:"en"` 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 var author MangaPlus
url := "https://api.mangadex.org/manga/" + mangaId + "?includes[]=author&includes[]=cover_art" url := "https://api.mangadex.org/manga/" + mangaId + "?includes[]=author&includes[]=cover_art"
data := mangacrawler.GetJson(url) data := mangacrawler.GetJson(url)
homepath, err := user.Current()
if err != nil {
panic(err)
}
if err := json.Unmarshal(data, &author); err != nil { if err := json.Unmarshal(data, &author); err != nil {
panic(err) panic(err)
@@ -69,7 +64,7 @@ func CreateEpub(mangaPath string, mangaTitle string, mangaId string) {
book := epub.NewEpub(mangaTitle) book := epub.NewEpub(mangaTitle)
book.SetAuthor(author.Data.Rels[0].Attributes.Name) book.SetAuthor(author.Data.Rels[0].Attributes.Name)
book.SetDescription(author.Data.Attr.Desc.En) 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) bookCover, _ := book.AddImage(coverPath, coverFile)
book.SetCover(bookCover, "") book.SetCover(bookCover, "")
@@ -79,21 +74,22 @@ func CreateEpub(mangaPath string, mangaTitle string, mangaId string) {
addPages(book, mangaPath, bookCss) addPages(book, mangaPath, bookCss)
fmt.Println("Writing EPUB to disk...") 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
fmt.Println("Adding EPUB to calibre DB") // TODO add check to skip calibre call if calibre not found
cmd := exec.Command("calibredb", "add", "--automerge", "overwrite", strings.Join([]string{homepath.HomeDir, "mangas/EPUB", mangaTitle + ".epub"}, "/")) fmt.Println("Adding EPUB to calibre DB")
var out strings.Builder cmd := exec.Command("calibredb", "add", "--automerge", "overwrite", strings.Join([]string{epubPath, mangaTitle + ".epub"}, "/"))
cmd.Stdout = &out var out strings.Builder
fmt.Println(cmd) cmd.Stdout = &out
err = cmd.Run() fmt.Println(cmd)
if err != nil { err = cmd.Run()
log.Fatal(err) if err != nil {
} log.Fatal(err)
fmt.Printf(out.String()) }
fmt.Printf(out.String())
} }

24
main.go
View File

@@ -15,12 +15,19 @@ import (
func main() { func main() {
// get infos for the manga we want to download // get infos for the manga we want to download
var path string
var file string var file string
var forceDl bool var forceDl bool
var forceEpub bool var forceEpub bool
var mangas []mangacrawler.MangaYaml var mangas []mangacrawler.MangaYaml
var skipDl bool 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(&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(&skipDl, "skip-download", false, "Flag for not downloading the manga")
flag.BoolVar(&forceDl, "force-download", false, "Download already downloaded chapters") 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 { for i, manga := range mangas {
manga.Name, manga.Completed = mangacrawler.GetMangaInfo(manga) manga.Name, manga.Completed = mangacrawler.GetMangaInfo(manga)
var newChapter bool var newChapter bool
mangapath := strings.Join([]string{homepath, "mangas/MangaDex", manga.Name}, "/") mangaPath := strings.Join([]string{path, "MangaDex", manga.Name}, "/")
os.MkdirAll(mangapath, 0755) os.MkdirAll(mangaPath, 0755)
if (!manga.Completed && !skipDl) || forceDl { if (!manga.Completed && !skipDl) || forceDl {
manga, newChapter = mangacrawler.GetManga(manga, mangapath, forceDl) manga, newChapter = mangacrawler.GetManga(manga, mangaPath, forceDl)
} else if manga.Completed { } else if manga.Completed {
fmt.Print(" Manga already completed!\n\n") fmt.Print(" Manga already completed!\n\n")
} }
if _, err := os.Stat(strings.Join([]string{homepath, "mangas/EPUB", manga.Name + ".epub"}, "/")); err != nil || forceEpub || newChapter { if _, err := os.Stat(strings.Join([]string{path, "EPUB", manga.Name + ".epub"}, "/")); err != nil || forceEpub || newChapter {
epubPath := strings.Join([]string{homepath, "mangas/EPUB"}, "/") epubPath := strings.Join([]string{path, "EPUB"}, "/")
os.MkdirAll(epubPath, 0755) os.MkdirAll(epubPath, 0755)
fmt.Println("Generating EPUB") 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) fmt.Printf("EPUB created and saved under: %s\n\n", epubPath)
} else { } else {
fmt.Print("No update on manga, skipping epub creation!\n\n") fmt.Print("No update on manga, skipping epub creation!\n\n")