/
iviag.go
477 lines (448 loc) · 11.8 KB
/
iviag.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
package main
import (
"bytes"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/organization/cloudflare-bypass"
)
const (
MangaPrefix = "http://marumaru.in/b/manga/"
ArchivePrefix = "http://www.shencomics.com/archives/"
UserAgent = "Opera/12.02 (Android 4.1; Linux; Opera Mobi/ADR-1111101157; U; en-US) Presto/2.9.201 Version/12.02" // Opera Mobile 12.02
MaxTries uint32 = 5
)
var (
workerID, fetchID uint32
Cookie = http.Cookie{}
CookieLock = new(sync.RWMutex)
Parsed = make(map[uint64]bool)
DupLock = new(sync.Mutex)
Filter = regexp.MustCompile("[\\[\\]\\:\\s<>\\=\\|\\+]+").ReplaceAllString
)
func main() {
maxPics := flag.Int64("max", -1, "Max count of pics to download (not implemented)")
downloaders := flag.Uint64("dl", 1, "Number of simultaneous manga downloader")
workers := flag.Uint64("worker", 3, "Number of simultaneous archive worker per downloader")
fetchers := flag.Uint64("fetch", 4, "Number of simultaneous image fetcher per worker")
flag.Parse()
mangas := flag.Args()
targets := make(chan uint64, len(mangas))
logfile, err := os.OpenFile("log.txt", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
return
}
defer logfile.Close()
logfile.WriteString(fmt.Sprintf(`======================================================
Fetch started at %v
======================================================
Targets supplied: %v
`, time.Now(), mangas))
log.SetOutput(io.MultiWriter(os.Stdout, logfile))
for _, manga := range mangas {
m, err := strconv.ParseUint(manga, 10, 64)
if err != nil {
log.Fatal(err)
}
log.Println("Adding download target:", m)
targets <- m
}
if len(mangas) == 0 {
fmt.Println("Please input space-seperated manga ID.")
fmt.Println("Manga ID: http://marumaru.in/b/manga/{ID}")
os.Exit(1)
}
if *fetchers == 0 || *workers == 0 || *maxPics == 0 || *downloaders == 0 {
fmt.Println("Invalid argument supplied")
os.Exit(1)
}
doc, err := goquery.NewDocument(MangaPrefix)
if err == nil && doc.Find("title").First().Text()[:7] == "You are" {
UpdateCookie(doc)
}
wg := new(sync.WaitGroup)
wg.Add(int(*downloaders))
for i := uint64(0); i < *downloaders; i++ {
dl := new(Downloader)
dl.Init(*workers, *fetchers, *maxPics)
go dl.Start(targets, wg)
}
close(targets)
wg.Wait()
log.Print("All tasks were done.")
}
func Get(url string) (string, error) { // From http://stackoverflow.com/questions/11692860/how-can-i-efficiently-download-a-large-file-using-go
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", fmt.Errorf("Request creation error: %s", err.Error())
}
req.Header.Set("User-Agent", UserAgent)
resp, err := new(http.Client).Do(req)
if err != nil {
return "", fmt.Errorf("HTTP request error: %s", err.Error())
}
defer resp.Body.Close()
buf := new(bytes.Buffer)
_, err = io.Copy(buf, resp.Body)
if err != nil {
return "", fmt.Errorf("Buffer copy error: %s", err.Error())
}
return string(buf.Bytes()), nil
}
type Archive struct {
ID uint64
Seq uint64
Title string
Subject string
Wait *sync.WaitGroup
}
func GetArchives(manga uint64) (title string, mangas []Archive, err error) {
req, err := http.NewRequest("GET", MangaPrefix+strconv.FormatUint(manga, 10), nil)
if err != nil {
return "", nil, fmt.Errorf("Request creation error: %s", err.Error())
}
req.Header.Set("User-Agent", UserAgent)
func() {
CookieLock.RLock()
defer CookieLock.RUnlock()
req.AddCookie(&Cookie)
}()
resp, err := new(http.Client).Do(req)
if err != nil {
return "", nil, fmt.Errorf("HTTP request error: %s", err.Error())
}
defer resp.Body.Close()
buf := new(bytes.Buffer)
io.Copy(buf, resp.Body)
var doc *goquery.Document
doc, err = goquery.NewDocumentFromReader(buf)
if err != nil {
return
}
if doc.Find("title").First().Text()[:7] == "You are" {
UpdateCookie(doc)
return GetArchives(manga)
}
links := make([]Archive, 0)
seq := uint64(1)
title = Filter(doc.Find("div .subject").Find("h1").Text(), "_")
if title == "" {
title = Filter(strings.Replace(doc.Find("title").First().Text(), " | SHENCOMICS", "", 1), "_")
if title == "" {
title = "Untitled"
}
}
needFix := make(map[uint64]*struct {
needs []int
fix int
})
doc.Find("div .content").Children().Find("a").Each(func(i int, s *goquery.Selection) {
if l, ok := s.Attr("href"); ok && strings.Index(l, ArchivePrefix) != -1 {
if link, err := strconv.ParseUint(l[strings.Index(l, ArchivePrefix)+len(ArchivePrefix):], 10, 64); err == nil {
sub := regexp.MustCompile("^[\xc2\xa0 \\t]+").ReplaceAllString(s.Text(), "")
if sub == "" {
for _, l := range links {
if l.ID == link {
return
}
}
sub = "Untitled"
if _, ok := needFix[link]; ok {
needFix[link].needs = append(needFix[link].needs, len(links))
} else {
needFix[link] = &struct {
needs []int
fix int
}{
[]int{len(links)},
-1,
}
}
} else {
if index, ok := needFix[link]; ok && index.fix < 0 {
needFix[link].fix = len(links)
}
}
links = append(links, Archive{
ID: link,
Seq: seq,
Title: fmt.Sprintf("%s(%d)", title, manga),
Subject: sub,
})
seq++
}
}
})
for _, st := range needFix {
if st.fix > 0 {
for _, need := range st.needs {
links[need].Subject = links[st.fix].Subject
}
}
}
return title, links, nil
}
func GetPics(archive uint64) (rawhtml string, urls []string, err error) {
req, err := http.NewRequest("GET", ArchivePrefix+strconv.FormatUint(archive, 10), nil)
if err != nil {
return "", nil, fmt.Errorf("Request creation error: %s", err.Error())
}
req.Header.Set("User-Agent", UserAgent)
func() {
CookieLock.RLock()
defer CookieLock.RUnlock()
req.AddCookie(&Cookie)
}()
resp, err := new(http.Client).Do(req)
if err != nil {
return "", nil, fmt.Errorf("HTTP request error: %s", err.Error())
}
defer resp.Body.Close()
buf := new(bytes.Buffer)
io.Copy(buf, resp.Body)
rawhtml = buf.String()
var doc *goquery.Document
doc, err = goquery.NewDocumentFromReader(buf)
if err != nil {
return
}
if doc.Find("title").First().Text()[:7] == "You are" {
UpdateCookie(doc)
return GetPics(archive)
}
urls = make([]string, 0)
doc.Find("div .entry-content").Find("img").Each(func(i int, s *goquery.Selection) {
if img, ok := s.Attr("data-lazy-src"); ok {
urls = append(urls, img)
}
})
return
}
func UpdateCookie(doc *goquery.Document) {
s := cfbypass.DecodeScript(doc)
key := cfbypass.GetCookieKey(s[1])
val := cfbypass.GetCookieValue(s[0])
c := http.Cookie{}
c.Name = key
c.Value = val
CookieLock.Lock()
defer CookieLock.Unlock()
Cookie = c
log.Printf("Sucuri proxy key update: %s=%s", c.Name, c.Value)
}
type Downloader struct {
Workers []*Worker
Wait *sync.WaitGroup
workers uint64
fetchers uint64
maxPics int64
target chan<- Archive
report <-chan Status
}
func (d *Downloader) Init(workers, fetchers uint64, maxPics int64) {
d.workers, d.fetchers, d.maxPics = workers, fetchers, maxPics
d.Workers = make([]*Worker, workers)
d.Wait = new(sync.WaitGroup)
tc, rc := make(chan Archive, 1), make(chan Status, 30)
for i := range d.Workers {
d.Workers[i] = NewWorker(fetchers, tc, rc)
}
d.target = tc
d.report = rc
go func() {
for r := range d.report {
log.Print(r)
}
}()
}
func (d *Downloader) Start(mangas <-chan uint64, wg *sync.WaitGroup) {
for manga := range mangas {
log.Printf("Parsing archive list: %s%d", MangaPrefix, manga)
_, links, err := GetArchives(manga)
if err != nil {
log.Printf("Archive list parse error: %s", err.Error())
continue
}
func() {
DupLock.Lock()
defer DupLock.Unlock()
for _, link := range links {
if _, ok := Parsed[link.ID]; !ok {
link.Wait = d.Wait
d.target <- link
d.Wait.Add(1)
Parsed[link.ID] = true
}
}
}()
}
d.Wait.Wait()
log.Println("Download done. exiting")
wg.Done()
}
type Worker struct {
ID uint32
Fetchers []*Fetcher
Targets <-chan Archive
Report chan<- Status
FRequest chan FetchRequest
FResult chan FetchResult
}
func NewWorker(fetchers uint64, tc <-chan Archive, rc chan<- Status) *Worker {
w := new(Worker)
w.ID = atomic.AddUint32(&workerID, 1)
w.Targets, w.Report = tc, rc
w.Fetchers = make([]*Fetcher, fetchers)
w.FRequest = make(chan FetchRequest, 30)
w.FResult = make(chan FetchResult, 30*fetchers)
for i := range w.Fetchers {
f := new(Fetcher)
f.ID = atomic.AddUint32(&fetchID, 1)
f.Request = w.FRequest
f.Result = w.FResult
w.Fetchers[i] = f
go w.Fetchers[i].fetch()
}
go w.Watch()
go func() {
for result := range w.FResult {
if !result.Ok {
log.Printf("Fetch error: %s", result.Description)
}
}
}()
return w
}
func (w Worker) Watch() {
for target := range w.Targets {
dir := target.Title + "/" + Filter(target.Subject, "_")
os.MkdirAll(dir, os.ModeDir)
if _, err := os.Stat(dir + "/raw.html"); err == nil {
log.Printf("Archive %d(%s) already exists in local: Skipping.", target.ID, target.Subject)
target.Wait.Done()
continue
}
log.Printf("Parsing archive: %d(%s)", target.ID, target.Subject)
raw, imgs, err := GetPics(target.ID)
if err != nil {
log.Printf("Archive parse error: %s", err.Error())
target.Wait.Done()
continue
}
log.Printf("Archive parse done for: %d(%s)", target.ID, target.Subject)
var file *os.File
afterFetch := func() func() {
file, err = os.Create(dir + "/raw.html")
if err == nil {
return func() {
defer file.Close()
file.WriteString(raw)
}
} else {
return func() {}
}
}()
wg := new(sync.WaitGroup)
wg.Add(len(imgs))
for i, img := range imgs {
w.FRequest <- FetchRequest{
URL: img,
FileDir: target.Title + "/" + Filter(target.Subject+"/"+strconv.Itoa(i)+".jpg", "_"),
Wait: wg,
}
}
log.Printf("Archive fetch request for %d(%s) sent. Waiting now.", target.ID, target.Subject)
wg.Wait()
files, err := filepath.Glob(dir + "/*.jpg")
if err != nil {
files = make([]string, 0)
}
target.Wait.Done()
afterFetch()
log.Printf("Archive fetch complete for %d(%s). Pics count: %d", target.ID, target.Subject, len(files))
}
}
func (w Worker) Download(manga uint64) { //TODO: Parse contents here and fetch them
}
type Fetcher struct {
ID uint32
Request <-chan FetchRequest
Result chan<- FetchResult
Report chan<- Status
}
func (f Fetcher) fetch() {
for req := range f.Request {
// log.Printf("Fetch start: %s -> %s", req.URL, req.FileDir)
func(req FetchRequest) {
defer req.Wait.Done()
file, err := os.Create(req.FileDir)
if err != nil {
f.Result <- FetchResult{
Ok: false,
Description: "Could not create file: " + err.Error(),
}
return
}
defer file.Close()
var s string
for tries := uint32(0); ; tries++ {
s, err = Get(req.URL)
if err != nil {
if tries >= MaxTries {
f.Result <- FetchResult{
Ok: false,
Description: err.Error(),
}
} else {
log.Printf("Fetch error: %s (retry count %d)", err.Error(), tries)
}
} else {
break
}
}
_, err = file.WriteString(s)
/*
f.Result <- FetchResult{
Ok: true,
Description: strconv.FormatInt(int64(n), 10) + " bytes were written",
}
*/
// log.Printf("Fetch to %s succeeded: %d bytes were written", req.FileDir, n)
}(req)
}
}
const (
FetchStart StatusID = iota
FetchComplete
FetchError
PicFetchStart
PicFetchComplete
PicFetchError
ParseStart
ParseError
)
type StatusID byte
type Status struct {
ID StatusID
Data string
}
type FetchRequest struct {
URL string
FileDir string
Wait *sync.WaitGroup
}
type FetchResult struct {
Ok bool
Description string
}