Use own functions to find seasons instead of regex

This commit is contained in:
zvon 2019-01-07 22:23:15 +01:00
parent 46528b1f29
commit ddcb8e3215
2 changed files with 75 additions and 22 deletions

View File

@ -24,7 +24,7 @@ Curl::~Curl() {
std::string Curl::execute(const std::string &url) { std::string Curl::execute(const std::string &url) {
std::string source; std::string source;
source.resize(100000); source.reserve(100000);
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, static_cast<void*>(&source)); curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, static_cast<void*>(&source));
curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
auto res = curl_easy_perform(curl_handle); auto res = curl_easy_perform(curl_handle);
@ -35,51 +35,99 @@ std::string Curl::execute(const std::string &url) {
return source; 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<std::string> &files, int season, const std::string &path) { void findSeason(std::set<std::string> &files, int season, const std::string &path) {
std::smatch match; auto number = std::to_string(season);
auto episode = std::regex("[sS][0]{0,2000}" + std::to_string(season) + "[eE][0-9]{1,2000}");
for( const auto& p: FSLib::Directory(path) ) { for( const auto p: FSLib::Directory(path) ) {
if(FSLib::isDirectory(path + "/" + p)) { if(FSLib::isDirectory(path + "/" + p)) {
findSeason(files, season, path + "/" + p); findSeason(files, season, path + "/" + p);
continue; continue;
} }
if( std::regex_search(p, episode) ) { if( searchSpecificSeason(p, number) )
files.insert(path + "/" + p); files.insert(path + "/" + p);
}
} }
} }
void iterateFS(std::set<int> &seasons, const std::string &path) { void iterateFS(std::set<int> &seasons, const std::string &path) {
std::smatch match; size_t season_pos{std::string::npos}; // season_pos - position of first digit of the season
auto episode = std::regex("[sS][0-9]{1,2000}[eE][0-9]{1,2000}"); for( const auto p: FSLib::Directory(path) ) {
for( const auto& p: FSLib::Directory(path) ) {
if(FSLib::isDirectory(path + "/" + p)) { if(FSLib::isDirectory(path + "/" + p)) {
iterateFS(seasons, path + "/" + p); iterateFS(seasons, path + "/" + p);
continue; continue;
} }
if( std::regex_search(p, match, episode) ) { if( searchSeason(p, season_pos) )
std::string a = match[0]; seasons.insert(atoi(p+season_pos));
a = std::regex_replace(a, std::regex("[eE][0-9]{1,2000}"), "");
a = a.substr(1);
seasons.insert(std::stoi(a));
}
} }
} }
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 ) {
auto search = std::regex_replace(show, std::regex(" "), "+"); std::replace(show.begin(), show.end(), ' ', '+');
auto source_code = c.execute("https://www.thetvdb.com/search?q=" + search + "&l=" + language); auto source_code = c.execute("https://www.thetvdb.com/search?q=" + show + "&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 = 0;
std::vector<std::string> urls; std::vector<std::string> urls;
for( std::sregex_iterator it(source_code.begin(), source_code.end(), series); it != std::sregex_iterator(); ++it) { 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 input = (*it).str();
auto text = std::regex_replace(input, std::regex(".*series.*?>"), ""); auto text = std::regex_replace(input, std::regex(".*series.*?>"), "");
text = text.substr(0, text.size() - 9); text = text.substr(0, text.size() - 9);
std::cout << pos << ". " << text << std::endl; std::cout << ++pos << ". " << text << std::endl;
pos++;
urls.push_back(std::regex_replace(input, std::regex("\">.*"), "").substr(13)); urls.push_back(std::regex_replace(input, std::regex("\">.*"), "").substr(13));
} }
std::cout << "Which TV Show is the right one? "; std::cout << "Which TV Show is the right one? ";

View File

@ -14,9 +14,14 @@ private:
CURL *curl_handle; 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<std::string> &files, int season, const std::string &path); void findSeason(std::set<std::string> &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();
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 #endif