Skip to content
This repository has been archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
add download wenku8 img option
Browse files Browse the repository at this point in the history
  • Loading branch information
SamsonMXVI committed Aug 17, 2023
1 parent 4b70686 commit ad49af6
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 14 deletions.
9 changes: 8 additions & 1 deletion downloader/download_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package downloader

import (
"fmt"
"os"
"path"

"github.com/samsonmxvi/go-wenku-downloader/util"
Expand All @@ -15,7 +16,13 @@ func DownloadImage(url string, dirPath string) error {

// check file already download
if util.CheckFileExist(filePath) {
return nil
// 获取文件信息
fileInfo, _ := os.Stat(filePath)
// 获取文件大小
fileSize := fileInfo.Size()
if fileSize > 0 {
return nil
}
}

// check dir if not exit create
Expand Down
23 changes: 14 additions & 9 deletions downloader/download_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
"log"
"os"
"path"
"strings"
"time"

"github.com/samsonmxvi/go-wenku-downloader/scraper"
"github.com/samsonmxvi/go-wenku-downloader/util"
"gopkg.in/cheggaaa/pb.v2"
)

func DownloadVolume(volume *scraper.Volume, dirPath string) error {
func DownloadVolume(volume *scraper.Volume, dirPath string, onlyWenku8Img bool) error {
var imageArray []string
imageDirPath := path.Join(dirPath, ImageFolderName)

Expand Down Expand Up @@ -55,14 +56,18 @@ func DownloadVolume(volume *scraper.Volume, dirPath string) error {
progressBar.Finish()

for _, imageURL := range imageArray {
isWenku8Source := strings.Contains(imageURL, "wenku8.com")
if !isWenku8Source && onlyWenku8Img {
continue
}
success := false
for i := 0; i < 3; i++ {
for i := 0; i < RetryTimes; i++ {
err := DownloadImage(imageURL, imageDirPath)
if err == nil {
success = true
break
} else {
time.Sleep(3 * time.Second) // temp fix rate limit
time.Sleep(RetryTimer) // temp fix rate limit
continue
}
}
Expand All @@ -75,14 +80,14 @@ func DownloadVolume(volume *scraper.Volume, dirPath string) error {
}

func getChapterArray(volume *scraper.Volume) ([]*scraper.Chapter, error) {
for i := 0; i < 3; i++ {
for i := 0; i < RetryTimes; i++ {
chaterArray, err := scraper.GetChapterArray(volume)
if err == nil {
time.Sleep(1 * time.Second)
time.Sleep(DownloadTimer)
return chaterArray, nil
} else {
log.Printf("获取章节列表失败 %v, 重试第%v次 \n", err, i)
time.Sleep(3 * time.Second) // temp fix rate limit
time.Sleep(RetryTimer) // temp fix rate limit
continue
}
}
Expand All @@ -91,13 +96,13 @@ func getChapterArray(volume *scraper.Volume) ([]*scraper.Chapter, error) {

func getChaterContent(chapter *scraper.Chapter) error {
var err error
for i := 0; i < 3; i++ {
for i := 0; i < RetryTimes; i++ {
err = scraper.GetChapterContent(chapter)
if err == nil {
time.Sleep(1 * time.Second)
time.Sleep(DownloadTimer)
return nil
} else {
time.Sleep(3 * time.Second) // temp fix rate limit
time.Sleep(RetryTimer) // temp fix rate limit
log.Printf("获取章节内容失败 %v, 重试第%v次 \n", err, i)
continue
}
Expand Down
2 changes: 1 addition & 1 deletion downloader/download_volume_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ func TestDownloadVolume(t *testing.T) {
volumeArray, err := scraper.GetNovelVolumeArray("https://www.wenku8.net/novel/1/1973/index.htm")
require.NoError(t, err)
require.NotEmpty(t, volumeArray)
err = DownloadVolume(volumeArray[0], "./")
err = DownloadVolume(volumeArray[0], "./", true)
require.NoError(t, err)
}
3 changes: 2 additions & 1 deletion downloader/grab.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package downloader

import (
"fmt"
"log"
"time"

"github.com/cavaliergopher/grab/v3"
Expand All @@ -14,7 +15,7 @@ func Grab(path string, url string) error {
req, _ := grab.NewRequest(path, url)

// start download
fmt.Printf("Downloading %v...\n", req.URL())
log.Printf("正在下载 %v...\n", req.URL())
resp := client.Do(req)

// start UI loop
Expand Down
5 changes: 5 additions & 0 deletions downloader/variable.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package downloader

import "time"

var (
ErrorJsonName = "error.json"
ImageFolderName = "images"
DownloadTimer = time.Second
RetryTimes = 6
RetryTimer = 6 * time.Second
)
11 changes: 10 additions & 1 deletion prompt/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,16 @@ func download(novelId int) error {
return fmt.Errorf("prompt failed %v", err)
}

// get onlyWenku8Img
onlyWenku8Img, err := getInputBool("是否只下载wenku8的图片(推荐使用默认数值, 非文库图片资源大多数情况已失效), 默认:y(y/n)", true)
if err != nil {
return fmt.Errorf("prompt failed %v", err)
}

// download volume
for _, volume := range volumeArray {
volumePath := path.Join(downloadPath, formatFilename(volume.Name))
err = downloader.DownloadVolume(volume, volumePath)
err = downloader.DownloadVolume(volume, volumePath, onlyWenku8Img)
if err != nil {
log.Printf("download volume error %v", err)
continue
Expand Down Expand Up @@ -114,6 +120,9 @@ func createEpub(novel *scraper.Novel, volumeName string, chapterCount int, cover
if len(chapter.Content.Images) != 0 {
for _, img := range chapter.Content.Images {
imgFile := path.Join(imagePath, util.GetUrlLastString(img))
if !util.CheckFileExist(imgFile) {
continue
}
internalPath, _ := util.AddImage(epub, imgFile)
xhtml = util.AddImageToXhtml(internalPath, xhtml)
imagePathList = append(imagePathList, imgFile)
Expand Down
31 changes: 31 additions & 0 deletions prompt/get_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package prompt

import (
"errors"
"fmt"
"regexp"
"strconv"

Expand Down Expand Up @@ -117,3 +118,33 @@ func getSelectedIndex(label string, itmes []string) (int, error) {
}
return selectedIndex, nil
}

func getInputBool(label string, defaultValue bool) (bool, error) {
validate := func(input string) error {
if input == "y" || input == "n" || input == "" {
return nil
} else {
return fmt.Errorf("只能填写y, n")
}
}

prompt := promptui.Prompt{
Label: label,
Validate: validate,
Stdout: &noBellStdout{},
}

result, err := prompt.Run()

if err != nil {
return false, err
}

if result == "y" {
return true, nil
} else if result == "n" {
return false, nil
} else {
return defaultValue, nil
}
}
3 changes: 2 additions & 1 deletion scraper/get_novel_details.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scraper

import (
"fmt"
"regexp"
"strconv"
"strings"
Expand All @@ -21,7 +22,7 @@ func GetNovelDetails(novelId int) (*Novel, error) {
}

if doc.Find(".blocktitle").First().Text() == "出现错误!" {
return nil, nil
return nil, fmt.Errorf("没有这本小说")
}

getNovelDetailsDoc(doc, novel)
Expand Down

0 comments on commit ad49af6

Please sign in to comment.