Use global curl object instead of creating one with every request
This commit is contained in:
parent
1641a5ed36
commit
1afc0fe1f3
@ -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<void*>(&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<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 "";
|
||||
@ -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;
|
||||
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("<td><a href=\"/series.*?</td>");
|
||||
int pos = 1;
|
||||
std::vector<std::string> urls;
|
||||
|
@ -4,9 +4,18 @@
|
||||
#include <string>
|
||||
#include <set>
|
||||
#include <experimental/filesystem>
|
||||
#include <curl/curl.h>
|
||||
|
||||
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<std::experimental::filesystem::path> &files, int season, const std::string &path);
|
||||
void iterateFS(std::set<int> &seasons, const std::string &path);
|
||||
void printHelp();
|
||||
|
@ -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<int> seasons) {
|
||||
auto url = getDefUrl(show, language);
|
||||
auto url = getDefUrl(show, language, c);
|
||||
for( const auto &x : seasons ) {
|
||||
singleSeason( path, show, x, url);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user