-
Notifications
You must be signed in to change notification settings - Fork 1
/
nettruyen.go
64 lines (52 loc) · 1.19 KB
/
nettruyen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package nettruyen
import (
"fmt"
"strings"
"github.com/gocolly/colly"
"github.com/vqhuy/kindle-manga/bot"
)
func init() {
bot.Register(&collector{
Bot: &bot.Bot{},
})
}
type collector struct {
*bot.Bot
}
var _ bot.Collector = (*collector)(nil)
func (b *collector) Page() string {
// now renamed to nhattruyen
return "http://nhattruyen.com"
}
func (b *collector) GetLink(base string, chap int) string {
// should not use the base colly's collector
chapstr := fmt.Sprintf("Chapter %d", chap)
c := colly.NewCollector()
var link string
c.OnHTML(`#nt_listchapter li div a`, func(e *colly.HTMLElement) {
s := e.DOM
href, _ := s.Attr("href")
title, _ := s.Html()
if strings.Contains(title, chapstr) {
link = href
}
})
c.Visit(base)
return link
}
func (b *collector) Collect(base string, chap int, outputDir string) error {
b.Colly = colly.NewCollector()
b.Colly.OnHTML(`div.reading-detail`, func(e *colly.HTMLElement) {
e.ForEach("img", func(i int, ee *colly.HTMLElement) {
link := ee.Attr("src")
b.Visit(link)
})
})
b.Bot.Collect(base, chap, outputDir)
link := b.GetLink(base, chap)
if link == "" {
return bot.ErrorChapNotFound
}
b.Colly.Visit(link)
return nil
}