mirror of
https://github.com/bestnite/bilinovel-downloader.git
synced 2025-10-26 17:14:24 +00:00
Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f1320cb978 | |||
|
|
434d5f54bd | ||
|
b8cd053b00
|
|||
|
560cdfdec9
|
|||
|
ed5440f5fb
|
|||
|
26f82dd9ea
|
|||
|
e9fbe5c5db
|
|||
| 75745b9431 | |||
| ca3fdf8980 | |||
| 042b383988 | |||
| c9a7853cef | |||
| b2130f60d5 | |||
| d80c6053ab | |||
| 0c746c984b | |||
| 9d1d3f0f17 | |||
| 6028e7d8c2 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
|||||||
novels/
|
novels/
|
||||||
|
dist/
|
||||||
|
|||||||
31
.goreleaser.yaml
Normal file
31
.goreleaser.yaml
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
project_name: bilinovel-downloader
|
||||||
|
before:
|
||||||
|
hooks:
|
||||||
|
- templ generate
|
||||||
|
builds:
|
||||||
|
- env:
|
||||||
|
- CGO_ENABLED=0
|
||||||
|
goos:
|
||||||
|
- windows
|
||||||
|
- linux
|
||||||
|
- darwin
|
||||||
|
goarch:
|
||||||
|
- amd64
|
||||||
|
- arm64
|
||||||
|
- arm
|
||||||
|
- "386"
|
||||||
|
ldflags:
|
||||||
|
- -s -w -X bilinovel-downloader/cmd.Version={{ .Version }}
|
||||||
|
flags:
|
||||||
|
- -trimpath
|
||||||
|
archives:
|
||||||
|
- format: tar.gz
|
||||||
|
format_overrides:
|
||||||
|
- format: zip
|
||||||
|
goos: windows
|
||||||
|
wrap_in_directory: true
|
||||||
|
release:
|
||||||
|
draft: true
|
||||||
|
upx:
|
||||||
|
- enabled: true
|
||||||
|
compress: best
|
||||||
8
.vscode/launch.json
vendored
8
.vscode/launch.json
vendored
@@ -2,12 +2,16 @@
|
|||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"name": "Debug download volume",
|
"name": "download",
|
||||||
"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",
|
||||||
|
"-n",
|
||||||
|
"3095"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
34
README.md
34
README.md
@@ -1,24 +1,24 @@
|
|||||||
# BiliNovel Downloader
|
# Bilinovel Downloader
|
||||||
|
|
||||||
这是一个用于下载和生成轻小说 EPUB 电子书的工具。
|
这是一个用于从 Bilinovel 下载和生成轻小说 EPUB 电子书的工具。
|
||||||
|
生成的 EPUB 文件完全符合 EPUB 标准,可以在 Calibre 检查中无错误通过。
|
||||||
|
|
||||||
## 功能特点
|
## 使用示例
|
||||||
|
|
||||||
- 支持下载轻小说并转换为标准 EPUB 格式
|
1. 下载整本 `https://www.bilinovel.com/novel/2388.html`
|
||||||
- 自动处理图片和文本内容
|
|
||||||
- 生成符合 EPUB 3.0 规范的电子书文件
|
|
||||||
- 支持多章节内容的组织和管理
|
|
||||||
- 保留原有插图和排版格式
|
|
||||||
|
|
||||||
## 使用说明
|
```bash
|
||||||
|
bilinovel-downloader download -n 2388
|
||||||
|
```
|
||||||
|
|
||||||
1. 确保系统环境满足要求
|
2. 下载单卷 `https://www.bilinovel.com/novel/2388/vol_84522.html`
|
||||||
2. 运行下载器获取小说内容
|
|
||||||
3. 程序会自动处理并生成标准格式的 EPUB 文件
|
|
||||||
4. 生成的电子书文件可以在任何支持 EPUB 3.0 的阅读器中打开
|
|
||||||
|
|
||||||
## 注意事项
|
```bash
|
||||||
|
bilinovel-downloader download -n 2388 -v 84522
|
||||||
|
```
|
||||||
|
|
||||||
- 生成的 EPUB 文件严格遵循 EPUB 3.0 规范
|
3. 对自动生成的 epub 格式不满意可以自行修改后使用命令打包
|
||||||
- 建议使用支持 EPUB 3.0 的阅读器以获得最佳阅读体验
|
|
||||||
- 请遵守相关法律法规,合理使用下载的内容
|
```bash
|
||||||
|
bilinovel-downloader pack -d <目录路径>
|
||||||
|
```
|
||||||
|
|||||||
145
cmd/download.go
145
cmd/download.go
@@ -1,8 +1,15 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bilinovel-downloader/downloader"
|
"bilinovel-downloader/downloader/bilinovel"
|
||||||
|
"bilinovel-downloader/epub"
|
||||||
|
"bilinovel-downloader/model"
|
||||||
|
"bilinovel-downloader/text"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@@ -11,74 +18,120 @@ var downloadCmd = &cobra.Command{
|
|||||||
Use: "download",
|
Use: "download",
|
||||||
Short: "Download a novel or volume",
|
Short: "Download a novel or volume",
|
||||||
Long: "Download a novel or volume",
|
Long: "Download a novel or volume",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
err := runDownloadNovel()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("failed to download novel: %v", err)
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var downloadNovelCmd = &cobra.Command{
|
type downloadCmdArgs struct {
|
||||||
Use: "novel",
|
|
||||||
Short: "Download a novel, default download all volumes",
|
|
||||||
Long: "Download a novel, default download all volumes",
|
|
||||||
RunE: runDownloadNovel,
|
|
||||||
}
|
|
||||||
|
|
||||||
var downloadVolumeCmd = &cobra.Command{
|
|
||||||
Use: "volume",
|
|
||||||
Short: "Download a volume",
|
|
||||||
Long: "Download a volume",
|
|
||||||
RunE: runDownloadVolume,
|
|
||||||
}
|
|
||||||
|
|
||||||
type downloadNovelArgs struct {
|
|
||||||
NovelId int `validate:"required"`
|
|
||||||
outputPath string
|
|
||||||
}
|
|
||||||
|
|
||||||
type downloadVolumeArgs struct {
|
|
||||||
NovelId int `validate:"required"`
|
NovelId int `validate:"required"`
|
||||||
VolumeId int `validate:"required"`
|
VolumeId int `validate:"required"`
|
||||||
outputPath string
|
outputPath string
|
||||||
|
outputType string
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
novelArgs downloadNovelArgs
|
downloadArgs downloadCmdArgs
|
||||||
volumeArgs downloadVolumeArgs
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
downloadNovelCmd.Flags().IntVarP(&novelArgs.NovelId, "novel-id", "n", 0, "novel id")
|
downloadCmd.Flags().IntVarP(&downloadArgs.NovelId, "novel-id", "n", 0, "novel id")
|
||||||
downloadNovelCmd.Flags().StringVarP(&novelArgs.outputPath, "output-path", "o", "./novels", "output path")
|
downloadCmd.Flags().IntVarP(&downloadArgs.VolumeId, "volume-id", "v", 0, "volume id")
|
||||||
|
downloadCmd.Flags().StringVarP(&downloadArgs.outputPath, "output-path", "o", "novels", "output path")
|
||||||
downloadVolumeCmd.Flags().IntVarP(&volumeArgs.NovelId, "novel-id", "n", 0, "novel id")
|
downloadCmd.Flags().StringVarP(&downloadArgs.outputType, "output-type", "t", "epub", "output type, epub or text")
|
||||||
downloadVolumeCmd.Flags().IntVarP(&volumeArgs.VolumeId, "volume-id", "v", 0, "volume id")
|
|
||||||
downloadVolumeCmd.Flags().StringVarP(&volumeArgs.outputPath, "output-path", "o", "./novels", "output path")
|
|
||||||
|
|
||||||
downloadCmd.AddCommand(downloadNovelCmd)
|
|
||||||
downloadCmd.AddCommand(downloadVolumeCmd)
|
|
||||||
RootCmd.AddCommand(downloadCmd)
|
RootCmd.AddCommand(downloadCmd)
|
||||||
}
|
}
|
||||||
|
|
||||||
func runDownloadNovel(cmd *cobra.Command, args []string) error {
|
func runDownloadNovel() error {
|
||||||
if novelArgs.NovelId == 0 {
|
downloader, err := bilinovel.New()
|
||||||
return fmt.Errorf("novel id is required")
|
|
||||||
}
|
|
||||||
err := downloader.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 create downloader: %v", err)
|
||||||
}
|
}
|
||||||
|
// 确保在函数结束时关闭资源
|
||||||
|
defer func() {
|
||||||
|
if closeErr := downloader.Close(); closeErr != nil {
|
||||||
|
log.Printf("Failed to close downloader: %v", closeErr)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
return nil
|
if downloadArgs.NovelId == 0 {
|
||||||
}
|
|
||||||
|
|
||||||
func runDownloadVolume(cmd *cobra.Command, args []string) error {
|
|
||||||
if volumeArgs.NovelId == 0 {
|
|
||||||
return fmt.Errorf("novel id is required")
|
return fmt.Errorf("novel id is required")
|
||||||
}
|
}
|
||||||
if volumeArgs.VolumeId == 0 {
|
|
||||||
return fmt.Errorf("volume id is required")
|
if downloadArgs.VolumeId == 0 {
|
||||||
|
// 下载整本小说
|
||||||
|
novel, err := downloader.GetNovel(downloadArgs.NovelId, true)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get novel: %v", err)
|
||||||
}
|
}
|
||||||
err := downloader.DownloadVolume(volumeArgs.NovelId, volumeArgs.VolumeId, volumeArgs.outputPath)
|
for _, volume := range novel.Volumes {
|
||||||
|
err = downloadVolume(downloader, volume.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to download volume: %v", err)
|
return fmt.Errorf("failed to download volume: %v", err)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 下载单卷
|
||||||
|
err = downloadVolume(downloader, downloadArgs.VolumeId)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to download volume: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func downloadVolume(downloader model.Downloader, volumeId int) error {
|
||||||
|
jsonPath := filepath.Join(downloadArgs.outputPath, fmt.Sprintf("volume-%d-%d.json", downloadArgs.NovelId, volumeId))
|
||||||
|
err := os.MkdirAll(filepath.Dir(jsonPath), 0755)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create directory: %v", err)
|
||||||
|
}
|
||||||
|
_, err = os.Stat(jsonPath)
|
||||||
|
volume := &model.Volume{}
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
volume, err = downloader.GetVolume(downloadArgs.NovelId, volumeId, false)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to get volume: %v", err)
|
||||||
|
}
|
||||||
|
jsonFile, err := os.Create(jsonPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create json file: %v", err)
|
||||||
|
}
|
||||||
|
err = json.NewEncoder(jsonFile).Encode(volume)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to encode json file: %v", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("failed to get volume: %v", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
jsonFile, err := os.Open(jsonPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to open json file: %v", err)
|
||||||
|
}
|
||||||
|
defer jsonFile.Close()
|
||||||
|
err = json.NewDecoder(jsonFile).Decode(volume)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to decode json file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch downloadArgs.outputType {
|
||||||
|
case "epub":
|
||||||
|
err = epub.PackVolumeToEpub(volume, downloadArgs.outputPath, downloader.GetStyleCSS(), downloader.GetExtraFiles())
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to pack volume: %v", err)
|
||||||
|
}
|
||||||
|
case "text":
|
||||||
|
err = text.PackVolumeToText(volume, downloadArgs.outputPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to pack volume: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bilinovel-downloader/utils"
|
"bilinovel-downloader/epub"
|
||||||
"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 := epub.PackEpub(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)
|
||||||
}
|
}
|
||||||
|
|||||||
22
cmd/version.go
Normal file
22
cmd/version.go
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Version = "dev"
|
||||||
|
)
|
||||||
|
|
||||||
|
var versionCmd = &cobra.Command{
|
||||||
|
Use: "version",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
fmt.Println("version: ", Version)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RootCmd.AddCommand(versionCmd)
|
||||||
|
}
|
||||||
@@ -1,616 +0,0 @@
|
|||||||
package downloader
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bilinovel-downloader/model"
|
|
||||||
"bilinovel-downloader/template"
|
|
||||||
"bilinovel-downloader/utils"
|
|
||||||
"bytes"
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"path"
|
|
||||||
"path/filepath"
|
|
||||||
"regexp"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/PuerkitoBio/goquery"
|
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
|
||||||
|
|
||||||
func GetNovel(novelId int) (*model.Novel, error) {
|
|
||||||
novelUrl := fmt.Sprintf("https://www.bilinovel.com/novel/%v.html", novelId)
|
|
||||||
resp, err := utils.Request().Get(novelUrl)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to get novel info: %v", err)
|
|
||||||
}
|
|
||||||
if resp.StatusCode() != http.StatusOK {
|
|
||||||
return nil, fmt.Errorf("failed to get novel info: %v", resp.Status())
|
|
||||||
}
|
|
||||||
|
|
||||||
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(resp.Body()))
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to parse html: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
novel := &model.Novel{}
|
|
||||||
|
|
||||||
novel.Title = strings.TrimSpace(doc.Find(".book-title").First().Text())
|
|
||||||
novel.Description = strings.TrimSpace(doc.Find(".book-summary>content").First().Text())
|
|
||||||
|
|
||||||
doc.Find(".authorname>a").Each(func(i int, s *goquery.Selection) {
|
|
||||||
novel.Authors = append(novel.Authors, strings.TrimSpace(s.Text()))
|
|
||||||
})
|
|
||||||
doc.Find(".illname>a").Each(func(i int, s *goquery.Selection) {
|
|
||||||
novel.Authors = append(novel.Authors, strings.TrimSpace(s.Text()))
|
|
||||||
})
|
|
||||||
|
|
||||||
volumes, err := GetNovelVolumes(novelId)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to get novel volumes: %v", err)
|
|
||||||
}
|
|
||||||
novel.Volumes = volumes
|
|
||||||
|
|
||||||
return novel, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetVolume(novelId int, volumeId int) (*model.Volume, error) {
|
|
||||||
novelUrl := fmt.Sprintf("https://www.bilinovel.com/novel/%v/vol_%v.html", novelId, volumeId)
|
|
||||||
resp, err := utils.Request().Get(novelUrl)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to get novel info: %v", err)
|
|
||||||
}
|
|
||||||
if resp.StatusCode() != http.StatusOK {
|
|
||||||
return nil, fmt.Errorf("failed to get novel info: %v", resp.Status())
|
|
||||||
}
|
|
||||||
|
|
||||||
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(resp.Body()))
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to parse html: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
volume := &model.Volume{}
|
|
||||||
volume.Title = strings.TrimSpace(doc.Find(".book-title").First().Text())
|
|
||||||
volume.Description = strings.TrimSpace(doc.Find(".book-summary>content").First().Text())
|
|
||||||
volume.Cover = doc.Find(".book-cover").First().AttrOr("src", "")
|
|
||||||
volume.Url = novelUrl
|
|
||||||
volume.Chapters = make([]*model.Chapter, 0)
|
|
||||||
|
|
||||||
doc.Find(".authorname>a").Each(func(i int, s *goquery.Selection) {
|
|
||||||
volume.Authors = append(volume.Authors, strings.TrimSpace(s.Text()))
|
|
||||||
})
|
|
||||||
doc.Find(".illname>a").Each(func(i int, s *goquery.Selection) {
|
|
||||||
volume.Authors = append(volume.Authors, strings.TrimSpace(s.Text()))
|
|
||||||
})
|
|
||||||
|
|
||||||
doc.Find(".chapter-li.jsChapter").Each(func(i int, s *goquery.Selection) {
|
|
||||||
volume.Chapters = append(volume.Chapters, &model.Chapter{
|
|
||||||
Title: s.Find("a").Text(),
|
|
||||||
Url: fmt.Sprintf("https://www.bilinovel.com%v", s.Find("a").AttrOr("href", "")),
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
return volume, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetNovelVolumes(novelId int) ([]*model.Volume, error) {
|
|
||||||
catelogUrl := fmt.Sprintf("https://www.bilinovel.com/novel/%v/catalog", novelId)
|
|
||||||
resp, err := utils.Request().Get(catelogUrl)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to get catelog: %v", err)
|
|
||||||
}
|
|
||||||
if resp.StatusCode() != http.StatusOK {
|
|
||||||
return nil, fmt.Errorf("failed to get catelog: %v", resp.Status())
|
|
||||||
}
|
|
||||||
|
|
||||||
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(resp.Body()))
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to parse html: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
volumeRegexp := regexp.MustCompile(fmt.Sprintf(`/novel/%v/vol_(\d+).html`, novelId))
|
|
||||||
|
|
||||||
volumeIds := make([]string, 0)
|
|
||||||
doc.Find("a.volume-cover-img").Each(func(i int, s *goquery.Selection) {
|
|
||||||
link := s.AttrOr("href", "")
|
|
||||||
matches := volumeRegexp.FindStringSubmatch(link)
|
|
||||||
if len(matches) > 0 {
|
|
||||||
volumeIds = append(volumeIds, matches[1])
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
volumes := make([]*model.Volume, 0)
|
|
||||||
for _, volumeIdStr := range volumeIds {
|
|
||||||
volumeId, err := strconv.Atoi(volumeIdStr)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to convert volume id: %v", err)
|
|
||||||
}
|
|
||||||
volume, err := GetVolume(novelId, volumeId)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to get volume info: %v", err)
|
|
||||||
}
|
|
||||||
volumes = append(volumes, volume)
|
|
||||||
}
|
|
||||||
|
|
||||||
return volumes, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func DownloadNovel(novelId int, outputPath string) error {
|
|
||||||
log.Printf("Downloading Novel: %v", novelId)
|
|
||||||
|
|
||||||
novel, err := GetNovel(novelId)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to get novel info: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
outputPath = filepath.Join(outputPath, utils.CleanDirName(novel.Title))
|
|
||||||
err = os.MkdirAll(outputPath, 0755)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create output directory: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, volume := range novel.Volumes {
|
|
||||||
err := downloadVolume(volume, outputPath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to download volume: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func DownloadVolume(novelId, volumeId int, outputPath string) error {
|
|
||||||
volume, err := GetVolume(novelId, volumeId)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to get volume info: %v", err)
|
|
||||||
}
|
|
||||||
err = downloadVolume(volume, outputPath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to download volume: %v", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func downloadVolume(volume *model.Volume, outputPath string) error {
|
|
||||||
log.Printf("Downloading Volume: %s", volume.Title)
|
|
||||||
outputPath = filepath.Join(outputPath, utils.CleanDirName(volume.Title))
|
|
||||||
err := os.MkdirAll(outputPath, 0755)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create output directory: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = os.Stat(filepath.Join(outputPath, "volume.json"))
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
for idx, chapter := range volume.Chapters {
|
|
||||||
err := DownloadChapter(idx, chapter, outputPath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to download chapter: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
jsonBytes, err := os.ReadFile(filepath.Join(outputPath, "volume.json"))
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to read volume: %v", err)
|
|
||||||
}
|
|
||||||
err = json.Unmarshal(jsonBytes, volume)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to unmarshal volume: %v", err)
|
|
||||||
}
|
|
||||||
for idx, chapter := range volume.Chapters {
|
|
||||||
file, err := os.Create(filepath.Join(outputPath, fmt.Sprintf("OEBPS/Text/chapter-%03v.xhtml", idx+1)))
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create chapter file: %v", err)
|
|
||||||
}
|
|
||||||
err = template.ContentXHTML(chapter).Render(context.Background(), file)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to render text file: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := range volume.Chapters {
|
|
||||||
volume.Chapters[i].ImageFullPaths = utils.Unique(volume.Chapters[i].ImageFullPaths)
|
|
||||||
volume.Chapters[i].ImageOEBPSPaths = utils.Unique(volume.Chapters[i].ImageOEBPSPaths)
|
|
||||||
}
|
|
||||||
|
|
||||||
jsonBytes, err := json.Marshal(volume)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to marshal volume: %v", err)
|
|
||||||
}
|
|
||||||
err = os.WriteFile(filepath.Join(outputPath, "volume.json"), jsonBytes, 0644)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to write volume: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
coverPath := filepath.Join(outputPath, "OEBPS/Images/cover.jpg")
|
|
||||||
err = os.MkdirAll(path.Dir(coverPath), 0755)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create cover directory: %v", err)
|
|
||||||
}
|
|
||||||
err = DownloadImg(volume.Cover, coverPath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to download cover: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
coverXHTMLPath := filepath.Join(outputPath, "OEBPS/Text/cover.xhtml")
|
|
||||||
err = os.MkdirAll(path.Dir(coverXHTMLPath), 0755)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create cover directory: %v", err)
|
|
||||||
}
|
|
||||||
file, err := os.Create(coverXHTMLPath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create cover file: %v", err)
|
|
||||||
}
|
|
||||||
err = template.ContentXHTML(&model.Chapter{
|
|
||||||
Title: "封面",
|
|
||||||
Content: fmt.Sprintf(`<img src="../Images/cover%s" />`, path.Ext(volume.Cover)),
|
|
||||||
}).Render(context.Background(), file)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to render cover: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
contentsXHTMLPath := filepath.Join(outputPath, "OEBPS/Text/contents.xhtml")
|
|
||||||
err = os.MkdirAll(path.Dir(contentsXHTMLPath), 0755)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create contents directory: %v", err)
|
|
||||||
}
|
|
||||||
file, err = os.Create(contentsXHTMLPath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create contents file: %v", err)
|
|
||||||
}
|
|
||||||
contents := strings.Builder{}
|
|
||||||
contents.WriteString(`<nav epub:type="toc" id="toc">`)
|
|
||||||
contents.WriteString(`<ol>`)
|
|
||||||
for _, chapter := range volume.Chapters {
|
|
||||||
contents.WriteString(fmt.Sprintf(`<li><a href="%s">%s</a></li>`, strings.TrimPrefix(chapter.TextOEBPSPath, "Text/"), chapter.Title))
|
|
||||||
}
|
|
||||||
contents.WriteString(`</ol>`)
|
|
||||||
contents.WriteString(`</nav>`)
|
|
||||||
err = template.ContentXHTML(&model.Chapter{
|
|
||||||
Title: "目录",
|
|
||||||
Content: contents.String(),
|
|
||||||
}).Render(context.Background(), file)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to render contents: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = CreateContainerXML(outputPath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create container xml: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
u, err := uuid.NewV7()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to generate uuid: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = CreateContentOPF(outputPath, u.String(), volume)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create content opf: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = CreateTocNCX(outputPath, u.String(), volume)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create toc ncx: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = utils.CreateEpub(outputPath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create epub: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func DownloadChapter(chapterIdx int, chapter *model.Chapter, outputPath string) error {
|
|
||||||
chapter.TextFullPath = filepath.Join(outputPath, fmt.Sprintf("OEBPS/Text/chapter-%03v.xhtml", chapterIdx+1))
|
|
||||||
chapter.TextOEBPSPath = fmt.Sprintf("Text/chapter-%03v.xhtml", chapterIdx+1)
|
|
||||||
err := os.MkdirAll(path.Dir(chapter.TextFullPath), 0755)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create text directory: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
page := 1
|
|
||||||
for {
|
|
||||||
hasNext, err := downloadChapterByPage(page, chapterIdx, chapter, outputPath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to download chapter: %v", err)
|
|
||||||
}
|
|
||||||
if !hasNext {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
page++
|
|
||||||
time.Sleep(time.Second)
|
|
||||||
}
|
|
||||||
|
|
||||||
file, err := os.Create(chapter.TextFullPath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create text file: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = template.ContentXHTML(chapter).Render(context.Background(), file)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to render text file: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func downloadChapterByPage(page, chapterIdx int, chapter *model.Chapter, outputPath string) (bool, error) {
|
|
||||||
Url := strings.TrimSuffix(chapter.Url, ".html") + fmt.Sprintf("_%v.html", page)
|
|
||||||
log.Printf("Downloading Chapter: %s", Url)
|
|
||||||
|
|
||||||
hasNext := false
|
|
||||||
headers := map[string]string{
|
|
||||||
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
|
|
||||||
"Accept-Language": "zh-CN,zh;q=0.9,en-GB;q=0.8,en;q=0.7,zh-TW;q=0.6",
|
|
||||||
"Cookie": "night=1;",
|
|
||||||
}
|
|
||||||
resp, err := utils.Request().SetHeaders(headers).Get(Url)
|
|
||||||
if err != nil {
|
|
||||||
return hasNext, err
|
|
||||||
}
|
|
||||||
if resp.StatusCode() != http.StatusOK {
|
|
||||||
return hasNext, fmt.Errorf("failed to get chapter: %v", resp.Status())
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.Contains(resp.String(), `<a onclick="window.location.href = ReadParams.url_next;">下一頁</a>`) {
|
|
||||||
hasNext = true
|
|
||||||
}
|
|
||||||
|
|
||||||
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(resp.Body()))
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return hasNext, err
|
|
||||||
}
|
|
||||||
|
|
||||||
imgSavePath := fmt.Sprintf("OEBPS/Images/chapter-%03v", chapterIdx+1)
|
|
||||||
|
|
||||||
content := doc.Find("#acontent").First()
|
|
||||||
content.Find(".cgo").Remove()
|
|
||||||
content.Find("center").Remove()
|
|
||||||
content.Find(".google-auto-placed").Remove()
|
|
||||||
|
|
||||||
content.Find("img").Each(func(i int, s *goquery.Selection) {
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
imgUrl := s.AttrOr("data-src", "")
|
|
||||||
if imgUrl == "" {
|
|
||||||
imgUrl = s.AttrOr("src", "")
|
|
||||||
if imgUrl == "" {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fileName := filepath.Join(imgSavePath, fmt.Sprintf("%03v%s", i+1, path.Ext(imgUrl)))
|
|
||||||
err = DownloadImg(imgUrl, filepath.Join(outputPath, fileName))
|
|
||||||
if err == nil {
|
|
||||||
s.SetAttr("src", "../"+strings.TrimPrefix(fileName, "OEBPS/"))
|
|
||||||
s.RemoveAttr("class")
|
|
||||||
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.ImageOEBPSPaths = append(chapter.ImageOEBPSPaths, strings.TrimPrefix(fileName, "OEBPS/"))
|
|
||||||
}
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return false, fmt.Errorf("failed to download img: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
html, err := content.Html()
|
|
||||||
if err != nil {
|
|
||||||
return false, fmt.Errorf("failed to get html: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
chapter.Content += strings.TrimSpace(html)
|
|
||||||
|
|
||||||
return hasNext, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func DownloadImg(url string, fileName string) error {
|
|
||||||
_, err := os.Stat(fileName)
|
|
||||||
if !os.IsNotExist(err) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Printf("Downloading Image: %s", url)
|
|
||||||
dir := filepath.Dir(fileName)
|
|
||||||
err = os.MkdirAll(dir, 0755)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
resp, err := utils.Request().SetHeader("Referer", "https://www.bilinovel.com").Get(url)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = os.WriteFile(fileName, resp.Body(), 0644)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func CreateContainerXML(dirPath string) error {
|
|
||||||
containerPath := filepath.Join(dirPath, "META-INF/container.xml")
|
|
||||||
err := os.MkdirAll(path.Dir(containerPath), 0755)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create container directory: %v", err)
|
|
||||||
}
|
|
||||||
file, err := os.Create(containerPath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create container file: %v", err)
|
|
||||||
}
|
|
||||||
err = template.ContainerXML().Render(context.Background(), file)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to render container: %v", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func CreateContentOPF(dirPath string, uuid string, volume *model.Volume) error {
|
|
||||||
creators := make([]model.DCCreator, 0)
|
|
||||||
for _, author := range volume.Authors {
|
|
||||||
creators = append(creators, model.DCCreator{
|
|
||||||
Value: author,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
dc := &model.DublinCoreMetadata{
|
|
||||||
Titles: []model.DCTitle{
|
|
||||||
{
|
|
||||||
Value: volume.Title,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Identifiers: []model.DCIdentifier{
|
|
||||||
{
|
|
||||||
Value: fmt.Sprintf("urn:uuid:%s", uuid),
|
|
||||||
ID: "book-id",
|
|
||||||
// Scheme: "UUID",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Languages: []model.DCLanguage{
|
|
||||||
{
|
|
||||||
Value: "zh-TW",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Descriptions: []model.DCDescription{
|
|
||||||
{
|
|
||||||
Value: volume.Description,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
Creators: creators,
|
|
||||||
Metas: []model.DublinCoreMeta{
|
|
||||||
{
|
|
||||||
Name: "cover",
|
|
||||||
Content: fmt.Sprintf("Images/cover%s", path.Ext(volume.Cover)),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Property: "dcterms:modified",
|
|
||||||
Value: time.Now().UTC().Format("2006-01-02T15:04:05Z"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
manifest := &model.Manifest{
|
|
||||||
Items: make([]model.ManifestItem, 0),
|
|
||||||
}
|
|
||||||
manifest.Items = append(manifest.Items, model.ManifestItem{
|
|
||||||
ID: "ncx",
|
|
||||||
Link: "toc.ncx",
|
|
||||||
Media: "application/x-dtbncx+xml",
|
|
||||||
})
|
|
||||||
manifest.Items = append(manifest.Items, model.ManifestItem{
|
|
||||||
ID: "cover",
|
|
||||||
Link: "Text/cover.xhtml",
|
|
||||||
Media: "application/xhtml+xml",
|
|
||||||
})
|
|
||||||
manifest.Items = append(manifest.Items, model.ManifestItem{
|
|
||||||
ID: "contents",
|
|
||||||
Link: "Text/contents.xhtml",
|
|
||||||
Media: "application/xhtml+xml",
|
|
||||||
Properties: "nav",
|
|
||||||
})
|
|
||||||
manifest.Items = append(manifest.Items, model.ManifestItem{
|
|
||||||
ID: "images-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")),
|
|
||||||
})
|
|
||||||
for _, chapter := range volume.Chapters {
|
|
||||||
manifest.Items = append(manifest.Items, model.ManifestItem{
|
|
||||||
ID: path.Base(chapter.TextOEBPSPath),
|
|
||||||
Link: chapter.TextOEBPSPath,
|
|
||||||
Media: "application/xhtml+xml",
|
|
||||||
})
|
|
||||||
for _, image := range chapter.ImageOEBPSPaths {
|
|
||||||
item := model.ManifestItem{
|
|
||||||
ID: strings.Join(strings.Split(strings.ToLower(image), string(filepath.Separator)), "-"),
|
|
||||||
Link: image,
|
|
||||||
}
|
|
||||||
item.Media = fmt.Sprintf("image/%s", strings.ReplaceAll(strings.TrimPrefix(path.Ext(volume.Cover), "."), "jpg", "jpeg"))
|
|
||||||
manifest.Items = append(manifest.Items, item)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
manifest.Items = append(manifest.Items, model.ManifestItem{
|
|
||||||
ID: "style",
|
|
||||||
Link: "Styles/style.css",
|
|
||||||
Media: "text/css",
|
|
||||||
})
|
|
||||||
|
|
||||||
spine := &model.Spine{
|
|
||||||
Items: make([]model.SpineItem, 0),
|
|
||||||
}
|
|
||||||
for _, item := range manifest.Items {
|
|
||||||
if filepath.Ext(item.Link) == ".xhtml" {
|
|
||||||
spine.Items = append(spine.Items, model.SpineItem{
|
|
||||||
IDref: item.ID,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
contentOPFPath := filepath.Join(dirPath, "OEBPS/content.opf")
|
|
||||||
err := os.MkdirAll(path.Dir(contentOPFPath), 0755)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create content directory: %v", err)
|
|
||||||
}
|
|
||||||
file, err := os.Create(contentOPFPath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create content file: %v", err)
|
|
||||||
}
|
|
||||||
err = template.ContentOPF("book-id", dc, manifest, spine, nil).Render(context.Background(), file)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to render content: %v", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func CreateTocNCX(dirPath string, uuid string, volume *model.Volume) error {
|
|
||||||
navMap := &model.NavMap{Points: make([]*model.NavPoint, 0)}
|
|
||||||
navMap.Points = append(navMap.Points, &model.NavPoint{
|
|
||||||
Id: "cover",
|
|
||||||
PlayOrder: 1,
|
|
||||||
Label: "封面",
|
|
||||||
Content: model.NavPointContent{Src: "Text/cover.xhtml"},
|
|
||||||
})
|
|
||||||
navMap.Points = append(navMap.Points, &model.NavPoint{
|
|
||||||
Id: "contents",
|
|
||||||
PlayOrder: 2,
|
|
||||||
Label: "目录",
|
|
||||||
Content: model.NavPointContent{Src: "Text/contents.xhtml"},
|
|
||||||
})
|
|
||||||
for idx, chapter := range volume.Chapters {
|
|
||||||
navMap.Points = append(navMap.Points, &model.NavPoint{
|
|
||||||
Id: fmt.Sprintf("chapter-%03v", idx+1),
|
|
||||||
PlayOrder: len(navMap.Points) + 1,
|
|
||||||
Label: chapter.Title,
|
|
||||||
Content: model.NavPointContent{Src: chapter.TextOEBPSPath},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
head := &model.TocNCXHead{
|
|
||||||
Meta: []model.TocNCXHeadMeta{
|
|
||||||
{Name: "dtb:uid", Content: fmt.Sprintf("urn:uuid:%s", uuid)},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
ncxPath := filepath.Join(dirPath, "OEBPS/toc.ncx")
|
|
||||||
err := os.MkdirAll(path.Dir(ncxPath), 0755)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create toc directory: %v", err)
|
|
||||||
}
|
|
||||||
file, err := os.Create(ncxPath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to create toc file: %v", err)
|
|
||||||
}
|
|
||||||
err = template.TocNCX(volume.Title, head, navMap).Render(context.Background(), file)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to render toc: %v", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
BIN
downloader/bilinovel/MI LANTING.ttf
Normal file
BIN
downloader/bilinovel/MI LANTING.ttf
Normal file
Binary file not shown.
557
downloader/bilinovel/bilinovel.go
Normal file
557
downloader/bilinovel/bilinovel.go
Normal file
@@ -0,0 +1,557 @@
|
|||||||
|
package bilinovel
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bilinovel-downloader/model"
|
||||||
|
"bilinovel-downloader/utils"
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"crypto/sha256"
|
||||||
|
_ "embed"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/PuerkitoBio/goquery"
|
||||||
|
mapper "github.com/bestnite/font-mapper"
|
||||||
|
"github.com/chromedp/cdproto/network"
|
||||||
|
"github.com/chromedp/chromedp"
|
||||||
|
)
|
||||||
|
|
||||||
|
//go:embed read.ttf
|
||||||
|
var readTTF []byte
|
||||||
|
|
||||||
|
//go:embed "MI LANTING.ttf"
|
||||||
|
var miLantingTTF []byte
|
||||||
|
|
||||||
|
type Bilinovel struct {
|
||||||
|
fontMapper *mapper.GlyphOutlineMapper
|
||||||
|
textOnly bool
|
||||||
|
restyClient *utils.RestyClient
|
||||||
|
|
||||||
|
// 浏览器实例复用
|
||||||
|
allocCtx context.Context
|
||||||
|
allocCancel context.CancelFunc
|
||||||
|
browserCtx context.Context
|
||||||
|
browserCancel context.CancelFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
func New() (*Bilinovel, error) {
|
||||||
|
fontMapper, err := mapper.NewGlyphOutlineMapper(readTTF, miLantingTTF)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to create font mapper: %v", err)
|
||||||
|
}
|
||||||
|
restyClient := utils.NewRestyClient(50)
|
||||||
|
|
||||||
|
b := &Bilinovel{
|
||||||
|
fontMapper: fontMapper,
|
||||||
|
textOnly: false,
|
||||||
|
restyClient: restyClient,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 初始化浏览器实例
|
||||||
|
err = b.initBrowser()
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to init browser: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return b, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bilinovel) SetTextOnly(textOnly bool) {
|
||||||
|
b.textOnly = textOnly
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bilinovel) GetExtraFiles() []model.ExtraFile {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// initBrowser 初始化浏览器实例
|
||||||
|
func (b *Bilinovel) initBrowser() error {
|
||||||
|
// 创建chromedp选项
|
||||||
|
opts := append(chromedp.DefaultExecAllocatorOptions[:],
|
||||||
|
chromedp.Flag("headless", true),
|
||||||
|
chromedp.Flag("disable-gpu", true),
|
||||||
|
chromedp.Flag("disable-dev-shm-usage", true),
|
||||||
|
chromedp.Flag("disable-extensions", true),
|
||||||
|
chromedp.Flag("no-sandbox", true),
|
||||||
|
chromedp.Flag("disable-background-timer-throttling", true),
|
||||||
|
chromedp.Flag("disable-backgrounding-occluded-windows", true),
|
||||||
|
chromedp.Flag("disable-renderer-backgrounding", true),
|
||||||
|
)
|
||||||
|
|
||||||
|
var err error
|
||||||
|
b.allocCtx, b.allocCancel = chromedp.NewExecAllocator(context.Background(), opts...)
|
||||||
|
b.browserCtx, b.browserCancel = chromedp.NewContext(b.allocCtx)
|
||||||
|
|
||||||
|
// 预热浏览器 - 导航到空白页
|
||||||
|
err = chromedp.Run(b.browserCtx, chromedp.Navigate("about:blank"))
|
||||||
|
if err != nil {
|
||||||
|
b.closeBrowser()
|
||||||
|
return fmt.Errorf("failed to initialize browser: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Println("Browser initialized successfully")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// closeBrowser 关闭浏览器实例
|
||||||
|
func (b *Bilinovel) closeBrowser() {
|
||||||
|
if b.browserCancel != nil {
|
||||||
|
b.browserCancel()
|
||||||
|
}
|
||||||
|
if b.allocCancel != nil {
|
||||||
|
b.allocCancel()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close 关闭下载器时清理资源
|
||||||
|
func (b *Bilinovel) Close() error {
|
||||||
|
b.closeBrowser()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
//go:embed style.css
|
||||||
|
var styleCSS []byte
|
||||||
|
|
||||||
|
func (b *Bilinovel) GetStyleCSS() string {
|
||||||
|
return string(styleCSS)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bilinovel) GetNovel(novelId int, skipChapter bool) (*model.Novel, error) {
|
||||||
|
log.Printf("Getting novel %v\n", novelId)
|
||||||
|
|
||||||
|
novelUrl := fmt.Sprintf("https://www.bilinovel.com/novel/%v.html", novelId)
|
||||||
|
resp, err := b.restyClient.R().Get(novelUrl)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get novel info: %w", err)
|
||||||
|
}
|
||||||
|
if resp.StatusCode() != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("failed to get novel info: %v", resp.Status())
|
||||||
|
}
|
||||||
|
|
||||||
|
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(resp.Body()))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse html: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
novel := &model.Novel{}
|
||||||
|
|
||||||
|
novel.Title = strings.TrimSpace(doc.Find(".book-title").First().Text())
|
||||||
|
novel.Description = strings.TrimSpace(doc.Find(".book-summary>content").First().Text())
|
||||||
|
novel.Id = novelId
|
||||||
|
|
||||||
|
doc.Find(".authorname>a").Each(func(i int, s *goquery.Selection) {
|
||||||
|
novel.Authors = append(novel.Authors, strings.TrimSpace(s.Text()))
|
||||||
|
})
|
||||||
|
doc.Find(".illname>a").Each(func(i int, s *goquery.Selection) {
|
||||||
|
novel.Authors = append(novel.Authors, strings.TrimSpace(s.Text()))
|
||||||
|
})
|
||||||
|
|
||||||
|
volumes, err := b.getAllVolumes(novelId, skipChapter)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get novel volumes: %v", err)
|
||||||
|
}
|
||||||
|
novel.Volumes = volumes
|
||||||
|
|
||||||
|
return novel, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bilinovel) GetVolume(novelId int, volumeId int, skipChapter bool) (*model.Volume, error) {
|
||||||
|
log.Printf("Getting volume %v of novel %v\n", volumeId, novelId)
|
||||||
|
|
||||||
|
novelUrl := fmt.Sprintf("https://www.bilinovel.com/novel/%v/catalog", novelId)
|
||||||
|
resp, err := b.restyClient.R().Get(novelUrl)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get novel info: %w", err)
|
||||||
|
}
|
||||||
|
if resp.StatusCode() != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("failed to get novel info: %v", resp.Status())
|
||||||
|
}
|
||||||
|
|
||||||
|
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(resp.Body()))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse html: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
seriesIdx := 0
|
||||||
|
doc.Find("a.volume-cover-img").Each(func(i int, s *goquery.Selection) {
|
||||||
|
if s.AttrOr("href", "") == fmt.Sprintf("/novel/%v/vol_%v.html", novelId, volumeId) {
|
||||||
|
seriesIdx = i + 1
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
novelTitle := strings.TrimSpace(doc.Find(".book-title").First().Text())
|
||||||
|
|
||||||
|
if seriesIdx == 0 {
|
||||||
|
return nil, fmt.Errorf("volume not found: %v", volumeId)
|
||||||
|
}
|
||||||
|
|
||||||
|
volumeUrl := fmt.Sprintf("https://www.bilinovel.com/novel/%v/vol_%v.html", novelId, volumeId)
|
||||||
|
resp, err = b.restyClient.R().Get(volumeUrl)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get novel info: %v", err)
|
||||||
|
}
|
||||||
|
if resp.StatusCode() != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("failed to get novel info: %v", resp.Status())
|
||||||
|
}
|
||||||
|
|
||||||
|
doc, err = goquery.NewDocumentFromReader(bytes.NewReader(resp.Body()))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse html: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
volume := &model.Volume{}
|
||||||
|
volume.NovelId = novelId
|
||||||
|
volume.NovelTitle = novelTitle
|
||||||
|
volume.Id = volumeId
|
||||||
|
volume.SeriesIdx = seriesIdx
|
||||||
|
volume.Title = strings.TrimSpace(doc.Find(".book-title").First().Text())
|
||||||
|
volume.Description = strings.TrimSpace(doc.Find(".book-summary>content").First().Text())
|
||||||
|
volume.Url = volumeUrl
|
||||||
|
volume.Chapters = make([]*model.Chapter, 0)
|
||||||
|
volume.CoverUrl = doc.Find(".book-cover").First().AttrOr("src", "")
|
||||||
|
cover, err := b.getImg(volume.CoverUrl)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get cover: %v", err)
|
||||||
|
}
|
||||||
|
volume.Cover = cover
|
||||||
|
|
||||||
|
doc.Find(".authorname>a").Each(func(i int, s *goquery.Selection) {
|
||||||
|
volume.Authors = append(volume.Authors, strings.TrimSpace(s.Text()))
|
||||||
|
})
|
||||||
|
doc.Find(".illname>a").Each(func(i int, s *goquery.Selection) {
|
||||||
|
volume.Authors = append(volume.Authors, strings.TrimSpace(s.Text()))
|
||||||
|
})
|
||||||
|
doc.Find(".chapter-li.jsChapter").Each(func(i int, s *goquery.Selection) {
|
||||||
|
volume.Chapters = append(volume.Chapters, &model.Chapter{
|
||||||
|
Title: s.Find("a").Text(),
|
||||||
|
Url: fmt.Sprintf("https://www.bilinovel.com%v", s.Find("a").AttrOr("href", "")),
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
idRegexp := regexp.MustCompile(`/novel/(\d+)/(\d+).html`)
|
||||||
|
|
||||||
|
if !skipChapter {
|
||||||
|
for i := range volume.Chapters {
|
||||||
|
matches := idRegexp.FindStringSubmatch(volume.Chapters[i].Url)
|
||||||
|
if len(matches) > 0 {
|
||||||
|
chapterId, err := strconv.Atoi(matches[2])
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to convert chapter id: %v", err)
|
||||||
|
}
|
||||||
|
chapter, err := b.GetChapter(novelId, volumeId, chapterId)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get chapter: %v", err)
|
||||||
|
}
|
||||||
|
chapter.Id = chapterId
|
||||||
|
volume.Chapters[i] = chapter
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("failed to get chapter id: %v", volume.Chapters[i].Url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return volume, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bilinovel) getAllVolumes(novelId int, skipChapter bool) ([]*model.Volume, error) {
|
||||||
|
log.Printf("Getting all volumes of novel %v\n", novelId)
|
||||||
|
|
||||||
|
catelogUrl := fmt.Sprintf("https://www.bilinovel.com/novel/%v/catalog", novelId)
|
||||||
|
resp, err := b.restyClient.R().Get(catelogUrl)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get catelog: %v", err)
|
||||||
|
}
|
||||||
|
if resp.StatusCode() != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("failed to get catelog: %v", resp.Status())
|
||||||
|
}
|
||||||
|
|
||||||
|
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(resp.Body()))
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to parse html: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
volumeRegexp := regexp.MustCompile(fmt.Sprintf(`/novel/%v/vol_(\d+).html`, novelId))
|
||||||
|
|
||||||
|
volumeIds := make([]string, 0)
|
||||||
|
doc.Find("a.volume-cover-img").Each(func(i int, s *goquery.Selection) {
|
||||||
|
link := s.AttrOr("href", "")
|
||||||
|
matches := volumeRegexp.FindStringSubmatch(link)
|
||||||
|
if len(matches) > 0 {
|
||||||
|
volumeIds = append(volumeIds, matches[1])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
volumes := make([]*model.Volume, 0)
|
||||||
|
for i, volumeIdStr := range volumeIds {
|
||||||
|
volumeId, err := strconv.Atoi(volumeIdStr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to convert volume id: %v", err)
|
||||||
|
}
|
||||||
|
volume, err := b.GetVolume(novelId, volumeId, skipChapter)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to get volume info: %v", err)
|
||||||
|
}
|
||||||
|
volume.SeriesIdx = i
|
||||||
|
volumes = append(volumes, volume)
|
||||||
|
}
|
||||||
|
|
||||||
|
return volumes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bilinovel) GetChapter(novelId int, volumeId int, chapterId int) (*model.Chapter, error) {
|
||||||
|
log.Printf("Getting chapter %v of novel %v\n", chapterId, novelId)
|
||||||
|
|
||||||
|
page := 1
|
||||||
|
chapter := &model.Chapter{
|
||||||
|
Id: chapterId,
|
||||||
|
NovelId: novelId,
|
||||||
|
VolumeId: volumeId,
|
||||||
|
Url: fmt.Sprintf("https://www.bilinovel.com/novel/%v/%v.html", novelId, chapterId),
|
||||||
|
}
|
||||||
|
for {
|
||||||
|
hasNext, err := b.getChapterByPage(chapter, page)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to download chapter: %w", err)
|
||||||
|
}
|
||||||
|
if !hasNext {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
page++
|
||||||
|
}
|
||||||
|
return chapter, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bilinovel) getChapterByPage(chapter *model.Chapter, page int) (bool, error) {
|
||||||
|
log.Printf("Getting chapter %v by page %v\n", chapter.Id, page)
|
||||||
|
|
||||||
|
Url := strings.TrimSuffix(chapter.Url, ".html") + fmt.Sprintf("_%v.html", page)
|
||||||
|
|
||||||
|
hasNext := false
|
||||||
|
headers := map[string]string{
|
||||||
|
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
|
||||||
|
"Accept-Language": "zh-CN,zh;q=0.9,en-GB;q=0.8,en;q=0.7,zh-TW;q=0.6",
|
||||||
|
"Cookie": "night=1;",
|
||||||
|
}
|
||||||
|
resp, err := b.restyClient.R().SetHeaders(headers).Get(Url)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("failed to get chapter: %w", err)
|
||||||
|
}
|
||||||
|
if resp.StatusCode() != http.StatusOK {
|
||||||
|
return false, fmt.Errorf("failed to get chapter: %v", resp.Status())
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.Contains(resp.String(), `<a onclick="window.location.href = ReadParams.url_next;">下一頁</a>`) {
|
||||||
|
hasNext = true
|
||||||
|
}
|
||||||
|
|
||||||
|
html := resp.Body()
|
||||||
|
// 解决乱序问题
|
||||||
|
resortedHtml, err := b.processContentWithChromedp(string(html))
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("failed to process html: %w", err)
|
||||||
|
}
|
||||||
|
doc, err := goquery.NewDocumentFromReader(strings.NewReader(resortedHtml))
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("failed to parse html: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if page == 1 {
|
||||||
|
chapter.Title = doc.Find("#atitle").Text()
|
||||||
|
}
|
||||||
|
content := doc.Find("#acontent").First()
|
||||||
|
content.Find(".cgo").Remove()
|
||||||
|
content.Find("center").Remove()
|
||||||
|
content.Find(".google-auto-placed").Remove()
|
||||||
|
|
||||||
|
if strings.Contains(resp.String(), `font-family: "read"`) {
|
||||||
|
html, err := content.Find("p").Last().Html()
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("failed to get html: %v", err)
|
||||||
|
}
|
||||||
|
builder := strings.Builder{}
|
||||||
|
for _, r := range html {
|
||||||
|
_, newRune, ok := b.fontMapper.MappingRune(r)
|
||||||
|
if ok {
|
||||||
|
builder.WriteRune(newRune)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
content.Find("p").Last().SetHtml(builder.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
if b.textOnly {
|
||||||
|
content.Find("img").Remove()
|
||||||
|
} else {
|
||||||
|
content.Find("img").Each(func(i int, s *goquery.Selection) {
|
||||||
|
imgUrl := s.AttrOr("data-src", "")
|
||||||
|
if imgUrl == "" {
|
||||||
|
imgUrl = s.AttrOr("src", "")
|
||||||
|
if imgUrl == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
imageHash := sha256.Sum256([]byte(imgUrl))
|
||||||
|
imageFilename := fmt.Sprintf("%x%s", string(imageHash[:]), path.Ext(imgUrl))
|
||||||
|
s.SetAttr("src", imageFilename)
|
||||||
|
s.SetAttr("alt", imgUrl)
|
||||||
|
img, err := b.getImg(imgUrl)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if chapter.Content == nil {
|
||||||
|
chapter.Content = &model.ChaperContent{}
|
||||||
|
}
|
||||||
|
if chapter.Content.Images == nil {
|
||||||
|
chapter.Content.Images = make(map[string][]byte)
|
||||||
|
}
|
||||||
|
chapter.Content.Images[imageFilename] = img
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
htmlStr, err := content.Html()
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("failed to get html: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if chapter.Content == nil {
|
||||||
|
chapter.Content = &model.ChaperContent{}
|
||||||
|
}
|
||||||
|
chapter.Content.Html += strings.TrimSpace(htmlStr)
|
||||||
|
|
||||||
|
return hasNext, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bilinovel) getImg(url string) ([]byte, error) {
|
||||||
|
log.Printf("Getting img %v\n", url)
|
||||||
|
resp, err := b.restyClient.R().SetHeader("Referer", "https://www.bilinovel.com").Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp.Body(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// processContentWithChromedp 使用复用的浏览器实例处理内容
|
||||||
|
func (b *Bilinovel) processContentWithChromedp(htmlContent string) (string, error) {
|
||||||
|
tempFile, err := os.CreateTemp("", "bilinovel-temp-*.html")
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to create temp file: %w", err)
|
||||||
|
}
|
||||||
|
defer os.Remove(tempFile.Name())
|
||||||
|
|
||||||
|
_, err = tempFile.WriteString(htmlContent)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("failed to write temp file: %w", err)
|
||||||
|
}
|
||||||
|
tempFile.Close()
|
||||||
|
tempFilePath := tempFile.Name()
|
||||||
|
|
||||||
|
// 为当前任务创建子上下文
|
||||||
|
ctx, cancel := context.WithTimeout(b.browserCtx, 30*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
var processedHTML string
|
||||||
|
|
||||||
|
// 设置网络事件监听
|
||||||
|
networkEventChan := make(chan bool, 1)
|
||||||
|
var requestID string
|
||||||
|
|
||||||
|
// 执行处理任务
|
||||||
|
err = chromedp.Run(ctx,
|
||||||
|
network.Enable(),
|
||||||
|
|
||||||
|
// 设置网络事件监听器
|
||||||
|
chromedp.ActionFunc(func(ctx context.Context) error {
|
||||||
|
chromedp.ListenTarget(ctx, func(ev interface{}) {
|
||||||
|
switch ev := ev.(type) {
|
||||||
|
case *network.EventRequestWillBeSent:
|
||||||
|
if strings.Contains(ev.Request.URL, "chapterlog.js") {
|
||||||
|
requestID = ev.RequestID.String()
|
||||||
|
}
|
||||||
|
case *network.EventLoadingFinished:
|
||||||
|
if ev.RequestID.String() == requestID && requestID != "" {
|
||||||
|
select {
|
||||||
|
case networkEventChan <- true:
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return nil
|
||||||
|
}),
|
||||||
|
|
||||||
|
// 导航到本地文件
|
||||||
|
chromedp.Navigate("file://"+filepath.ToSlash(tempFilePath)),
|
||||||
|
|
||||||
|
// 等待页面加载完成
|
||||||
|
chromedp.WaitVisible(`#acontent`, chromedp.ByID),
|
||||||
|
|
||||||
|
// 等待外部脚本加载或超时
|
||||||
|
chromedp.ActionFunc(func(ctx context.Context) error {
|
||||||
|
select {
|
||||||
|
case <-networkEventChan:
|
||||||
|
log.Println("External script loaded successfully")
|
||||||
|
case <-time.After(10 * time.Second):
|
||||||
|
log.Println("Timeout waiting for external script, continuing anyway")
|
||||||
|
case <-ctx.Done():
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}),
|
||||||
|
|
||||||
|
// 遍历所有 #acontent 的子元素, 通过 window.getComputedStyle().display 检测是否是 none, 如果是 none 则从页面删除这个元素
|
||||||
|
chromedp.ActionFunc(func(ctx context.Context) error {
|
||||||
|
// 执行JavaScript来移除display:none的元素
|
||||||
|
var result string
|
||||||
|
err := chromedp.Evaluate(`
|
||||||
|
(function() {
|
||||||
|
const acontent = document.getElementById('acontent');
|
||||||
|
if (!acontent) {
|
||||||
|
return 'acontent element not found';
|
||||||
|
}
|
||||||
|
|
||||||
|
let removedCount = 0;
|
||||||
|
const elements = acontent.querySelectorAll('*');
|
||||||
|
|
||||||
|
// 从后往前遍历,避免删除元素时影响索引
|
||||||
|
for (let i = elements.length - 1; i >= 0; i--) {
|
||||||
|
const element = elements[i];
|
||||||
|
const computedStyle = window.getComputedStyle(element);
|
||||||
|
|
||||||
|
if (computedStyle.display === 'none') {
|
||||||
|
element.remove();
|
||||||
|
removedCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'Removed ' + removedCount + ' hidden elements';
|
||||||
|
})()
|
||||||
|
`, &result).Do(ctx)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Failed to remove hidden elements: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Printf("Hidden elements removal result: %s", result)
|
||||||
|
return nil
|
||||||
|
}),
|
||||||
|
|
||||||
|
// 获取页面的HTML代码
|
||||||
|
chromedp.OuterHTML("html", &processedHTML, chromedp.ByQuery),
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("chromedp execution failed: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return processedHTML, nil
|
||||||
|
}
|
||||||
BIN
downloader/bilinovel/read.ttf
Normal file
BIN
downloader/bilinovel/read.ttf
Normal file
Binary file not shown.
@@ -1,6 +1,3 @@
|
|||||||
package template
|
|
||||||
|
|
||||||
const StyleCSS = `
|
|
||||||
body > div {
|
body > div {
|
||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
padding: 20px;
|
padding: 20px;
|
||||||
@@ -40,4 +37,3 @@ img {
|
|||||||
margin-top: 1em;
|
margin-top: 1em;
|
||||||
margin-bottom: 1em;
|
margin-bottom: 1em;
|
||||||
}
|
}
|
||||||
`
|
|
||||||
365
epub/wrapper.go
Normal file
365
epub/wrapper.go
Normal file
@@ -0,0 +1,365 @@
|
|||||||
|
package epub
|
||||||
|
|
||||||
|
import (
|
||||||
|
"archive/zip"
|
||||||
|
"bilinovel-downloader/model"
|
||||||
|
"bilinovel-downloader/template"
|
||||||
|
"bilinovel-downloader/utils"
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PackVolumeToEpub(volume *model.Volume, outputPath string, styleCSS string, extraFiles []model.ExtraFile) error {
|
||||||
|
outputPath = filepath.Join(outputPath, utils.CleanDirName(volume.Title))
|
||||||
|
_, err := os.Stat(outputPath)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
err = os.MkdirAll(outputPath, 0755)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create output directory: %v", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("failed to get output directory: %v", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err = os.RemoveAll(outputPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to remove output directory: %v", err)
|
||||||
|
}
|
||||||
|
err = os.MkdirAll(outputPath, 0755)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create output directory: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将文字写入 OEBPS/Text/chapter-%03v.xhtml
|
||||||
|
// 将图片写入 OEBPS/Images/chapter-%03v/
|
||||||
|
for i, chapter := range volume.Chapters {
|
||||||
|
imageNames := make([]string, 0)
|
||||||
|
for imgName, imgData := range chapter.Content.Images {
|
||||||
|
imageNames = append(imageNames, imgName)
|
||||||
|
imgPath := filepath.Join(outputPath, fmt.Sprintf("OEBPS/Images/chapter-%03v/%s", i, imgName))
|
||||||
|
err := os.MkdirAll(filepath.Dir(imgPath), 0755)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create image directory: %v", err)
|
||||||
|
}
|
||||||
|
err = os.WriteFile(imgPath, imgData, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write image: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
chapterPath := filepath.Join(outputPath, fmt.Sprintf("OEBPS/Text/chapter-%03v.xhtml", i))
|
||||||
|
err = os.MkdirAll(filepath.Dir(chapterPath), 0755)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create chapter directory: %v", err)
|
||||||
|
}
|
||||||
|
file, err := os.Create(chapterPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create chapter file: %v", err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
text := chapter.Content.Html
|
||||||
|
for _, imgName := range imageNames {
|
||||||
|
text = strings.ReplaceAll(text, imgName, fmt.Sprintf("../Images/chapter-%03v/%s", i, imgName))
|
||||||
|
}
|
||||||
|
err = template.ContentXHTML(chapter.Title, text).Render(context.Background(), file)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write chapter: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将 Cover 写入
|
||||||
|
coverPath := filepath.Join(outputPath, fmt.Sprintf("cover%s", filepath.Ext(volume.CoverUrl)))
|
||||||
|
err = os.WriteFile(coverPath, volume.Cover, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write cover: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将 CoverXHTML 写入 OEBPS/Text/cover.xhtml
|
||||||
|
coverXHTMLPath := filepath.Join(outputPath, "OEBPS/Text/cover.xhtml")
|
||||||
|
file, err := os.Create(coverXHTMLPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create cover XHTML file: %v", err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
err = template.CoverXHTML(fmt.Sprintf("../../%s", filepath.Base(coverPath))).Render(context.Background(), file)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to render cover XHTML: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// OEBPS/Text/contents.xhtml 目录
|
||||||
|
contentsXHTMLPath := filepath.Join(outputPath, "OEBPS/Text/contents.xhtml")
|
||||||
|
file, err = os.Create(contentsXHTMLPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create contents XHTML file: %v", err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
contents := strings.Builder{}
|
||||||
|
contents.WriteString(`<nav epub:type="toc" id="toc">`)
|
||||||
|
contents.WriteString(`<ol>`)
|
||||||
|
for i, chapter := range volume.Chapters {
|
||||||
|
contents.WriteString(fmt.Sprintf(`<li><a href="chapter-%03v.xhtml">%s</a></li>`, i, chapter.Title))
|
||||||
|
}
|
||||||
|
contents.WriteString(`</ol>`)
|
||||||
|
contents.WriteString(`</nav>`)
|
||||||
|
err = template.ContentXHTML("目录", contents.String()).Render(context.Background(), file)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to render contents XHTML: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ContainerXML
|
||||||
|
containerPath := filepath.Join(outputPath, "META-INF/container.xml")
|
||||||
|
err = os.MkdirAll(filepath.Dir(containerPath), 0755)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create container directory: %v", err)
|
||||||
|
}
|
||||||
|
file, err = os.Create(containerPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create container file: %v", err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
err = template.ContainerXML().Render(context.Background(), file)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to render container: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ContentOPF
|
||||||
|
u := uuid.New()
|
||||||
|
err = CreateContentOPF(outputPath, u.String(), volume, extraFiles)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create content OPF: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 写入 CSS
|
||||||
|
cssPath := filepath.Join(outputPath, "style.css")
|
||||||
|
err = os.WriteFile(cssPath, []byte(styleCSS), 0644)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write CSS: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 写入 extraFiles
|
||||||
|
for _, file := range extraFiles {
|
||||||
|
extraFilePath := filepath.Join(outputPath, file.Path)
|
||||||
|
err = os.WriteFile(extraFilePath, file.Data, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write extra file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 打包成 epub 文件
|
||||||
|
err = PackEpub(outputPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to pack epub: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateContentOPF(outputPath string, uuid string, volume *model.Volume, extraFiles []model.ExtraFile) error {
|
||||||
|
creators := make([]model.DCCreator, 0)
|
||||||
|
for _, author := range volume.Authors {
|
||||||
|
creators = append(creators, model.DCCreator{
|
||||||
|
Value: author,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
dc := &model.DublinCoreMetadata{
|
||||||
|
Titles: []model.DCTitle{
|
||||||
|
{
|
||||||
|
Value: volume.Title,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Identifiers: []model.DCIdentifier{
|
||||||
|
{
|
||||||
|
Value: fmt.Sprintf("urn:uuid:%s", uuid),
|
||||||
|
ID: "book-id",
|
||||||
|
// Scheme: "UUID",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Languages: []model.DCLanguage{
|
||||||
|
{
|
||||||
|
Value: "zh-CN",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Descriptions: []model.DCDescription{
|
||||||
|
{
|
||||||
|
Value: volume.Description,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Creators: creators,
|
||||||
|
Metas: []model.DublinCoreMeta{
|
||||||
|
{
|
||||||
|
Name: "cover",
|
||||||
|
Content: "cover",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Property: "dcterms:modified",
|
||||||
|
Value: time.Now().UTC().Format("2006-01-02T15:04:05Z"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "calibre:series",
|
||||||
|
Content: volume.NovelTitle,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Name: "calibre:series_index",
|
||||||
|
Content: strconv.Itoa(volume.SeriesIdx),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
manifest := &model.Manifest{
|
||||||
|
Items: make([]model.ManifestItem, 0),
|
||||||
|
}
|
||||||
|
manifest.Items = append(manifest.Items, model.ManifestItem{
|
||||||
|
ID: "cover.xhtml",
|
||||||
|
Link: "OEBPS/Text/cover.xhtml",
|
||||||
|
Media: "application/xhtml+xml",
|
||||||
|
})
|
||||||
|
manifest.Items = append(manifest.Items, model.ManifestItem{
|
||||||
|
ID: "contents.xhtml",
|
||||||
|
Link: "OEBPS/Text/contents.xhtml",
|
||||||
|
Media: "application/xhtml+xml",
|
||||||
|
Properties: "nav",
|
||||||
|
})
|
||||||
|
manifest.Items = append(manifest.Items, model.ManifestItem{
|
||||||
|
ID: "cover",
|
||||||
|
Link: fmt.Sprintf("cover%s", filepath.Ext(volume.CoverUrl)),
|
||||||
|
Media: fmt.Sprintf("image/%s", strings.ReplaceAll(strings.TrimPrefix(filepath.Ext(volume.CoverUrl), "."), "jpg", "jpeg")),
|
||||||
|
Properties: "cover-image",
|
||||||
|
})
|
||||||
|
for i, chapter := range volume.Chapters {
|
||||||
|
manifest.Items = append(manifest.Items, model.ManifestItem{
|
||||||
|
ID: fmt.Sprintf("chapter-%03v.xhtml", i),
|
||||||
|
Link: fmt.Sprintf("OEBPS/Text/chapter-%03v.xhtml", i),
|
||||||
|
Media: "application/xhtml+xml",
|
||||||
|
})
|
||||||
|
for filename := range chapter.Content.Images {
|
||||||
|
item := model.ManifestItem{
|
||||||
|
ID: fmt.Sprintf("chapter-%03v-%s", i, filepath.Base(filename)),
|
||||||
|
Link: fmt.Sprintf("OEBPS/Images/chapter-%03v/%s", i, filepath.Base(filename)),
|
||||||
|
Media: fmt.Sprintf("image/%s", strings.ReplaceAll(strings.TrimPrefix(filepath.Ext(filename), "."), "jpg", "jpeg")),
|
||||||
|
}
|
||||||
|
manifest.Items = append(manifest.Items, item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
manifest.Items = append(manifest.Items, model.ManifestItem{
|
||||||
|
ID: "style",
|
||||||
|
Link: "style.css",
|
||||||
|
Media: "text/css",
|
||||||
|
})
|
||||||
|
// ExtraFiles
|
||||||
|
for _, file := range extraFiles {
|
||||||
|
manifest.Items = append(manifest.Items, file.ManifestItem)
|
||||||
|
}
|
||||||
|
|
||||||
|
spine := &model.Spine{
|
||||||
|
Items: make([]model.SpineItem, 0),
|
||||||
|
}
|
||||||
|
for _, item := range manifest.Items {
|
||||||
|
if filepath.Ext(item.Link) == ".xhtml" {
|
||||||
|
spine.Items = append(spine.Items, model.SpineItem{
|
||||||
|
IDref: item.ID,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
contentOPFPath := filepath.Join(outputPath, "content.opf")
|
||||||
|
err := os.MkdirAll(path.Dir(contentOPFPath), 0755)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create content directory: %v", err)
|
||||||
|
}
|
||||||
|
file, err := os.Create(contentOPFPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create content file: %v", err)
|
||||||
|
}
|
||||||
|
err = template.ContentOPF("book-id", dc, manifest, spine, nil).Render(context.Background(), file)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to render content: %v", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func PackEpub(dirPath string) error {
|
||||||
|
savePath := strings.TrimSuffix(dirPath, string(filepath.Separator)) + ".epub"
|
||||||
|
zipFile, err := os.Create(savePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer zipFile.Close()
|
||||||
|
|
||||||
|
zipWriter := zip.NewWriter(zipFile)
|
||||||
|
defer zipWriter.Close()
|
||||||
|
|
||||||
|
err = addStringToZip(zipWriter, "mimetype", "application/epub+zip", zip.Store)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = addDirContentToZip(zipWriter, dirPath, zip.Deflate)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func addStringToZip(zipWriter *zip.Writer, relPath, content string, method uint16) error {
|
||||||
|
header := &zip.FileHeader{
|
||||||
|
Name: relPath,
|
||||||
|
Method: method,
|
||||||
|
}
|
||||||
|
writer, err := zipWriter.CreateHeader(header)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = writer.Write([]byte(content))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func addDirContentToZip(zipWriter *zip.Writer, dirPath string, method uint16) 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 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if info.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
relPath, err := filepath.Rel(dirPath, filePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
relPath = filepath.ToSlash(relPath)
|
||||||
|
|
||||||
|
file, err := os.Open(filePath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
header, err := zip.FileInfoHeader(info)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
header.Name = relPath
|
||||||
|
header.Method = method
|
||||||
|
|
||||||
|
writer, err := zipWriter.CreateHeader(header)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = io.Copy(writer, file)
|
||||||
|
return err
|
||||||
|
})
|
||||||
|
}
|
||||||
17
go.mod
17
go.mod
@@ -4,7 +4,10 @@ go 1.24.2
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/PuerkitoBio/goquery v1.10.3
|
github.com/PuerkitoBio/goquery v1.10.3
|
||||||
github.com/a-h/templ v0.3.857
|
github.com/a-h/templ v0.3.943
|
||||||
|
github.com/bestnite/font-mapper v0.0.0-20250823155658-56c76d820267
|
||||||
|
github.com/chromedp/cdproto v0.0.0-20250803210736-d308e07a266d
|
||||||
|
github.com/chromedp/chromedp v0.14.1
|
||||||
github.com/go-resty/resty/v2 v2.16.5
|
github.com/go-resty/resty/v2 v2.16.5
|
||||||
github.com/google/uuid v1.6.0
|
github.com/google/uuid v1.6.0
|
||||||
github.com/spf13/cobra v1.9.1
|
github.com/spf13/cobra v1.9.1
|
||||||
@@ -12,7 +15,15 @@ require (
|
|||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/andybalholm/cascadia v1.3.3 // indirect
|
github.com/andybalholm/cascadia v1.3.3 // indirect
|
||||||
|
github.com/chromedp/sysutil v1.1.0 // indirect
|
||||||
|
github.com/go-json-experiment/json v0.0.0-20250813233538-9b1f9ea2e11b // indirect
|
||||||
|
github.com/gobwas/httphead v0.1.0 // indirect
|
||||||
|
github.com/gobwas/pool v0.2.1 // indirect
|
||||||
|
github.com/gobwas/ws v1.4.0 // indirect
|
||||||
|
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
github.com/spf13/pflag v1.0.6 // indirect
|
github.com/spf13/pflag v1.0.7 // indirect
|
||||||
golang.org/x/net v0.39.0 // indirect
|
golang.org/x/image v0.30.0 // indirect
|
||||||
|
golang.org/x/net v0.43.0 // indirect
|
||||||
|
golang.org/x/sys v0.35.0 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
37
go.sum
37
go.sum
@@ -1,23 +1,49 @@
|
|||||||
github.com/PuerkitoBio/goquery v1.10.3 h1:pFYcNSqHxBD06Fpj/KsbStFRsgRATgnf3LeXiUkhzPo=
|
github.com/PuerkitoBio/goquery v1.10.3 h1:pFYcNSqHxBD06Fpj/KsbStFRsgRATgnf3LeXiUkhzPo=
|
||||||
github.com/PuerkitoBio/goquery v1.10.3/go.mod h1:tMUX0zDMHXYlAQk6p35XxQMqMweEKB7iK7iLNd4RH4Y=
|
github.com/PuerkitoBio/goquery v1.10.3/go.mod h1:tMUX0zDMHXYlAQk6p35XxQMqMweEKB7iK7iLNd4RH4Y=
|
||||||
github.com/a-h/templ v0.3.857 h1:6EqcJuGZW4OL+2iZ3MD+NnIcG7nGkaQeF2Zq5kf9ZGg=
|
github.com/a-h/templ v0.3.906 h1:ZUThc8Q9n04UATaCwaG60pB1AqbulLmYEAMnWV63svg=
|
||||||
github.com/a-h/templ v0.3.857/go.mod h1:qhrhAkRFubE7khxLZHsBFHfX+gWwVNKbzKeF9GlPV4M=
|
github.com/a-h/templ v0.3.906/go.mod h1:FFAu4dI//ESmEN7PQkJ7E7QfnSEMdcnu7QrAY8Dn334=
|
||||||
|
github.com/a-h/templ v0.3.943 h1:o+mT/4yqhZ33F3ootBiHwaY4HM5EVaOJfIshvd5UNTY=
|
||||||
|
github.com/a-h/templ v0.3.943/go.mod h1:oCZcnKRf5jjsGpf2yELzQfodLphd2mwecwG4Crk5HBo=
|
||||||
github.com/andybalholm/cascadia v1.3.3 h1:AG2YHrzJIm4BZ19iwJ/DAua6Btl3IwJX+VI4kktS1LM=
|
github.com/andybalholm/cascadia v1.3.3 h1:AG2YHrzJIm4BZ19iwJ/DAua6Btl3IwJX+VI4kktS1LM=
|
||||||
github.com/andybalholm/cascadia v1.3.3/go.mod h1:xNd9bqTn98Ln4DwST8/nG+H0yuB8Hmgu1YHNnWw0GeA=
|
github.com/andybalholm/cascadia v1.3.3/go.mod h1:xNd9bqTn98Ln4DwST8/nG+H0yuB8Hmgu1YHNnWw0GeA=
|
||||||
|
github.com/bestnite/font-mapper v0.0.0-20250823155658-56c76d820267 h1:nmUTJV2u/0XmVjQ++VIy/Hu+MtxdpQvOevvcSZtUATA=
|
||||||
|
github.com/bestnite/font-mapper v0.0.0-20250823155658-56c76d820267/go.mod h1:cfB1e9YhoI/QWrXPp3h6QVAKU6iCI2ifbjRPHP3xf/0=
|
||||||
|
github.com/chromedp/cdproto v0.0.0-20250803210736-d308e07a266d h1:ZtA1sedVbEW7EW80Iz2GR3Ye6PwbJAJXjv7D74xG6HU=
|
||||||
|
github.com/chromedp/cdproto v0.0.0-20250803210736-d308e07a266d/go.mod h1:NItd7aLkcfOA/dcMXvl8p1u+lQqioRMq/SqDp71Pb/k=
|
||||||
|
github.com/chromedp/chromedp v0.14.1 h1:0uAbnxewy/Q+Bg7oafVePE/6EXEho9hnaC38f+TTENg=
|
||||||
|
github.com/chromedp/chromedp v0.14.1/go.mod h1:rHzAv60xDE7VNy/MYtTUrYreSc0ujt2O1/C3bzctYBo=
|
||||||
|
github.com/chromedp/sysutil v1.1.0 h1:PUFNv5EcprjqXZD9nJb9b/c9ibAbxiYo4exNWZyipwM=
|
||||||
|
github.com/chromedp/sysutil v1.1.0/go.mod h1:WiThHUdltqCNKGc4gaU50XgYjwjYIhKWoHGPTUfWTJ8=
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||||
|
github.com/go-json-experiment/json v0.0.0-20250813233538-9b1f9ea2e11b h1:6Q4zRHXS/YLOl9Ng1b1OOOBWMidAQZR3Gel0UKPC/KU=
|
||||||
|
github.com/go-json-experiment/json v0.0.0-20250813233538-9b1f9ea2e11b/go.mod h1:TiCD2a1pcmjd7YnhGH0f/zKNcCD06B029pHhzV23c2M=
|
||||||
github.com/go-resty/resty/v2 v2.16.5 h1:hBKqmWrr7uRc3euHVqmh1HTHcKn99Smr7o5spptdhTM=
|
github.com/go-resty/resty/v2 v2.16.5 h1:hBKqmWrr7uRc3euHVqmh1HTHcKn99Smr7o5spptdhTM=
|
||||||
github.com/go-resty/resty/v2 v2.16.5/go.mod h1:hkJtXbA2iKHzJheXYvQ8snQES5ZLGKMwQ07xAwp/fiA=
|
github.com/go-resty/resty/v2 v2.16.5/go.mod h1:hkJtXbA2iKHzJheXYvQ8snQES5ZLGKMwQ07xAwp/fiA=
|
||||||
|
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
|
||||||
|
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
|
||||||
|
github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
|
||||||
|
github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=
|
||||||
|
github.com/gobwas/ws v1.4.0 h1:CTaoG1tojrh4ucGPcoJFiAQUAsEWekEWvLy7GsVNqGs=
|
||||||
|
github.com/gobwas/ws v1.4.0/go.mod h1:G3gNqMNtPppf5XUz7O4shetPpcZ1VJ7zt18dlUeakrc=
|
||||||
|
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
|
||||||
|
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
|
||||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||||
|
github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80 h1:6Yzfa6GP0rIo/kULo2bwGEkFvCePZ3qHDDTC3/J9Swo=
|
||||||
|
github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J+dCPAknuiaGOshXAs=
|
||||||
|
github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde h1:x0TT0RDC7UhAVbbWWBzr41ElhJx5tXPWkIHA2HWPRuw=
|
||||||
|
github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde/go.mod h1:nZgzbfBr3hhjoZnS66nKrHmduYNpc34ny7RK4z5/HM0=
|
||||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
|
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
|
||||||
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
|
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
|
||||||
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
|
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
|
||||||
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
|
github.com/spf13/pflag v1.0.7 h1:vN6T9TfwStFPFM5XzjsvmzZkLuaLX+HS+0SeFLRgU6M=
|
||||||
|
github.com/spf13/pflag v1.0.7/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||||
@@ -25,6 +51,8 @@ golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliY
|
|||||||
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
|
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
|
||||||
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||||
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
|
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
|
||||||
|
golang.org/x/image v0.30.0 h1:jD5RhkmVAnjqaCUXfbGBrn3lpxbknfN9w2UhHHU+5B4=
|
||||||
|
golang.org/x/image v0.30.0/go.mod h1:SAEUTxCCMWSrJcCy/4HwavEsfZZJlYxeHLc6tTiAe/c=
|
||||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||||
@@ -41,6 +69,8 @@ golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
|||||||
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
|
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
|
||||||
golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY=
|
golang.org/x/net v0.39.0 h1:ZCu7HMWDxpXpaiKdhzIfaltL9Lp31x/3fCP11bc6/fY=
|
||||||
golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E=
|
golang.org/x/net v0.39.0/go.mod h1:X7NRbYVEA+ewNkCNyJ513WmMdQ3BineSwVtN2zD/d+E=
|
||||||
|
golang.org/x/net v0.43.0 h1:lat02VYK2j4aLzMzecihNvTlJNQUq316m2Mr9rnM6YE=
|
||||||
|
golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg=
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||||
@@ -54,11 +84,14 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
|
|||||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
|
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||||
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
|
||||||
|
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||||
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
|
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
|
||||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||||
|
|||||||
5
main.go
5
main.go
@@ -2,11 +2,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bilinovel-downloader/cmd"
|
"bilinovel-downloader/cmd"
|
||||||
"log"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if err := cmd.RootCmd.Execute(); err != nil {
|
_ = cmd.RootCmd.Execute()
|
||||||
log.Fatalf("Error executing command: %v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
16
model/downloader.go
Normal file
16
model/downloader.go
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
type ExtraFile struct {
|
||||||
|
Data []byte
|
||||||
|
Path string
|
||||||
|
ManifestItem ManifestItem
|
||||||
|
}
|
||||||
|
|
||||||
|
type Downloader interface {
|
||||||
|
GetNovel(novelId int, skipChapter bool) (*Novel, error)
|
||||||
|
GetVolume(novelId int, volumeId int, skipChapter bool) (*Volume, error)
|
||||||
|
GetChapter(novelId int, volumeId int, chapterId int) (*Chapter, error)
|
||||||
|
GetStyleCSS() string
|
||||||
|
GetExtraFiles() []ExtraFile
|
||||||
|
Close() error
|
||||||
|
}
|
||||||
@@ -158,7 +158,6 @@ type Spine struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *Spine) Marshal() (string, error) {
|
func (s *Spine) Marshal() (string, error) {
|
||||||
s.Toc = "ncx"
|
|
||||||
xmlBytes, err := xml.Marshal(s)
|
xmlBytes, err := xml.Marshal(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
37
model/novel.go
Normal file
37
model/novel.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
type ChaperContent struct {
|
||||||
|
Html string
|
||||||
|
Images map[string][]byte
|
||||||
|
}
|
||||||
|
|
||||||
|
type Chapter struct {
|
||||||
|
Id int
|
||||||
|
NovelId int
|
||||||
|
VolumeId int
|
||||||
|
Title string
|
||||||
|
Url string
|
||||||
|
Content *ChaperContent
|
||||||
|
}
|
||||||
|
|
||||||
|
type Volume struct {
|
||||||
|
Id int
|
||||||
|
SeriesIdx int
|
||||||
|
Title string
|
||||||
|
Url string
|
||||||
|
CoverUrl string
|
||||||
|
Cover []byte
|
||||||
|
Description string
|
||||||
|
Authors []string
|
||||||
|
Chapters []*Chapter
|
||||||
|
NovelId int
|
||||||
|
NovelTitle string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Novel struct {
|
||||||
|
Id int
|
||||||
|
Title string
|
||||||
|
Description string
|
||||||
|
Authors []string
|
||||||
|
Volumes []*Volume
|
||||||
|
}
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
package model
|
|
||||||
|
|
||||||
type Chapter struct {
|
|
||||||
Title string
|
|
||||||
Url string
|
|
||||||
Content string
|
|
||||||
ImageOEBPSPaths []string
|
|
||||||
ImageFullPaths []string
|
|
||||||
TextOEBPSPath string
|
|
||||||
TextFullPath string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Volume struct {
|
|
||||||
Title string
|
|
||||||
Url string
|
|
||||||
Cover string
|
|
||||||
Description string
|
|
||||||
Authors []string
|
|
||||||
Chapters []*Chapter
|
|
||||||
}
|
|
||||||
|
|
||||||
type Novel struct {
|
|
||||||
Title string
|
|
||||||
Description string
|
|
||||||
Authors []string
|
|
||||||
Volumes []*Volume
|
|
||||||
}
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
package model
|
|
||||||
|
|
||||||
import "encoding/xml"
|
|
||||||
|
|
||||||
type TocNCXHead struct {
|
|
||||||
XMLName xml.Name `xml:"head"`
|
|
||||||
Meta []TocNCXHeadMeta `xml:"meta"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type TocNCXHeadMeta struct {
|
|
||||||
XMLName xml.Name `xml:"meta"`
|
|
||||||
Content string `xml:"content,attr"`
|
|
||||||
Name string `xml:"name,attr"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (h *TocNCXHead) Marshal() (string, error) {
|
|
||||||
xmlBytes, err := xml.Marshal(h)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return string(xmlBytes), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type NavPoint struct {
|
|
||||||
Id string `xml:"id,attr"`
|
|
||||||
PlayOrder int `xml:"playOrder,attr"`
|
|
||||||
Label string `xml:"navLabel>text"`
|
|
||||||
Content NavPointContent `xml:"content"`
|
|
||||||
NavPoints []*NavPoint `xml:"navPoint"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type NavPointContent struct {
|
|
||||||
Src string `xml:"src,attr"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type NavMap struct {
|
|
||||||
XMLName xml.Name `xml:"navMap"`
|
|
||||||
Points []*NavPoint `xml:"navPoint"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (n *NavMap) Marshal() (string, error) {
|
|
||||||
xmlBytes, err := xml.Marshal(n)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return string(xmlBytes), nil
|
|
||||||
}
|
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
package template
|
package template
|
||||||
|
|
||||||
templ ContainerXML() {
|
templ ContainerXML() {
|
||||||
@templ.Raw(`<?xml version="1.0" encoding="UTF-8"?>`)
|
@templ.Raw(`<?xml version='1.0' encoding='utf-8'?>`)
|
||||||
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
|
<container xmlns="urn:oasis:names:tc:opendocument:xmlns:container" version="1.0">
|
||||||
<rootfiles>
|
<rootfiles>
|
||||||
<rootfile full-path="OEBPS/content.opf" media-type="application/oebps-package+xml"></rootfile>
|
<rootfile full-path="content.opf" media-type="application/oebps-package+xml"></rootfile>
|
||||||
</rootfiles>
|
</rootfiles>
|
||||||
</container>
|
</container>
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Code generated by templ - DO NOT EDIT.
|
// Code generated by templ - DO NOT EDIT.
|
||||||
|
|
||||||
// templ: version: v0.3.857
|
// templ: version: v0.3.943
|
||||||
package template
|
package template
|
||||||
|
|
||||||
//lint:file-ignore SA4006 This context is only used if a nested component is present.
|
//lint:file-ignore SA4006 This context is only used if a nested component is present.
|
||||||
@@ -29,11 +29,11 @@ func ContainerXML() templ.Component {
|
|||||||
templ_7745c5c3_Var1 = templ.NopComponent
|
templ_7745c5c3_Var1 = templ.NopComponent
|
||||||
}
|
}
|
||||||
ctx = templ.ClearChildren(ctx)
|
ctx = templ.ClearChildren(ctx)
|
||||||
templ_7745c5c3_Err = templ.Raw(`<?xml version="1.0" encoding="UTF-8"?>`).Render(ctx, templ_7745c5c3_Buffer)
|
templ_7745c5c3_Err = templ.Raw(`<?xml version='1.0' encoding='utf-8'?>`).Render(ctx, templ_7745c5c3_Buffer)
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<container version=\"1.0\" xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\"><rootfiles><rootfile full-path=\"OEBPS/content.opf\" media-type=\"application/oebps-package+xml\"></rootfile></rootfiles></container>")
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<container xmlns=\"urn:oasis:names:tc:opendocument:xmlns:container\" version=\"1.0\"><rootfiles><rootfile full-path=\"content.opf\" media-type=\"application/oebps-package+xml\"></rootfile></rootfiles></container>")
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package template
|
|||||||
import "bilinovel-downloader/model"
|
import "bilinovel-downloader/model"
|
||||||
|
|
||||||
templ ContentOPF(uniqueIdentifier string, dc *model.DublinCoreMetadata, manifest *model.Manifest, spine *model.Spine, guide *model.Guide) {
|
templ ContentOPF(uniqueIdentifier string, dc *model.DublinCoreMetadata, manifest *model.Manifest, spine *model.Spine, guide *model.Guide) {
|
||||||
@templ.Raw(`<?xml version="1.0" encoding="UTF-8"?>`)
|
@templ.Raw(`<?xml version='1.0' encoding='utf-8'?>`)
|
||||||
<package version="3.0" xmlns="http://www.idpf.org/2007/opf" xmlns:dc="http://purl.org/dc/elements/1.1/" unique-identifier={ uniqueIdentifier }>
|
<package version="3.0" xmlns="http://www.idpf.org/2007/opf" xmlns:dc="http://purl.org/dc/elements/1.1/" unique-identifier={ uniqueIdentifier }>
|
||||||
if dc != nil {
|
if dc != nil {
|
||||||
{{ metadata, err := dc.Marshal() }}
|
{{ metadata, err := dc.Marshal() }}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Code generated by templ - DO NOT EDIT.
|
// Code generated by templ - DO NOT EDIT.
|
||||||
|
|
||||||
// templ: version: v0.3.857
|
// templ: version: v0.3.943
|
||||||
package template
|
package template
|
||||||
|
|
||||||
//lint:file-ignore SA4006 This context is only used if a nested component is present.
|
//lint:file-ignore SA4006 This context is only used if a nested component is present.
|
||||||
@@ -31,7 +31,7 @@ func ContentOPF(uniqueIdentifier string, dc *model.DublinCoreMetadata, manifest
|
|||||||
templ_7745c5c3_Var1 = templ.NopComponent
|
templ_7745c5c3_Var1 = templ.NopComponent
|
||||||
}
|
}
|
||||||
ctx = templ.ClearChildren(ctx)
|
ctx = templ.ClearChildren(ctx)
|
||||||
templ_7745c5c3_Err = templ.Raw(`<?xml version="1.0" encoding="UTF-8"?>`).Render(ctx, templ_7745c5c3_Buffer)
|
templ_7745c5c3_Err = templ.Raw(`<?xml version='1.0' encoding='utf-8'?>`).Render(ctx, templ_7745c5c3_Buffer)
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +1,19 @@
|
|||||||
package template
|
package template
|
||||||
|
|
||||||
import "bilinovel-downloader/model"
|
templ ContentXHTML(title, content string) {
|
||||||
|
@templ.Raw(`<?xml version='1.0' encoding='utf-8'?>`)
|
||||||
templ ContentXHTML(content *model.Chapter) {
|
// @templ.Raw(`<!DOCTYPE html>`)
|
||||||
@templ.Raw(`<?xml version="1.0" encoding="utf-8" standalone="no"?>`)
|
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="zh-CN">
|
||||||
@templ.Raw(`<!DOCTYPE html>`)
|
|
||||||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" xmlns:epub="http://www.idpf.org/2007/ops" xmlns:xml="http://www.w3.org/XML/1998/namespace">
|
|
||||||
<head>
|
<head>
|
||||||
<title>{ content.Title }</title>
|
<title>{ title }</title>
|
||||||
@templ.Raw(`<link href="../Styles/style.css" rel="stylesheet" type="text/css"/>`)
|
@templ.Raw(`<link href="../../style.css" rel="stylesheet" type="text/css"/>`)
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div class="chapter">
|
<div class="chapter">
|
||||||
<h1>{ content.Title }</h1>
|
<h1>{ title }</h1>
|
||||||
@templ.Raw(`<hr/>`)
|
@templ.Raw(`<hr/>`)
|
||||||
<div class="content">
|
<div class="content">
|
||||||
@templ.Raw(content.Content)
|
@templ.Raw(content)
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// Code generated by templ - DO NOT EDIT.
|
// Code generated by templ - DO NOT EDIT.
|
||||||
|
|
||||||
// templ: version: v0.3.857
|
// templ: version: v0.3.943
|
||||||
package template
|
package template
|
||||||
|
|
||||||
//lint:file-ignore SA4006 This context is only used if a nested component is present.
|
//lint:file-ignore SA4006 This context is only used if a nested component is present.
|
||||||
@@ -8,9 +8,7 @@ package template
|
|||||||
import "github.com/a-h/templ"
|
import "github.com/a-h/templ"
|
||||||
import templruntime "github.com/a-h/templ/runtime"
|
import templruntime "github.com/a-h/templ/runtime"
|
||||||
|
|
||||||
import "bilinovel-downloader/model"
|
func ContentXHTML(title, content string) templ.Component {
|
||||||
|
|
||||||
func ContentXHTML(content *model.Chapter) templ.Component {
|
|
||||||
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||||
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
||||||
@@ -31,22 +29,18 @@ func ContentXHTML(content *model.Chapter) templ.Component {
|
|||||||
templ_7745c5c3_Var1 = templ.NopComponent
|
templ_7745c5c3_Var1 = templ.NopComponent
|
||||||
}
|
}
|
||||||
ctx = templ.ClearChildren(ctx)
|
ctx = templ.ClearChildren(ctx)
|
||||||
templ_7745c5c3_Err = templ.Raw(`<?xml version="1.0" encoding="utf-8" standalone="no"?>`).Render(ctx, templ_7745c5c3_Buffer)
|
templ_7745c5c3_Err = templ.Raw(`<?xml version='1.0' encoding='utf-8'?>`).Render(ctx, templ_7745c5c3_Buffer)
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
templ_7745c5c3_Err = templ.Raw(`<!DOCTYPE html>`).Render(ctx, templ_7745c5c3_Buffer)
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\" xml:lang=\"zh-CN\"><head><title>")
|
||||||
if templ_7745c5c3_Err != nil {
|
|
||||||
return templ_7745c5c3_Err
|
|
||||||
}
|
|
||||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"zh-CN\" xmlns:epub=\"http://www.idpf.org/2007/ops\" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\"><head><title>")
|
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
var templ_7745c5c3_Var2 string
|
var templ_7745c5c3_Var2 string
|
||||||
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(content.Title)
|
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(title)
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `template/content.xhtml.templ`, Line: 10, Col: 25}
|
return templ.Error{Err: templ_7745c5c3_Err, FileName: `template/content.xhtml.templ`, Line: 8, Col: 17}
|
||||||
}
|
}
|
||||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
|
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
@@ -56,7 +50,7 @@ func ContentXHTML(content *model.Chapter) templ.Component {
|
|||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
templ_7745c5c3_Err = templ.Raw(`<link href="../Styles/style.css" rel="stylesheet" type="text/css"/>`).Render(ctx, templ_7745c5c3_Buffer)
|
templ_7745c5c3_Err = templ.Raw(`<link href="../../style.css" rel="stylesheet" type="text/css"/>`).Render(ctx, templ_7745c5c3_Buffer)
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
@@ -65,9 +59,9 @@ func ContentXHTML(content *model.Chapter) templ.Component {
|
|||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
var templ_7745c5c3_Var3 string
|
var templ_7745c5c3_Var3 string
|
||||||
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(content.Title)
|
templ_7745c5c3_Var3, templ_7745c5c3_Err = templ.JoinStringErrs(title)
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `template/content.xhtml.templ`, Line: 15, Col: 23}
|
return templ.Error{Err: templ_7745c5c3_Err, FileName: `template/content.xhtml.templ`, Line: 13, Col: 15}
|
||||||
}
|
}
|
||||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
|
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var3))
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
@@ -85,7 +79,7 @@ func ContentXHTML(content *model.Chapter) templ.Component {
|
|||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
templ_7745c5c3_Err = templ.Raw(content.Content).Render(ctx, templ_7745c5c3_Buffer)
|
templ_7745c5c3_Err = templ.Raw(content).Render(ctx, templ_7745c5c3_Buffer)
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
|
|||||||
37
template/cover.xhtml.templ
Normal file
37
template/cover.xhtml.templ
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package template
|
||||||
|
|
||||||
|
templ CoverXHTML(coverPath string) {
|
||||||
|
@templ.Raw(`<?xml version='1.0' encoding='utf-8'?>`)
|
||||||
|
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops"
|
||||||
|
xml:lang="zh-CN">
|
||||||
|
<head>
|
||||||
|
<title>Cover</title>
|
||||||
|
</head>
|
||||||
|
<style type="text/css">
|
||||||
|
@page {
|
||||||
|
padding: 0pt;
|
||||||
|
margin: 0pt
|
||||||
|
}
|
||||||
|
body {
|
||||||
|
text-align: center;
|
||||||
|
padding: 0pt;
|
||||||
|
margin: 0pt;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
<body>
|
||||||
|
<div>
|
||||||
|
<svg
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
xmlns:xlink="http://www.w3.org/1999/xlink"
|
||||||
|
version="1.1"
|
||||||
|
width="100%"
|
||||||
|
height="100%"
|
||||||
|
viewBox="0 0 400 581"
|
||||||
|
preserveAspectRatio="none"
|
||||||
|
>
|
||||||
|
<image width="400" height="581" xlink:href={ coverPath }></image>
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
// Code generated by templ - DO NOT EDIT.
|
// Code generated by templ - DO NOT EDIT.
|
||||||
|
|
||||||
// templ: version: v0.3.857
|
// templ: version: v0.3.943
|
||||||
package template
|
package template
|
||||||
|
|
||||||
//lint:file-ignore SA4006 This context is only used if a nested component is present.
|
//lint:file-ignore SA4006 This context is only used if a nested component is present.
|
||||||
@@ -8,9 +8,7 @@ package template
|
|||||||
import "github.com/a-h/templ"
|
import "github.com/a-h/templ"
|
||||||
import templruntime "github.com/a-h/templ/runtime"
|
import templruntime "github.com/a-h/templ/runtime"
|
||||||
|
|
||||||
import "bilinovel-downloader/model"
|
func CoverXHTML(coverPath string) templ.Component {
|
||||||
|
|
||||||
func TocNCX(title string, head *model.TocNCXHead, navMap *model.NavMap) templ.Component {
|
|
||||||
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) {
|
||||||
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context
|
||||||
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil {
|
||||||
@@ -31,54 +29,24 @@ func TocNCX(title string, head *model.TocNCXHead, navMap *model.NavMap) templ.Co
|
|||||||
templ_7745c5c3_Var1 = templ.NopComponent
|
templ_7745c5c3_Var1 = templ.NopComponent
|
||||||
}
|
}
|
||||||
ctx = templ.ClearChildren(ctx)
|
ctx = templ.ClearChildren(ctx)
|
||||||
templ_7745c5c3_Err = templ.Raw(`<?xml version="1.0" encoding="UTF-8"?>`).Render(ctx, templ_7745c5c3_Buffer)
|
templ_7745c5c3_Err = templ.Raw(`<?xml version='1.0' encoding='utf-8'?>`).Render(ctx, templ_7745c5c3_Buffer)
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
templ_7745c5c3_Err = templ.Raw(`<!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN" "http://www.daisy.org/z3986/2005/ncx-2005-1.dtd">`).Render(ctx, templ_7745c5c3_Buffer)
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\" xml:lang=\"zh-CN\"><head><title>Cover</title></head><style type=\"text/css\">\n\t\t@page {\n\t\tpadding: 0pt;\n\t\tmargin: 0pt\n\t\t}\n\t\tbody {\n\t\ttext-align: center;\n\t\tpadding: 0pt;\n\t\tmargin: 0pt;\n\t\t}\n\t</style><body><div><svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"1.1\" width=\"100%\" height=\"100%\" viewBox=\"0 0 400 581\" preserveAspectRatio=\"none\"><image width=\"400\" height=\"581\" xlink:href=\"")
|
||||||
if templ_7745c5c3_Err != nil {
|
|
||||||
return templ_7745c5c3_Err
|
|
||||||
}
|
|
||||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 1, "<ncx xmlns=\"http://www.daisy.org/z3986/2005/ncx/\" version=\"2005-1\">")
|
|
||||||
if templ_7745c5c3_Err != nil {
|
|
||||||
return templ_7745c5c3_Err
|
|
||||||
}
|
|
||||||
if head != nil {
|
|
||||||
head, err := head.Marshal()
|
|
||||||
if err == nil {
|
|
||||||
templ_7745c5c3_Err = templ.Raw(head).Render(ctx, templ_7745c5c3_Buffer)
|
|
||||||
if templ_7745c5c3_Err != nil {
|
|
||||||
return templ_7745c5c3_Err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "<docTitle><text>")
|
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
var templ_7745c5c3_Var2 string
|
var templ_7745c5c3_Var2 string
|
||||||
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(title)
|
templ_7745c5c3_Var2, templ_7745c5c3_Err = templ.JoinStringErrs(coverPath)
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `template/toc.ncx.templ`, Line: 16, Col: 16}
|
return templ.Error{Err: templ_7745c5c3_Err, FileName: `template/cover.xhtml.templ`, Line: 32, Col: 58}
|
||||||
}
|
}
|
||||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
|
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var2))
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 3, "</text></docTitle> ")
|
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 2, "\"></image></svg></div></body></html>")
|
||||||
if templ_7745c5c3_Err != nil {
|
|
||||||
return templ_7745c5c3_Err
|
|
||||||
}
|
|
||||||
if navMap != nil {
|
|
||||||
navMap, err := navMap.Marshal()
|
|
||||||
if err == nil {
|
|
||||||
templ_7745c5c3_Err = templ.Raw(navMap).Render(ctx, templ_7745c5c3_Buffer)
|
|
||||||
if templ_7745c5c3_Err != nil {
|
|
||||||
return templ_7745c5c3_Err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 4, "</ncx>")
|
|
||||||
if templ_7745c5c3_Err != nil {
|
if templ_7745c5c3_Err != nil {
|
||||||
return templ_7745c5c3_Err
|
return templ_7745c5c3_Err
|
||||||
}
|
}
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
package template
|
|
||||||
|
|
||||||
import "bilinovel-downloader/model"
|
|
||||||
|
|
||||||
templ TocNCX(title string, head *model.TocNCXHead, navMap *model.NavMap) {
|
|
||||||
@templ.Raw(`<?xml version="1.0" encoding="UTF-8"?>`)
|
|
||||||
@templ.Raw(`<!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN" "http://www.daisy.org/z3986/2005/ncx-2005-1.dtd">`)
|
|
||||||
<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">
|
|
||||||
if head != nil {
|
|
||||||
{{ head, err := head.Marshal() }}
|
|
||||||
if err == nil {
|
|
||||||
@templ.Raw(head)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
<docTitle>
|
|
||||||
<text>{ title }</text>
|
|
||||||
</docTitle>
|
|
||||||
if navMap != nil {
|
|
||||||
{{ navMap, err := navMap.Marshal() }}
|
|
||||||
if err == nil {
|
|
||||||
@templ.Raw(navMap)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</ncx>
|
|
||||||
}
|
|
||||||
59
test/bilinovel_test.go
Normal file
59
test/bilinovel_test.go
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bilinovel-downloader/downloader/bilinovel"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBilinovel_GetNovel(t *testing.T) {
|
||||||
|
bilinovel, err := bilinovel.New()
|
||||||
|
bilinovel.SetTextOnly(true)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to create bilinovel: %v", err)
|
||||||
|
}
|
||||||
|
novel, err := bilinovel.GetNovel(4519, false)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to get novel: %v", err)
|
||||||
|
}
|
||||||
|
jsonBytes, err := json.Marshal(novel)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal novel: %v", err)
|
||||||
|
}
|
||||||
|
fmt.Println(string(jsonBytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBilinovel_GetVolume(t *testing.T) {
|
||||||
|
bilinovel, err := bilinovel.New()
|
||||||
|
bilinovel.SetTextOnly(true)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to create bilinovel: %v", err)
|
||||||
|
}
|
||||||
|
volume, err := bilinovel.GetVolume(1410, 52748, false)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to get volume: %v", err)
|
||||||
|
}
|
||||||
|
jsonBytes, err := json.Marshal(volume)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal volume: %v", err)
|
||||||
|
}
|
||||||
|
fmt.Println(string(jsonBytes))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBilinovel_GetChapter(t *testing.T) {
|
||||||
|
bilinovel, err := bilinovel.New()
|
||||||
|
bilinovel.SetTextOnly(true)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to create bilinovel: %v", err)
|
||||||
|
}
|
||||||
|
chapter, err := bilinovel.GetChapter(3095, 154930, 154933)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to get chapter: %v", err)
|
||||||
|
}
|
||||||
|
jsonBytes, err := json.Marshal(chapter)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal chapter: %v", err)
|
||||||
|
}
|
||||||
|
fmt.Println(string(jsonBytes))
|
||||||
|
}
|
||||||
55
text/wrapper.go
Normal file
55
text/wrapper.go
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package text
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bilinovel-downloader/model"
|
||||||
|
"bilinovel-downloader/utils"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/PuerkitoBio/goquery"
|
||||||
|
)
|
||||||
|
|
||||||
|
func PackVolumeToText(volume *model.Volume, outputPath string) error {
|
||||||
|
outputPath = filepath.Join(outputPath, utils.CleanDirName(volume.Title))
|
||||||
|
_, err := os.Stat(outputPath)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
err = os.MkdirAll(outputPath, 0755)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create output directory: %v", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return fmt.Errorf("failed to get output directory: %v", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
err = os.RemoveAll(outputPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to remove output directory: %v", err)
|
||||||
|
}
|
||||||
|
err = os.MkdirAll(outputPath, 0755)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create output directory: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for i, chapter := range volume.Chapters {
|
||||||
|
chapterPath := filepath.Join(outputPath, fmt.Sprintf("%03d-%s.txt", i, chapter.Title))
|
||||||
|
chapterFile, err := os.Create(chapterPath)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create chapter file: %v", err)
|
||||||
|
}
|
||||||
|
defer chapterFile.Close()
|
||||||
|
doc, err := goquery.NewDocumentFromReader(strings.NewReader(chapter.Content.Html))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create chapter file: %v", err)
|
||||||
|
}
|
||||||
|
doc.Find("img").Remove()
|
||||||
|
text := doc.Text()
|
||||||
|
_, err = chapterFile.WriteString(strings.TrimSpace(text))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write chapter file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
117
utils/epub.go
117
utils/epub.go
@@ -1,117 +0,0 @@
|
|||||||
package utils
|
|
||||||
|
|
||||||
import (
|
|
||||||
"archive/zip"
|
|
||||||
"bilinovel-downloader/template"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
)
|
|
||||||
|
|
||||||
func CreateEpub(path string) error {
|
|
||||||
savePath := path + ".epub"
|
|
||||||
zipFile, err := os.Create(savePath)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer zipFile.Close()
|
|
||||||
|
|
||||||
zipWriter := zip.NewWriter(zipFile)
|
|
||||||
defer zipWriter.Close()
|
|
||||||
|
|
||||||
err = addStringToZip(zipWriter, "mimetype", "application/epub+zip", zip.Store)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = addDirContentToZip(zipWriter, path, zip.Deflate)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = addStringToZip(zipWriter, "OEBPS/Styles/style.css", template.StyleCSS, zip.Deflate)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// func addFileToZip(zipWriter *zip.Writer, filename string, relPath string, method uint16) error {
|
|
||||||
// file, err := os.Open(filename)
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// defer file.Close()
|
|
||||||
|
|
||||||
// info, err := file.Stat()
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
|
|
||||||
// header, err := zip.FileInfoHeader(info)
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
// header.Name = relPath
|
|
||||||
// header.Method = method
|
|
||||||
|
|
||||||
// writer, err := zipWriter.CreateHeader(header)
|
|
||||||
// if err != nil {
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
|
|
||||||
// _, err = io.Copy(writer, file)
|
|
||||||
// return err
|
|
||||||
// }
|
|
||||||
|
|
||||||
func addStringToZip(zipWriter *zip.Writer, relPath, content string, method uint16) error {
|
|
||||||
header := &zip.FileHeader{
|
|
||||||
Name: relPath,
|
|
||||||
Method: method,
|
|
||||||
}
|
|
||||||
writer, err := zipWriter.CreateHeader(header)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = writer.Write([]byte(content))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func addDirContentToZip(zipWriter *zip.Writer, dirPath string, method uint16) error {
|
|
||||||
return filepath.Walk(dirPath, func(filePath string, info os.FileInfo, err error) error {
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if info.IsDir() {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
relPath, err := filepath.Rel(dirPath, filePath)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
file, err := os.Open(filePath)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
header, err := zip.FileInfoHeader(info)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
header.Name = relPath
|
|
||||||
header.Method = method
|
|
||||||
|
|
||||||
writer, err := zipWriter.CreateHeader(header)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = io.Copy(writer, file)
|
|
||||||
return err
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -9,11 +9,19 @@ import (
|
|||||||
"github.com/go-resty/resty/v2"
|
"github.com/go-resty/resty/v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
var client *resty.Client
|
type RestyClient struct {
|
||||||
|
client *resty.Client
|
||||||
|
concurrency int
|
||||||
|
sem chan struct{}
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func NewRestyClient(concurrency int) *RestyClient {
|
||||||
client = resty.New()
|
client := &RestyClient{
|
||||||
client.SetTransport(&http.Transport{
|
client: resty.New(),
|
||||||
|
concurrency: concurrency,
|
||||||
|
sem: make(chan struct{}, concurrency),
|
||||||
|
}
|
||||||
|
client.client.SetTransport(&http.Transport{
|
||||||
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
if addr == "www.bilinovel.com:443" {
|
if addr == "www.bilinovel.com:443" {
|
||||||
addr = "64.140.161.52:443"
|
addr = "64.140.161.52:443"
|
||||||
@@ -24,7 +32,16 @@ func init() {
|
|||||||
},
|
},
|
||||||
TLSHandshakeTimeout: 10 * time.Second,
|
TLSHandshakeTimeout: 10 * time.Second,
|
||||||
})
|
})
|
||||||
client.SetRetryCount(10).
|
client.client.
|
||||||
|
OnBeforeRequest(func(c *resty.Client, req *resty.Request) error {
|
||||||
|
client.sem <- struct{}{}
|
||||||
|
return nil
|
||||||
|
}).
|
||||||
|
OnAfterResponse(func(c *resty.Client, resp *resty.Response) error {
|
||||||
|
<-client.sem
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
client.client.SetRetryCount(10).
|
||||||
SetRetryWaitTime(3 * time.Second).
|
SetRetryWaitTime(3 * time.Second).
|
||||||
SetRetryAfter(func(client *resty.Client, resp *resty.Response) (time.Duration, error) {
|
SetRetryAfter(func(client *resty.Client, resp *resty.Response) (time.Duration, error) {
|
||||||
if resp.StatusCode() == http.StatusTooManyRequests {
|
if resp.StatusCode() == http.StatusTooManyRequests {
|
||||||
@@ -43,10 +60,13 @@ func init() {
|
|||||||
AddRetryCondition(func(r *resty.Response, err error) bool {
|
AddRetryCondition(func(r *resty.Response, err error) bool {
|
||||||
return err != nil || r.StatusCode() == http.StatusTooManyRequests
|
return err != nil || r.StatusCode() == http.StatusTooManyRequests
|
||||||
})
|
})
|
||||||
|
|
||||||
|
client.client.SetLogger(disableLogger{}).SetHeader("Accept-Charset", "utf-8").SetHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0")
|
||||||
|
return client
|
||||||
}
|
}
|
||||||
|
|
||||||
func Request() *resty.Request {
|
func (c *RestyClient) R() *resty.Request {
|
||||||
return client.R().SetLogger(disableLogger{}).SetHeader("Accept-Charset", "utf-8").SetHeader("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0")
|
return c.client.R()
|
||||||
}
|
}
|
||||||
|
|
||||||
type disableLogger struct{}
|
type disableLogger struct{}
|
||||||
|
|||||||
Reference in New Issue
Block a user