-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapter.go
172 lines (137 loc) · 3.31 KB
/
chapter.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"strings"
"sync"
"time"
)
type Chapter struct {
Result string `json:"result"`
BaseUrl string `json:"baseUrl"`
Chapter struct {
Hash string `json:"hash"`
Data []string `json:"data"`
DataSaver []string `json:"dataSaver"`
} `json:"chapter"`
}
type Image struct {
Url string
Path string
Idx int
}
func (c *ChapterDownloader) StartDownloading() {
maxWorkers := c.Query.Threads
var wg sync.WaitGroup
wg.Add(maxWorkers)
var chapter Chapter
var res *http.Response
var err error
for {
res, err = c.Client.Get(c.Url)
if err != nil {
fmt.Println(err.Error())
time.Sleep(time.Duration(3) * time.Second)
}
if res.StatusCode == 200 {
break
} else if res.StatusCode == 429 {
retryAfterStr := res.Header.Get("X-RateLimit-Retry-After")
retryAfterUnix, _ := strconv.ParseInt(retryAfterStr, 10, 64)
currentUnixTime := time.Now().Unix()
durationSeconds := retryAfterUnix - currentUnixTime
if durationSeconds > 0 {
fmt.Printf("Hit rate limit. Waiting %ds before continuing.\n", durationSeconds)
time.Sleep(time.Duration(durationSeconds) * time.Second)
}
} else {
fmt.Println(res.StatusCode, res.Status, err)
}
}
// fmt.Println(res.StatusCode)
if err != nil {
fmt.Println(err.Error())
}
json.NewDecoder(res.Body).Decode(&chapter)
res.Body.Close()
// fmt.Println(chapter)
jobs := make(chan Image)
results := make(chan error, len(chapter.Chapter.Data))
for i := 1; i <= maxWorkers; i++ {
go Worker(i, jobs, results, c.Client, &wg)
}
var coll []string
var ds string
if c.Query.ChapterQuery.DataSaver {
coll = chapter.Chapter.DataSaver
ds = "data-saver"
} else {
coll = chapter.Chapter.Data
ds = "data"
}
var pages []string
for _, p := range c.Query.ChapterQuery.Pages {
if p != "" {
pages = append(pages, p)
}
}
// fmt.Println(pages)
if len(pages) > 0 {
// fmt.Println("this is page array", pages)
var pagesSerialized, pagesUniq []int
for _, p := range pages {
if strings.Contains(p, "-") {
arr := strings.Split(p, "-")
var lb, ub int
if arr[0] == "" {
lb = 1
} else {
lb, _ = strconv.Atoi(arr[0])
}
if arr[1] == "" {
ub = len(coll)
} else {
ub, _ = strconv.Atoi(arr[1])
if ub > len(coll) {
ub = len(coll)
}
}
// fmt.Println(lb, ub)
for i := lb; i <= ub; i++ {
pagesSerialized = append(pagesSerialized, i)
}
} else {
i, _ := strconv.Atoi(p)
if i > len(coll) {
// fmt.Println("in continue block", i, p)
// fmt.Println(chapter.Chapter.Data, c.Url)
continue
}
pagesSerialized = append(pagesSerialized, i)
}
}
mp := make(map[int]bool)
for _, v := range pagesSerialized {
if _, ok := mp[v]; !ok {
mp[v] = true
pagesUniq = append(pagesUniq, v)
}
}
// fmt.Println(pagesSerialized, pagesUniq)
for _, i := range pagesUniq {
img := coll[i-1]
fullUrl := fmt.Sprintf("%s/%s/%s/%s", chapter.BaseUrl, ds, chapter.Chapter.Hash, img)
jobs <- Image{fullUrl, c.Query.ChapterQuery.Path, i - 1}
}
} else {
for i, img := range coll {
fullUrl := fmt.Sprintf("%s/%s/%s/%s", chapter.BaseUrl, ds, chapter.Chapter.Hash, img)
jobs <- Image{fullUrl, c.Query.ChapterQuery.Path, i}
}
}
close(jobs)
wg.Wait()
close(results)
}