From 1afc0fe1f391425d5bb64e99965cf489ba984d78 Mon Sep 17 00:00:00 2001 From: zvon Date: Wed, 26 Sep 2018 12:45:37 +0200 Subject: [PATCH] Use global curl object instead of creating one with every request --- functions.cpp | 25 +++++++++++++------------ functions.hpp | 13 +++++++++++-- tv_rename.cpp | 7 ++++--- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/functions.cpp b/functions.cpp index de531dc..b5c28d2 100644 --- a/functions.cpp +++ b/functions.cpp @@ -9,23 +9,24 @@ size_t writeCallback( void *contents, size_t size, size_t nmemb, void *target ) return size * nmemb; } -std::string getSource(const std::string &url) { - CURL *curl_handle; - CURLcode res; - - std::string source{}; - +Curl::Curl() { curl_global_init(CURL_GLOBAL_ALL); 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_WRITEDATA, static_cast(&source)); 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_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(&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 ""; @@ -68,10 +69,10 @@ void iterateFS(std::set &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; - auto search = std::regex_replace(show, std::regex(" "), "+"); - auto source_code = getSource("https://www.thetvdb.com/search?q=" + search + "&l=" + language); + auto search = std::regex_replace(show, std::regex(" "), "+"); + auto source_code = c.execute("https://www.thetvdb.com/search?q=" + search + "&l=" + language); auto series = std::regex(""); int pos = 1; std::vector urls; diff --git a/functions.hpp b/functions.hpp index 6119f0a..2e62621 100644 --- a/functions.hpp +++ b/functions.hpp @@ -4,9 +4,18 @@ #include #include #include +#include -std::string getSource(const std::string &url); -std::string getDefUrl( const std::string &show, const std::string &language ); +class Curl { +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 &files, int season, const std::string &path); void iterateFS(std::set &seasons, const std::string &path); void printHelp(); diff --git a/tv_rename.cpp b/tv_rename.cpp index dc1f5f1..0f97414 100644 --- a/tv_rename.cpp +++ b/tv_rename.cpp @@ -15,6 +15,7 @@ void allSeasons( const std::string &path, const std::string &show ); std::string language{"en"}; bool trust{false}; bool linux{false}; +Curl c; int main(int argc, char** argv) { 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) { if( url.empty() ) - url = getDefUrl(show, language); + url = getDefUrl(show, language, c); url += "/seasons/"; url += std::to_string(season); //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 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 @@ -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 seasons) { - auto url = getDefUrl(show, language); + auto url = getDefUrl(show, language, c); for( const auto &x : seasons ) { singleSeason( path, show, x, url); }