From 41837e5df2e36f397452d67e3de961cf63710748 Mon Sep 17 00:00:00 2001 From: YSTYLE Date: Wed, 1 Jul 2020 00:30:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dhcomic=E7=9A=84?= =?UTF-8?q?=E6=BC=AB=E7=94=BB=E4=B8=8B=E8=BD=BD=E4=B8=8D=E4=BA=86=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/hcomic.go | 12 +++++++++++- services/hcomic.go | 7 ++++++- util/hcomic/images.go | 12 ++++++++++-- util/web/http.go | 3 +++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/model/hcomic.go b/model/hcomic.go index 166c894..de6f56f 100644 --- a/model/hcomic.go +++ b/model/hcomic.go @@ -29,6 +29,7 @@ type HcomicSection struct { Index int // 顺序 Title string // 标题 Url string // 地址 + BackupUrl string // 备选地址 ImgFile string // 图片缓存路径 HtmlFile string // html 缓存路径 InnerHtml string // html 内部目录 @@ -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) +} diff --git a/services/hcomic.go b/services/hcomic.go index 57a4185..e199bda 100644 --- a/services/hcomic.go +++ b/services/hcomic.go @@ -14,6 +14,7 @@ import ( "io/ioutil" "os" "path" + "strings" "sync" "time" ) @@ -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)) } diff --git a/util/hcomic/images.go b/util/hcomic/images.go index b6c53ea..5c349a2 100644 --- a/util/hcomic/images.go +++ b/util/hcomic/images.go @@ -6,6 +6,7 @@ import ( "github.com/ystyle/kas/util/web" "net/url" "path" + "regexp" "strings" ) @@ -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 == "" { @@ -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") @@ -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), "") } } } diff --git a/util/web/http.go b/util/web/http.go index 94ff60c..edd20c1 100644 --- a/util/web/http.go +++ b/util/web/http.go @@ -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("服务器错误: 无法创建文件")