From 46abfe3dd9379a2865ac3d665c9ff997e928f7ae Mon Sep 17 00:00:00 2001 From: zvon Date: Thu, 17 Jan 2019 17:08:51 +0100 Subject: [PATCH] Fewer filesystem operations when using 'all' --- functions.cpp | 13 +++++++++++++ functions.hpp | 2 ++ tv_rename.cpp | 34 ++++++++++++++++++++++------------ 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/functions.cpp b/functions.cpp index e23147e..30b96b7 100644 --- a/functions.cpp +++ b/functions.cpp @@ -104,6 +104,19 @@ void findSeason(std::set &files, int season, const std::string &pat } } +void iterateFS(std::map> &seasons, const std::string &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( searchSeason(p, season_pos) ) + seasons[atoi(p+season_pos)].insert(path + "/" + p); + } +} + void iterateFS(std::set &seasons, const std::string &path) { size_t season_pos{std::string::npos}; // season_pos - position of first digit of the season for( const auto p: FSLib::Directory(path) ) { diff --git a/functions.hpp b/functions.hpp index 70734fa..36affef 100644 --- a/functions.hpp +++ b/functions.hpp @@ -4,6 +4,7 @@ #include #include #include +#include class Curl { public: @@ -16,6 +17,7 @@ private: 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::map> &seasons, 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 d93b1b4..a041dd5 100644 --- a/tv_rename.cpp +++ b/tv_rename.cpp @@ -10,7 +10,7 @@ #include #include "filesystem.hpp" -void singleSeason( const std::string &path, const std::string &show, int season, std::string url, const std::string &language, const bool &linux, const bool &trust, Curl &c); +void singleSeason( const std::string &path, const std::string &show, int season, std::string url, const std::string &language, const bool &linux, const bool &trust, Curl &c, std::set const *files=nullptr); void multipleSeasons( const std::string &path, const std::string &show, const std::set seasons, const std::string &language, const bool &linux, const bool &trust, Curl &c); void allSeasons( const std::string &path, const std::string &show, const std::string &language, const bool &linux, const bool &trust, Curl &c); bool findLanguage( const char *language ); @@ -173,7 +173,8 @@ int main(int argc, char** argv) { } } -void parseEpisodeNames( const std::string &season_code, std::vector &episodes, const std::string &language) { +std::vector parseEpisodeNames( const std::string &season_code, const std::string &language) { + std::vector episodes; size_t pos = 0; while( true ) { pos = season_code.find("ge=\"" + language + "\" ", pos); @@ -191,9 +192,10 @@ void parseEpisodeNames( const std::string &season_code, std::vector break; } } + return episodes; } -void singleSeason( const std::string &path, const std::string &show, int season, std::string url, const std::string &language, const bool &linux, const bool &trust, Curl &c) { +void singleSeason( const std::string &path, const std::string &show, int season, std::string url, const std::string &language, const bool &linux, const bool &trust, Curl &c, std::set const *files) { if( url.empty() ) url = getDefUrl(show, language, c); url += "/seasons/" + std::to_string(season); @@ -216,24 +218,25 @@ void singleSeason( const std::string &path, const std::string &show, int season, return; } - std::vector episodes; - //get episode names in all languages - parseEpisodeNames(season_code, episodes, language); + auto episodes = parseEpisodeNames(season_code, language); if( episodes.empty() ) return; - std::set files; + std::set found_files; std::set renamed_files; std::vector renamed_episodes; renamed_episodes.resize(episodes.size()); - findSeason(files, season, path); + if( files == nullptr ) { + findSeason(found_files, season, path); + files = &found_files; + } - if( files.empty() ) + if( files->empty() ) return; - for( const auto &x : files ) { + for( const auto &x : *files ) { auto last = x.find_last_of("/"); std::string name; std::string dir; @@ -292,7 +295,7 @@ void singleSeason( const std::string &path, const std::string &show, int season, return; } - auto orig = files.begin(); + auto orig = files->begin(); for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) { FSLib::rename(*orig, *renamed); @@ -307,8 +310,15 @@ void multipleSeasons( const std::string &path, const std::string &show, const st } } +void multipleSeasons( const std::string &path, const std::string &show, const std::map> &seasons, const std::string &language, const bool &linux, const bool &trust, Curl &c) { + auto url = getDefUrl(show, language, c); + for( const auto &x : seasons ) { + singleSeason( path, show, x.first, url, language, linux, trust, c, &x.second); + } +} + void allSeasons( const std::string &path, const std::string &show, const std::string &language, const bool &linux, const bool &trust, Curl &c) { - std::set seasons; + std::map> seasons; //get all season number from this directory and subdirectories iterateFS(seasons, path); multipleSeasons( path, show, seasons, language, linux, trust, c);