2018-09-22 22:50:42 +00:00
|
|
|
#include "functions.hpp"
|
2019-01-03 18:01:43 +00:00
|
|
|
#include "filesystem.hpp"
|
2019-01-07 22:24:28 +00:00
|
|
|
#include <algorithm>
|
2018-09-22 22:50:42 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include <stdlib.h>
|
2019-01-07 22:24:28 +00:00
|
|
|
#include <vector>
|
2018-09-22 22:50:42 +00:00
|
|
|
|
|
|
|
size_t writeCallback( void *contents, size_t size, size_t nmemb, void *target ) {
|
2019-01-04 19:19:08 +00:00
|
|
|
*static_cast<std::string*>(target) += std::string(static_cast<char*>(contents), size*nmemb);
|
|
|
|
return size * nmemb;
|
2018-09-22 22:50:42 +00:00
|
|
|
}
|
|
|
|
|
2018-09-26 10:45:37 +00:00
|
|
|
Curl::Curl() {
|
2019-01-04 19:19:08 +00:00
|
|
|
curl_global_init(CURL_GLOBAL_ALL);
|
|
|
|
curl_handle = curl_easy_init();
|
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, writeCallback);
|
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0");
|
2018-09-26 10:45:37 +00:00
|
|
|
}
|
2018-09-22 22:50:42 +00:00
|
|
|
|
2018-09-26 10:45:37 +00:00
|
|
|
Curl::~Curl() {
|
2019-01-04 19:19:08 +00:00
|
|
|
curl_easy_cleanup(curl_handle);
|
|
|
|
curl_global_cleanup();
|
2018-09-26 10:45:37 +00:00
|
|
|
}
|
2018-09-22 22:50:42 +00:00
|
|
|
|
2018-09-26 10:45:37 +00:00
|
|
|
std::string Curl::execute(const std::string &url) {
|
2019-01-04 19:19:08 +00:00
|
|
|
std::string source;
|
2019-01-07 21:23:15 +00:00
|
|
|
source.reserve(100000);
|
2019-01-04 19:19:08 +00:00
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, static_cast<void*>(&source));
|
|
|
|
curl_easy_setopt(curl_handle, CURLOPT_URL, url.c_str());
|
|
|
|
auto res = curl_easy_perform(curl_handle);
|
|
|
|
if(res != CURLE_OK) {
|
|
|
|
std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return source;
|
2018-09-22 22:50:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 21:23:15 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-01-03 18:01:43 +00:00
|
|
|
void findSeason(std::set<std::string> &files, int season, const std::string &path) {
|
2019-01-07 21:23:15 +00:00
|
|
|
auto number = std::to_string(season);
|
|
|
|
|
|
|
|
for( const auto p: FSLib::Directory(path) ) {
|
2019-01-04 19:19:08 +00:00
|
|
|
if(FSLib::isDirectory(path + "/" + p)) {
|
|
|
|
findSeason(files, season, path + "/" + p);
|
|
|
|
continue;
|
|
|
|
}
|
2019-01-03 18:01:43 +00:00
|
|
|
|
2019-01-07 21:23:15 +00:00
|
|
|
if( searchSpecificSeason(p, number) )
|
2019-01-04 19:19:08 +00:00
|
|
|
files.insert(path + "/" + p);
|
|
|
|
}
|
2018-09-22 22:50:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-17 16:23:15 +00:00
|
|
|
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)) {
|
2019-01-17 16:26:19 +00:00
|
|
|
findSeasons(seasons, path + "/" + p, season_numbers);
|
2019-01-17 16:23:15 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-17 16:08:51 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-22 22:50:42 +00:00
|
|
|
void iterateFS(std::set<int> &seasons, const std::string &path) {
|
2019-01-07 21:23:15 +00:00
|
|
|
size_t season_pos{std::string::npos}; // season_pos - position of first digit of the season
|
|
|
|
for( const auto p: FSLib::Directory(path) ) {
|
2019-01-04 19:19:08 +00:00
|
|
|
if(FSLib::isDirectory(path + "/" + p)) {
|
|
|
|
iterateFS(seasons, path + "/" + p);
|
|
|
|
continue;
|
|
|
|
}
|
2019-01-03 18:01:43 +00:00
|
|
|
|
2019-01-07 21:23:15 +00:00
|
|
|
if( searchSeason(p, season_pos) )
|
|
|
|
seasons.insert(atoi(p+season_pos));
|
2019-01-04 19:19:08 +00:00
|
|
|
}
|
2018-09-22 22:50:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 21:23:15 +00:00
|
|
|
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=" + show + "&l=" + language);
|
2019-01-07 22:24:28 +00:00
|
|
|
size_t order{}, pos{};
|
2019-01-04 19:19:08 +00:00
|
|
|
std::vector<std::string> urls;
|
2019-01-07 22:24:28 +00:00
|
|
|
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;
|
|
|
|
}
|
2019-01-04 19:19:08 +00:00
|
|
|
}
|
|
|
|
std::cout << "Which TV Show is the right one? ";
|
|
|
|
std::cin >> pos;
|
|
|
|
std::cin.clear();
|
|
|
|
std::cin.ignore(1, '\n');
|
2018-09-22 22:50:42 +00:00
|
|
|
return "https://www.thetvdb.com" + urls[pos-1];
|
|
|
|
}
|
|
|
|
|
|
|
|
void printHelp() {
|
2019-01-04 19:19:08 +00:00
|
|
|
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;
|
2019-01-07 22:34:22 +00:00
|
|
|
std::cout << " --correct-path, -c\tThis is the correct path, stop asking me!" << std::endl;
|
2019-01-04 19:19:08 +00:00
|
|
|
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;
|
2018-09-22 22:50:42 +00:00
|
|
|
}
|
|
|
|
|