362 lines
13 KiB
C++
362 lines
13 KiB
C++
#include "functions.hpp"
|
|
#include "filesystem.hpp"
|
|
#include <algorithm>
|
|
#include <iomanip>
|
|
#include <sstream>
|
|
|
|
#ifndef GUI
|
|
|
|
#include <iostream>
|
|
#include <curl/curl.h>
|
|
#include <stdlib.h>
|
|
#include <vector>
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
#include <pwd.h>
|
|
|
|
#endif
|
|
|
|
#ifndef GUI
|
|
|
|
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"
|
|
};
|
|
|
|
#endif
|
|
|
|
// encode url so it's valid even with UTF-8 characters
|
|
std::string encodeUrl( const std::string &url ) {
|
|
//stolen from here - https://stackoverflow.com/questions/154536/encode-decode-urls-in-c
|
|
std::ostringstream encoded;
|
|
encoded.fill('0');
|
|
encoded << std::hex;
|
|
for( auto &x : url ) {
|
|
if( std::isalnum(x) || x == '-' || x == '_' || x == '.' || x == '~' ) {
|
|
encoded << x;
|
|
continue;
|
|
}
|
|
encoded << std::uppercase << '%' << std::setw(2);
|
|
encoded << int(static_cast<unsigned char>(x)) << std::nouppercase;
|
|
}
|
|
return encoded.str();
|
|
}
|
|
|
|
// return true if filename has specified season
|
|
// set ep_pos to position where episode number starts
|
|
bool searchSpecificSeason(const char *const p, size_t &ep_pos, const std::string &number) {
|
|
size_t cur_pos{};
|
|
bool found_season{false};
|
|
while( p[cur_pos] != '\0' ) {
|
|
if( (p[cur_pos] == 's' || p[cur_pos] == 'S') && isdigit(p[cur_pos+1]) ) {
|
|
cur_pos++;
|
|
while( p[cur_pos] == '0' )
|
|
cur_pos++;
|
|
size_t offset{};
|
|
while( offset < number.size() && p[cur_pos + offset] == number[offset] )
|
|
offset++;
|
|
cur_pos += offset;
|
|
if( offset != number.size() )
|
|
continue;
|
|
if( (p[cur_pos] == 'e' || p[cur_pos] == 'E') && isdigit(p[cur_pos+1]) ) {
|
|
found_season = true;
|
|
ep_pos = cur_pos + 1;
|
|
break;
|
|
}
|
|
}
|
|
cur_pos++;
|
|
}
|
|
return found_season;
|
|
}
|
|
|
|
bool searchSpecificSeason(const char *const p, const std::string &number) {
|
|
size_t tmp;
|
|
return searchSpecificSeason(p, tmp, number);
|
|
}
|
|
|
|
bool searchSeason(const char *const p, size_t &season_pos) {
|
|
size_t cur_pos{};
|
|
bool found_season{false};
|
|
while( p[cur_pos] != '\0' ) {
|
|
if( (p[cur_pos] == 's' || p[cur_pos] == 'S') && isdigit(p[cur_pos+1]) ) {
|
|
cur_pos++;
|
|
season_pos = cur_pos; // after ++ because we want the first pos to point to season's number
|
|
while( isdigit(p[cur_pos]) )
|
|
cur_pos++;
|
|
if( (p[cur_pos] == 'e' || p[cur_pos] == 'E') && isdigit(p[cur_pos+1]) ) {
|
|
found_season = true;
|
|
break;
|
|
}
|
|
}
|
|
cur_pos++;
|
|
}
|
|
return found_season;
|
|
}
|
|
|
|
bool searchSeason(const char *const p) {
|
|
size_t tmp{};
|
|
return searchSeason(p, tmp);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
#ifndef GUI
|
|
|
|
void findSeason(std::set<std::string> &files, int season, const std::string &path) {
|
|
auto number = std::to_string(season);
|
|
|
|
for( const auto p: FSLib::Directory(path) ) {
|
|
if(FSLib::isDirectory(path + "/" + p)) {
|
|
findSeason(files, season, path + "/" + p);
|
|
continue;
|
|
}
|
|
|
|
if( searchSpecificSeason(p, number) )
|
|
files.insert(path + "/" + p);
|
|
}
|
|
}
|
|
|
|
void findSeasons(std::map<int, std::set<std::string>> &seasons, const std::string &path, const std::set<int> &season_numbers) {
|
|
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)) {
|
|
findSeasons(seasons, path + "/" + p, season_numbers);
|
|
continue;
|
|
}
|
|
|
|
if( searchSeason(p, season_pos) ) {
|
|
auto num = atoi(p+season_pos);
|
|
if( season_numbers.find(num) != season_numbers.end() )
|
|
seasons[num].insert(path + "/" + p);
|
|
}
|
|
}
|
|
}
|
|
|
|
std::string getDefUrl( std::string show, const std::string &language, Curl &c ) {
|
|
std::replace(show.begin(), show.end(), ' ', '+');
|
|
auto source_code = c.execute("https://www.thetvdb.com/search?q=" + encodeUrl(show) + "&l=" + language);
|
|
size_t order{}, pos{};
|
|
std::vector<std::string> urls;
|
|
while( true ) {
|
|
pos = source_code.find("/ser", pos);
|
|
if( pos != std::string::npos ) {
|
|
auto end = source_code.find(">", pos);
|
|
end--;
|
|
urls.push_back(source_code.substr(pos, end - pos));
|
|
pos = end + 2;
|
|
end = source_code.find("<", pos);
|
|
std::cout << ++order << ". " << source_code.substr(pos, end - pos) << std::endl;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
std::cout << "Which TV Show is the right one? ";
|
|
std::cin >> pos;
|
|
std::cin.clear();
|
|
std::cin.ignore(1, '\n');
|
|
return "https://www.thetvdb.com" + urls[pos-1];
|
|
}
|
|
|
|
void printHelp() {
|
|
std::cout << "usage: tv_rename [--help] [--show show name] [--season season number]" << std::endl;
|
|
std::cout << " [--correct-path] [--show-path show path] [--trust]" << std::endl;
|
|
std::cout << " [--linux] [--lang language] [--print-langs]" << std::endl;
|
|
std::cout << std::endl << "Rename TV episodes" << std::endl << std::endl << "optional arguments:" << std::endl;
|
|
std::cout << " -h, --help\t\tshow this help message and exit" << std::endl;
|
|
std::cout << " --show show name, -s show name" << std::endl;
|
|
std::cout << "\t\t\tTV show from which you want episode names (needs to be" << std::endl;
|
|
std::cout << "\t\t\tin quotation marks if it has more than one word)" << std::endl;
|
|
std::cout << " --season season number, -n season number" << std::endl;
|
|
std::cout << "\t\t\tSeason number/s (if multiple seasons, put them in" << std::endl;
|
|
std::cout << "\t\t\tquotation marks and seperate by one space)" << std::endl;
|
|
std::cout << "\t\t\tor 'all' for all seasons in selected subdirectory" << std::endl;
|
|
std::cout << " --show-path show path, -p show path" << std::endl;
|
|
std::cout << "\t\t\tPath of the directory with episodes" << std::endl;
|
|
std::cout << " --correct-path, -c\tThis is the correct path, stop asking me!" << std::endl;
|
|
std::cout << " --name-pattern pattern" << std::endl;
|
|
std::cout << "\t\t\tPattern to which change the file name. Possible sequences are:" << std::endl;
|
|
std::cout << "\t\t\t\t\%filename - original filename (without filetype extension)" << std::endl;
|
|
std::cout << "\t\t\t\t\%show - show name from thetvdb" << std::endl;
|
|
std::cout << "\t\t\t\t\%epname - episode name from thetvdb" << std::endl;
|
|
std::cout << "\t\t\t\t\%season - season number" << std::endl;
|
|
std::cout << "\t\t\t\t\tpossible to specify leading 0 like this: \%2season (number means how many leading zeros)" << std::endl;
|
|
std::cout << "\t\t\t\t\%episode - episode number" << std::endl;
|
|
std::cout << "\t\t\t\t\tpossible to specify leading 0 like this: \%2episode (number means how many leading zeros)" << std::endl;
|
|
std::cout << "\t\t\tDefault pattern is \"$filename - $epname\"" << std::endl;
|
|
std::cout << " --trust, -t\t\tDon't ask whether the names are correct" << std::endl;
|
|
std::cout << " --linux, -x\t\tDon't replace characters characters that are illegal in Windows" << std::endl;
|
|
std::cout << " --lang language, -l language" << std::endl;
|
|
std::cout << "\t\t\tSelect which language the episode names shoud be in" << std::endl;
|
|
std::cout << " --print-langs\t\tPring available language" << std::endl;
|
|
}
|
|
|
|
void parseSeasonNumbers(std::set<int> &seasons_num, const char *argument) {
|
|
size_t pos{0};
|
|
|
|
while(!isdigit(argument[pos]) && argument[pos] != '\0')
|
|
pos++;
|
|
|
|
if( argument[pos] == '\0' ) {
|
|
seasons_num.clear();
|
|
return;
|
|
}
|
|
|
|
int temp;
|
|
std::istringstream iss(argument + pos);
|
|
while(iss >> temp) {
|
|
seasons_num.insert(temp);
|
|
}
|
|
}
|
|
|
|
void printLangs() {
|
|
for( size_t i = 0; i < languages.size(); i += 2 ) {
|
|
std::cout << languages[i] << " - " << languages[i+1] << std::endl;
|
|
}
|
|
}
|
|
|
|
bool findLanguage( const char *language ) {
|
|
for( size_t i = 0; i < languages.size(); i += 2 ) {
|
|
if( !strcmp(language, languages[i]) )
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
#else
|
|
|
|
std::vector<std::pair<std::string, std::string>> getPossibleShows( std::string show, const std::string &language, Curl &c ) {
|
|
std::replace(show.begin(), show.end(), ' ', '+');
|
|
auto source_code = c.execute("https://www.thetvdb.com/search?q=" + encodeUrl(show) + "&l=" + language);
|
|
size_t pos{};
|
|
std::vector<std::pair<std::string, std::string>> urls;
|
|
while( true ) {
|
|
pos = source_code.find("/ser", pos);
|
|
if( pos != std::string::npos ) {
|
|
auto end = source_code.find(">", pos);
|
|
auto end2 = source_code.find("<", end+1);
|
|
end--;
|
|
urls.emplace_back(source_code.substr(end+2, end2 - (end+2)), source_code.substr(pos, end - pos));
|
|
pos = end + 2;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
return urls;
|
|
}
|
|
|
|
std::string userHome() {
|
|
uid_t user_uid;
|
|
{
|
|
uid_t eid;
|
|
uid_t sid;
|
|
getresuid( &user_uid, &eid, &sid ); // don't need eid and sid
|
|
}
|
|
auto user_passwd = getpwuid( user_uid );
|
|
|
|
if ( user_passwd == nullptr )
|
|
throw std::runtime_error(
|
|
"User with uid " + std::to_string( user_uid ) + " doesn't exist!" );
|
|
return user_passwd->pw_dir;
|
|
}
|
|
|
|
#endif
|
|
|
|
std::string compilePattern(const std::string &pattern, int season, int episode, const std::string &filename, const std::string &episodeName, const std::string &showName) {
|
|
std::string output;
|
|
|
|
for( size_t i = 0; i < pattern.size(); i++ ) {
|
|
if( pattern[i] == '%' ) {
|
|
// if current character is % check if a pattern follows, otherwise put %
|
|
// check for numbers right after % indicating size of zero padding for numbers
|
|
auto pos = pattern.find_first_not_of("0123456789", i+1);
|
|
|
|
if( pattern.find( "season", pos - 1 ) == pos ) {
|
|
// if season is AFTER numbers, put season number padded
|
|
// with zeros
|
|
|
|
// get number of leading zeros
|
|
auto leading = std::atoi( pattern.c_str() + i + 1 );
|
|
// move i to the last char of 'season'
|
|
i = pos + 5;
|
|
auto season_num = std::to_string(season);
|
|
|
|
// get number of zeros to be put before the season number
|
|
leading -= season_num.size();
|
|
if( leading < 0 )
|
|
leading = 0;
|
|
|
|
// add padded season to output
|
|
output += std::string(leading, '0') + season_num;
|
|
} else if ( pattern.find( "season", i ) == i + 1 ) {
|
|
// if season isn't after numbers, just put season number to output
|
|
i += 6;
|
|
output += std::to_string(season);
|
|
} else if ( pattern.find( "episode", pos - 1 ) == pos ) {
|
|
// same principle as with season after number
|
|
auto leading = std::atoi( pattern.c_str() + i + 1 );
|
|
|
|
i = pos + 6;
|
|
auto ep_num = std::to_string(episode);
|
|
|
|
leading -= ep_num.size();
|
|
if( leading < 0 )
|
|
leading = 0;
|
|
|
|
output += std::string(leading, '0') + ep_num;
|
|
} else if ( pattern.find( "episode", i ) == i + 1 ) {
|
|
// if episode isn't after number, just put the episode number to output
|
|
i += 7;
|
|
output += std::to_string(episode);
|
|
} else if ( pattern.find( "epname", i ) == i + 1 ) {
|
|
// episode name from thetvdb
|
|
i += 6;
|
|
output += episodeName;
|
|
} else if ( pattern.find( "show", i ) == i + 1 ) {
|
|
// show name from thetvdb
|
|
i += 4;
|
|
output += showName;
|
|
} else if ( pattern.find( "filename", i ) == i + 1 ) {
|
|
// original file name
|
|
i += 8;
|
|
output += filename;
|
|
} else {
|
|
// output % if no escape sequence was found
|
|
output += '%';
|
|
}
|
|
} else if ( pattern[i] == '\\' ) {
|
|
// possibility to escape %
|
|
if( pattern[i+1] == '%' ) {
|
|
output += '%';
|
|
i++;
|
|
} else if ( pattern[i+1] == '\\' ) {
|
|
output += '\\';
|
|
i++;
|
|
} else {
|
|
output += '\\';
|
|
}
|
|
} else {
|
|
// if char isn't % or / just add it to the output string
|
|
output += pattern[i];
|
|
}
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|