224 lines
7.3 KiB
C++
224 lines
7.3 KiB
C++
#include <iostream>
|
|
#include <string.h>
|
|
#include "functions.hpp"
|
|
#include <map>
|
|
#include <regex>
|
|
#include <set>
|
|
#include <string>
|
|
#include <sstream>
|
|
#include <filesystem>
|
|
|
|
void singleSeason( const std::string &path, const std::string &show, int season, std::string url );
|
|
void multipleSeasons( const std::string &path, const std::string &show, const std::set<int> seasons );
|
|
void allSeasons( const std::string &path, const std::string &show );
|
|
|
|
std::string language{"en"};
|
|
bool trust{false};
|
|
bool linux{false};
|
|
Curl c;
|
|
|
|
int main(int argc, char** argv) {
|
|
namespace fs = std::filesystem;
|
|
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"}
|
|
};
|
|
|
|
std::string show{};
|
|
std::string seasons{""};
|
|
std::set<int> seasons_num;
|
|
std::string path{"."};
|
|
bool change_dir{true};
|
|
|
|
int x = 1;
|
|
while ( x < argc ) {
|
|
if( !(strcmp("-s", argv[x]) && strcmp("--show", argv[x])) ) {
|
|
show = argv[x+1];
|
|
x++;
|
|
} else if ( !(strcmp("-n", argv[x]) && strcmp("--season", argv[x])) ) {
|
|
seasons = std::regex_replace(argv[x+1], std::regex("[^[0-9 ]]*"), "");
|
|
x++;
|
|
} else if ( !(strcmp("-c", argv[x]) && strcmp("--correct-path", argv[x])) ) {
|
|
change_dir = false;
|
|
} else if ( !(strcmp("-sp", argv[x]) && strcmp("--show-path", argv[x])) ) {
|
|
path = std::string(argv[x+1]);
|
|
x++;
|
|
if( !fs::exists(fs::absolute(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 ) {
|
|
auto abs = fs::absolute(path);
|
|
if( !fs::exists(abs) ) {
|
|
std::cout << "This directory doesn't exist, please insert a correct path: " << std::endl;
|
|
std::getline(std::cin, path);
|
|
continue;
|
|
}
|
|
std::cout << "Is this the right directory? " << fs::canonical(abs) << std::endl;
|
|
std::string response;
|
|
std::cin >> response;
|
|
std::cin.ignore(1,'\n');
|
|
std::cin.clear();
|
|
if ( response[0] == 'y' || response[0] == 'Y' ) {
|
|
change_dir = false;
|
|
} else {
|
|
std::cout << "Insert correct path:" << std::endl;
|
|
std::getline(std::cin, path);
|
|
}
|
|
}
|
|
|
|
path = fs::absolute(path);
|
|
|
|
if( show.empty() ) {
|
|
show = std::regex_replace(fs::canonical(path).c_str(), std::regex(".*/"), "");
|
|
std::cout << "Is this the right show name? " << show << std::endl;
|
|
std::string response;
|
|
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) {
|
|
if( url.empty() )
|
|
url = getDefUrl(show, language, c);
|
|
url += "/seasons/";
|
|
url += std::to_string(season);
|
|
//get source code of season's page
|
|
auto season_code = c.execute(url);
|
|
//remove newlines cause regex can't multiline
|
|
season_code = std::regex_replace(season_code, std::regex("[\r\n]"), "");
|
|
//first 900 chars or so are useless to us, no need to regex through them
|
|
season_code = season_code.substr(900);
|
|
//get only the episode names
|
|
std::smatch season_match;
|
|
if( std::regex_search(season_code, season_match, std::regex("<table class=.*?id=\"translations\">.*</table>")) ) {
|
|
season_code = season_match[0];
|
|
} else {
|
|
return;
|
|
}
|
|
std::regex title(".*<span.*?language=\"" + language + "\".*?>\\s*(.*?)\\s*?</span>.*");
|
|
std::regex episode_link("<td>.*?</td>");
|
|
std::regex language_reg("<span.*?</span>");
|
|
std::smatch ep_match;
|
|
std::vector<std::string> 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"));
|
|
}
|
|
if( episodes.empty() )
|
|
return;
|
|
std::set<std::experimental::filesystem::path> files;
|
|
std::set<std::experimental::filesystem::path> renamed_files;
|
|
std::vector<std::string> renamed_episodes;
|
|
renamed_episodes.resize(episodes.size());
|
|
findSeason(files, season, path);
|
|
if( files.empty() )
|
|
return;
|
|
for( const auto &x : files ) {
|
|
std::string name = x.filename();
|
|
std::string dir = x.parent_path();
|
|
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) {
|
|
continue;
|
|
}
|
|
num -= 1;
|
|
if( num < episodes.size() ) {
|
|
name = std::regex_replace(name, std::regex("(.*)\\.(.*)"), "$1 - " + episodes[num] + ".$2");
|
|
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(":"), " -");
|
|
}
|
|
renamed_files.insert(dir + "/" + name);
|
|
renamed_episodes[num] = name;
|
|
}
|
|
}
|
|
auto orig = files.begin();
|
|
namespace fs = std::experimental::filesystem;
|
|
for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) {
|
|
std::cout << std::string((*orig).filename()) << " -> " << std::string((*renamed).filename()) << std::endl;
|
|
++orig;
|
|
}
|
|
if( !trust ) {
|
|
std::cout << "Does this seem ok? (y/n) ";
|
|
std::string response;
|
|
std::cin >> response;
|
|
std::cin.clear();
|
|
std::cin.ignore(1, '\n');
|
|
if( response[0] != 'y' && response[0] != 'Y' )
|
|
return;
|
|
}
|
|
orig = files.begin();
|
|
namespace fs = std::experimental::filesystem;
|
|
for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) {
|
|
fs::rename(*orig, *renamed);
|
|
++orig;
|
|
}
|
|
}
|
|
|
|
void multipleSeasons( const std::string &path, const std::string &show, const std::set<int> seasons) {
|
|
auto url = getDefUrl(show, language, c);
|
|
for( const auto &x : seasons ) {
|
|
singleSeason( path, show, x, url);
|
|
}
|
|
}
|
|
|
|
void allSeasons( const std::string &path, const std::string &show) {
|
|
std::set<int> seasons;
|
|
//get all season number from this directory and subdirectories
|
|
iterateFS(seasons, path);
|
|
multipleSeasons( path, show, seasons);
|
|
}
|
|
|