Skip to content

Commit

Permalink
fix(book): follow detail page change
Browse files Browse the repository at this point in the history
  • Loading branch information
NateScarlet committed Aug 16, 2022
1 parent c01bd9b commit c0291a1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pkg/book/__snapshots__/TestBook_Fetch.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"ID": "1004608738",
"Introduction": "在破败中崛起,在寂灭中复苏。\n沧海成尘,雷电枯竭,那一缕幽雾又一次临近大地,世间的枷锁被打开了,一个全新的世界就此揭开神秘的一角……",
"LastUpdated": {
"$Time": "2021-05-01 00:00:00 +0800 CST"
"$Time": "2021-05-01 10:10:55 +0800 CST"
},
"MonthRecommendCount": "*count*",
"MonthTicketCount": "*count*",
Expand Down
2 changes: 1 addition & 1 deletion pkg/book/__snapshots__/TestBook_Fetch_Free.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"ID": "8361",
"Introduction": "一个懒惰的少年,因性格原因选学了无人问津的光系魔法,却无意中踏近了命运的巨轮,一步一步的成为了传说中的大魔导师。正是在他的努力下结束了东西大陆的分界,让整个大陆不再有种族之分,成为了后世各族共尊的光之子。",
"LastUpdated": {
"$Time": "2008-01-14 00:00:00 +0800 CST"
"$Time": "2008-01-14 12:08:00 +0800 CST"
},
"MonthRecommendCount": "*count*",
"MonthTicketCount": "*count*",
Expand Down
47 changes: 28 additions & 19 deletions pkg/book/book.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,25 @@ func (b *Book) Fetch(ctx context.Context) (err error) {
}()
infoElem := doc.Find(".book-info").Clone()
stateElem := doc.Find(".book-state")
var ok bool

// Title
b.Title, ok = doc.Find("meta[property=\"og:novel:book_name\"]").First().Attr("content")
if !ok {
err = fmt.Errorf("'og:novel:book_name' meta tag not found")
return
}

// LastUpdated
lastUpdatedText, ok := doc.Find("meta[property=\"og:novel:update_time\"]").First().Attr("content")
if !ok {
err = fmt.Errorf("'og:novel:update_time' meta tag not found")
return
}
b.LastUpdated, err = ParseTime(lastUpdatedText)
if err != nil {
return err
}

// Author
writerElem := infoElem.Find("a.writer")
Expand All @@ -91,9 +110,6 @@ func (b *Book) Fetch(ctx context.Context) (err error) {
}
}

// Title
b.Title = strings.TrimSpace(infoElem.Find("h1").Text())

// Categories
infoElem.Find("a").Each(func(i int, s *goquery.Selection) {
href, ok := s.Attr("href")
Expand All @@ -107,7 +123,7 @@ func (b *Book) Fetch(ctx context.Context) (err error) {
})

// Cover
b.CoverURL = util.AbsoluteURL(doc.Find("#bookImg > img").AttrOr("src", ""))
b.CoverURL = util.AbsoluteURL(doc.Find("meta[property=\"og:image\"]").AttrOr("content", ""))

// Tags
tagElemList := infoElem.Find(".tag > span").
Expand All @@ -119,17 +135,16 @@ func (b *Book) Fetch(ctx context.Context) (err error) {

// Introduction
b.Summary = infoElem.Find(".intro").Text()
b.Introduction = nodesText(doc.Find(".book-info-detail .book-intro").Nodes)
b.Introduction, ok = doc.Find("meta[property=\"og:description\"]").First().Attr("content")
if !ok {
err = fmt.Errorf("'og:description' meta tag not found")
return
}
b.Introduction = strings.TrimSpace(b.Introduction)

// Count
infoElem.Find("style").EachWithBreak(func(i int, s *goquery.Selection) bool {
var parent = s.Parent()
var c string
c, err = deobfuscate(parent)
if err != nil {
return false
}
c += parent.Next().Text()
infoElem.Find(".intro + p > cite").EachWithBreak(func(i int, s *goquery.Selection) bool {
var c = s.Prev().AddSelection(s).Text()
if strings.HasSuffix(c, "字") {
b.WordCount, err = ParseCount(c[:len(c)-len("字")])
if err != nil {
Expand All @@ -152,12 +167,6 @@ func (b *Book) Fetch(ctx context.Context) (err error) {
return err
}

// LastUpdated
b.LastUpdated, err = ParseTime(stateElem.Find(".update .time").First().Text())
if err != nil {
return err
}

// MonthTicket
monthlyTickerEl := doc.Find("#monthCount")
if monthlyTickerEl.Length() > 0 {
Expand Down
2 changes: 2 additions & 0 deletions pkg/book/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ func parseTimeAt(s string, t time.Time) (ret time.Time, err error) {
0,
t.Location(),
)
case len(s) == 19:
ret, err = time.ParseInLocation("2006-01-02 15:04:05", s, TZ)
default:
ret, err = time.ParseInLocation("2006-01-02 15:04", s, TZ)
}
Expand Down

0 comments on commit c0291a1

Please sign in to comment.