Skip to content

Commit

Permalink
fix: 修复hcomic的漫画下载不了的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
ystyle committed Jun 30, 2020
1 parent 610c937 commit 41837e5
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
12 changes: 11 additions & 1 deletion model/hcomic.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type HcomicSection struct {
Index int // 顺序
Title string // 标题
Url string // 地址
BackupUrl string // 备选地址
ImgFile string // 图片缓存路径
HtmlFile string // html 缓存路径
InnerHtml string // html 内部目录
Expand All @@ -51,16 +52,25 @@ func (hcomic *HcomicInfo) SetDefault() {
hcomic.ZipFile = path.Join(config.StoreDir, "hcomic", fmt.Sprintf("%s.zip", hcomic.ID))
}

func (hcomic *HcomicInfo) AddSection(title, url string) {
func (hcomic *HcomicInfo) AddSection(title, url, backupURL string) {
index := len(hcomic.Sections)
imageFilename := path.Base(url)
hcomic.Sections = append(hcomic.Sections, &HcomicSection{
Index: index,
Title: title,
Url: url,
BackupUrl: backupURL,
ImgFile: path.Join(hcomic.ScaledImagesDir, imageFilename),
HtmlFile: path.Join(hcomic.HtmlDir, fmt.Sprintf("Page-%d.html", index)),
InnerHtml: fmt.Sprintf("html/Page-%d.html", index),
InnerImage: fmt.Sprintf("scaled-images/%s", imageFilename),
})
}

func (hs *HcomicSection) ResetToBackupURL() {
// 重置文件名, 在预览图和高清图格式不一致时用
imageFilename := path.Base(hs.BackupUrl)
ScaledImagesDir := path.Dir(hs.ImgFile)
hs.ImgFile = path.Join(ScaledImagesDir, imageFilename)
hs.InnerImage = fmt.Sprintf("scaled-images/%s", imageFilename)
}
7 changes: 6 additions & 1 deletion services/hcomic.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"io/ioutil"
"os"
"path"
"strings"
"sync"
"time"
)
Expand Down Expand Up @@ -110,7 +111,11 @@ func Submit(client *core.WsClient, message core.Message) {

func download(wg *sync.WaitGroup, client *core.WsClient, section *model.HcomicSection) {
defer wg.Done()
_ = web.Download(section.Url, section.ImgFile)
err := web.Download(section.Url, section.ImgFile)
if err != nil && section.BackupUrl != "" {
section.ResetToBackupURL()
_ = web.Download(section.BackupUrl, section.ImgFile)
}
client.WsSend <- core.NewMessage("info", fmt.Sprintf("下载完成: %s", section.Url))
}

Expand Down
12 changes: 10 additions & 2 deletions util/hcomic/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/ystyle/kas/util/web"
"net/url"
"path"
"regexp"
"strings"
)

Expand All @@ -14,6 +15,7 @@ func GetAllImages(book *model.HcomicInfo) error {
if err != nil {
return err
}
reg, _ := regexp.Compile(`this.src='(.*)';`)
meta := html.Find("meta[name=\"applicable-device\"]")
if attr, has := meta.Attr("content"); has && attr == "pc,mobile" {
if book.BookName == "" {
Expand All @@ -23,7 +25,13 @@ func GetAllImages(book *model.HcomicInfo) error {
for i := range imgs.Nodes {
img := imgs.Eq(i)
src, _ := img.Attr("data-src")
book.AddSection(fmt.Sprintf("#%d", i+1), GetHDImage(src))
jstext, _ := img.Attr("onerror")
group := reg.FindStringSubmatch(jstext)
backurl := ""
if len(group) > 0 {
backurl = group[1]
}
book.AddSection(fmt.Sprintf("#%d", i+1), GetHDImage(src), GetHDImage(backurl))
}
} else {
lis := html.Find(".img_list li")
Expand All @@ -35,7 +43,7 @@ func GetAllImages(book *model.HcomicInfo) error {
url, _ := li.Find("img").First().Attr("src")
title := li.Find("label").Text()
if url != "" {
book.AddSection(title, GetHDImage(url))
book.AddSection(title, GetHDImage(url), "")
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions util/web/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func Download(url string, dir string) error {
if err != nil {
return fmt.Errorf("目标网站无法连接: %w", err)
}
if res.StatusCode >= 400 {
return fmt.Errorf("目标网站不存在: %w", err)
}
f, err := os.Create(dir)
if err != nil {
return fmt.Errorf("服务器错误: 无法创建文件")
Expand Down

0 comments on commit 41837e5

Please sign in to comment.