-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
339 lines (277 loc) · 8.37 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
package main
import (
"crypto/tls"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/logrusorgru/aurora"
"golang.org/x/net/proxy"
"gopkg.in/matryer/try.v1"
)
type Post struct {
PreviewURL string `json:"preview_url"`
SampleURL string `json:"sample_url"`
FileURL string `json:"file_url"`
Directory int `json:"directory"`
Hash string `json:"hash"`
Width int `json:"width"`
Height int `json:"height"`
ID int `json:"id"`
Image string `json:"image"`
Status string `json:"status"`
HasNotes bool `json:"has_notes"`
CommentCount int `json:"comment_count"`
}
// SetProxy
// set http proxy in the format `http://HOST:PORT`
// set socket proxy in the format `socks5://HOST:PORT`
func SetProxy(proxyAddr string, timeout int) (http.Transport, error) {
if strings.HasPrefix(proxyAddr, "http") {
urlproxy, err := url.Parse(proxyAddr)
if err != nil {
return http.Transport{}, err
}
transport := &http.Transport{
Proxy: http.ProxyURL(urlproxy),
TLSNextProto: make(map[string]func(authority string, c *tls.Conn) http.RoundTripper),
DialContext: (&net.Dialer{
Timeout: time.Duration(timeout) * time.Second,
}).DialContext,
}
return *transport, nil
}
if strings.HasPrefix(proxyAddr, "socks5") {
baseDialer := &net.Dialer{
Timeout: time.Duration(timeout) * time.Second,
KeepAlive: time.Duration(timeout) * time.Second,
}
socksHostPort := strings.ReplaceAll(proxyAddr, "socks5://", "")
dialSocksProxy, err := proxy.SOCKS5("tcp", socksHostPort, nil, baseDialer)
if err != nil {
return http.Transport{}, errors.New("error creating socks5 proxy :" + err.Error())
}
if contextDialer, ok := dialSocksProxy.(proxy.ContextDialer); ok {
dialContext := contextDialer.DialContext
transport := &http.Transport{
DialContext: dialContext,
}
return *transport, nil
} else {
return http.Transport{}, errors.New("failed type assertion to DialContext")
}
}
return http.Transport{}, errors.New("only support http(s) or socks5 protocol")
}
func GetPosts(tags string, page int, limit int, transport http.Transport) ([]Post, error) {
client := &http.Client{Transport: &transport}
posts := []Post{}
req, _ := http.NewRequest("GET", "https://api.rule34.xxx/index.php", nil)
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:6.1) Gecko/20100101 Firefox/6.1.9")
qs := req.URL.Query()
qs.Add("page", "dapi")
qs.Add("s", "post")
qs.Add("q", "index")
qs.Add("json", "1")
qs.Add("tags", tags)
qs.Add("limit", strconv.Itoa(limit))
qs.Add("pid", strconv.Itoa(page))
req.URL.RawQuery = qs.Encode()
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = json.Unmarshal(body, &posts)
if err != nil {
return nil, err
}
return posts, nil
}
// workState stores the state of all the jobs and
// is shared across workers
type workState struct {
Total int
Completed int
Successes int
Failures int
SaveDirectory string
}
// BeginDownload takes a slice of posts, a directory to save them in, and a
// number of concurrent workers to make. It blocks until all the post have
// been processed. It returns the number of successes, failures, and the total
// amount of posts.
func BeginDownload(posts *[]Post, saveDirectory *string, maxConcurrents *int, transport http.Transport) (*int, *int, *int) {
// Channel for main goroutine to give workers a post when they are done downloading one
wc := make(chan *Post)
var current int
total := len(*posts)
state := workState{
Total: total,
SaveDirectory: *saveDirectory,
}
// If we have more workers than posts, then we don't need all of them
if *maxConcurrents > total {
*maxConcurrents = total
}
for i := 0; i < *maxConcurrents; i++ {
// Create our workers
go work(i+1, &state, wc, transport)
// Give them their initial posts
wc <- &(*posts)[current]
current++
time.Sleep(time.Millisecond * 50)
}
for {
// Wait for a worker to be done (they send nil to wc)
<-wc
// If we finished downloading all posts, break out of the loop
if state.Successes+state.Failures == total {
break
}
// If there's no more posts to give, stop the worker
if current >= total {
wc <- nil
continue
}
// Give the worker the next post in the array
wc <- &(*posts)[current]
current++
}
return &state.Successes, &state.Failures, &total
}
func work(wn int, state *workState, wc chan *Post, transport http.Transport) {
for {
state.Completed++
// Wait for a post from main
post := <-wc
if post == nil { // nil means there aren't any more posts, so we're OK to break
return
}
progress := aurora.Sprintf(aurora.Green("[%d/%d]"), state.Completed, state.Total)
workerText := aurora.Sprintf(aurora.Cyan("[w%d]"), wn)
err := try.Do(func(attempt int) (retry bool, err error) {
if attempt == 1 {
fmt.Println(aurora.Sprintf(
"%s %s Downloading post %d -> %s",
progress,
workerText,
post.ID,
getSavePath(post, &state.SaveDirectory),
))
} else {
fmt.Println(aurora.Sprintf(
"%s %s (%d) Retry Download post %d -> %s",
progress,
workerText,
attempt,
post.ID,
getSavePath(post, &state.SaveDirectory),
))
}
err = downloadPost(post, state.SaveDirectory, transport)
return attempt < 5, err
})
if err != nil {
fmt.Printf("[w%d] Failed to download post %d: %v\n", wn, post.ID, err)
state.Failures++
} else {
state.Successes++
}
// Signal to main goroutine that we are done with this download
wc <- nil
}
}
func getSavePath(post *Post, directory *string) string {
savePath := path.Join(*directory, strconv.Itoa(post.ID)+path.Ext(post.FileURL))
return savePath
}
func downloadPost(post *Post, directory string, transport http.Transport) error {
savePath := getSavePath(post, &directory)
if _, err := os.Stat(savePath); err == nil {
fmt.Print("File exists, skip...\n")
} else {
resp, err := HTTPGet(post.FileURL, transport)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
err = ioutil.WriteFile(savePath, body, 0755)
if err != nil {
return err
}
}
return nil
}
// HTTPGet is a helper function that automatically adds the
// tool's UA to an HTTP GET request
func HTTPGet(url string, transport http.Transport) (*http.Response, error) {
client := &http.Client{Transport: &transport}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.3; Win64; x64; rv:6.1) Gecko/20100101 Firefox/6.1.9")
return client.Do(req)
}
func main() {
tags := flag.String("tags", "", "Tags to search for")
maxConcurrents := flag.Int("concurrents", 30, "Maximum amount of concurrent downloads")
saveDirectory := flag.String("out", "dl", "The directory to write the downloaded posts to")
postLimit := flag.Int("limit", 9999999999, "Maximum amount of posts to grab from rule34")
proxyAddr := flag.String("proxy", "", "Proxy address to parsing")
timeout := flag.Int("timeout", 10, "Timeout proxy to parsing")
flag.Parse()
transport, err := SetProxy(*proxyAddr, *timeout)
if err != nil {
fmt.Println("[WARN] Proxy is not using or proxy error!")
}
allPosts := []Post{}
nlimit := (*postLimit)
i := 0
for {
fmt.Printf("Fetching page %d...", i)
currentPostsCount := 100
nlimit = nlimit - currentPostsCount
if nlimit <= 0 {
currentPostsCount += nlimit
}
posts, err := GetPosts(*tags, i, currentPostsCount, transport)
if err != nil {
panic(err)
}
fmt.Printf(" fetched %d posts\n", len(posts))
if len(posts) == 0 {
break
}
allPosts = append(allPosts, posts...)
if nlimit <= 0 {
break
}
i++
}
fmt.Printf("Found %d posts. Starting download with %d workers...\n\n", len(allPosts), *maxConcurrents)
cwd, _ := os.Getwd()
absSaveDir := path.Join(cwd, *saveDirectory)
err = os.MkdirAll(absSaveDir, 0755)
if err != nil {
fmt.Printf("Cannot create output directory (%s). Do you have the right permissions?\n", absSaveDir)
os.Exit(1)
}
successes, failures, _ := BeginDownload(&allPosts, saveDirectory, maxConcurrents, transport)
fmt.Printf("\nAll done! %d posts downloaded and saved. (%d failed to download)\n", *successes, *failures)
}