mirror of
https://github.com/bestnite/bilinovel-downloader.git
synced 2025-04-27 18:55:55 +08:00
Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
c9a7853cef | |||
b2130f60d5 | |||
d80c6053ab | |||
0c746c984b | |||
9d1d3f0f17 | |||
6028e7d8c2 | |||
6076069338 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1 +1,2 @@
|
||||
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
|
10
.vscode/launch.json
vendored
10
.vscode/launch.json
vendored
@ -2,12 +2,20 @@
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Debug download volume",
|
||||
"name": "volume",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "${workspaceFolder}",
|
||||
"args": ["download", "volume", "-n", "2013", "-v", "165880"]
|
||||
},
|
||||
{
|
||||
"name": "novel",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "${workspaceFolder}",
|
||||
"args": ["download", "novel", "-n", "4325"]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bilinovel-downloader/downloader"
|
||||
"bilinovel-downloader/downloader/bilinovel"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@ -9,6 +9,8 @@ import (
|
||||
|
||||
var downloadCmd = &cobra.Command{
|
||||
Use: "download",
|
||||
Short: "Download a novel or volume",
|
||||
Long: "Download a novel or volume",
|
||||
}
|
||||
|
||||
var downloadNovelCmd = &cobra.Command{
|
||||
@ -58,7 +60,7 @@ func runDownloadNovel(cmd *cobra.Command, args []string) error {
|
||||
if novelArgs.NovelId == 0 {
|
||||
return fmt.Errorf("novel id is required")
|
||||
}
|
||||
err := downloader.DownloadNovel(novelArgs.NovelId, novelArgs.outputPath)
|
||||
err := bilinovel.DownloadNovel(novelArgs.NovelId, novelArgs.outputPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to download novel: %v", err)
|
||||
}
|
||||
@ -73,7 +75,7 @@ func runDownloadVolume(cmd *cobra.Command, args []string) error {
|
||||
if volumeArgs.VolumeId == 0 {
|
||||
return fmt.Errorf("volume id is required")
|
||||
}
|
||||
err := downloader.DownloadVolume(volumeArgs.NovelId, volumeArgs.VolumeId, volumeArgs.outputPath)
|
||||
err := bilinovel.DownloadVolume(volumeArgs.NovelId, volumeArgs.VolumeId, volumeArgs.outputPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to download volume: %v", err)
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bilinovel-downloader/utils"
|
||||
"bilinovel-downloader/downloader/bilinovel"
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
@ -28,7 +28,7 @@ func init() {
|
||||
}
|
||||
|
||||
func runPackage(cmd *cobra.Command, args []string) error {
|
||||
err := utils.CreateEpub(pArgs.DirPath)
|
||||
err := bilinovel.CreateEpub(pArgs.DirPath)
|
||||
if err != nil {
|
||||
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,4 +1,4 @@
|
||||
package downloader
|
||||
package bilinovel
|
||||
|
||||
import (
|
||||
"bilinovel-downloader/model"
|
||||
@ -6,6 +6,7 @@ import (
|
||||
"bilinovel-downloader/utils"
|
||||
"bytes"
|
||||
"context"
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
@ -41,6 +42,7 @@ func GetNovel(novelId int) (*model.Novel, error) {
|
||||
|
||||
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()))
|
||||
@ -49,7 +51,7 @@ func GetNovel(novelId int) (*model.Novel, error) {
|
||||
novel.Authors = append(novel.Authors, strings.TrimSpace(s.Text()))
|
||||
})
|
||||
|
||||
volumes, err := GetNovelVolumes(novelId)
|
||||
volumes, err := getNovelVolumes(novelId)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get novel volumes: %v", err)
|
||||
}
|
||||
@ -59,7 +61,7 @@ func GetNovel(novelId int) (*model.Novel, error) {
|
||||
}
|
||||
|
||||
func GetVolume(novelId int, volumeId int) (*model.Volume, error) {
|
||||
novelUrl := fmt.Sprintf("https://www.bilinovel.com/novel/%v/vol_%v.html", novelId, volumeId)
|
||||
novelUrl := fmt.Sprintf("https://www.bilinovel.com/novel/%v/catalog", novelId)
|
||||
resp, err := utils.Request().Get(novelUrl)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get novel info: %v", err)
|
||||
@ -73,11 +75,42 @@ func GetVolume(novelId int, volumeId int) (*model.Volume, error) {
|
||||
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 = utils.Request().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.Cover = doc.Find(".book-cover").First().AttrOr("src", "")
|
||||
volume.Url = novelUrl
|
||||
volume.Url = volumeUrl
|
||||
volume.Chapters = make([]*model.Chapter, 0)
|
||||
|
||||
doc.Find(".authorname>a").Each(func(i int, s *goquery.Selection) {
|
||||
@ -97,7 +130,7 @@ func GetVolume(novelId int, volumeId int) (*model.Volume, error) {
|
||||
return volume, nil
|
||||
}
|
||||
|
||||
func GetNovelVolumes(novelId int) ([]*model.Volume, error) {
|
||||
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 {
|
||||
@ -124,7 +157,7 @@ func GetNovelVolumes(novelId int) ([]*model.Volume, error) {
|
||||
})
|
||||
|
||||
volumes := make([]*model.Volume, 0)
|
||||
for _, volumeIdStr := range volumeIds {
|
||||
for i, volumeIdStr := range volumeIds {
|
||||
volumeId, err := strconv.Atoi(volumeIdStr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to convert volume id: %v", err)
|
||||
@ -133,6 +166,7 @@ func GetNovelVolumes(novelId int) ([]*model.Volume, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get volume info: %v", err)
|
||||
}
|
||||
volume.SeriesIdx = i
|
||||
volumes = append(volumes, volume)
|
||||
}
|
||||
|
||||
@ -253,6 +287,11 @@ func downloadVolume(volume *model.Volume, outputPath string) error {
|
||||
return fmt.Errorf("failed to render cover: %v", err)
|
||||
}
|
||||
|
||||
err = DownloadFont(filepath.Join(outputPath, "OEBPS/Fonts"))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to download font: %v", err)
|
||||
}
|
||||
|
||||
contentsXHTMLPath := filepath.Join(outputPath, "OEBPS/Text/contents.xhtml")
|
||||
err = os.MkdirAll(path.Dir(contentsXHTMLPath), 0755)
|
||||
if err != nil {
|
||||
@ -298,7 +337,7 @@ func downloadVolume(volume *model.Volume, outputPath string) error {
|
||||
return fmt.Errorf("failed to create toc ncx: %v", err)
|
||||
}
|
||||
|
||||
err = utils.CreateEpub(outputPath)
|
||||
err = CreateEpub(outputPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create epub: %v", err)
|
||||
}
|
||||
@ -374,6 +413,9 @@ func downloadChapterByPage(page, chapterIdx int, chapter *model.Chapter, outputP
|
||||
content.Find(".cgo").Remove()
|
||||
content.Find("center").Remove()
|
||||
content.Find(".google-auto-placed").Remove()
|
||||
if strings.Contains(resp.String(), `font-family: "read"`) {
|
||||
content.Find("p").Last().AddClass("read-font")
|
||||
}
|
||||
|
||||
content.Find("img").Each(func(i int, s *goquery.Selection) {
|
||||
if err != nil {
|
||||
@ -387,15 +429,12 @@ func downloadChapterByPage(page, chapterIdx int, chapter *model.Chapter, outputP
|
||||
}
|
||||
}
|
||||
|
||||
fileName := filepath.Join(imgSavePath, fmt.Sprintf("%03v%s", i+1, path.Ext(imgUrl)))
|
||||
fileName := filepath.Join(imgSavePath, fmt.Sprintf("%03v%s", len(chapter.ImageFullPaths)+1, path.Ext(imgUrl)))
|
||||
err = DownloadImg(imgUrl, filepath.Join(outputPath, fileName))
|
||||
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/"))
|
||||
}
|
||||
@ -491,12 +530,20 @@ func CreateContentOPF(dirPath string, uuid string, volume *model.Volume) error {
|
||||
Metas: []model.DublinCoreMeta{
|
||||
{
|
||||
Name: "cover",
|
||||
Content: fmt.Sprintf("Images/cover%s", path.Ext(volume.Cover)),
|
||||
Content: "images-cover" + path.Ext(volume.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{
|
||||
@ -508,21 +555,26 @@ func CreateContentOPF(dirPath string, uuid string, volume *model.Volume) error {
|
||||
Media: "application/x-dtbncx+xml",
|
||||
})
|
||||
manifest.Items = append(manifest.Items, model.ManifestItem{
|
||||
ID: "cover",
|
||||
ID: "cover.xhtml",
|
||||
Link: "Text/cover.xhtml",
|
||||
Media: "application/xhtml+xml",
|
||||
})
|
||||
manifest.Items = append(manifest.Items, model.ManifestItem{
|
||||
ID: "contents",
|
||||
ID: "contents.xhtml",
|
||||
Link: "Text/contents.xhtml",
|
||||
Media: "application/xhtml+xml",
|
||||
Properties: "nav",
|
||||
})
|
||||
manifest.Items = append(manifest.Items, model.ManifestItem{
|
||||
ID: "images-cover",
|
||||
ID: "images-cover" + path.Ext(volume.Cover),
|
||||
Link: fmt.Sprintf("Images/cover%s", path.Ext(volume.Cover)),
|
||||
Media: fmt.Sprintf("image/%s", strings.ReplaceAll(strings.TrimPrefix(path.Ext(volume.Cover), "."), "jpg", "jpeg")),
|
||||
})
|
||||
manifest.Items = append(manifest.Items, model.ManifestItem{
|
||||
ID: "read.ttf",
|
||||
Link: "Fonts/read.ttf",
|
||||
Media: "application/vnd.ms-opentype",
|
||||
})
|
||||
for _, chapter := range volume.Chapters {
|
||||
manifest.Items = append(manifest.Items, model.ManifestItem{
|
||||
ID: path.Base(chapter.TextOEBPSPath),
|
||||
@ -614,3 +666,23 @@ func CreateTocNCX(dirPath string, uuid string, volume *model.Volume) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//go:embed read.ttf
|
||||
var readTTF []byte
|
||||
|
||||
func DownloadFont(outputPath string) error {
|
||||
log.Printf("Writing Font: %s", outputPath)
|
||||
|
||||
fontPath := filepath.Join(outputPath, "read.ttf")
|
||||
err := os.MkdirAll(path.Dir(fontPath), 0755)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create font directory: %v", err)
|
||||
}
|
||||
|
||||
err = os.WriteFile(fontPath, readTTF, 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to write font: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
@ -1,14 +1,16 @@
|
||||
package utils
|
||||
package bilinovel
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"bilinovel-downloader/template"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func CreateEpub(path string) error {
|
||||
log.Printf("Creating epub for %s", path)
|
||||
|
||||
savePath := path + ".epub"
|
||||
zipFile, err := os.Create(savePath)
|
||||
if err != nil {
|
||||
@ -29,7 +31,7 @@ func CreateEpub(path string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
err = addStringToZip(zipWriter, "OEBPS/Styles/style.css", template.StyleCSS, zip.Deflate)
|
||||
err = addStringToZip(zipWriter, "OEBPS/Styles/style.css", StyleCSS, zip.Deflate)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -81,6 +83,9 @@ func addStringToZip(zipWriter *zip.Writer, relPath, content string, method uint1
|
||||
|
||||
func addDirContentToZip(zipWriter *zip.Writer, dirPath string, method uint16) error {
|
||||
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
|
||||
}
|
BIN
downloader/bilinovel/read.ttf
Normal file
BIN
downloader/bilinovel/read.ttf
Normal file
Binary file not shown.
@ -1,6 +1,15 @@
|
||||
package template
|
||||
package bilinovel
|
||||
|
||||
const StyleCSS = `
|
||||
@font-face {
|
||||
font-family: "MI LANTING";
|
||||
src: url(../Fonts/read.ttf);
|
||||
}
|
||||
|
||||
.read-font {
|
||||
font-family: "MI LANTING", serif !important;
|
||||
}
|
||||
|
||||
body > div {
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
@ -11,15 +11,20 @@ type Chapter struct {
|
||||
}
|
||||
|
||||
type Volume struct {
|
||||
Id int
|
||||
SeriesIdx int
|
||||
Title string
|
||||
Url string
|
||||
Cover string
|
||||
Description string
|
||||
Authors []string
|
||||
Chapters []*Chapter
|
||||
NovelId int
|
||||
NovelTitle string
|
||||
}
|
||||
|
||||
type Novel struct {
|
||||
Id int
|
||||
Title string
|
||||
Description string
|
||||
Authors []string
|
||||
|
Loading…
x
Reference in New Issue
Block a user