added latest states

This commit is contained in:
m3philis
2025-04-03 15:34:00 +02:00
parent 14116309dd
commit 37edab5197
6 changed files with 49 additions and 16 deletions

View File

@@ -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 {

Submodule go-epub updated: 9229546637...3c72d1128e

2
go.mod
View File

@@ -1,6 +1,6 @@
module mangacrawler
go 1.21.0
go 1.22.0
replace github.com/go-shiori/go-epub => ./go-epub

View File

@@ -1,4 +1,4 @@
go 1.21.0
go 1.22.0
use (
.

View File

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

View File

@@ -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,7 +47,12 @@ func GetMangaInfo(mangaYaml MangaYaml) (string, bool) {
}
// set home directory and create subdir to save manga in
mangaTitles := []string{manga.Data.Attributes.Title.EN}
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)