mirror of
https://github.com/bestnite/bilinovel-downloader.git
synced 2025-04-27 18:55:55 +08:00
Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
c9a7853cef | |||
b2130f60d5 | |||
d80c6053ab | |||
0c746c984b | |||
9d1d3f0f17 |
10
.vscode/launch.json
vendored
10
.vscode/launch.json
vendored
@ -2,12 +2,20 @@
|
|||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"name": "Debug download volume",
|
"name": "volume",
|
||||||
"type": "go",
|
"type": "go",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"mode": "auto",
|
"mode": "auto",
|
||||||
"program": "${workspaceFolder}",
|
"program": "${workspaceFolder}",
|
||||||
"args": ["download", "volume", "-n", "2013", "-v", "165880"]
|
"args": ["download", "volume", "-n", "2013", "-v", "165880"]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "novel",
|
||||||
|
"type": "go",
|
||||||
|
"request": "launch",
|
||||||
|
"mode": "auto",
|
||||||
|
"program": "${workspaceFolder}",
|
||||||
|
"args": ["download", "novel", "-n", "4325"]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bilinovel-downloader/downloader"
|
"bilinovel-downloader/downloader/bilinovel"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -60,7 +60,7 @@ func runDownloadNovel(cmd *cobra.Command, args []string) error {
|
|||||||
if novelArgs.NovelId == 0 {
|
if novelArgs.NovelId == 0 {
|
||||||
return fmt.Errorf("novel id is required")
|
return fmt.Errorf("novel id is required")
|
||||||
}
|
}
|
||||||
err := downloader.DownloadNovel(novelArgs.NovelId, novelArgs.outputPath)
|
err := bilinovel.DownloadNovel(novelArgs.NovelId, novelArgs.outputPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to download novel: %v", err)
|
return fmt.Errorf("failed to download novel: %v", err)
|
||||||
}
|
}
|
||||||
@ -75,7 +75,7 @@ func runDownloadVolume(cmd *cobra.Command, args []string) error {
|
|||||||
if volumeArgs.VolumeId == 0 {
|
if volumeArgs.VolumeId == 0 {
|
||||||
return fmt.Errorf("volume id is required")
|
return fmt.Errorf("volume id is required")
|
||||||
}
|
}
|
||||||
err := downloader.DownloadVolume(volumeArgs.NovelId, volumeArgs.VolumeId, volumeArgs.outputPath)
|
err := bilinovel.DownloadVolume(volumeArgs.NovelId, volumeArgs.VolumeId, volumeArgs.outputPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to download volume: %v", err)
|
return fmt.Errorf("failed to download volume: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bilinovel-downloader/utils"
|
"bilinovel-downloader/downloader/bilinovel"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -28,7 +28,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runPackage(cmd *cobra.Command, args []string) error {
|
func runPackage(cmd *cobra.Command, args []string) error {
|
||||||
err := utils.CreateEpub(pArgs.DirPath)
|
err := bilinovel.CreateEpub(pArgs.DirPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create epub: %v", err)
|
return fmt.Errorf("failed to create epub: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package downloader
|
package bilinovel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bilinovel-downloader/model"
|
"bilinovel-downloader/model"
|
||||||
@ -6,6 +6,7 @@ import (
|
|||||||
"bilinovel-downloader/utils"
|
"bilinovel-downloader/utils"
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
|
_ "embed"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
@ -286,6 +287,11 @@ func downloadVolume(volume *model.Volume, outputPath string) error {
|
|||||||
return fmt.Errorf("failed to render cover: %v", err)
|
return fmt.Errorf("failed to render cover: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = DownloadFont(filepath.Join(outputPath, "OEBPS/Fonts"))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to download font: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
contentsXHTMLPath := filepath.Join(outputPath, "OEBPS/Text/contents.xhtml")
|
contentsXHTMLPath := filepath.Join(outputPath, "OEBPS/Text/contents.xhtml")
|
||||||
err = os.MkdirAll(path.Dir(contentsXHTMLPath), 0755)
|
err = os.MkdirAll(path.Dir(contentsXHTMLPath), 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -331,7 +337,7 @@ func downloadVolume(volume *model.Volume, outputPath string) error {
|
|||||||
return fmt.Errorf("failed to create toc ncx: %v", err)
|
return fmt.Errorf("failed to create toc ncx: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = utils.CreateEpub(outputPath)
|
err = CreateEpub(outputPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create epub: %v", err)
|
return fmt.Errorf("failed to create epub: %v", err)
|
||||||
}
|
}
|
||||||
@ -407,6 +413,9 @@ func downloadChapterByPage(page, chapterIdx int, chapter *model.Chapter, outputP
|
|||||||
content.Find(".cgo").Remove()
|
content.Find(".cgo").Remove()
|
||||||
content.Find("center").Remove()
|
content.Find("center").Remove()
|
||||||
content.Find(".google-auto-placed").Remove()
|
content.Find(".google-auto-placed").Remove()
|
||||||
|
if strings.Contains(resp.String(), `font-family: "read"`) {
|
||||||
|
content.Find("p").Last().AddClass("read-font")
|
||||||
|
}
|
||||||
|
|
||||||
content.Find("img").Each(func(i int, s *goquery.Selection) {
|
content.Find("img").Each(func(i int, s *goquery.Selection) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -420,15 +429,12 @@ func downloadChapterByPage(page, chapterIdx int, chapter *model.Chapter, outputP
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fileName := filepath.Join(imgSavePath, fmt.Sprintf("%03v%s", i+1, path.Ext(imgUrl)))
|
fileName := filepath.Join(imgSavePath, fmt.Sprintf("%03v%s", len(chapter.ImageFullPaths)+1, path.Ext(imgUrl)))
|
||||||
err = DownloadImg(imgUrl, filepath.Join(outputPath, fileName))
|
err = DownloadImg(imgUrl, filepath.Join(outputPath, fileName))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
s.SetAttr("src", "../"+strings.TrimPrefix(fileName, "OEBPS/"))
|
s.SetAttr("src", "../"+strings.TrimPrefix(fileName, "OEBPS/"))
|
||||||
s.RemoveAttr("class")
|
s.RemoveAttr("class")
|
||||||
s.RemoveAttr("data-src")
|
s.RemoveAttr("data-src")
|
||||||
if s.AttrOr("alt", "") == "" {
|
|
||||||
s.SetAttr("alt", fmt.Sprintf("image-%03d", i+1))
|
|
||||||
}
|
|
||||||
chapter.ImageFullPaths = append(chapter.ImageFullPaths, filepath.Join(outputPath, fileName))
|
chapter.ImageFullPaths = append(chapter.ImageFullPaths, filepath.Join(outputPath, fileName))
|
||||||
chapter.ImageOEBPSPaths = append(chapter.ImageOEBPSPaths, strings.TrimPrefix(fileName, "OEBPS/"))
|
chapter.ImageOEBPSPaths = append(chapter.ImageOEBPSPaths, strings.TrimPrefix(fileName, "OEBPS/"))
|
||||||
}
|
}
|
||||||
@ -524,7 +530,7 @@ func CreateContentOPF(dirPath string, uuid string, volume *model.Volume) error {
|
|||||||
Metas: []model.DublinCoreMeta{
|
Metas: []model.DublinCoreMeta{
|
||||||
{
|
{
|
||||||
Name: "cover",
|
Name: "cover",
|
||||||
Content: fmt.Sprintf("Images/cover%s", path.Ext(volume.Cover)),
|
Content: "images-cover" + path.Ext(volume.Cover),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Property: "dcterms:modified",
|
Property: "dcterms:modified",
|
||||||
@ -549,21 +555,26 @@ func CreateContentOPF(dirPath string, uuid string, volume *model.Volume) error {
|
|||||||
Media: "application/x-dtbncx+xml",
|
Media: "application/x-dtbncx+xml",
|
||||||
})
|
})
|
||||||
manifest.Items = append(manifest.Items, model.ManifestItem{
|
manifest.Items = append(manifest.Items, model.ManifestItem{
|
||||||
ID: "cover",
|
ID: "cover.xhtml",
|
||||||
Link: "Text/cover.xhtml",
|
Link: "Text/cover.xhtml",
|
||||||
Media: "application/xhtml+xml",
|
Media: "application/xhtml+xml",
|
||||||
})
|
})
|
||||||
manifest.Items = append(manifest.Items, model.ManifestItem{
|
manifest.Items = append(manifest.Items, model.ManifestItem{
|
||||||
ID: "contents",
|
ID: "contents.xhtml",
|
||||||
Link: "Text/contents.xhtml",
|
Link: "Text/contents.xhtml",
|
||||||
Media: "application/xhtml+xml",
|
Media: "application/xhtml+xml",
|
||||||
Properties: "nav",
|
Properties: "nav",
|
||||||
})
|
})
|
||||||
manifest.Items = append(manifest.Items, model.ManifestItem{
|
manifest.Items = append(manifest.Items, model.ManifestItem{
|
||||||
ID: "images-cover",
|
ID: "images-cover" + path.Ext(volume.Cover),
|
||||||
Link: fmt.Sprintf("Images/cover%s", path.Ext(volume.Cover)),
|
Link: fmt.Sprintf("Images/cover%s", path.Ext(volume.Cover)),
|
||||||
Media: fmt.Sprintf("image/%s", strings.ReplaceAll(strings.TrimPrefix(path.Ext(volume.Cover), "."), "jpg", "jpeg")),
|
Media: fmt.Sprintf("image/%s", strings.ReplaceAll(strings.TrimPrefix(path.Ext(volume.Cover), "."), "jpg", "jpeg")),
|
||||||
})
|
})
|
||||||
|
manifest.Items = append(manifest.Items, model.ManifestItem{
|
||||||
|
ID: "read.ttf",
|
||||||
|
Link: "Fonts/read.ttf",
|
||||||
|
Media: "application/vnd.ms-opentype",
|
||||||
|
})
|
||||||
for _, chapter := range volume.Chapters {
|
for _, chapter := range volume.Chapters {
|
||||||
manifest.Items = append(manifest.Items, model.ManifestItem{
|
manifest.Items = append(manifest.Items, model.ManifestItem{
|
||||||
ID: path.Base(chapter.TextOEBPSPath),
|
ID: path.Base(chapter.TextOEBPSPath),
|
||||||
@ -655,3 +666,23 @@ func CreateTocNCX(dirPath string, uuid string, volume *model.Volume) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//go:embed read.ttf
|
||||||
|
var readTTF []byte
|
||||||
|
|
||||||
|
func DownloadFont(outputPath string) error {
|
||||||
|
log.Printf("Writing Font: %s", outputPath)
|
||||||
|
|
||||||
|
fontPath := filepath.Join(outputPath, "read.ttf")
|
||||||
|
err := os.MkdirAll(path.Dir(fontPath), 0755)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create font directory: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.WriteFile(fontPath, readTTF, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write font: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@ -1,14 +1,16 @@
|
|||||||
package utils
|
package bilinovel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
"bilinovel-downloader/template"
|
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateEpub(path string) error {
|
func CreateEpub(path string) error {
|
||||||
|
log.Printf("Creating epub for %s", path)
|
||||||
|
|
||||||
savePath := path + ".epub"
|
savePath := path + ".epub"
|
||||||
zipFile, err := os.Create(savePath)
|
zipFile, err := os.Create(savePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -29,7 +31,7 @@ func CreateEpub(path string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = addStringToZip(zipWriter, "OEBPS/Styles/style.css", template.StyleCSS, zip.Deflate)
|
err = addStringToZip(zipWriter, "OEBPS/Styles/style.css", StyleCSS, zip.Deflate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -81,6 +83,9 @@ func addStringToZip(zipWriter *zip.Writer, relPath, content string, method uint1
|
|||||||
|
|
||||||
func addDirContentToZip(zipWriter *zip.Writer, dirPath string, method uint16) error {
|
func addDirContentToZip(zipWriter *zip.Writer, dirPath string, method uint16) error {
|
||||||
return filepath.Walk(dirPath, func(filePath string, info os.FileInfo, err error) error {
|
return filepath.Walk(dirPath, func(filePath string, info os.FileInfo, err error) error {
|
||||||
|
if filepath.Base(filePath) == "volume.json" {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
BIN
downloader/bilinovel/read.ttf
Normal file
BIN
downloader/bilinovel/read.ttf
Normal file
Binary file not shown.
@ -1,6 +1,15 @@
|
|||||||
package template
|
package bilinovel
|
||||||
|
|
||||||
const StyleCSS = `
|
const StyleCSS = `
|
||||||
|
@font-face {
|
||||||
|
font-family: "MI LANTING";
|
||||||
|
src: url(../Fonts/read.ttf);
|
||||||
|
}
|
||||||
|
|
||||||
|
.read-font {
|
||||||
|
font-family: "MI LANTING", serif !important;
|
||||||
|
}
|
||||||
|
|
||||||
body > div {
|
body > div {
|
||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
padding: 20px;
|
padding: 20px;
|
Loading…
x
Reference in New Issue
Block a user