Use global curl object instead of creating one with every request

This commit is contained in:
zvon 2018-09-26 12:45:37 +02:00
parent 1641a5ed36
commit 1afc0fe1f3
3 changed files with 28 additions and 17 deletions

View File

@ -9,23 +9,24 @@ size_t writeCallback( void *contents, size_t size, size_t nmemb, void *target )
return size * nmemb; return size * nmemb;
} }
std::string getSource(const std::string &url) { Curl::Curl() {
CURL *curl_handle;
CURLcode res;
std::string source{};
curl_global_init(CURL_GLOBAL_ALL); curl_global_init(CURL_GLOBAL_ALL);
curl_handle = curl_easy_init(); curl_handle = curl_easy_init();
curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writeCallback); curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writeCallback);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, static_cast<void*>(&source));
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
res = curl_easy_perform(curl_handle); }
Curl::~Curl() {
curl_easy_cleanup(curl_handle); curl_easy_cleanup(curl_handle);
curl_global_cleanup(); curl_global_cleanup();
}
std::string Curl::execute(const std::string &url) {
std::string source;
source.resize(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) { if(res != CURLE_OK) {
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl; std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
return ""; return "";
@ -68,10 +69,10 @@ void iterateFS(std::set<int> &seasons, const std::string &path) {
} }
} }
std::string getDefUrl( const std::string &show, const std::string &language ) { std::string getDefUrl( const std::string &show, const std::string &language, Curl &c ) {
using namespace std::string_literals; using namespace std::string_literals;
auto search = std::regex_replace(show, std::regex(" "), "+"); auto search = std::regex_replace(show, std::regex(" "), "+");
auto source_code = getSource("https://www.thetvdb.com/search?q=" + search + "&l=" + language); auto source_code = c.execute("https://www.thetvdb.com/search?q=" + search + "&l=" + language);
auto series = std::regex("<td><a href=\"/series.*?</td>"); auto series = std::regex("<td><a href=\"/series.*?</td>");
int pos = 1; int pos = 1;
std::vector<std::string> urls; std::vector<std::string> urls;

View File

@ -4,9 +4,18 @@
#include <string> #include <string>
#include <set> #include <set>
#include <experimental/filesystem> #include <experimental/filesystem>
#include <curl/curl.h>
std::string getSource(const std::string &url); class Curl {
std::string getDefUrl( const std::string &show, const std::string &language ); public:
Curl();
~Curl();
std::string execute( const std::string &url );
private:
CURL *curl_handle;
};
std::string getDefUrl( const std::string &show, const std::string &language, Curl &c );
void findSeason(std::set<std::experimental::filesystem::path> &files, int season, const std::string &path); void findSeason(std::set<std::experimental::filesystem::path> &files, int season, const std::string &path);
void iterateFS(std::set<int> &seasons, const std::string &path); void iterateFS(std::set<int> &seasons, const std::string &path);
void printHelp(); void printHelp();

View File

@ -15,6 +15,7 @@ void allSeasons( const std::string &path, const std::string &show );
std::string language{"en"}; std::string language{"en"};
bool trust{false}; bool trust{false};
bool linux{false}; bool linux{false};
Curl c;
int main(int argc, char** argv) { int main(int argc, char** argv) {
namespace fs = std::experimental::filesystem; namespace fs = std::experimental::filesystem;
@ -124,11 +125,11 @@ int main(int argc, char** argv) {
void singleSeason( const std::string &path, const std::string &show, int season, std::string url) { void singleSeason( const std::string &path, const std::string &show, int season, std::string url) {
if( url.empty() ) if( url.empty() )
url = getDefUrl(show, language); url = getDefUrl(show, language, c);
url += "/seasons/"; url += "/seasons/";
url += std::to_string(season); url += std::to_string(season);
//get source code of season's page //get source code of season's page
auto season_code = getSource(url); auto season_code = c.execute(url);
//remove newlines cause regex can't multiline //remove newlines cause regex can't multiline
season_code = std::regex_replace(season_code, std::regex("[\r\n]"), ""); season_code = std::regex_replace(season_code, std::regex("[\r\n]"), "");
//first 900 chars or so are useless to us, no need to regex through them //first 900 chars or so are useless to us, no need to regex through them
@ -207,7 +208,7 @@ void singleSeason( const std::string &path, const std::string &show, int season,
} }
void multipleSeasons( const std::string &path, const std::string &show, const std::set<int> seasons) { void multipleSeasons( const std::string &path, const std::string &show, const std::set<int> seasons) {
auto url = getDefUrl(show, language); auto url = getDefUrl(show, language, c);
for( const auto &x : seasons ) { for( const auto &x : seasons ) {
singleSeason( path, show, x, url); singleSeason( path, show, x, url);
} }