tv_rename/tv_rename.cpp

193 lines
6.8 KiB
C++
Raw Normal View History

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
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 ) {
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;
}
}
return episodes;
2019-01-07 22:24:28 +00:00
}
#ifdef GUI
std::vector<std::pair<std::string, std::pair<std::string, std::string>>> defaultSingleSeasonReturn() {
return {};
}
2019-01-23 19:46:03 +00:00
#else
void defaultSingleSeasonReturn() {
return;
}
2019-01-23 19:46:03 +00:00
#endif
#ifdef GUI
std::vector<std::pair<std::string, std::pair<std::string, std::string>>> singleSeason( 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) {
#else
void singleSeason( const std::string &path, const 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) {
#endif
2019-01-23 19:46:03 +00:00
#ifndef GUI
if( url.empty() )
url = getDefUrl(show, language, c);
2019-01-23 19:46:03 +00:00
#endif
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 defaultSingleSeasonReturn();
} else {
return defaultSingleSeasonReturn();
}
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 defaultSingleSeasonReturn();
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 19:46:03 +00:00
#ifndef GUI
std::set<std::string> found_files;
2019-01-07 22:24:28 +00:00
if( files_ptr == nullptr ) {
findSeason(found_files, season, path);
files_ptr = &found_files;
}
2019-01-07 22:24:28 +00:00
const std::set<std::string> &files = *files_ptr;
#endif
2019-01-07 22:24:28 +00:00
if( files.empty() )
return defaultSingleSeasonReturn();
2019-01-23 19:46:03 +00:00
for( const auto &x : files ) {
auto last = x.find_last_of("/");
std::string og_name;
std::string dir;
if( last == std::string::npos ) {
og_name = x;
dir = ".";
} else {
og_name = x.substr(last+1);
dir = x.substr(0, last);
}
unsigned long num;
// 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 {
continue;
}
num -= 1;
if( num < episodes.size() ) {
auto pos = og_name.find_last_of('.');
2019-01-23 13:08:40 +00:00
// get desired filename
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
if( !linux ) {
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++;
}
}
}
renamed_files.emplace_back(dir, std::pair<std::string, std::string>(og_name, name));
}
}
2019-01-07 22:24:28 +00:00
#ifdef GUI
return renamed_files;
#else
for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) {
std::cout << renamed->second.first << " --> " << renamed->second.second << 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
for(auto renamed = renamed_files.begin(); renamed != renamed_files.end(); ++renamed) {
FSLib::rename(renamed->first + "/" + renamed->second.first, renamed->first + "/" + renamed->second.second);
}
2019-01-23 19:46:03 +00:00
#endif
2018-09-22 22:50:42 +00:00
}
2019-01-23 19:46:03 +00:00
#ifndef GUI
2019-01-23 13:08:40 +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 std::string &pattern, const bool &linux, const bool &trust, Curl &c) {
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-23 13:08:40 +00:00
void multipleSeasons( const std::string &path, const std::string &show, const std::set<int> seasons, const std::string &language, const std::string &pattern, const bool &linux, const bool &trust, Curl &c) {
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-23 13:08:40 +00:00
void allSeasons( const std::string &path, const std::string &show, const std::string &language, const std::string &pattern, 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-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-23 19:46:03 +00:00
#endif
2019-01-17 00:24:04 +00:00