-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
105 lines (90 loc) · 2.22 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
package main
import (
"flag"
"log"
"time"
"github.com/cheggaaa/pb/v3"
)
type Downloader struct {
Client *CachedHttpClient
OutputDir string
Url string
Delay time.Duration
}
func NewDownloader(url string, cacheDir string, outputDir string, delay int64) (*Downloader, error) {
client, err := NewCachedHttpClient(cacheDir)
if err != nil {
return nil, err
}
err = DirExists(outputDir, true)
if err != nil {
return nil, err
}
d := &Downloader {
Client: client,
Url: url,
OutputDir: outputDir,
Delay: time.Duration(delay) * time.Millisecond,
}
return d, nil
}
func main() {
// url := "https://www.webtoons.com/en/challenge/furi2play/list?title_no=172967"
var url string
flag.StringVar(&url, "u", "", "Specify chapter list page to download from")
var cacheDir string
flag.StringVar(&cacheDir, "c", "", "Specify directory to cache pages. Blank means no caching. You probably don't want to cache.")
var outputDir string
flag.StringVar(&outputDir, "o", "./output", "output directory")
var delay int64
flag.Int64Var(&delay, "d", 500, "delay between requests in ms")
flag.Parse()
if url == "" {
log.Fatal("Please specify a url!")
}
d, err := NewDownloader(url, cacheDir, outputDir, delay)
if err != nil {
log.Fatal(err)
}
log.Println("getting chapters...")
chapterUrls, err := d.GetChapterUrls()
if err != nil {
log.Fatal(err)
}
if len(chapterUrls) == 0 {
log.Fatal("number of retrieved chapters is 0, are you sure you input the correct url?")
}
log.Println("getting image urls...")
total := len(chapterUrls)
bar := pb.StartNew(total)
titles, imageUrls, err := d.GetImageUrls(chapterUrls, func(_ int) {
bar.Increment()
})
bar.Finish()
if err != nil {
log.Fatal(err)
}
for i, iu := range imageUrls {
if len(iu) == 0 {
log.Printf("number of images retrievable for chapter %s is 0", titles[i])
}
}
origins := make([]string, 0)
for _, cu := range chapterUrls {
origins = append(origins, cu[1])
}
log.Println("getting images...")
total = 0
for _, iu := range imageUrls {
total += len(iu)
}
bar = pb.StartNew(total)
err = d.GetChaptersImages(origins, titles, imageUrls, func(_ int) {
bar.Increment()
})
bar.Finish()
if err != nil {
log.Fatal(err)
}
log.Println("done!")
}