-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetcher.cpp
646 lines (539 loc) · 18.3 KB
/
fetcher.cpp
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
#include "stdafx.h"
#include "fetcher.h"
#include "exception.h"
#include "utils.h"
char USER_AGENT[] = "User-Agent: <<user agent>>";
const double RL_AVG_THRESHOLD = 0.5;
const size_t RL_REMAINING_THRESHOLD = 30;
#include <iomanip>
#include <optional>
#include <ostream>
#include <iostream>
std::ostream& operator<<(std::ostream& os, std::chrono::nanoseconds ns)
{
using namespace std::chrono;
using days = duration<int, std::ratio<86400>>;
auto d = duration_cast<days>(ns);
ns -= d;
auto h = duration_cast<hours>(ns);
ns -= h;
auto m = duration_cast<minutes>(ns);
ns -= m;
auto s = duration_cast<seconds>(ns);
ns -= s;
std::optional<t_uint32> fs_count;
switch (os.precision()) {
case 9: fs_count = (t_uint32)ns.count();
break;
case 6: fs_count = (t_uint32)duration_cast<microseconds>(ns).count();
break;
case 3: fs_count = (t_uint32)duration_cast<milliseconds>(ns).count();
break;
}
char fill = os.fill('0');
if (d.count())
os << d.count() << "d ";
if (d.count() || h.count())
os << std::setw(2) << h.count() << ":";
if (d.count() || h.count() || m.count())
os << std::setw(d.count() || h.count() ? 2 : 1) << m.count() << ":";
os << std::setw(d.count() || h.count() || m.count() ? 2 : 1) << s.count();
if (fs_count.has_value())
os << "." << std::setw(os.precision()) << fs_count.value();
if (!d.count() && !h.count() && !m.count())
os << "s";
os.fill(fill);
return os;
}
void Fetcher::fetch_html(const pfc::string8 &url, const pfc::string8 ¶ms, pfc::string8 &html, abort_callback &p_abort, bool use_oauth) {
pfc::array_t<t_uint8> buffer;
fetch_url(url, params, buffer, p_abort, use_oauth, "application/xml");
html = pfc::string8((char*)buffer.get_ptr(), buffer.get_size());
}
void Fetcher::fetch_url(const pfc::string8 &url, const pfc::string8 ¶ms, pfc::array_t<t_uint8> & out, abort_callback &p_abort, bool use_oauth, const pfc::string8 &content_type) {
try {
p_abort.check();
}
catch (exception_aborted) {
return;
}
pfc::string8 msg_average;
pfc::string8 status;
pfc::string8 request_url = "";
pfc::string8 clean_url(url);
if (!url.get_length()) {
foo_discogs_exception e;
e << "(empty url)";
pfc::string8 log_msg("trying to fetch empty url");
throw e;
return;
}
if (params.get_length()) clean_url << "?" << params;
bool use_api = url.find_first("api") != ~0;
use_oauth &= use_api;
bool isImageUrl = url.has_prefix("https://img.discogs.com");
isImageUrl |= url.has_prefix("https://i.discogs.com");
isImageUrl &= !std::string(url.c_str()).find("h:150"); //ignore img previews
if (isImageUrl) {
time_t currentTime;
struct tm* localTime;
time(¤tTime);
localTime = localtime(¤tTime);
int Min = localTime->tm_min;
m_ratelimit_max = 60/3;
m_ratelimit_threshold = 30/3;
if (!use_api) {
m_ratelimit_max = 20;
if (m_ratelimit_remaining > m_ratelimit_max) {
m_ratelimit_remaining = m_ratelimit_max;
}
else if (Min != m_current_minute) {
m_ratelimit_remaining = m_ratelimit_max;
}
else if (m_ratelimit_remaining < 0) {
m_ratelimit_remaining = 0;
}
}
m_current_minute = Min;
}
else {
m_ratelimit_max = 60;
m_ratelimit_threshold = 30;
}
//log: url and oauth status
log_msg(clean_url);
if (use_oauth) {
log_msg("Url OAuth enabled");
}
if (use_api || isImageUrl) {
chrono::steady_clock::time_point time_now = chrono::steady_clock::now();
chrono::duration<double> time_span = time_now - m_last_fetch;
m_fetch_wait_rolling_avg = ROLLING_AVG_ALPHA * time_span.count() + (1.0 - ROLLING_AVG_ALPHA) * m_fetch_wait_rolling_avg;
m_fetch_wait_rolling_avg = (std::min)(m_fetch_wait_rolling_avg, 1.0);
//record time
m_last_fetch = time_now;
//log: average and last delta
if (time_span != chrono::steady_clock::duration::zero()) {
msg_average << "Lapse: ";
//double microsecs = time_span.count();
pfc::string8 msg_delta;
if (m_throttle_delta) {
std::stringstream human_read_time;
human_read_time.precision(2);
human_read_time << std::fixed << m_throttle_delta;
msg_delta << " (+ " << human_read_time.str().c_str();
msg_delta << ")";
}
std::stringstream human_read_time;
human_read_time << std::setprecision(3) << time_span.count();
msg_average << pfc::string8(human_read_time.str().c_str()) << msg_delta;
}
//reset when rate remaining reaches max m_value again
m_ratelimit_cruise &= (m_ratelimit_remaining < m_ratelimit_max); //60
m_ratelimit_cruise |= isImageUrl;
log_msg(msg_average);
//apply delta/set cruise mode
if (m_ratelimit_cruise ||
(m_fetch_wait_rolling_avg < (60/m_ratelimit_max) /*RL_AVG_THRESHOLD*/ /*(0.5)*/ && m_ratelimit_remaining <= m_ratelimit_threshold /*(30)*/)) {
m_ratelimit_cruise = true;
chrono::duration<double, std::milli> time_span_milli = time_span;
double sleep_base = m_ratelimit_remaining < (m_ratelimit_max / 6) ? 5000 : m_ratelimit_remaining < (m_ratelimit_max / 3) ? 2000 : 1000;
m_throttle_delta = (std::max)(sleep_base - (isImageUrl ? 0 : time_span_milli.count()), 0.0);
DWORD dw = static_cast<DWORD>((int)m_throttle_delta);
Sleep(dw);
}
else {
m_throttle_delta = 0;
}
}
try {
const http_request::ptr request = client->create_request("GET");
//OAuth header
if (use_oauth) {
request->add_header(oauth_sign_url_header(url, params));
}
//Accept-Encoding header
request->add_header(dllinflateInit2 != NULL ? "Accept-Encoding: gzip, deflate" : "Accept-Encoding: identity");
//Accept user User-Agent and header
request->add_header(USER_AGENT);
pfc::string8 accept_header;
accept_header << "Accept: " << content_type;
request->add_header(accept_header);
int tries = 1; //3x on error loading resource
while (1) {
try {
file::ptr f;
f = request->run_ex(clean_url.get_ptr(), p_abort);
http_reply::ptr r;
f->service_query_t(r);
pfc::string8 status;
r->get_status(status);
pfc::string8 rate_header_buffer;
pfc::string8 msg_ratelimits;
if (r->get_http_header("X-Discogs-Ratelimit", rate_header_buffer)) {
m_ratelimit_max = atoi(rate_header_buffer); //currently 60, discoggs may update these rate limits at any time
m_ratelimit_threshold = m_ratelimit_max - RL_REMAINING_THRESHOLD; //max - 30
msg_ratelimits << "RL: " << rate_header_buffer;
}
if (r->get_http_header("X-Discogs-Ratelimit-Used", rate_header_buffer)) {
msg_ratelimits << " - Used: " << rate_header_buffer;
}
if (r->get_http_header("X-Discogs-Ratelimit-Remaining", rate_header_buffer)) {
m_ratelimit_remaining = atoi(rate_header_buffer);
msg_ratelimits << " - RMNG: " << rate_header_buffer;
}
if (isImageUrl && !use_api) {
m_ratelimit_max = 20;
--m_ratelimit_remaining;
msg_ratelimits << "RL: " << m_ratelimit_max;
msg_ratelimits << " - Used: " << m_ratelimit_max - m_ratelimit_remaining;
msg_ratelimits << " - RMNG: " << m_ratelimit_remaining;
}
if (m_throttle_delta) {
std::stringstream human_read_time;
human_read_time << std::setprecision(3) << m_throttle_delta;
if (msg_ratelimits.get_length()) {
msg_ratelimits << " - Throttle engaged (" << pfc::string8(human_read_time.str().c_str()) << u8" \u03BCs)";
}
else {
msg_ratelimits << "Image Throttle engaged (" << pfc::string8(human_read_time.str().c_str()) << u8" \u03BCs)";
}
}
if (msg_ratelimits.length())
log_msg(msg_ratelimits);
else {
m_ratelimit_remaining = m_ratelimit_threshold; //if rate info is empty reset to threshold
}
// check status
if (pfc::string8(status).find_first("200") == ~0) {
pfc::string8 msg_status;
msg_status << "HTTP error status: " << status;
log_msg(msg_status);
int error_code = std::stoi(substr(status, 9, 3).get_ptr());
switch (error_code) {
case 404:
throw http_404_exception();
case 401: //unauthorized
throw http_401_exception();
case 429: //too many requests (need to wait for remaining 60 or slow to 1 req/sec)
throw http_429_exception();
default:
throw http_exception(error_code);
}
}
//Sleep(1000);
pfc::string8 reply_content_type;
f->get_content_type(reply_content_type);
if (reply_content_type.is_empty()) {
out.set_size(0);
throw network_exception("Error reading network response.");
}
// read stream...
out.set_size(1024);
size_t bufferUsed = 0;
for (;;) {
p_abort.check();
size_t delta = out.get_size() - bufferUsed;
size_t done = f->read(out.get_ptr() + bufferUsed, delta, p_abort);
bufferUsed += done;
if (done != delta) {
break;
}
out.set_size(out.get_size() << 1);
}
out.set_size(bufferUsed);
// see if we must unzip buffer
if (out.get_size() >= 6 && out[0] == 0x1f && out[1] == 0x8b) {
pfc::array_t<t_uint8> unzipped;
unzipped.set_size(pfc::decode_little_endian<t_uint32>(out.get_ptr() + out.get_size() - 4));
uLongf destLen = unzipped.get_size();
int state = myUncompress(unzipped.get_ptr(), &destLen, out.get_ptr(), out.get_size());
if (state != Z_OK) {
throw network_exception("Error unzipping network response.");
}
PFC_ASSERT(destLen == unzipped.get_size());
out = unzipped;
}
break;
}
catch (http_429_exception) {
if (tries > 20) throw;
pfc::string8 error_msg;
error_msg << "Rate-limited. Retrying: " << tries;
log_msg(error_msg);
Sleep(2000 * tries);
}
catch (foobar2000_io::exception_io &e) {
if (tries > 5) throw;
pfc::string8 error_msg;
error_msg << "Network error";
if (!error_msg.has_prefix(pfc::string8(e.what())))
error_msg << ": " << e.what();
error_msg << ". Retrying: " << tries;
log_msg(error_msg);
Sleep(2000 * (tries > 1 ? 2 : 1));
}
tries++;
}
}
catch (exception_aborted) {
throw;
}
catch (foo_discogs_exception &e) {
e << "(url: " << clean_url << ")";
pfc::string8 error_msg;
error_msg << "Exception handling: " << clean_url;
log_msg(error_msg);
throw;
}
catch (const foobar2000_io::exception_io &e) {
//retries > max retries
network_exception ex("Network exception: ");
ex << e.what() << " (url: " << clean_url << ")";
pfc::string8 error_msg;
error_msg << "Network exception fetching url: " << clean_url;
log_msg(error_msg);
throw ex;
}
catch (const std::exception& e) {
log_msg(e.what());
}
catch (...) {
out.set_size(0);
network_exception ex("Unknown network exception: ");
ex << "(url: " << clean_url << ")";
pfc::string8 error_msg;
error_msg << "Unknown network exception handling: " << clean_url;
log_msg(error_msg);
throw ex;
}
}
void Fetcher::fetch_html_simple_log(const pfc::string8& url, const pfc::string8& params, pfc::string8& html, abort_callback& p_abort, bool throw_abort, const pfc::string8& content_type) {
pfc::array_t<t_uint8> buffer;
pfc::string8 status;
pfc::string8 request_url = "";
pfc::string8 clean_url(url);
if (params.get_length()) clean_url << "?" << params;
log_msg(clean_url);
try {
const http_request::ptr request = client->create_request("GET");
//HEADERS
//Accept-Encoding header
request->add_header(dllinflateInit2 != NULL ? "Accept-Encoding: gzip, deflate" : "Accept-Encoding: identity");
//Accept user User-Agent and header
request->add_header(USER_AGENT);
pfc::string8 accept_header;
accept_header << "Accept: " << content_type;
request->add_header(accept_header);
//END HEADERS
int tries = 1; //3x on error loading resource
file::ptr f;
while (1) {
try {
f = request->run_ex(clean_url.get_ptr(), p_abort);
http_reply::ptr r;
f->service_query_t(r);
pfc::string8 status;
r->get_status(status);
pfc::string8 debug_keep_alive;
r->get_http_header("Connection", debug_keep_alive);
// check status (applies only to request->run_ex)
if (pfc::string8(status).find_first("200") == ~0) {
pfc::string8 msg_status;
msg_status << "HTTP error status: " << status;
log_msg(msg_status);
}
//Sleep(1000);
pfc::string8 reply_content_type;
f->get_content_type(reply_content_type);
if (reply_content_type.is_empty()) {
buffer.set_size(0);
log_msg("Error reading network response.");
break;
}
// read stream...
buffer.set_size(1024);
size_t bufferUsed = 0;
for (;;) {
p_abort.check();
size_t delta = buffer.get_size() - bufferUsed;
size_t done = f->read(buffer.get_ptr() + bufferUsed, delta, p_abort);
bufferUsed += done;
if (done != delta) {
break;
}
buffer.set_size(buffer.get_size() << 1);
}
buffer.set_size(bufferUsed);
// see if we must unzip buffer
if (buffer.get_size() >= 6 && buffer[0] == 0x1f && buffer[1] == 0x8b) {
pfc::array_t<t_uint8> unzipped;
unzipped.set_size(pfc::decode_little_endian<t_uint32>(buffer.get_ptr() + buffer.get_size() - 4));
uLongf destLen = unzipped.get_size();
int state = myUncompress(unzipped.get_ptr(), &destLen, buffer.get_ptr(), buffer.get_size());
if (state != Z_OK) {
log_msg("Error unzipping network response.");
break;
}
PFC_ASSERT(destLen == unzipped.get_size());
buffer = unzipped;
}
break;
}
catch (foobar2000_io::exception_io& e) {
if (tries > 5) throw;
pfc::string8 error_msg;
error_msg << "Network error";
if (!error_msg.has_prefix(pfc::string8(e.what())))
error_msg << ": " << e.what();
error_msg << ". Retrying: " << tries;
log_msg(error_msg);
Sleep(2000 * (tries > 1 ? 2 : 1));
}
tries++;
}
}
catch (exception_aborted) {
if (throw_abort)
throw;
}
catch (foo_discogs_exception& e) {
e << "(url: " << clean_url << ")";
pfc::string8 error_msg;
error_msg << "Exception handling: " << clean_url;
log_msg(error_msg);
}
catch (const foobar2000_io::exception_io& e) {
//retries > max retries
network_exception ex("Network exception: ");
ex << e.what() << " (url: " << clean_url << ")";
pfc::string8 error_msg = PFC_string_formatter() << "Network exception (simlog fetching url): " << clean_url;
log_msg(error_msg);
throw ex;
}
catch (const std::exception& e) {
log_msg(e.what());
}
catch (...) {
// fallback for request->run errors
buffer.set_size(0);
network_exception ex("Unknown network exception: ");
ex << "(url: " << clean_url << ")";
pfc::string8 error_msg;
error_msg << "Unknown network exception handling: " << clean_url;
log_msg(error_msg);
}
html = pfc::string8((char*)buffer.get_ptr(), buffer.get_size());
}
void Fetcher::set_oauth(const pfc::string8 &token, const pfc::string8 &token_secret) {
consumer = new OAuth::Consumer(CONSUMER_KEY, CONSUMER_SECRET);
oauth_token = token;
oauth_token_secret = token_secret;
init_oauth();
}
void Fetcher::init_oauth() {
if (!oauth_token.get_length() && !oauth_token_secret.get_length()) {
if (oauth != nullptr) {
delete oauth;
oauth = nullptr;
}
if (access_token != nullptr) {
delete access_token;
access_token = nullptr;
}
oauth = new OAuth::Client(consumer);
}
else {
if (access_token != nullptr) {
delete access_token;
access_token = nullptr;
}
if (oauth != nullptr) {
delete oauth;
oauth = nullptr;
}
access_token = new OAuth::Token(oauth_token.get_ptr(), oauth_token_secret.get_ptr());
oauth = new OAuth::Client(consumer, access_token);
}
}
void Fetcher::update_oauth(const pfc::string8 &token, const pfc::string8 &token_secret) {
if (token == oauth_token && token_secret == oauth_token_secret) {
return;
}
oauth_token = token;
oauth_token_secret = token_secret;
init_oauth();
}
void Fetcher::test_oauth(const pfc::string8 &token, const pfc::string8 &token_secret, abort_callback &p_abort) {
OAuth::Consumer c = OAuth::Consumer(CONSUMER_KEY, CONSUMER_SECRET);
OAuth::Token at = OAuth::Token(token.get_ptr(), token_secret.get_ptr());
OAuth::Client oa = OAuth::Client(&c, &at);
const char *identity = "https://api.discogs.com/oauth/identity";
pfc::string8 url = "";
url << identity << "?";
url << oa.getURLQueryString(OAuth::Http::Get, identity, "").c_str();
pfc::string8 html;
// throws network_exception on error
fetch_html(url, "", html, p_abort, false);
}
pfc::string8 Fetcher::oauth_sign_url(const pfc::string8 &url, const pfc::string8 ¶ms) {
pfc::string8 oauth_params;
if (params.get_length()) {
pfc::string8 tmp = url;
tmp << "?" << params;
oauth_params = oauth->getURLQueryString(OAuth::Http::Get, tmp.get_ptr()).c_str();
}
else {
oauth_params = oauth->getURLQueryString(OAuth::Http::Get, url.get_ptr()).c_str();
}
pfc::string8 ret(url);
ret << "?" << oauth_params;
return ret;
}
pfc::string8 Fetcher::oauth_sign_url_header(const pfc::string8& url, const pfc::string8& params) {
pfc::string8 oauth_header;
if (params.get_length()) {
pfc::string8 tmp = url;
tmp << "?" << params;
oauth_header = oauth->getFormattedHttpHeader(OAuth::Http::Get, tmp.get_ptr()).c_str();
}
else {
oauth_header = oauth->getFormattedHttpHeader(OAuth::Http::Get, url.get_ptr()).c_str();
}
return oauth_header;
}
pfc::string8 Fetcher::get_oauth_authorize_url(abort_callback &p_abort) {
OAuth::Client oa = OAuth::Client(consumer);
pfc::string8 oauth_query_str = oa.getURLQueryString(OAuth::Http::Get, REQUEST_TOKEN_URL).c_str();
pfc::string8 html;
fetch_html(REQUEST_TOKEN_URL, oauth_query_str, html, p_abort, false);
pfc::string8 msg("OAuth request response: ");
msg << html;
log_msg(msg);
if (request_token != nullptr) {
delete request_token;
request_token = nullptr;
}
OAuth::Token t = OAuth::Token::extract(html.get_ptr());
request_token = new OAuth::Token(t.key(), t.secret());
pfc::string8 auth_url;
auth_url << AUTHORIZE_URL << "?oauth_token=" << request_token->key().c_str();
return auth_url;
}
OAuth::Token * Fetcher::generate_oauth(pfc::string8 &code, abort_callback &p_abort) {
if (request_token == nullptr) {
return nullptr;
}
request_token->setPin(code.get_ptr());
OAuth::Client oa = OAuth::Client(consumer, request_token);
pfc::string8 oauth_query_str= oa.getURLQueryString(OAuth::Http::Get, ACCESS_TOKEN_URL, "", true).c_str();
pfc::string8 html;
fetch_html(ACCESS_TOKEN_URL, oauth_query_str, html, p_abort, false);
pfc::string8 msg("OAuth generated response: ");
msg << html;
log_msg(msg);
OAuth::KeyValuePairs access_token_resp_data = OAuth::ParseKeyValuePairs(html.get_ptr());
OAuth::Token t = OAuth::Token::extract(access_token_resp_data);
OAuth::Token *generated_token = new OAuth::Token(t.key(), t.secret());
return generated_token;
}