2019-01-07 22:24:28 +00:00
|
|
|
#include <algorithm>
|
2019-01-23 19:46:03 +00:00
|
|
|
#include "functions.hpp"
|
|
|
|
#include "tv_rename.hpp"
|
|
|
|
|
|
|
|
#ifndef GUI
|
|
|
|
|
2018-09-22 22:50:42 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <sstream>
|
2019-01-07 22:24:28 +00:00
|
|
|
#include <vector>
|
2019-01-03 18:01:43 +00:00
|
|
|
#include "filesystem.hpp"
|
2019-01-23 19:46:03 +00:00
|
|
|
|
|
|
|
#endif
|
2018-09-22 22:50:42 +00:00
|
|
|
|
2019-01-17 16:08:51 +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 ) {
|
2019-01-23 01:10:01 +00:00
|
|
|
if( language == "en" )
|
|
|
|
pos += 17;
|
|
|
|
else
|
|
|
|
pos += 29;
|
2019-01-07 22:24:28 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2019-01-17 16:08:51 +00:00
|
|
|
return episodes;
|
2019-01-07 22:24:28 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 16:50:19 +00:00
|
|
|
std::vector<std::pair<std::string, std::pair<std::string, std::string>>> getRenamedFiles( const std::string &show, int season, std::string url, const std::string &language, const std::string &pattern, const bool &linux, Curl &c, const std::set<std::string> &files ) {
|
2019-01-07 21:24:00 +00:00
|
|
|
url += "/seasons/" + std::to_string(season);
|
2019-01-04 19:18:18 +00:00
|
|
|
//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);
|
2019-01-22 23:47:08 +00:00
|
|
|
pos = season_code.find("</table>");
|
2019-01-04 19:18:18 +00:00
|
|
|
if( pos != std::string::npos )
|
|
|
|
season_code = season_code.substr(0,pos);
|
|
|
|
else
|
2019-01-29 16:50:19 +00:00
|
|
|
return {};
|
2019-01-04 19:18:18 +00:00
|
|
|
} else {
|
2019-01-29 16:50:19 +00:00
|
|
|
return {};
|
2019-01-04 19:18:18 +00:00
|
|
|
}
|
2019-01-07 22:24:28 +00:00
|
|
|
|
2019-01-17 16:08:51 +00:00
|
|
|
auto episodes = parseEpisodeNames(season_code, language);
|
2019-01-07 22:24:28 +00:00
|
|
|
|
2019-01-04 19:18:18 +00:00
|
|
|
if( episodes.empty() )
|
2019-01-29 16:50:19 +00:00
|
|
|
return {};
|
2019-01-23 21:57:52 +00:00
|
|
|
|
|
|
|
std::vector<std::pair<std::string, std::pair<std::string, std::string>>> renamed_files;
|
2019-01-07 22:24:28 +00:00
|
|
|
|
2019-01-23 21:57:52 +00:00
|
|
|
if( files.empty() )
|
2019-01-29 16:50:19 +00:00
|
|
|
return {};
|
2019-01-23 19:46:03 +00:00
|
|
|
|
|
|
|
for( const auto &x : files ) {
|
2019-01-04 19:18:18 +00:00
|
|
|
auto last = x.find_last_of("/");
|
2019-01-23 21:57:52 +00:00
|
|
|
std::string og_name;
|
2019-01-04 19:18:18 +00:00
|
|
|
std::string dir;
|
2019-01-23 21:57:52 +00:00
|
|
|
if( last == std::string::npos ) {
|
|
|
|
og_name = x;
|
2019-01-04 19:18:18 +00:00
|
|
|
dir = ".";
|
|
|
|
} else {
|
2019-01-23 21:57:52 +00:00
|
|
|
og_name = x.substr(last+1);
|
2019-01-04 19:18:18 +00:00
|
|
|
dir = x.substr(0, last);
|
|
|
|
}
|
|
|
|
unsigned long num;
|
2019-01-17 16:33:13 +00:00
|
|
|
// get file's episode number
|
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 {
|
2019-01-04 19:18:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
num -= 1;
|
|
|
|
if( num < episodes.size() ) {
|
2019-01-23 21:57:52 +00:00
|
|
|
auto pos = og_name.find_last_of('.');
|
2019-01-23 13:08:40 +00:00
|
|
|
// get desired filename
|
2019-01-23 21:57:52 +00:00
|
|
|
auto name = compilePattern(pattern, season, num+1, og_name.substr(0, pos), episodes[num], show) + og_name.substr(pos);
|
2019-01-23 13:08:40 +00:00
|
|
|
// replace characters illegal in windows if desired
|
2019-01-04 19:18:18 +00:00
|
|
|
if( !linux ) {
|
2019-01-24 15:37:25 +00:00
|
|
|
name.erase(std::remove_if(name.begin(), name.end(), [](char x) {return x == '?' || x == '"' || x == '\\' || x == '|' || x == '*';}), name.end());
|
2019-01-07 22:24:28 +00:00
|
|
|
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++;
|
|
|
|
}
|
|
|
|
}
|
2019-01-04 19:18:18 +00:00
|
|
|
}
|
2019-01-23 21:57:52 +00:00
|
|
|
renamed_files.emplace_back(dir, std::pair<std::string, std::string>(og_name, name));
|
2019-01-04 19:18:18 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-23 21:57:52 +00:00
|
|
|
return renamed_files;
|
2019-01-29 16:50:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef GUI
|
|
|
|
|
|
|
|
void singleSeason( const std::string &path, std::string &show, int season, std::string url, const std::string &language, const std::string &pattern, const bool &linux, const bool &trust, Curl &c, std::set<std::string> const *files_ptr) {
|
|
|
|
if( url.empty() )
|
|
|
|
url = getDefUrl(show, language, c);
|
|
|
|
|
|
|
|
std::set<std::string> *found_files = nullptr;
|
|
|
|
|
|
|
|
if( files_ptr == nullptr ) {
|
|
|
|
found_files = new std::set<std::string>;
|
|
|
|
findSeason(*found_files, season, path);
|
|
|
|
files_ptr = found_files;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto renamed_files = getRenamedFiles(show, season, std::move(url), language, pattern, linux, c, *files_ptr);
|
|
|
|
|
2019-01-04 19:18:18 +00:00
|
|
|
for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) {
|
2019-01-23 21:57:52 +00:00
|
|
|
std::cout << renamed->second.first << " --> " << renamed->second.second << std::endl;
|
2019-01-04 19:18:18 +00:00
|
|
|
}
|
2019-01-07 22:24:28 +00:00
|
|
|
|
2019-01-04 19:18:18 +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
|
|
|
|
2019-01-04 19:18:18 +00:00
|
|
|
for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) {
|
2019-01-23 21:57:52 +00:00
|
|
|
FSLib::rename(renamed->first + "/" + renamed->second.first, renamed->first + "/" + renamed->second.second);
|
2019-01-04 19:18:18 +00:00
|
|
|
}
|
2019-01-29 16:50:19 +00:00
|
|
|
|
|
|
|
if( found_files != nullptr ) {
|
|
|
|
delete found_files;
|
|
|
|
}
|
2018-09-22 22:50:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-25 23:50:26 +00:00
|
|
|
void multipleSeasons( const std::string &path, std::string &show, const std::map<int, std::set<std::string>> &seasons, const std::string &language, const std::string &pattern, const bool &linux, const bool &trust, Curl &c) {
|
2019-01-17 16:08:51 +00:00
|
|
|
auto url = getDefUrl(show, language, c);
|
|
|
|
for( const auto &x : seasons ) {
|
2019-01-23 13:08:40 +00:00
|
|
|
singleSeason( path, show, x.first, url, language, pattern, linux, trust, c, &x.second);
|
2019-01-17 16:08:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-25 23:50:26 +00:00
|
|
|
void multipleSeasons( const std::string &path, std::string &show, const std::set<int> seasons, const std::string &language, const std::string &pattern, const bool &linux, const bool &trust, Curl &c) {
|
2019-01-17 16:23:15 +00:00
|
|
|
std::map<int, std::set<std::string>> season_map;
|
|
|
|
findSeasons(season_map, path, seasons);
|
2019-01-23 13:08:40 +00:00
|
|
|
multipleSeasons(path, show, season_map, language, pattern, linux, trust, c);
|
2019-01-17 16:23:15 +00:00
|
|
|
}
|
|
|
|
|
2019-01-25 23:50:26 +00:00
|
|
|
void allSeasons( const std::string &path, std::string &show, const std::string &language, const std::string &pattern, const bool &linux, const bool &trust, Curl &c) {
|
2019-01-17 16:08:51 +00:00
|
|
|
std::map<int, std::set<std::string>> seasons;
|
2019-01-04 19:18:18 +00:00
|
|
|
//get all season number from this directory and subdirectories
|
|
|
|
iterateFS(seasons, path);
|
2019-01-23 13:08:40 +00:00
|
|
|
multipleSeasons( path, show, seasons, language, pattern, linux, trust, c);
|
2019-01-17 00:24:04 +00:00
|
|
|
}
|
2019-01-29 16:50:19 +00:00
|
|
|
|
2019-01-23 19:46:03 +00:00
|
|
|
#endif
|
2019-01-17 00:24:04 +00:00
|
|
|
|