add download to file function for curl

This commit is contained in:
Markus 2019-10-13 12:54:58 +02:00
parent b1d4aaee17
commit 0bda3b6c4c
2 changed files with 72 additions and 0 deletions

View File

@ -26,6 +26,7 @@ void PageManager::setDebugMode(bool status)
this->debugMode = status;
}

//Save reply to string
size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
//Function für CURL
@ -33,6 +34,13 @@ size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
return size * nmemb;
}

//Write data to file
static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
size_t written = fwrite(ptr, size, nmemb, reinterpret_cast<FILE *>(stream));
return written;
}

Reply PageManager::getServerRequest(std::string Url, bool useCookies, std::string data, bool generateCookieFile)
{
CURL *curl;
@ -98,6 +106,68 @@ Reply PageManager::getServerRequest(std::string Url, bool useCookies, std::strin
return Reply(readBuffer, returnUrl);
}


int PageManager::downLoadToFile(std::string filePath, std::string url)
{
CURL *curl_handle;
FILE *pagefile;
CURLcode res;

/* open the file */
pagefile = fopen(filePath.c_str(), "wb"); // w == write; b == binäre
if(!pagefile) {
perror("Open File filed");
return 1;
}

/* init the curl session */
curl_handle = curl_easy_init();
if(!curl_handle) {
perror("\33[2K\r => Error: Curl easy init failed");
return 2;
}

/* set URL to get here */
curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());

/* Switch on full protocol/debug output while testing */
curl_easy_setopt(curl_handle, CURLOPT_VERBOSE, false);

/* disable progress meter, set to 0L to enable and disable debug output */
curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, true);

/* send all data to this function */
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);

//Sock5Proxy für Curl
curl_easy_setopt(curl_handle, CURLOPT_PROXY, sock5Proxy.c_str() );

//User Agent
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0");

/* write the page body to this file handle */
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, pagefile);


/* get it! */
if( (res = curl_easy_perform(curl_handle)) != CURLE_OK) {
perror((std::string("\33[2K\r => Error: curl_easy_perform() failed: ") + curl_easy_strerror(res)).c_str());
return 3;
}

/* close the header file */
if(fclose(pagefile) != 0) {
perror(" => Error: fclose failed");
return 4;
}

/* cleanup curl stuff */
curl_easy_cleanup(curl_handle);


return 0;
}

int PageManager::login(Account account)
{
if(debugMode)

View File

@ -30,6 +30,8 @@ public:
void setDebugMode(bool status);

Reply getServerRequest(std::string Url, bool useCookies = false, std::string data = "", bool generateCookieFile = false);
int downLoadToFile(std::string filePath, std::string url);

int login(Account account);
std::string getUrlAfterRedirect(std::string Url);
std::string checkName(std::string Name);