Fewer filesystem operations when using 'all'

This commit is contained in:
zvon 2019-01-17 17:08:51 +01:00
parent 309b07d353
commit 46abfe3dd9
3 changed files with 37 additions and 12 deletions

View File

@ -104,6 +104,19 @@ void findSeason(std::set<std::string> &files, int season, const std::string &pat
} }
} }
void iterateFS(std::map<int, std::set<std::string>> &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<int> &seasons, const std::string &path) { void iterateFS(std::set<int> &seasons, const std::string &path) {
size_t season_pos{std::string::npos}; // season_pos - position of first digit of the season size_t season_pos{std::string::npos}; // season_pos - position of first digit of the season
for( const auto p: FSLib::Directory(path) ) { for( const auto p: FSLib::Directory(path) ) {

View File

@ -4,6 +4,7 @@
#include <string> #include <string>
#include <set> #include <set>
#include <curl/curl.h> #include <curl/curl.h>
#include <map>
class Curl { class Curl {
public: public:
@ -16,6 +17,7 @@ private:
std::string getDefUrl( 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::map<int, std::set<std::string>> &seasons, 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();

View File

@ -10,7 +10,7 @@
#include <vector> #include <vector>
#include "filesystem.hpp" #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<std::string> const *files=nullptr);
void multipleSeasons( const std::string &path, const std::string &show, const std::set<int> seasons, const std::string &language, const bool &linux, const bool &trust, Curl &c); void multipleSeasons( const std::string &path, const std::string &show, const std::set<int> 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); 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 ); bool findLanguage( const char *language );
@ -173,7 +173,8 @@ int main(int argc, char** argv) {
} }
} }
void parseEpisodeNames( const std::string &season_code, std::vector<std::string> &episodes, const std::string &language) { std::vector<std::string> parseEpisodeNames( const std::string &season_code, const std::string &language) {
std::vector<std::string> episodes;
size_t pos = 0; size_t pos = 0;
while( true ) { while( true ) {
pos = season_code.find("ge=\"" + language + "\" ", pos); pos = season_code.find("ge=\"" + language + "\" ", pos);
@ -191,9 +192,10 @@ void parseEpisodeNames( const std::string &season_code, std::vector<std::string>
break; 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<std::string> const *files) {
if( url.empty() ) if( url.empty() )
url = getDefUrl(show, language, c); url = getDefUrl(show, language, c);
url += "/seasons/" + std::to_string(season); url += "/seasons/" + std::to_string(season);
@ -216,24 +218,25 @@ void singleSeason( const std::string &path, const std::string &show, int season,
return; return;
} }
std::vector<std::string> episodes; auto episodes = parseEpisodeNames(season_code, language);
//get episode names in all languages
parseEpisodeNames(season_code, episodes, language);
if( episodes.empty() ) if( episodes.empty() )
return; return;
std::set<std::string> files; std::set<std::string> found_files;
std::set<std::string> renamed_files; std::set<std::string> renamed_files;
std::vector<std::string> renamed_episodes; std::vector<std::string> renamed_episodes;
renamed_episodes.resize(episodes.size()); 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; return;
for( const auto &x : files ) { for( const auto &x : *files ) {
auto last = x.find_last_of("/"); auto last = x.find_last_of("/");
std::string name; std::string name;
std::string dir; std::string dir;
@ -292,7 +295,7 @@ void singleSeason( const std::string &path, const std::string &show, int season,
return; return;
} }
auto orig = files.begin(); auto orig = files->begin();
for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) { for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) {
FSLib::rename(*orig, *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<int, std::set<std::string>> &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) { void allSeasons( const std::string &path, const std::string &show, const std::string &language, const bool &linux, const bool &trust, Curl &c) {
std::set<int> seasons; std::map<int, std::set<std::string>> seasons;
//get all season number from this directory and subdirectories //get all season number from this directory and subdirectories
iterateFS(seasons, path); iterateFS(seasons, path);
multipleSeasons( path, show, seasons, language, linux, trust, c); multipleSeasons( path, show, seasons, language, linux, trust, c);