-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
217 lines (177 loc) · 4.73 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package main
import (
"fmt"
"github.com/gocolly/colly"
"hentai/util"
"log"
"os"
"strconv"
"strings"
)
var (
logger util.ILogger
keywords *string
pageTotal *int
)
const (
outputDir = "./out/"
)
func init() {
logger = &util.Logger{}
}
func main() {
if flag, err := util.New().CheckDirExist(outputDir); !flag || err != nil {
log.Panicln("file dir check faild")
}
constantKeyword := "chinese"
keywords = &constantKeyword
searchKW := util.Scanf("Search Your Keywords(Enter to search):")
if searchKW != "" {
keywords = &searchKW
}
constantPageTotal := 1
pageTotal = &constantPageTotal
pageTotalStr := util.Scanf("Request Page Size Set:")
if pageTotalStr != "" {
totalNum, err := strconv.Atoi(pageTotalStr)
if err != nil {
log.Panicln("Please Input Shaping Number !")
}
pageTotal = &totalNum
}
util.New().ReadyGo(5)
chNum := *pageTotal
ch := make(chan int, chNum)
c := colly.NewCollector(
colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36"),
//colly.Debugger(&debug.LogDebugger{}),
)
go cheerioFirstListPage(c, ch)
i := 1
for {
if i >= *pageTotal {
break
}
go cheerioListPage(c, i, ch)
i++
}
cashPool := 0
for cashPool < chNum {
cashPool += <-ch
}
}
/**
请求根地址,列表页
*/
func cheerioFirstListPage(c *colly.Collector, ch chan int) {
c.OnRequest(func(r *colly.Request) {
util.New().SetHeader(r)
logger.Info("【Visiting】", r.URL.String())
})
// 获取每个list .gl3t a标签元素的详情链接
c.OnHTML(".gl3t a[href]", func(e *colly.HTMLElement) {
d := c.Clone()
requestDetailPage(d, e)
})
c.Visit("https://exhentai.org/?f_search=" + *keywords)
ch <- 1
}
func cheerioListPage(c *colly.Collector, pageIndex int, ch chan int) {
c.OnRequest(func(r *colly.Request) {
util.New().SetHeader(r)
logger.Info("【Visiting】", r.URL.String())
})
// 获取每个list .gl3t a标签元素的详情链接
c.OnHTML(".gl3t a[href]", func(e *colly.HTMLElement) {
d := c.Clone()
requestDetailPage(d, e)
})
c.Visit(fmt.Sprintf("https://exhentai.org/?page=%d&f_search=%s", pageIndex, *keywords))
ch <- 1
}
/**
列表各个详情页访问
*/
func requestDetailPage(c *colly.Collector, cheeioEl *colly.HTMLElement) {
var title *string
link := cheeioEl.Attr("href")
c.OnRequest(func(dr *colly.Request) {
util.New().SetHeader(dr)
logger.Normal("【Detail Visiting】: ", dr.URL.String())
})
// 获取标题名称创建漫画文件夹
c.OnHTML("#gn", func(de *colly.HTMLElement) {
title = &de.Text
groupDir := fmt.Sprintf("%s%s", outputDir, *title)
_ = os.Mkdir(groupDir, os.ModePerm)
logger.Normal("【detail title】: ", de.Text)
})
// 获取详情第一页图片链接
c.OnHTML(".gdtl:first-child > a[href]", func(dde *colly.HTMLElement) {
d := c.Clone()
mapImageForHentai(d, dde, title)
})
logger.Complate("End Request")
c.Visit(cheeioEl.Request.AbsoluteURL(link))
}
func mapImageForHentai(c *colly.Collector, detailEl *colly.HTMLElement, title *string) {
var num *int
index := 2
link := detailEl.Attr("href")
c.OnRequest(func(mr *colly.Request) {
util.New().SetHeader(mr)
})
c.OnHTML("#i2 .sn span:last-child", func(e *colly.HTMLElement) {
atoiNum, _ := strconv.Atoi(e.Text)
num = &atoiNum
})
// 获取图片存储图片
c.OnHTML("#i3 img", func(e *colly.HTMLElement) {
d := c.Clone()
reduceImage(d, e, title)
})
c.OnHTML("#i3 > a[href]", func(e *colly.HTMLElement) {
next := e.Attr("href")
logger.Info("【HEAD】Start Image Download!!", next)
d := c.Clone()
nextImageGiveMe(d, e, title, &index, *num)
})
c.Visit(detailEl.Request.AbsoluteURL(link))
}
func nextImageGiveMe(c *colly.Collector, mapEl *colly.HTMLElement, title *string, nextIndex *int, total int) {
if *nextIndex == total {
logger.Underline("=======================> total ", total)
}
if *nextIndex <= total {
next := mapEl.Attr("href")
c.OnRequest(func(mr *colly.Request) {
util.New().SetHeader(mr)
})
// 获取图片存储图片
c.OnHTML("#i3 img", func(e *colly.HTMLElement) {
d := c.Clone()
reduceImage(d, e, title)
})
c.OnHTML("#i3 > a[href]", func(e *colly.HTMLElement) {
next := e.Attr("href")
logger.Info("【Next Image Page】: ", next)
d := c.Clone()
*nextIndex++
nextImageGiveMe(d, e, title, nextIndex, total)
})
c.Visit(mapEl.Request.AbsoluteURL(next))
}
}
func reduceImage(c *colly.Collector, mapEl *colly.HTMLElement, title *string) {
link := mapEl.Attr("src")
c.OnRequest(func(rr *colly.Request) {
util.New().SetHeader(rr)
})
c.OnResponse(func(r *colly.Response) {
if strings.Index(r.Headers.Get("Content-Type"), "image") > -1 {
r.Save(fmt.Sprintf("%s%s/", outputDir, *title) + r.FileName())
return
}
})
c.Visit(link)
}