From 3bfa1e586e386eb2f14fbff718546cf57030b521 Mon Sep 17 00:00:00 2001 From: zvon Date: Mon, 7 Jan 2019 23:24:28 +0100 Subject: [PATCH] No more regex, yay! --- functions.cpp | 25 ++++++++++++-------- tv_rename.cpp | 64 ++++++++++++++++++++++++++++++++++++++++----------- 2 files changed, 65 insertions(+), 24 deletions(-) diff --git a/functions.cpp b/functions.cpp index c9e0a4c..0df8795 100644 --- a/functions.cpp +++ b/functions.cpp @@ -1,9 +1,10 @@ #include "functions.hpp" #include "filesystem.hpp" +#include #include #include #include -#include +#include size_t writeCallback( void *contents, size_t size, size_t nmemb, void *target ) { *static_cast(target) += std::string(static_cast(contents), size*nmemb); @@ -119,16 +120,20 @@ void iterateFS(std::set &seasons, const std::string &path) { 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 = 0; + size_t order{}, pos{}; 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; - urls.push_back(std::regex_replace(input, std::regex("\">.*"), "").substr(13)); + while( true ) { + pos = source_code.find("/ser", pos); + if( pos != std::string::npos ) { + auto end = source_code.find(">", pos); + end--; + urls.push_back(source_code.substr(pos, end - pos)); + pos = end + 2; + end = source_code.find("<", pos); + std::cout << ++order << ". " << source_code.substr(pos, end - pos) << std::endl; + } else { + break; + } } std::cout << "Which TV Show is the right one? "; std::cin >> pos; diff --git a/tv_rename.cpp b/tv_rename.cpp index d258dff..e0454bb 100644 --- a/tv_rename.cpp +++ b/tv_rename.cpp @@ -1,12 +1,13 @@ +#include #include #include #include "functions.hpp" #include -#include #include #include #include #include +#include #include "filesystem.hpp" void singleSeason( const std::string &path, const std::string &show, int season, std::string url ); @@ -168,6 +169,26 @@ int main(int argc, char** argv) { } } +void parseEpisodeNames( const std::string &season_code, std::vector &episodes) { + size_t pos = 0; + while( true ) { + pos = season_code.find("ge=\"" + language + "\" ", pos); + if( pos != std::string::npos ) { + pos+= 17; + while( isspace(season_code[pos]) ) + pos++; + auto end = season_code.find("<", pos); + end--; + while( isspace(season_code[end]) ) + end--; + end++; + episodes.push_back(season_code.substr(pos, end - pos)); + } else { + break; + } + } +} + void singleSeason( const std::string &path, const std::string &show, int season, std::string url) { if( url.empty() ) url = getDefUrl(show, language, c); @@ -190,26 +211,24 @@ void singleSeason( const std::string &path, const std::string &show, int season, } else { return; } - std::regex title(".*\\s*(.*?)\\s*?.*"); - std::regex episode_link(".*?"); - std::smatch ep_match; + std::vector episodes; //get episode names in all languages - for( std::sregex_iterator it(season_code.begin(), season_code.end(), episode_link); it != std::sregex_iterator(); ++it) { - auto input = (*it).str(); - //only get the selected language - if( std::regex_search( input, title ) ) - episodes.push_back(std::regex_replace(input, title, "$1")); - } + parseEpisodeNames(season_code, episodes); + if( episodes.empty() ) return; + std::set files; std::set renamed_files; std::vector renamed_episodes; + renamed_episodes.resize(episodes.size()); findSeason(files, season, path); + if( files.empty() ) return; + for( const auto &x : files ) { auto last = x.find_last_of("/"); std::string name; @@ -232,18 +251,33 @@ void singleSeason( const std::string &path, const std::string &show, int season, auto pos = name.find_last_of('.'); name.insert(pos, " - " + episodes[num]); if( !linux ) { - name = std::regex_replace(name, std::regex("[\\?\"\\\\|\\*]"), ""); - name = std::regex_replace(name, std::regex("<"), "is less than"); - name = std::regex_replace(name, std::regex(">"), "is more than"); - name = std::regex_replace(name, std::regex(":"), " -"); + season_code.erase(std::remove_if(name.begin(), name.end(), [](char x) {return x == '?' || x == '"' || x == '\\' || x == '|' || x == '*';}), season_code.end()); + size_t max{name.size()}; + for( size_t i = 0; i < max ; i++ ) { + if( name[i] == '<' ) { + name[i] = 'i'; + name.insert(i+1, "s less than"); + max += 11; + } else if ( name[i] == '>' ) { + name[i] = 'i'; + name.insert(i+1, "s more than"); + max += 11; + } else if ( name[i] == ':' ) { + name[i] = ' '; + name.insert(i+1, 1, '-'); + max++; + } + } } renamed_files.insert(dir + "/" + name); renamed_episodes[num] = name; } } + for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) { std::cout << *renamed << std::endl; } + if( !trust ) { std::cout << "Does this seem ok? (y/n) "; std::string response; @@ -253,7 +287,9 @@ void singleSeason( const std::string &path, const std::string &show, int season, if( response[0] != 'y' && response[0] != 'Y' ) return; } + auto orig = files.begin(); + for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) { FSLib::rename(*orig, *renamed); ++orig;