diff --git a/createepub/createepub.go b/createepub/createepub.go index 0f5cc03..7db305d 100644 --- a/createepub/createepub.go +++ b/createepub/createepub.go @@ -42,8 +42,31 @@ type MangaDesc struct { En string `json:"en"` } -func CreateEpub(mangaPath string, epubPath string, mangaTitle string, mangaId string) { +func CreateEpub(mangaPath string, mangaTitle string, mangaId string) { var author MangaPlus + var epubCss string + + epubCss = `@page { + background-color: #FFF; + margin-bottom: 0pt; + margin-left: 0pt; + margin-right: 0pt; + margin-top: 0pt; +} +.page { + width: auto; + height: auto; + break-after: page; + break-before: page; +} +.chapter { + width: auto; + height: auto; + break-after: page; + break-before: page; +}` + + _ = os.WriteFile("/tmp/epub/epub.css", []byte(epubCss), 0644) url := "https://api.mangadex.org/manga/" + mangaId + "?includes[]=author&includes[]=cover_art" data := mangacrawler.GetJson(url) @@ -61,10 +84,13 @@ func CreateEpub(mangaPath string, epubPath string, mangaTitle string, mangaId st } } - book := epub.NewEpub(mangaTitle) + book, err := epub.NewEpub(mangaTitle) + if err != nil { + panic(err) + } book.SetAuthor(author.Data.Rels[0].Attributes.Name) book.SetDescription(author.Data.Attr.Desc.En) - bookCss, _ := book.AddCSS(strings.Join([]string{epubPath, "epub.css"}, "/"), "") + bookCss, _ := book.AddCSS("/tmp/epub/epub.css", "epubCss") bookCover, _ := book.AddImage(coverPath, coverFile) book.SetCover(bookCover, "") @@ -73,15 +99,15 @@ func CreateEpub(mangaPath string, epubPath string, mangaTitle string, mangaId st fmt.Println("Adding pages to EPUB. Each chapter is a section\nIf chapter title is available that will be used for section title") addPages(book, mangaPath, bookCss) - fmt.Println("Writing EPUB to disk...") - err := book.Write(strings.Join([]string{epubPath, mangaTitle + ".epub"}, "/")) + fmt.Println("Writing EPUB to disk to add it to calibre DB") + err = book.Write(strings.Join([]string{"/tmp/epub", 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{epubPath, mangaTitle + ".epub"}, "/")) + cmd := exec.Command("calibredb", "add", "--automerge", "overwrite", strings.Join([]string{"/tmp/epub", mangaTitle + ".epub"}, "/")) var out strings.Builder cmd.Stdout = &out fmt.Println(cmd) @@ -91,6 +117,8 @@ func CreateEpub(mangaPath string, epubPath string, mangaTitle string, mangaId st } fmt.Printf(out.String()) + _ = os.Remove(strings.Join([]string{"/tmp/epub", mangaTitle + ".epub"}, "/")) + } func addPages(book *epub.Epub, mangaPath string, bookCss string) *epub.Epub { diff --git a/go-epub b/go-epub index 9229546..3c72d11 160000 --- a/go-epub +++ b/go-epub @@ -1 +1 @@ -Subproject commit 92295466371650445c7817072fad3227c78e4b2f +Subproject commit 3c72d1128ec3d342221e0df0eb4e31fc5134a196 diff --git a/go.mod b/go.mod index a8126c4..ffcc7c2 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module mangacrawler -go 1.21.0 +go 1.22.0 replace github.com/go-shiori/go-epub => ./go-epub diff --git a/go.work b/go.work index b6937e0..43145fe 100644 --- a/go.work +++ b/go.work @@ -1,4 +1,4 @@ -go 1.21.0 +go 1.22.0 use ( . diff --git a/main.go b/main.go index 16adab7..72429ce 100644 --- a/main.go +++ b/main.go @@ -67,12 +67,11 @@ func main() { fmt.Print(" Manga already completed!\n\n") } - 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) + if forceEpub || newChapter { + os.MkdirAll("/tmp/epub", 0755) fmt.Println("Generating EPUB") - createepub.CreateEpub(mangaPath, epubPath, manga.Name, manga.ID) - fmt.Printf("EPUB created and saved under: %s\n\n", epubPath) + createepub.CreateEpub(mangaPath, manga.Name, manga.ID) + fmt.Printf("EPUB created and saved under: %s\n\n", "/tmp/epub") } else { fmt.Print("No update on manga, skipping epub creation!\n\n") } diff --git a/mangacrawler/mangasearch.go b/mangacrawler/mangasearch.go index 7d22849..f66119b 100644 --- a/mangacrawler/mangasearch.go +++ b/mangacrawler/mangasearch.go @@ -31,6 +31,7 @@ type Titles struct { func GetMangaInfo(mangaYaml MangaYaml) (string, bool) { var manga Manga + var mangaTitles []string status := false homepath, _ := os.UserHomeDir() @@ -46,8 +47,13 @@ func GetMangaInfo(mangaYaml MangaYaml) (string, bool) { } // set home directory and create subdir to save manga in - mangaTitles := []string{manga.Data.Attributes.Title.EN} - for _, title := range manga.Data.Attributes.AltTitle { + if manga.Data.Attributes.Title.EN != "" { + mangaTitles = append(mangaTitles, manga.Data.Attributes.Title.EN) + } + if manga.Data.Attributes.Title.JP != "" { + mangaTitles = append(mangaTitles, manga.Data.Attributes.Title.JP) + } + for _, title := range manga.Data.Attributes.AltTitle { if title.EN != "" { mangaTitles = append(mangaTitles, title.EN) } else if title.JP != "" {