-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
401 lines (366 loc) · 10.5 KB
/
api.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
package fantia
import (
"context"
"errors"
"fmt"
"net/http"
"strconv"
"sync"
"github.com/KJHJason/Cultured-Downloader-Logic/api"
"github.com/KJHJason/Cultured-Downloader-Logic/constants"
cdlerrors "github.com/KJHJason/Cultured-Downloader-Logic/errors"
"github.com/KJHJason/Cultured-Downloader-Logic/httpfuncs"
"github.com/KJHJason/Cultured-Downloader-Logic/logger"
"github.com/KJHJason/Cultured-Downloader-Logic/progress"
"github.com/PuerkitoBio/goquery"
)
type fantiaPostArgs struct {
msgSuffix string
postId string
url string
}
func getFantiaPostDetails(postArg *fantiaPostArgs, dlOptions *FantiaDlOptions) (*http.Response, error) {
// Now that we have the post ID, we can query Fantia's API
// to get the post's contents from the JSON response.
progress := dlOptions.MainProgBar
progress.SetToSpinner()
progress.UpdateBaseMsg(
fmt.Sprintf(
"Getting post %s's contents from Fantia %s...",
postArg.postId,
postArg.msgSuffix,
),
)
progress.UpdateSuccessMsg(
fmt.Sprintf(
"Finished getting post %s's contents from Fantia %s!",
postArg.postId,
postArg.msgSuffix,
),
)
progress.UpdateErrorMsg(
fmt.Sprintf(
"Something went wrong while getting post %s's cotents from Fantia %s.\nPlease refer to the logs for more details.",
postArg.postId,
postArg.msgSuffix,
),
)
progress.Start()
defer progress.SnapshotTask()
postApiUrl := postArg.url + postArg.postId
header := map[string]string{
"Referer": fmt.Sprintf("%s/posts/%s", constants.FANTIA_URL, postArg.postId),
"X-Csrf-Token": dlOptions.CsrfToken,
"X-Requested-With": "XMLHttpRequest",
}
useHttp3 := httpfuncs.IsHttp3Supported(constants.FANTIA, true)
res, err := httpfuncs.CallRequest(
&httpfuncs.RequestArgs{
Method: "GET",
Url: postApiUrl,
Cookies: dlOptions.SessionCookies,
Headers: header,
Http2: !useHttp3,
Http3: useHttp3,
UserAgent: dlOptions.Configs.UserAgent,
Context: dlOptions.GetContext(),
},
)
if err != nil || res.StatusCode != 200 {
errCode := cdlerrors.CONNECTION_ERROR
if err == nil {
errCode = res.StatusCode
}
errMsg := fmt.Sprintf(
"fantia error %d: failed to get post details for %s",
errCode,
postApiUrl,
)
if err != nil {
err = fmt.Errorf(
"%s, more info => %w",
errMsg,
err,
)
} else {
err = errors.New(errMsg)
}
progress.Stop(true)
return nil, err
}
progress.Stop(false)
return res, nil
}
func DlFantiaPost(count, maxCount int, postId string, dlOptions *FantiaDlOptions) (cancelled bool, gdriveUrls []*httpfuncs.ToDownload, errSlice []error) {
msgSuffix := fmt.Sprintf(
"[%d/%d]",
count,
maxCount,
)
res, err := getFantiaPostDetails(
&fantiaPostArgs{
msgSuffix: msgSuffix,
postId: postId,
url: constants.FANTIA_POST_API_URL,
},
dlOptions,
)
if err != nil {
return false, nil, []error{err}
}
urlsToDownload, postGdriveUrls, err := processIllustDetailApiRes(
&processIllustArgs{
res: res,
postId: postId,
postIdsLen: maxCount,
msgSuffix: msgSuffix,
},
dlOptions,
)
if errors.Is(err, cdlerrors.ErrRecaptcha) {
err = SolveCaptcha(dlOptions)
if err != nil {
// stop the download if the captcha auto-solving fails
dlOptions.CancelCtx()
return false, nil, []error{err}
}
return DlFantiaPost(count, maxCount, postId, dlOptions)
} else if err != nil {
return false, nil, []error{err}
}
// Download the urls
cancelled, errorSlice := httpfuncs.DownloadUrls(
urlsToDownload,
&httpfuncs.DlOptions{
Context: dlOptions.GetContext(),
MaxConcurrency: constants.FANTIA_MAX_CONCURRENT,
Headers: nil,
Cookies: dlOptions.SessionCookies,
UseHttp3: false,
HeadReqTimeout: constants.DEFAULT_HEAD_REQ_TIMEOUT,
SupportRange: constants.FANTIA_RANGE_SUPPORTED,
ProgressBarInfo: &progress.ProgressBarInfo{
MainProgressBar: dlOptions.MainProgBar,
DownloadProgressBars: dlOptions.DownloadProgressBars,
},
},
dlOptions.Configs,
)
if cancelled {
dlOptions.CancelCtx()
return true, nil, errorSlice
}
return false, postGdriveUrls, nil
}
// Query Fantia's API based on the slice of post IDs and get a map of urls to download from.
//
// Note that only the downloading of the URL(s) is/are executed concurrently
// to reduce the chance of the signed AWS S3 URL(s) from expiring before the download is
// executed or completed due to a download queue to avoid resource exhaustion of the user's system.
func (f *FantiaDl) DlFantiaPosts(dlOptions *FantiaDlOptions) ([]*httpfuncs.ToDownload, []error) {
var errSlice []error
var gdriveLinks []*httpfuncs.ToDownload
postIdsLen := len(f.PostIds)
for i, postId := range f.PostIds {
cancelled, postGdriveLinks, err := DlFantiaPost(i+1, postIdsLen, postId, dlOptions)
if len(err) > 0 {
if cancelled {
return nil, nil
}
errSlice = append(errSlice, err...)
continue
}
if len(postGdriveLinks) > 0 {
gdriveLinks = append(gdriveLinks, postGdriveLinks...)
}
}
if len(errSlice) > 0 {
logger.LogErrors(false, logger.ERROR, errSlice...)
}
return gdriveLinks, errSlice
}
// Parse the HTML response from the creator's page to get the post IDs.
func parseCreatorHtml(res *http.Response, creatorId string) ([]string, error) {
// parse the response
doc, err := goquery.NewDocumentFromReader(res.Body)
res.Body.Close()
if err != nil {
err = fmt.Errorf(
"fantia error %d, failed to parse response body when getting posts for Fantia Fanclub %s, more info => %w",
cdlerrors.HTML_ERROR,
creatorId,
err,
)
return nil, err
}
// get the post ids similar to using the xpath of //a[@class='link-block']
hasHtmlErr := false
var postIds []string
doc.Find("a.link-block").Each(func(i int, s *goquery.Selection) {
if href, exists := s.Attr("href"); exists {
postIds = append(postIds, httpfuncs.GetLastPartOfUrl(href))
} else if !hasHtmlErr {
hasHtmlErr = true
}
})
if hasHtmlErr {
return nil, fmt.Errorf(
"fantia error %d, failed to get href attribute for Fantia Fanclub %s, please report this issue",
cdlerrors.HTML_ERROR,
creatorId,
)
}
return postIds, nil
}
// Get all the creator's posts by using goquery to parse the HTML response to get the post IDs
func getCreatorPosts(creatorId, pageNum string, dlOptions *FantiaDlOptions) ([]string, error) {
var postIds []string
minPage, maxPage, hasMax, err := api.GetMinMaxFromStr(pageNum)
if err != nil {
return nil, err
}
useHttp3 := httpfuncs.IsHttp3Supported(constants.FANTIA, false)
curPage := minPage
for {
url := fmt.Sprintf("%s/fanclubs/%s/posts", constants.FANTIA_URL, creatorId)
params := map[string]string{
"page": strconv.Itoa(curPage),
"q[s]": "newer",
"q[tag]": "",
}
// note that even if the max page is more than
// the actual number of pages, the response will still be 200 OK.
res, err := httpfuncs.CallRequest(
&httpfuncs.RequestArgs{
Method: "GET",
Url: url,
Cookies: dlOptions.SessionCookies,
Params: params,
Http2: !useHttp3,
Http3: useHttp3,
CheckStatus: true,
UserAgent: dlOptions.Configs.UserAgent,
Context: dlOptions.GetContext(),
},
)
if err != nil {
if err != context.Canceled {
err = fmt.Errorf(
"fantia error %d: failed to get creator's pages for %s, more info => %w",
cdlerrors.CONNECTION_ERROR,
url,
err,
)
}
return nil, err
}
creatorPostIds, err := parseCreatorHtml(res, creatorId)
if err != nil {
return nil, err
}
postIds = append(postIds, creatorPostIds...)
// if there are no more posts, break
if len(creatorPostIds) == 0 || (hasMax && curPage >= maxPage) {
break
}
curPage++
}
return postIds, nil
}
// Retrieves all the posts based on the slice of creator IDs and updates its PostIds slice
func (f *FantiaDl) GetCreatorsPosts(dlOptions *FantiaDlOptions) []error {
creatorIdsLen := len(f.FanclubIds)
if creatorIdsLen == 0 {
return nil
}
if creatorIdsLen != len(f.FanclubPageNums) {
return []error{
fmt.Errorf(
"fantia error %d: creator IDs and page numbers slices are not the same length",
cdlerrors.DEV_ERROR,
),
}
}
var wg sync.WaitGroup
maxConcurrency := constants.FANTIA_MAX_CONCURRENT
if creatorIdsLen < maxConcurrency {
maxConcurrency = creatorIdsLen
}
queue := make(chan struct{}, maxConcurrency)
resChan := make(chan []string, creatorIdsLen)
errChan := make(chan error, creatorIdsLen)
progress := dlOptions.MainProgBar
if creatorIdsLen > 1 {
baseMsg := "Getting post ID(s) from Fanclubs(s) on Fantia [%d/" + fmt.Sprintf("%d]...", creatorIdsLen)
progress.SetToProgressBar()
progress.UpdateBaseMsg(baseMsg)
progress.UpdateSuccessMsg(
fmt.Sprintf(
"Finished getting post ID(s) from %d Fanclubs(s) on Fantia!",
creatorIdsLen,
),
)
progress.UpdateErrorMsg(
fmt.Sprintf(
"Something went wrong while getting post IDs from %d Fanclubs(s) on Fantia.\nPlease refer to the logs for more details.",
creatorIdsLen,
),
)
progress.UpdateMax(creatorIdsLen)
} else {
progress.SetToSpinner()
fanclubId := f.FanclubIds[0]
progress.UpdateBaseMsg("Getting post ID(s) from Fanclub, " + fanclubId + ", on Fantia...")
progress.UpdateSuccessMsg("Finished getting post ID(s) from Fanclub, " + fanclubId + ", on Fantia!")
progress.UpdateErrorMsg("Something went wrong while getting post ID(s) from Fanclub, " + fanclubId + ", on Fantia.\nPlease refer to the logs for more details.")
}
progress.Start()
for idx, creatorId := range f.FanclubIds {
wg.Add(1)
go func(creatorId string, pageNumIdx int) {
defer func() {
wg.Done()
<-queue
}()
queue <- struct{}{}
postIds, err := getCreatorPosts(
creatorId,
f.FanclubPageNums[pageNumIdx],
dlOptions,
)
if err != nil {
errChan <- err
} else {
resChan <- postIds
}
progress.Increment()
}(creatorId, idx)
}
wg.Wait()
close(queue)
close(resChan)
close(errChan)
var errorSlice []error
hasErr, hasCancelled := false, false
if len(errChan) > 0 {
hasErr = true
if errCtxCancelled, errSlice := logger.LogChanErrors(false, logger.ERROR, errChan); !hasCancelled && errCtxCancelled {
hasCancelled = true
} else {
errorSlice = append(errorSlice, errSlice...)
}
}
if hasCancelled {
dlOptions.CancelCtx()
progress.StopInterrupt("Stopped getting post ID(s) from Fanclub(s) on Fantia...")
progress.SnapshotTask()
return nil
}
progress.Stop(hasErr)
progress.SnapshotTask()
for postIdsRes := range resChan {
f.PostIds = append(f.PostIds, postIdsRes...)
}
f.PostIds = api.RemoveSliceDuplicates(f.PostIds)
return errorSlice
}