diff --git a/functions.cpp b/functions.cpp index 40f1b80..c9e0a4c 100644 --- a/functions.cpp +++ b/functions.cpp @@ -24,7 +24,7 @@ Curl::~Curl() { std::string Curl::execute(const std::string &url) { std::string source; - source.resize(100000); + source.reserve(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); @@ -35,51 +35,99 @@ std::string Curl::execute(const std::string &url) { return source; } +bool searchSpecificSeason(const char *const p, size_t &ep_pos, const std::string &number) { + size_t cur_pos{}; + bool found_season{false}; + while( p[cur_pos] != '\0' ) { + if( (p[cur_pos] == 's' || p[cur_pos] == 'S') && isdigit(p[cur_pos+1]) ) { + cur_pos++; + while( p[cur_pos] == '0' ) + cur_pos++; + size_t offset{}; + while( offset < number.size() && p[cur_pos + offset] == number[offset] ) + offset++; + cur_pos += offset; + if( offset != number.size() ) + continue; + if( (p[cur_pos] == 'e' || p[cur_pos] == 'E') && isdigit(p[cur_pos+1]) ) { + found_season = true; + ep_pos = cur_pos + 1; + break; + } + } + cur_pos++; + } + return found_season; +} + +bool searchSpecificSeason(const char *const p, const std::string &number) { + size_t tmp; + return searchSpecificSeason(p, tmp, number); +} + +bool searchSeason(const char *const p, size_t &season_pos) { + size_t cur_pos{}; + bool found_season{false}; + while( p[cur_pos] != '\0' ) { + if( (p[cur_pos] == 's' || p[cur_pos] == 'S') && isdigit(p[cur_pos+1]) ) { + cur_pos++; + season_pos = cur_pos; // after ++ because we want the first pos to point to season's number + while( isdigit(p[cur_pos]) ) + cur_pos++; + if( (p[cur_pos] == 'e' || p[cur_pos] == 'E') && isdigit(p[cur_pos+1]) ) { + found_season = true; + break; + } + } + cur_pos++; + } + return found_season; +} + +bool searchSeason(const char *const p) { + size_t tmp{}; + return searchSeason(p, tmp); +} + void findSeason(std::set &files, int season, const std::string &path) { - std::smatch match; - auto episode = std::regex("[sS][0]{0,2000}" + std::to_string(season) + "[eE][0-9]{1,2000}"); - for( const auto& p: FSLib::Directory(path) ) { + auto number = std::to_string(season); + + for( const auto p: FSLib::Directory(path) ) { if(FSLib::isDirectory(path + "/" + p)) { findSeason(files, season, path + "/" + p); continue; } - if( std::regex_search(p, episode) ) { + if( searchSpecificSeason(p, number) ) files.insert(path + "/" + p); - } } } void iterateFS(std::set &seasons, const std::string &path) { - std::smatch match; - auto episode = std::regex("[sS][0-9]{1,2000}[eE][0-9]{1,2000}"); - for( const auto& p: FSLib::Directory(path) ) { + size_t season_pos{std::string::npos}; // season_pos - position of first digit of the season + for( const auto p: FSLib::Directory(path) ) { if(FSLib::isDirectory(path + "/" + p)) { iterateFS(seasons, path + "/" + p); continue; } - if( std::regex_search(p, match, episode) ) { - std::string a = match[0]; - a = std::regex_replace(a, std::regex("[eE][0-9]{1,2000}"), ""); - a = a.substr(1); - seasons.insert(std::stoi(a)); - } + if( searchSeason(p, season_pos) ) + seasons.insert(atoi(p+season_pos)); } } -std::string getDefUrl( const std::string &show, const std::string &language, Curl &c ) { - auto search = std::regex_replace(show, std::regex(" "), "+"); - auto source_code = c.execute("https://www.thetvdb.com/search?q=" + search + "&l=" + language); +std::string getDefUrl( std::string show, const std::string &language, Curl &c ) { + std::replace(show.begin(), show.end(), ' ', '+'); + auto source_code = c.execute("https://www.thetvdb.com/search?q=" + show + "&l=" + language); auto series = std::regex(""); - int pos = 1; + int pos = 0; std::vector urls; for( std::sregex_iterator it(source_code.begin(), source_code.end(), series); it != std::sregex_iterator(); ++it) { + // get show's name from link auto input = (*it).str(); auto text = std::regex_replace(input, std::regex(".*series.*?>"), ""); text = text.substr(0, text.size() - 9); - std::cout << pos << ". " << text << std::endl; - pos++; + std::cout << ++pos << ". " << text << std::endl; urls.push_back(std::regex_replace(input, std::regex("\">.*"), "").substr(13)); } std::cout << "Which TV Show is the right one? "; diff --git a/functions.hpp b/functions.hpp index 38bad19..70734fa 100644 --- a/functions.hpp +++ b/functions.hpp @@ -14,9 +14,14 @@ private: CURL *curl_handle; }; -std::string getDefUrl( const std::string &show, const std::string &language, Curl &c ); +std::string getDefUrl( 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(); +bool searchSpecificSeason(const char *const p, size_t &ep_pos, const std::string &number); +bool searchSpecificSeason(const char *const p, const std::string &number); +bool searchSeason(const char *const p, size_t &season_pos); +bool searchSeason(const char *const p); + #endif