Use getopt_long for command line parsing, fix seasons parsing (when only a-zA-Z were present it behaved unpredictably), spaces instead of tabs
This commit is contained in:
parent
e45615b6c0
commit
c014a059b6
433
tv_rename.cpp
433
tv_rename.cpp
@ -6,6 +6,7 @@
|
|||||||
#include <set>
|
#include <set>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <getopt.h>
|
||||||
#include "filesystem.hpp"
|
#include "filesystem.hpp"
|
||||||
|
|
||||||
void singleSeason( const std::string &path, const std::string &show, int season, std::string url );
|
void singleSeason( const std::string &path, const std::string &show, int season, std::string url );
|
||||||
@ -15,221 +16,255 @@ void allSeasons( const std::string &path, const std::string &show );
|
|||||||
std::string language{"en"};
|
std::string language{"en"};
|
||||||
bool trust{false};
|
bool trust{false};
|
||||||
bool linux{false};
|
bool linux{false};
|
||||||
Curl c;
|
Curl c; // global so the connection doesn't close at every function
|
||||||
|
|
||||||
|
std::map<std::string, std::string> languages{
|
||||||
|
{"en", "English"}, {"sv", "Svenska"}, {"no", "Norsk"}, {"da", "Dansk"}, {"fi", "Suomeksi"},
|
||||||
|
{"nl", "Nederlands"}, {"de", "Deutsch"}, {"it", "Italiano"}, {"es", "Español"}, {"fr", "Français"},
|
||||||
|
{"pl", "Polski"}, {"hu", "Magyar"}, {"el", "Greek"}, {"tr", "Turkish"}, {"ru", "Russian"},
|
||||||
|
{"he", "Hebrew"}, {"ja", "Japanese"}, {"pt", "Portuguese"}, {"zh", "Chinese"}, {"cs", "Czech"},
|
||||||
|
{"sl", "Slovenian"}, {"hr", "Croatian"}, {"ko","Korea"}
|
||||||
|
};
|
||||||
|
|
||||||
|
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')
|
||||||
|
pos++;
|
||||||
|
|
||||||
|
if( argument[pos] == '\0' ) {
|
||||||
|
seasons_num.clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int temp;
|
||||||
|
std::istringstream iss(&optarg[pos]);
|
||||||
|
while(iss >> temp) {
|
||||||
|
seasons_num.insert(temp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool parseCommandLine(std::string &show, std::set<int> &seasons_num, std::string &path, bool &change_dir, int argc, char **argv) {
|
||||||
|
static struct option long_options[] = {
|
||||||
|
{"show", required_argument, 0, 's' },
|
||||||
|
{"season", required_argument, 0, 'n' },
|
||||||
|
{"correct-path", no_argument, 0, 'c' },
|
||||||
|
{"show-path", required_argument, 0, 'p' },
|
||||||
|
{"trust", no_argument, 0, 't' },
|
||||||
|
{"linux", no_argument, 0, 'x' },
|
||||||
|
{"lang", required_argument, 0, 'l' },
|
||||||
|
{"print-langs", no_argument, 0, 0 },
|
||||||
|
{"help", no_argument, 0, 'h' },
|
||||||
|
{0, 0, 0, 0 }
|
||||||
|
};
|
||||||
|
|
||||||
|
while(1) {
|
||||||
|
int option_index{0};
|
||||||
|
auto c = getopt_long(argc, argv, "s:n:cp:txl:0h", long_options, &option_index);
|
||||||
|
if( c == -1 )
|
||||||
|
break;
|
||||||
|
switch(c) {
|
||||||
|
case 's':
|
||||||
|
show = optarg;
|
||||||
|
break;
|
||||||
|
case 'n':
|
||||||
|
parseSeasonNumbers(seasons_num, optarg);
|
||||||
|
break;
|
||||||
|
case 'c':
|
||||||
|
change_dir = false;
|
||||||
|
break;
|
||||||
|
case 'p':
|
||||||
|
path = std::string(optarg);
|
||||||
|
if( !FSLib::isDirectory(path) )
|
||||||
|
change_dir = true;
|
||||||
|
break;
|
||||||
|
case 't':
|
||||||
|
trust = true;
|
||||||
|
break;
|
||||||
|
case 'x':
|
||||||
|
linux = true;
|
||||||
|
break;
|
||||||
|
case 'l':
|
||||||
|
if( languages.find(optarg) != languages.end() ) {
|
||||||
|
language = optarg;
|
||||||
|
} else {
|
||||||
|
std::cerr << "Invalid language choice" << std::endl;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case '0':
|
||||||
|
for( const auto &x : languages ) {
|
||||||
|
std::cout << x.first << " - " << x.second << std::endl;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
case 'h':
|
||||||
|
printHelp();
|
||||||
|
return true;
|
||||||
|
default:
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
std::map<std::string, std::string> languages{
|
std::string show{};
|
||||||
{"en", "English"}, {"sv", "Svenska"}, {"no", "Norsk"}, {"da", "Dansk"}, {"fi", "Suomeksi"},
|
std::set<int> seasons_num;
|
||||||
{"nl", "Nederlands"}, {"de", "Deutsch"}, {"it", "Italiano"}, {"es", "Español"}, {"fr", "Français"},
|
std::string path{"."};
|
||||||
{"pl", "Polski"}, {"hu", "Magyar"}, {"el", "Greek"}, {"tr", "Turkish"}, {"ru", "Russian"},
|
bool change_dir{true};
|
||||||
{"he", "Hebrew"}, {"ja", "Japanese"}, {"pt", "Portuguese"}, {"zh", "Chinese"}, {"cs", "Czech"},
|
|
||||||
{"sl", "Slovenian"}, {"hr", "Croatian"}, {"ko","Korea"}
|
|
||||||
};
|
|
||||||
|
|
||||||
std::string show{};
|
parseCommandLine(show, seasons_num, path, change_dir, argc, argv);
|
||||||
std::string seasons{""};
|
|
||||||
std::set<int> seasons_num;
|
|
||||||
std::string path{"."};
|
|
||||||
bool change_dir{true};
|
|
||||||
|
|
||||||
int x = 1;
|
while( change_dir ) {
|
||||||
while ( x < argc ) {
|
if( !FSLib::isDirectory(path) ) {
|
||||||
if( !(strcmp("-s", argv[x]) && strcmp("--show", argv[x])) ) {
|
std::cout << "This directory doesn't exist, please insert a correct path: " << std::endl;
|
||||||
show = argv[x+1];
|
std::getline(std::cin, path);
|
||||||
x++;
|
continue;
|
||||||
} else if ( !(strcmp("-n", argv[x]) && strcmp("--season", argv[x])) ) {
|
}
|
||||||
size_t pos{0};
|
std::cout << "Is this the right directory? " << FSLib::canonical(path) << std::endl;
|
||||||
while((argv[x+1][pos] < '0' || argv[x+1][pos] > '9') && argv[x+1][pos] != ' ')
|
std::string response;
|
||||||
pos++;
|
std::cin >> response;
|
||||||
seasons = std::string(&argv[x+1][pos]);
|
std::cin.ignore(1,'\n');
|
||||||
x++;
|
std::cin.clear();
|
||||||
} else if ( !(strcmp("-c", argv[x]) && strcmp("--correct-path", argv[x])) ) {
|
if ( response[0] == 'y' || response[0] == 'Y' ) {
|
||||||
change_dir = false;
|
change_dir = false;
|
||||||
} else if ( !(strcmp("-sp", argv[x]) && strcmp("--show-path", argv[x])) ) {
|
} else {
|
||||||
path = std::string(argv[x+1]);
|
std::cout << "Insert correct path:" << std::endl;
|
||||||
x++;
|
std::getline(std::cin, path);
|
||||||
if( !FSLib::isDirectory(path) )
|
}
|
||||||
change_dir = true;
|
}
|
||||||
} else if ( !(strcmp("-t", argv[x]) && strcmp("--trust", argv[x])) ) {
|
|
||||||
trust = true;
|
|
||||||
} else if ( !(strcmp("-x", argv[x]) && strcmp("--linux", argv[x])) ) {
|
|
||||||
linux = true;
|
|
||||||
} else if ( !(strcmp("-l", argv[x]) && strcmp("--lang", argv[x])) ) {
|
|
||||||
if( languages.find(argv[x+1]) != languages.end() ) {
|
|
||||||
language = argv[x+1];
|
|
||||||
} else {
|
|
||||||
std::cerr << "Invalid language choice" << std::endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
x++;
|
|
||||||
} else if ( !(strcmp("-p", argv[x]) && strcmp("--print-langs", argv[x])) ) {
|
|
||||||
for( const auto &x : languages ) {
|
|
||||||
std::cout << x.first << " - " << x.second << std::endl;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
} else if ( !(strcmp("-h", argv[x]) && strcmp("--help", argv[x])) ) {
|
|
||||||
printHelp();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
x++;
|
|
||||||
}
|
|
||||||
|
|
||||||
while( change_dir ) {
|
if( show.empty() ) {
|
||||||
if( !FSLib::isDirectory(path) ) {
|
auto pos = show.find_last_of('/');
|
||||||
std::cout << "This directory doesn't exist, please insert a correct path: " << std::endl;
|
if( pos != std::string::npos )
|
||||||
std::getline(std::cin, path);
|
show = show.substr(++pos);
|
||||||
continue;
|
std::cout << "Is this the right show name? " << show << std::endl;
|
||||||
}
|
std::string response;
|
||||||
std::cout << "Is this the right directory? " << FSLib::canonical(path) << std::endl;
|
std::cin >> response;
|
||||||
std::string response;
|
std::cin.ignore(1, '\n');
|
||||||
std::cin >> response;
|
std::cin.clear();
|
||||||
std::cin.ignore(1,'\n');
|
if( response[0] != 'y' && response[0] != 'Y' ) {
|
||||||
std::cin.clear();
|
std::cout << "Insert the correct show name: " << std::endl;
|
||||||
if ( response[0] == 'y' || response[0] == 'Y' ) {
|
std::getline(std::cin, show);
|
||||||
change_dir = false;
|
}
|
||||||
} else {
|
}
|
||||||
std::cout << "Insert correct path:" << std::endl;
|
|
||||||
std::getline(std::cin, path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if( show.empty() ) {
|
if( seasons_num.size() == 1 ) {
|
||||||
auto pos = show.find_last_of('/');
|
singleSeason(path, show, *seasons_num.begin(), "");
|
||||||
if( pos != std::string::npos )
|
} else if ( seasons_num.size() != 0 ) {
|
||||||
show = show.substr(++pos);
|
multipleSeasons(path, show, seasons_num);
|
||||||
std::cout << "Is this the right show name? " << show << std::endl;
|
} else {
|
||||||
std::string response;
|
allSeasons(path, show);
|
||||||
std::cin >> response;
|
}
|
||||||
std::cin.ignore(1, '\n');
|
|
||||||
std::cin.clear();
|
|
||||||
if( response[0] != 'y' && response[0] != 'Y' ) {
|
|
||||||
std::cout << "Insert the correct show name: " << std::endl;
|
|
||||||
std::getline(std::cin, show);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if( !seasons.empty() ) {
|
|
||||||
int temp;
|
|
||||||
std::istringstream iss(seasons);
|
|
||||||
while(iss >> temp) {
|
|
||||||
seasons_num.insert(temp);
|
|
||||||
}
|
|
||||||
if( seasons_num.size() == 1 ) {
|
|
||||||
singleSeason(path, show, temp, "");
|
|
||||||
} else {
|
|
||||||
multipleSeasons(path, show, seasons_num);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
allSeasons(path, show);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void singleSeason( const std::string &path, const std::string &show, int season, std::string url) {
|
void singleSeason( const std::string &path, const std::string &show, int season, std::string url) {
|
||||||
if( url.empty() )
|
if( url.empty() )
|
||||||
url = getDefUrl(show, language, c);
|
url = getDefUrl(show, language, c);
|
||||||
url += "/seasons/";
|
url += "/seasons/";
|
||||||
url += std::to_string(season);
|
url += std::to_string(season);
|
||||||
//get source code of season's page
|
//get source code of season's page
|
||||||
auto season_code = c.execute(url);
|
auto season_code = c.execute(url);
|
||||||
//remove newlines
|
//remove newlines
|
||||||
season_code.erase(std::remove_if(season_code.begin(), season_code.end(), [](char x) {return x == '\r' || x == '\n';}), season_code.end());
|
season_code.erase(std::remove_if(season_code.begin(), season_code.end(), [](char x) {return x == '\r' || x == '\n';}), season_code.end());
|
||||||
//first 900 chars or so are useless to us
|
//first 900 chars or so are useless to us
|
||||||
season_code = season_code.substr(900);
|
season_code = season_code.substr(900);
|
||||||
//get only the episode names
|
//get only the episode names
|
||||||
auto pos = season_code.find("\"translations\"");
|
auto pos = season_code.find("\"translations\"");
|
||||||
if( pos != std::string::npos ) {
|
if( pos != std::string::npos ) {
|
||||||
season_code = season_code.substr(pos);
|
season_code = season_code.substr(pos);
|
||||||
pos = season_code.find("table");
|
pos = season_code.find("table");
|
||||||
if( pos != std::string::npos )
|
if( pos != std::string::npos )
|
||||||
season_code = season_code.substr(0,pos);
|
season_code = season_code.substr(0,pos);
|
||||||
else
|
else
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
std::regex title(".*<span.*?language=\"" + language + "\".*?>\\s*(.*?)\\s*?</span>.*");
|
std::regex title(".*<span.*?language=\"" + language + "\".*?>\\s*(.*?)\\s*?</span>.*");
|
||||||
std::regex episode_link("<td>.*?</td>");
|
std::regex episode_link("<td>.*?</td>");
|
||||||
std::smatch ep_match;
|
std::smatch ep_match;
|
||||||
std::vector<std::string> episodes;
|
std::vector<std::string> episodes;
|
||||||
//get episode names in all languages
|
//get episode names in all languages
|
||||||
for( std::sregex_iterator it(season_code.begin(), season_code.end(), episode_link); it != std::sregex_iterator(); ++it) {
|
for( std::sregex_iterator it(season_code.begin(), season_code.end(), episode_link); it != std::sregex_iterator(); ++it) {
|
||||||
auto input = (*it).str();
|
auto input = (*it).str();
|
||||||
//only get the selected language
|
//only get the selected language
|
||||||
if( std::regex_search( input, title ) )
|
if( std::regex_search( input, title ) )
|
||||||
episodes.push_back(std::regex_replace(input, title, "$1"));
|
episodes.push_back(std::regex_replace(input, title, "$1"));
|
||||||
}
|
}
|
||||||
if( episodes.empty() )
|
if( episodes.empty() )
|
||||||
return;
|
return;
|
||||||
std::set<std::string> files;
|
std::set<std::string> 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);
|
findSeason(files, season, path);
|
||||||
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;
|
||||||
if( last == static_cast<size_t>(-1) ) {
|
if( last == static_cast<size_t>(-1) ) {
|
||||||
name = x;
|
name = x;
|
||||||
dir = ".";
|
dir = ".";
|
||||||
} else {
|
} else {
|
||||||
name = x.substr(last+1);
|
name = x.substr(last+1);
|
||||||
dir = x.substr(0, last);
|
dir = x.substr(0, last);
|
||||||
}
|
}
|
||||||
unsigned long num;
|
unsigned long num;
|
||||||
try {
|
try {
|
||||||
num = std::stoi(std::regex_replace(name, std::regex(".*[sS][0]{0,2000}" + std::to_string(season) + "[eE]([0-9]{1,2000}).*"), "$1"));
|
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) {
|
} catch (std::exception &e) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
num -= 1;
|
num -= 1;
|
||||||
if( num < episodes.size() ) {
|
if( num < episodes.size() ) {
|
||||||
auto pos = name.find_last_of('.');
|
auto pos = name.find_last_of('.');
|
||||||
name.insert(pos, " - " + episodes[num]);
|
name.insert(pos, " - " + episodes[num]);
|
||||||
if( !linux ) {
|
if( !linux ) {
|
||||||
name = std::regex_replace(name, std::regex("[\\?\"\\\\|\\*]"), "");
|
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 less than");
|
||||||
name = std::regex_replace(name, std::regex(">"), "is more than");
|
name = std::regex_replace(name, std::regex(">"), "is more than");
|
||||||
name = std::regex_replace(name, std::regex(":"), " -");
|
name = std::regex_replace(name, std::regex(":"), " -");
|
||||||
}
|
}
|
||||||
renamed_files.insert(dir + "/" + name);
|
renamed_files.insert(dir + "/" + name);
|
||||||
renamed_episodes[num] = name;
|
renamed_episodes[num] = name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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) {
|
||||||
std::cout << *renamed << std::endl;
|
std::cout << *renamed << std::endl;
|
||||||
++orig;
|
++orig;
|
||||||
}
|
}
|
||||||
if( !trust ) {
|
if( !trust ) {
|
||||||
std::cout << "Does this seem ok? (y/n) ";
|
std::cout << "Does this seem ok? (y/n) ";
|
||||||
std::string response;
|
std::string response;
|
||||||
std::cin >> response;
|
std::cin >> response;
|
||||||
std::cin.clear();
|
std::cin.clear();
|
||||||
std::cin.ignore(1, '\n');
|
std::cin.ignore(1, '\n');
|
||||||
if( response[0] != 'y' && response[0] != 'Y' )
|
if( response[0] != 'y' && response[0] != 'Y' )
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
orig = files.begin();
|
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);
|
||||||
++orig;
|
++orig;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void multipleSeasons( const std::string &path, const std::string &show, const std::set<int> seasons) {
|
void multipleSeasons( const std::string &path, const std::string &show, const std::set<int> seasons) {
|
||||||
auto url = getDefUrl(show, language, c);
|
auto url = getDefUrl(show, language, c);
|
||||||
for( const auto &x : seasons ) {
|
for( const auto &x : seasons ) {
|
||||||
singleSeason( path, show, x, url);
|
singleSeason( path, show, x, url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void allSeasons( const std::string &path, const std::string &show) {
|
void allSeasons( const std::string &path, const std::string &show) {
|
||||||
std::set<int> seasons;
|
std::set<int> 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);
|
multipleSeasons( path, show, seasons);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user