tv_rename/network.cpp
2019-01-19 13:40:10 +01:00

33 lines
1.0 KiB
C++

#include <iostream>
#include "network.hpp"
size_t writeCallback( void *contents, size_t size, size_t nmemb, void *target ) {
*static_cast<std::string*>(target) += std::string(static_cast<char*>(contents), size*nmemb);
return size * nmemb;
}
Curl::Curl() {
curl_global_init(CURL_GLOBAL_ALL);
curl_handle = curl_easy_init();
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writeCallback);
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
}
Curl::~Curl() {
curl_easy_cleanup(curl_handle);
curl_global_cleanup();
}
std::string Curl::execute(const std::string &url) {
std::string source;
source.reserve(100000);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, static_cast<void*>(&source));
curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
auto res = curl_easy_perform(curl_handle);
if(res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
return "";
}
return source;
}