Use new functions, slight clean up

This commit is contained in:
zvon 2019-01-07 22:24:00 +01:00
parent ddcb8e3215
commit c2fd4cd106

View File

@ -26,10 +26,16 @@ std::map<std::string, std::string> languages{
{"sl", "Slovenian"}, {"hr", "Croatian"}, {"ko","Korea"}
};
void printLangs() {
for( const auto &x : languages ) {
std::cout << x.first << " - " << x.second << std::endl;
}
}
void parseSeasonNumbers(std::set<int> &seasons_num, const char *argument) {
size_t pos{0};
while((argument[pos] < '0' || argument[pos] > '9') && argument[pos] != ' ' && argument[pos] != '\0')
while(!isdigit(argument[pos]) && argument[pos] != '\0')
pos++;
if( argument[pos] == '\0' ) {
@ -75,8 +81,8 @@ bool parseCommandLine(std::string &show, std::set<int> &seasons_num, std::string
break;
case 'p':
path = std::string(optarg);
if( !FSLib::isDirectory(path) )
change_dir = true;
// if path provided, assume it's correct
change_dir = false;
break;
case 't':
trust = true;
@ -89,13 +95,12 @@ bool parseCommandLine(std::string &show, std::set<int> &seasons_num, std::string
language = optarg;
} else {
std::cerr << "Invalid language choice" << std::endl;
printLangs();
return false;
}
break;
case '0':
for( const auto &x : languages ) {
std::cout << x.first << " - " << x.second << std::endl;
}
printLangs();
return true;
case 'h':
printHelp();
@ -117,6 +122,9 @@ int main(int argc, char** argv) {
if( !parseCommandLine(show, seasons_num, path, change_dir, argc, argv) )
return 1;
if( !FSLib::isDirectory(path) )
change_dir = true;
while( change_dir ) {
if( !FSLib::isDirectory(path) ) {
std::cout << "This directory doesn't exist, please insert a correct path: " << std::endl;
@ -163,8 +171,7 @@ int main(int argc, char** argv) {
void singleSeason( const std::string &path, const std::string &show, int season, std::string url) {
if( url.empty() )
url = getDefUrl(show, language, c);
url += "/seasons/";
url += std::to_string(season);
url += "/seasons/" + std::to_string(season);
//get source code of season's page
auto season_code = c.execute(url);
//remove newlines
@ -215,9 +222,9 @@ void singleSeason( const std::string &path, const std::string &show, int season,
dir = x.substr(0, last);
}
unsigned long num;
try {
num = std::stoi(std::regex_replace(name, std::regex(".*[sS][0]{0,2000}" + std::to_string(season) + "[eE]([0-9]{1,2000}).*"), "$1"));
} catch (std::exception &e) {
if( searchSpecificSeason(x.c_str(), pos, std::to_string(season)) ) {
num = atoi(x.c_str()+pos);
} else {
continue;
}
num -= 1;
@ -234,10 +241,8 @@ void singleSeason( const std::string &path, const std::string &show, int season,
renamed_episodes[num] = name;
}
}
auto orig = files.begin();
for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) {
std::cout << *renamed << std::endl;
++orig;
}
if( !trust ) {
std::cout << "Does this seem ok? (y/n) ";
@ -248,7 +253,7 @@ void singleSeason( const std::string &path, const std::string &show, int season,
if( response[0] != 'y' && response[0] != 'Y' )
return;
}
orig = files.begin();
auto orig = files.begin();
for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) {
FSLib::rename(*orig, *renamed);
++orig;