tv_rename/tv_rename.cpp

285 lines
8.6 KiB
C++
Raw Normal View History

2019-01-07 22:24:28 +00:00
#include <algorithm>
2019-02-04 16:39:48 +00:00
#include <map>
#ifdef _WIN32
#include <codecvt>
#include <fcntl.h>
#include <io.h>
#include <locale>
#include <windows.h>
#endif
2019-01-23 19:46:03 +00:00
#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-02-04 16:39:48 +00:00
#include "functions.hpp"
#include "tv_rename.hpp"
#ifdef _WIN32
constexpr const char_t *dir_divider = L"\\";
#define cout std::wcout
#define cerr std::wcerr
#define cin std::wcin
#else
constexpr const char_t *dir_divider = "/";
#define cout std::cout
#define cerr std::cerr
#define cin std::cin
#define TEXT( a ) a
#endif
// get names for all episodes for a given season
std::vector< string > parseEpisodeNames( const string &season_code,
const string &language ) {
std::vector< string > episodes;
2019-01-07 22:24:28 +00:00
size_t pos = 0;
2019-02-04 16:39:48 +00:00
while ( true ) {
pos =
season_code.find( TEXT( "ge=\"" ) + language + TEXT( "\" " ), pos );
if ( pos != string::npos ) {
if ( language == TEXT( "en" ) )
pos += 17;
else
pos += 29;
2019-02-04 16:39:48 +00:00
while ( iswspace( season_code[pos] ) )
2019-01-07 22:24:28 +00:00
pos++;
2019-02-04 16:39:48 +00:00
auto end = season_code.find( TEXT( "<" ), pos );
2019-01-07 22:24:28 +00:00
end--;
2019-02-04 16:39:48 +00:00
while ( iswspace( season_code[end] ) )
2019-01-07 22:24:28 +00:00
end--;
2019-02-04 16:39:48 +00:00
2019-01-07 22:24:28 +00:00
end++;
2019-02-04 16:39:48 +00:00
episodes.push_back( season_code.substr( pos, end - pos ) );
2019-01-07 22:24:28 +00:00
} else {
break;
}
}
return episodes;
2019-01-07 22:24:28 +00:00
}
2019-02-04 16:39:48 +00:00
std::vector< std::pair< string, std::pair< string, string > > >
getRenamedFiles( const string &show, int season, string url,
const string &language, const string &pattern,
const bool &linux, Curl &c, const std::set< string > &files ) {
#ifdef _WIN32
auto season_num = std::to_wstring( season );
#else
auto season_num = std::to_string( season );
#endif
url += TEXT( "/seasons/" );
url += season_num;
// 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 != string::npos ) {
season_code = season_code.substr( pos );
pos = season_code.find( "</table>" );
if ( pos != string::npos )
season_code = season_code.substr( 0, pos );
else
return {};
} else {
return {};
}
2019-01-07 22:24:28 +00:00
2019-02-04 16:39:48 +00:00
#ifdef _WIN32
auto episodes =
parseEpisodeNames( utf8_to_wstring( season_code ), language );
#else
auto episodes = parseEpisodeNames( season_code, language );
#endif
2019-01-07 22:24:28 +00:00
2019-02-04 16:39:48 +00:00
if ( episodes.empty() )
return {};
2019-02-04 16:39:48 +00:00
if ( files.empty() )
return {};
2019-01-23 19:46:03 +00:00
2019-02-04 16:39:48 +00:00
std::vector< std::pair< string, std::pair< string, string > > >
renamed_files;
for ( const auto &x : files ) {
auto last = x.find_last_of( dir_divider );
string og_name;
string dir;
if ( last == string::npos ) {
og_name = x;
2019-02-04 16:39:48 +00:00
dir = TEXT( "." );
} else {
2019-02-04 16:39:48 +00:00
og_name = x.substr( last + 1 );
dir = x.substr( 0, last );
}
unsigned long num;
// get file's episode number
2019-02-04 16:39:48 +00:00
if ( searchSpecificSeason( x.c_str(), pos, season_num ) ) {
num = std::stoi( x.c_str() + pos );
2019-01-07 21:24:00 +00:00
} else {
continue;
}
2019-02-04 16:39:48 +00:00
num -= 1;
2019-02-04 16:39:48 +00:00
if ( num < episodes.size() ) {
auto pos = og_name.find_last_of( TEXT( "." ) );
2019-01-23 13:08:40 +00:00
// get desired filename
2019-02-04 16:39:48 +00:00
auto name = compilePattern( pattern, season, num + 1,
og_name.substr( 0, pos ), episodes[num],
show ) +
og_name.substr( pos );
// replace '/' with '|'
2019-02-04 16:39:48 +00:00
for ( size_t i = 0; i < name.size(); i++ ) {
if ( name[i] == '/' ) {
name[i] = '|';
}
}
2019-01-23 13:08:40 +00:00
// replace characters illegal in windows if desired
2019-02-04 16:39:48 +00:00
if ( !linux ) {
name.erase( std::remove_if( name.begin(), name.end(),
[]( char_t x ) {
return x == '?' || x == '"' ||
x == '\\' || x == '*';
} ),
name.end() );
size_t max{ name.size() };
for ( size_t i = 0; i < max; i++ ) {
if ( name[i] == '|' ) {
name[i] = '-';
2019-02-04 16:39:48 +00:00
} else if ( name[i] == '<' ) {
2019-01-07 22:24:28 +00:00
name[i] = 'i';
2019-02-04 16:39:48 +00:00
name.insert( i + 1, TEXT( "s less than" ) );
2019-01-07 22:24:28 +00:00
max += 11;
} else if ( name[i] == '>' ) {
name[i] = 'i';
2019-02-04 16:39:48 +00:00
name.insert( i + 1, TEXT( "s more than" ) );
2019-01-07 22:24:28 +00:00
max += 11;
} else if ( name[i] == ':' ) {
name[i] = ' ';
2019-02-04 16:39:48 +00:00
name.insert( i + 1, 1, '-' );
2019-01-07 22:24:28 +00:00
max++;
}
}
2019-02-04 16:39:48 +00:00
for ( size_t i = 0; i < max; i++ ) {
if ( name[i] == ' ' && name[i + 1] == ' ' ) {
name.erase( i, 1 );
max--;
i--;
}
}
}
2019-02-04 16:39:48 +00:00
renamed_files.emplace_back(
dir, std::pair< string, string >( og_name, name ) );
}
}
return renamed_files;
}
#ifndef GUI
2019-02-04 16:39:48 +00:00
void singleSeason( const string &path, string &show, int season, string url,
const string &language, const string &pattern,
const bool &linux, const bool &trust, Curl &c,
std::set< string > const *files_ptr ) {
if ( url.empty() )
url = getDefUrl( show, language, c );
2019-02-04 16:39:48 +00:00
std::set< string > *found_files = nullptr;
2019-02-04 16:39:48 +00:00
if ( files_ptr == nullptr ) {
found_files = new std::set< string >;
findSeason( *found_files, season, path );
files_ptr = found_files;
}
2019-02-04 16:39:48 +00:00
auto renamed_files =
getRenamedFiles( show, season, std::move( url ), language, pattern,
linux, c, *files_ptr );
2019-02-04 16:39:48 +00:00
for ( auto renamed = renamed_files.begin(); renamed != renamed_files.end();
++renamed ) {
cout << renamed->second.first << " --> " << renamed->second.second
<< std::endl;
}
2019-01-07 22:24:28 +00:00
2019-02-04 16:39:48 +00:00
if ( !trust ) {
cout << "Does this seem ok? (y/n) ";
string response;
cin >> response;
cin.clear();
cin.ignore( 1, '\n' );
if ( response[0] != 'y' && response[0] != 'Y' )
return;
}
2019-01-07 22:24:28 +00:00
2019-02-04 16:39:48 +00:00
for ( auto renamed = renamed_files.begin(); renamed != renamed_files.end();
++renamed ) {
FSLib::rename( renamed->first + dir_divider + renamed->second.first,
renamed->first + dir_divider + renamed->second.second );
}
2019-02-04 16:39:48 +00:00
if ( found_files != nullptr ) {
delete found_files;
}
2018-09-22 22:50:42 +00:00
}
2019-02-04 16:39:48 +00:00
void multipleSeasons( const string &path, string &show,
const std::map< int, std::set< string > > &seasons,
const string &language, const string &pattern,
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, pattern, linux, trust,
c, &x.second );
}
}
2019-02-04 16:39:48 +00:00
void multipleSeasons( const string &path, string &show,
const std::set< int > seasons, const string &language,
const string &pattern, const bool &linux,
const bool &trust, Curl &c ) {
std::map< int, std::set< string > > season_map;
findSeasons( season_map, path, seasons );
multipleSeasons( path, show, season_map, language, pattern, linux, trust,
c );
}
2019-02-04 16:39:48 +00:00
void allSeasons( const string &path, string &show, const string &language,
const string &pattern, const bool &linux, const bool &trust,
Curl &c ) {
std::map< int, std::set< string > > seasons;
// get all season number from this directory and subdirectories
iterateFS( seasons, path );
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