tv_rename/tv_rename.cpp

335 lines
11 KiB
C++
Raw Normal View History

2019-01-07 22:24:28 +00:00
#include <algorithm>
2018-09-22 22:50:42 +00:00
#include <iostream>
#include <string.h>
#include "functions.hpp"
#include <map>
#include <set>
#include <string>
#include <sstream>
#include <getopt.h>
2019-01-07 22:24:28 +00:00
#include <vector>
2019-01-03 18:01:43 +00:00
#include "filesystem.hpp"
2018-09-22 22:50:42 +00:00
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);
2019-01-17 00:24:04 +00:00
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);
bool findLanguage( const char *language );
constexpr std::array<const char *, 46> 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"
};
2019-01-07 21:24:00 +00:00
void printLangs() {
2019-01-17 00:24:04 +00:00
for( size_t i = 0; i < languages.size(); i += 2 ) {
std::cout << languages[i] << " - " << languages[i+1] << std::endl;
2019-01-07 21:24:00 +00:00
}
}
void parseSeasonNumbers(std::set<int> &seasons_num, const char *argument) {
size_t pos{0};
2019-01-07 21:24:00 +00:00
while(!isdigit(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);
}
}
2019-01-17 00:24:04 +00:00
int parseCommandLine(std::string &show, std::set<int> &seasons_num, std::string &path, bool &change_dir, std::string &language, bool &linux, bool &trust, 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' },
2019-01-09 21:19:47 +00:00
{"print-langs", no_argument, 0, '0' },
{"help", no_argument, 0, 'h' }
};
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);
2019-01-07 21:24:00 +00:00
// if path provided, assume it's correct
change_dir = false;
break;
case 't':
trust = true;
break;
case 'x':
linux = true;
break;
case 'l':
2019-01-17 00:24:04 +00:00
if( findLanguage(optarg) ) {
language = optarg;
} else {
std::cerr << "Invalid language choice" << std::endl;
2019-01-07 21:24:00 +00:00
printLangs();
2019-01-09 21:19:47 +00:00
return -1;
}
break;
case '0':
2019-01-07 21:24:00 +00:00
printLangs();
2019-01-09 21:19:47 +00:00
return 1;
case 'h':
printHelp();
2019-01-09 21:19:47 +00:00
return 1;
default:
2019-01-09 21:19:47 +00:00
return -1;
}
}
2019-01-09 21:19:47 +00:00
return 0;
}
2018-09-22 22:50:42 +00:00
int main(int argc, char** argv) {
std::string show{};
std::set<int> seasons_num;
std::string path{"."};
bool change_dir{true};
2019-01-17 00:24:04 +00:00
std::string language{"en"};
bool linux{false};
bool trust{false};
Curl c;
2019-01-09 21:19:47 +00:00
{
2019-01-17 00:24:04 +00:00
auto tmp = parseCommandLine(show, seasons_num, path, change_dir, language, linux, trust, argc, argv);
2019-01-09 21:19:47 +00:00
if( tmp == -1 )
return 1;
else if ( tmp == 1 )
return 0;
}
2019-01-07 21:24:00 +00:00
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;
std::getline(std::cin, path);
continue;
}
std::cout << "Is this the right directory? " << FSLib::canonical(path) << 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);
}
}
if( show.empty() ) {
auto pos = show.find_last_of('/');
if( pos != std::string::npos )
show = show.substr(++pos);
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_num.size() == 1 ) {
2019-01-17 00:24:04 +00:00
singleSeason(path, show, *seasons_num.begin(), "", language, linux, trust, c);
} else if ( seasons_num.size() != 0 ) {
2019-01-17 00:24:04 +00:00
multipleSeasons(path, show, seasons_num, language, linux, trust, c);
} else {
2019-01-17 00:24:04 +00:00
allSeasons(path, show, language, linux, trust, c);
}
2018-09-22 22:50:42 +00:00
}
std::vector<std::string> parseEpisodeNames( const std::string &season_code, const std::string &language) {
std::vector<std::string> episodes;
2019-01-07 22:24:28 +00:00
size_t pos = 0;
while( true ) {
pos = season_code.find("ge=\"" + language + "\" ", pos);
if( pos != std::string::npos ) {
pos+= 17;
while( isspace(season_code[pos]) )
pos++;
auto end = season_code.find("<", pos);
end--;
while( isspace(season_code[end]) )
end--;
end++;
episodes.push_back(season_code.substr(pos, end - pos));
} else {
break;
}
}
return episodes;
2019-01-07 22:24:28 +00:00
}
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() )
url = getDefUrl(show, language, c);
2019-01-07 21:24:00 +00:00
url += "/seasons/" + std::to_string(season);
//get source code of season's page
auto season_code = c.execute(url);
//remove newlines
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
season_code = season_code.substr(900);
//get only the episode names
auto pos = season_code.find("\"translations\"");
if( pos != std::string::npos ) {
season_code = season_code.substr(pos);
pos = season_code.find("table");
if( pos != std::string::npos )
season_code = season_code.substr(0,pos);
else
return;
} else {
return;
}
2019-01-07 22:24:28 +00:00
auto episodes = parseEpisodeNames(season_code, language);
2019-01-07 22:24:28 +00:00
if( episodes.empty() )
return;
2019-01-07 22:24:28 +00:00
std::set<std::string> found_files;
std::set<std::string> renamed_files;
std::vector<std::string> renamed_episodes;
2019-01-07 22:24:28 +00:00
renamed_episodes.resize(episodes.size());
if( files == nullptr ) {
findSeason(found_files, season, path);
files = &found_files;
}
2019-01-07 22:24:28 +00:00
if( files->empty() )
return;
2019-01-07 22:24:28 +00:00
for( const auto &x : *files ) {
auto last = x.find_last_of("/");
std::string name;
std::string dir;
if( last == static_cast<size_t>(-1) ) {
name = x;
dir = ".";
} else {
name = x.substr(last+1);
dir = x.substr(0, last);
}
unsigned long num;
2019-01-07 21:24:00 +00:00
if( searchSpecificSeason(x.c_str(), pos, std::to_string(season)) ) {
num = atoi(x.c_str()+pos);
} else {
continue;
}
num -= 1;
if( num < episodes.size() ) {
auto pos = name.find_last_of('.');
name.insert(pos, " - " + episodes[num]);
if( !linux ) {
2019-01-07 22:24:28 +00:00
season_code.erase(std::remove_if(name.begin(), name.end(), [](char x) {return x == '?' || x == '"' || x == '\\' || x == '|' || x == '*';}), season_code.end());
size_t max{name.size()};
for( size_t i = 0; i < max ; i++ ) {
if( name[i] == '<' ) {
name[i] = 'i';
name.insert(i+1, "s less than");
max += 11;
} else if ( name[i] == '>' ) {
name[i] = 'i';
name.insert(i+1, "s more than");
max += 11;
} else if ( name[i] == ':' ) {
name[i] = ' ';
name.insert(i+1, 1, '-');
max++;
}
}
}
renamed_files.insert(dir + "/" + name);
renamed_episodes[num] = name;
}
}
2019-01-07 22:24:28 +00:00
for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) {
std::cout << *renamed << std::endl;
}
2019-01-07 22:24:28 +00:00
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;
}
2019-01-07 22:24:28 +00:00
auto orig = files->begin();
2019-01-07 22:24:28 +00:00
for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) {
FSLib::rename(*orig, *renamed);
++orig;
}
2018-09-22 22:50:42 +00:00
}
2019-01-17 00:24:04 +00:00
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) {
auto url = getDefUrl(show, language, c);
for( const auto &x : seasons ) {
2019-01-17 00:24:04 +00:00
singleSeason( path, show, x, url, language, linux, trust, c);
}
2018-09-22 22:50:42 +00:00
}
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);
}
}
2019-01-17 00:24:04 +00:00
void allSeasons( const std::string &path, const std::string &show, const std::string &language, const bool &linux, const bool &trust, Curl &c) {
std::map<int, std::set<std::string>> seasons;
//get all season number from this directory and subdirectories
iterateFS(seasons, path);
2019-01-17 00:24:04 +00:00
multipleSeasons( path, show, seasons, language, linux, trust, c);
}
bool findLanguage( const char *language ) {
for( size_t i = 0; i < languages.size(); i += 2 ) {
if( !strcmp(language, languages[i]) )
return true;
}
return false;
2018-09-22 22:50:42 +00:00
}