mirror of
https://github.com/bestnite/bilinovel-downloader.git
synced 2025-04-27 02:35:54 +08:00
fix font error
This commit is contained in:
parent
6028e7d8c2
commit
9d1d3f0f17
55
a_test.go
Normal file
55
a_test.go
Normal 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))
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bilinovel-downloader/downloader"
|
"bilinovel-downloader/downloader/bilinovel"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -60,7 +60,7 @@ func runDownloadNovel(cmd *cobra.Command, args []string) error {
|
|||||||
if novelArgs.NovelId == 0 {
|
if novelArgs.NovelId == 0 {
|
||||||
return fmt.Errorf("novel id is required")
|
return fmt.Errorf("novel id is required")
|
||||||
}
|
}
|
||||||
err := downloader.DownloadNovel(novelArgs.NovelId, novelArgs.outputPath)
|
err := bilinovel.DownloadNovel(novelArgs.NovelId, novelArgs.outputPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to download novel: %v", err)
|
return fmt.Errorf("failed to download novel: %v", err)
|
||||||
}
|
}
|
||||||
@ -75,7 +75,7 @@ func runDownloadVolume(cmd *cobra.Command, args []string) error {
|
|||||||
if volumeArgs.VolumeId == 0 {
|
if volumeArgs.VolumeId == 0 {
|
||||||
return fmt.Errorf("volume id is required")
|
return fmt.Errorf("volume id is required")
|
||||||
}
|
}
|
||||||
err := downloader.DownloadVolume(volumeArgs.NovelId, volumeArgs.VolumeId, volumeArgs.outputPath)
|
err := bilinovel.DownloadVolume(volumeArgs.NovelId, volumeArgs.VolumeId, volumeArgs.outputPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to download volume: %v", err)
|
return fmt.Errorf("failed to download volume: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bilinovel-downloader/utils"
|
"bilinovel-downloader/downloader/bilinovel"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -28,7 +28,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runPackage(cmd *cobra.Command, args []string) error {
|
func runPackage(cmd *cobra.Command, args []string) error {
|
||||||
err := utils.CreateEpub(pArgs.DirPath)
|
err := bilinovel.CreateEpub(pArgs.DirPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create epub: %v", err)
|
return fmt.Errorf("failed to create epub: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package downloader
|
package bilinovel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bilinovel-downloader/model"
|
"bilinovel-downloader/model"
|
||||||
@ -286,6 +286,11 @@ func downloadVolume(volume *model.Volume, outputPath string) error {
|
|||||||
return fmt.Errorf("failed to render cover: %v", err)
|
return fmt.Errorf("failed to render cover: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = DownloadFont(filepath.Join(outputPath, "OEBPS/Fonts"))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to download font: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
contentsXHTMLPath := filepath.Join(outputPath, "OEBPS/Text/contents.xhtml")
|
contentsXHTMLPath := filepath.Join(outputPath, "OEBPS/Text/contents.xhtml")
|
||||||
err = os.MkdirAll(path.Dir(contentsXHTMLPath), 0755)
|
err = os.MkdirAll(path.Dir(contentsXHTMLPath), 0755)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -331,7 +336,7 @@ func downloadVolume(volume *model.Volume, outputPath string) error {
|
|||||||
return fmt.Errorf("failed to create toc ncx: %v", err)
|
return fmt.Errorf("failed to create toc ncx: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = utils.CreateEpub(outputPath)
|
err = CreateEpub(outputPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create epub: %v", err)
|
return fmt.Errorf("failed to create epub: %v", err)
|
||||||
}
|
}
|
||||||
@ -407,6 +412,7 @@ func downloadChapterByPage(page, chapterIdx int, chapter *model.Chapter, outputP
|
|||||||
content.Find(".cgo").Remove()
|
content.Find(".cgo").Remove()
|
||||||
content.Find("center").Remove()
|
content.Find("center").Remove()
|
||||||
content.Find(".google-auto-placed").Remove()
|
content.Find(".google-auto-placed").Remove()
|
||||||
|
content.Find("p").Last().AddClass("read-font")
|
||||||
|
|
||||||
content.Find("img").Each(func(i int, s *goquery.Selection) {
|
content.Find("img").Each(func(i int, s *goquery.Selection) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -549,21 +555,26 @@ func CreateContentOPF(dirPath string, uuid string, volume *model.Volume) error {
|
|||||||
Media: "application/x-dtbncx+xml",
|
Media: "application/x-dtbncx+xml",
|
||||||
})
|
})
|
||||||
manifest.Items = append(manifest.Items, model.ManifestItem{
|
manifest.Items = append(manifest.Items, model.ManifestItem{
|
||||||
ID: "cover",
|
ID: "cover.xhtml",
|
||||||
Link: "Text/cover.xhtml",
|
Link: "Text/cover.xhtml",
|
||||||
Media: "application/xhtml+xml",
|
Media: "application/xhtml+xml",
|
||||||
})
|
})
|
||||||
manifest.Items = append(manifest.Items, model.ManifestItem{
|
manifest.Items = append(manifest.Items, model.ManifestItem{
|
||||||
ID: "contents",
|
ID: "contents.xhtml",
|
||||||
Link: "Text/contents.xhtml",
|
Link: "Text/contents.xhtml",
|
||||||
Media: "application/xhtml+xml",
|
Media: "application/xhtml+xml",
|
||||||
Properties: "nav",
|
Properties: "nav",
|
||||||
})
|
})
|
||||||
manifest.Items = append(manifest.Items, model.ManifestItem{
|
manifest.Items = append(manifest.Items, model.ManifestItem{
|
||||||
ID: "images-cover",
|
ID: "images-cover" + path.Ext(volume.Cover),
|
||||||
Link: fmt.Sprintf("Images/cover%s", path.Ext(volume.Cover)),
|
Link: fmt.Sprintf("Images/cover%s", path.Ext(volume.Cover)),
|
||||||
Media: fmt.Sprintf("image/%s", strings.ReplaceAll(strings.TrimPrefix(path.Ext(volume.Cover), "."), "jpg", "jpeg")),
|
Media: fmt.Sprintf("image/%s", strings.ReplaceAll(strings.TrimPrefix(path.Ext(volume.Cover), "."), "jpg", "jpeg")),
|
||||||
})
|
})
|
||||||
|
manifest.Items = append(manifest.Items, model.ManifestItem{
|
||||||
|
ID: "read.woff2",
|
||||||
|
Link: "Fonts/read.woff2",
|
||||||
|
Media: "font/woff2",
|
||||||
|
})
|
||||||
for _, chapter := range volume.Chapters {
|
for _, chapter := range volume.Chapters {
|
||||||
manifest.Items = append(manifest.Items, model.ManifestItem{
|
manifest.Items = append(manifest.Items, model.ManifestItem{
|
||||||
ID: path.Base(chapter.TextOEBPSPath),
|
ID: path.Base(chapter.TextOEBPSPath),
|
||||||
@ -655,3 +666,24 @@ func CreateTocNCX(dirPath string, uuid string, volume *model.Volume) error {
|
|||||||
}
|
}
|
||||||
return nil
|
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
|
||||||
|
}
|
@ -1,14 +1,16 @@
|
|||||||
package utils
|
package bilinovel
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
"bilinovel-downloader/template"
|
|
||||||
"io"
|
"io"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CreateEpub(path string) error {
|
func CreateEpub(path string) error {
|
||||||
|
log.Printf("Creating epub for %s", path)
|
||||||
|
|
||||||
savePath := path + ".epub"
|
savePath := path + ".epub"
|
||||||
zipFile, err := os.Create(savePath)
|
zipFile, err := os.Create(savePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -29,7 +31,7 @@ func CreateEpub(path string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = addStringToZip(zipWriter, "OEBPS/Styles/style.css", template.StyleCSS, zip.Deflate)
|
err = addStringToZip(zipWriter, "OEBPS/Styles/style.css", StyleCSS, zip.Deflate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
@ -1,6 +1,15 @@
|
|||||||
package template
|
package bilinovel
|
||||||
|
|
||||||
const StyleCSS = `
|
const StyleCSS = `
|
||||||
|
@font-face{
|
||||||
|
font-family: "read";
|
||||||
|
src: url(../Fonts/read.woff2);
|
||||||
|
}
|
||||||
|
|
||||||
|
.read-font{
|
||||||
|
font-family: "read" !important;
|
||||||
|
}
|
||||||
|
|
||||||
body > div {
|
body > div {
|
||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
padding: 20px;
|
padding: 20px;
|
1
go.mod
1
go.mod
@ -8,6 +8,7 @@ require (
|
|||||||
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
|
||||||
|
golang.org/x/text v0.24.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
2
go.sum
2
go.sum
@ -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.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
golang.org/x/text v0.15.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.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 h1:eTDhh4ZXt5Qf0augr54TN6suAUudPcawVZeIAPU7D4U=
|
||||||
golang.org/x/time v0.6.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
|
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=
|
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||||
|
Loading…
x
Reference in New Issue
Block a user