3 Commits

Author SHA1 Message Date
9d1d3f0f17 fix font error 2025-04-20 02:13:59 +10:00
6028e7d8c2 add metadatas 2025-04-20 01:13:51 +10:00
6076069338 mod cmd/download.go 2025-04-20 00:35:45 +10:00
12 changed files with 223 additions and 20 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
novels/
dist/

31
.goreleaser.yaml Normal file
View 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

55
a_test.go Normal file
View File

@ -0,0 +1,55 @@
package main
import (
"bytes"
"fmt"
"io"
"testing"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
// 尝试使用不同编码进行解码
func tryDecodeWithEncoding(data []byte, decoder transform.Transformer) (string, error) {
reader := transform.NewReader(bytes.NewReader(data), decoder)
decoded, err := io.ReadAll(reader)
if err != nil {
return "", err
}
return string(decoded), nil
}
// 检测可能的编码
func detectEncoding(data []byte) {
// 尝试 GBK 解码
if result, err := tryDecodeWithEncoding(data, simplifiedchinese.GBK.NewDecoder()); err == nil {
fmt.Printf("可能是 GBK 编码: %s\n", result)
}
// 尝试 GB18030 解码
if result, err := tryDecodeWithEncoding(data, simplifiedchinese.GB18030.NewDecoder()); err == nil {
fmt.Printf("可能是 GB18030 编码: %s\n", result)
}
// 尝试 UTF-8 解码(直接输出)
fmt.Printf("原始 UTF-8 输出: %s\n", string(data))
}
func TestA(t *testing.T) {
chaos := "凳蹦戎昆储。"
body := []byte(chaos)
fmt.Println("尝试检测编码...")
detectEncoding(body)
// 使用 GBK 解码
reader := transform.NewReader(bytes.NewReader(body), simplifiedchinese.GBK.NewDecoder())
decoded, err := io.ReadAll(reader)
if err != nil {
fmt.Printf("解码失败: %v\n", err)
return
}
fmt.Println("GBK 解码结果:", string(decoded))
}

View File

@ -1,14 +1,16 @@
package cmd
import (
"bilinovel-downloader/downloader"
"bilinovel-downloader/downloader/bilinovel"
"fmt"
"github.com/spf13/cobra"
)
var downloadCmd = &cobra.Command{
Use: "download",
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)
}

View File

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

View File

@ -1,4 +1,4 @@
package downloader
package bilinovel
import (
"bilinovel-downloader/model"
@ -41,6 +41,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 +50,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 +60,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 +74,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 +129,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 +156,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 +165,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 +286,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 +336,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 +412,7 @@ func downloadChapterByPage(page, chapterIdx int, chapter *model.Chapter, outputP
content.Find(".cgo").Remove()
content.Find("center").Remove()
content.Find(".google-auto-placed").Remove()
content.Find("p").Last().AddClass("read-font")
content.Find("img").Each(func(i int, s *goquery.Selection) {
if err != nil {
@ -497,6 +536,14 @@ func CreateContentOPF(dirPath string, uuid string, volume *model.Volume) error {
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.woff2",
Link: "Fonts/read.woff2",
Media: "font/woff2",
})
for _, chapter := range volume.Chapters {
manifest.Items = append(manifest.Items, model.ManifestItem{
ID: path.Base(chapter.TextOEBPSPath),
@ -614,3 +666,24 @@ func CreateTocNCX(dirPath string, uuid string, volume *model.Volume) error {
}
return nil
}
func DownloadFont(outputPath string) error {
log.Printf("Downloading Font: read.woff2")
fontPath := filepath.Join(outputPath, "read.woff2")
err := os.MkdirAll(path.Dir(fontPath), 0755)
if err != nil {
return fmt.Errorf("failed to create font directory: %v", err)
}
resp, err := utils.Request().Get("https://www.bilinovel.com/public/font/read.woff2")
if err != nil {
return fmt.Errorf("failed to download font: %v", err)
}
err = os.WriteFile(fontPath, resp.Body(), 0644)
if err != nil {
return fmt.Errorf("failed to write font: %v", err)
}
return nil
}

View File

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

View File

@ -1,6 +1,15 @@
package template
package bilinovel
const StyleCSS = `
@font-face{
font-family: "read";
src: url(../Fonts/read.woff2);
}
.read-font{
font-family: "read" !important;
}
body > div {
margin: 0 auto;
padding: 20px;

1
go.mod
View File

@ -8,6 +8,7 @@ require (
github.com/go-resty/resty/v2 v2.16.5
github.com/google/uuid v1.6.0
github.com/spf13/cobra v1.9.1
golang.org/x/text v0.24.0
)
require (

2
go.sum
View File

@ -77,6 +77,8 @@ golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
golang.org/x/time v0.6.0 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

View File

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