-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcURLing.cpp
More file actions
79 lines (62 loc) · 1.83 KB
/
Copy pathcURLing.cpp
File metadata and controls
79 lines (62 loc) · 1.83 KB
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
#ifdef _WIN32
#include <Windows.h>
#define SLEEPMAC(seconds) Sleep(seconds*1000)
#else
#include <unistd.h>
#define SLEEPMAC(seconds) sleep(seconds)
#endif
//internal
#include <cURLing.h>
#include <Logger.h>
#include <curl/curl.h>
#include <Postgre.h>
//system
#include <iostream>
#include <string>
cURLing::cURLing(Logger* log) {
LOG = log;
curl = curl_easy_init();
}
void cURLing::getHTML(const char* link)
{
readBuffer.clear();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, link);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
long response_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
if (res != CURLE_OK) {
std::string error = curl_easy_strerror(res);
LOG->post("cURLing", 0, "curl_error: " + error, "error");
cURLing again(LOG);
again.getHTML(link);
}
else
if (response_code == 429) {
//this post() is creating multiple connections and dont close them. Why?
LOG->post("cURLing", 0, "curl_error: 429", "error");
int awake = 0;
while (awake != 1) {
SLEEPMAC(60);
awake++;
}
cURLing again(LOG);
again.getHTML(link);
}
curl_easy_cleanup(curl);
}
}
size_t cURLing::WriteCallback(void* contents, size_t size, size_t nmemb, void* userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
std::string cURLing::GetBuffer() {
return this->readBuffer;
}
void cURLing::clearBuffer()
{
readBuffer.clear();
}