Skip to content

Commit

Permalink
hcomic 支持手机版
Browse files Browse the repository at this point in the history
  • Loading branch information
ystyle committed Apr 22, 2020
1 parent 98e196b commit 355f260
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
32 changes: 32 additions & 0 deletions test/goquery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import (
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/ystyle/kas/model"
"github.com/ystyle/kas/util/config"
"github.com/ystyle/kas/util/site"
"github.com/ystyle/kas/util/web"
"io/ioutil"
"net/url"
"path"
"regexp"
"strings"
"testing"
Expand Down Expand Up @@ -98,3 +101,32 @@ func TestGoquery(t *testing.T) {
//div := node.Find("<div class='summary'>")
fmt.Println(div.Html())
}

func TestHcomic(t *testing.T) {
html, err := web.GetHtmlNode("https://bhmog.com/s/70622/")
if err != nil {
fmt.Println(err)
return
}
meta := html.Find("meta[name=\"applicable-device\"]")
if attr, has := meta.Attr("content"); has && attr == "pc,mobile" {
imgs := html.Find(".container .gallery img")
for i := range imgs.Nodes {
img := imgs.Eq(i)
src, _ := img.Attr("data-src")
fmt.Println(src)
}
}
fmt.Println()
fmt.Println(html.Html())
}

func TestDownload(t *testing.T) {
dir := path.Join(config.CacheDir, "19.jpg")
fmt.Println(dir)
// https://aa.hcomics.club/uploads/1615779/2.jpg
err := web.Download("https://aa.hcomics.club/uploads/1615779/3.jpg", "2.jpg")
if err != nil {
fmt.Println(err)
}
}
36 changes: 25 additions & 11 deletions util/hcomic/images.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hcomic

import (
"fmt"
"github.com/ystyle/kas/model"
"github.com/ystyle/kas/util/web"
"net/url"
Expand All @@ -13,16 +14,29 @@ func GetAllImages(book *model.HcomicInfo) error {
if err != nil {
return err
}
lis := html.Find(".img_list li")
if book.BookName == "" {
book.BookName = html.Find(".page_tit .tit").Text()
}
for i := range lis.Nodes {
li := lis.Eq(i)
url, _ := li.Find("img").First().Attr("src")
title := li.Find("label").Text()
if url != "" {
book.AddSection(title, GetHDImage(url))
meta := html.Find("meta[name=\"applicable-device\"]")
if attr, has := meta.Attr("content"); has && attr == "pc,mobile" {
if book.BookName == "" {
book.BookName = html.Find("#info-block #info h1").Text()
}
imgs := html.Find(".container .gallery img")
for i := range imgs.Nodes {
img := imgs.Eq(i)
src, _ := img.Attr("data-src")
book.AddSection(fmt.Sprintf("#%d", i+1), fmt.Sprintf("https://aa.hcomics.club%s", src))
}
} else {
lis := html.Find(".img_list li")
if book.BookName == "" {
book.BookName = html.Find(".page_tit .tit").Text()
}
for i := range lis.Nodes {
li := lis.Eq(i)
url, _ := li.Find("img").First().Attr("src")
title := li.Find("label").Text()
if url != "" {
book.AddSection(title, GetHDImage(url))
}
}
}
return nil
Expand All @@ -34,7 +48,7 @@ func GetHDImage(url string) string {
// 高清图
// https://img.comicstatic.icu/img/cn/1570141/1.jpg
if strings.Contains(url, "pic.") {
return strings.ReplaceAll(url, "pic.", "img.")
return strings.ReplaceAll(url, "pic.comicstatic.icu", "img.comicstatic.icu")
}
// 没有匹配到则用预览图
return url
Expand Down

0 comments on commit 355f260

Please sign in to comment.