tv_rename/functions.cpp

937 lines
30 KiB
C++
Raw Normal View History

2019-01-21 19:30:14 +00:00
#include <iomanip>
2019-06-04 19:54:00 +00:00
#include <map>
2019-01-23 19:46:03 +00:00
#include <sstream>
2019-06-04 19:54:00 +00:00
#include <unordered_set>
2019-02-04 16:39:48 +00:00
#include <vector>
2019-01-23 19:46:03 +00:00
2019-02-04 16:39:48 +00:00
#include "filesystem.hpp"
#include "functions.hpp"
2019-06-04 19:54:00 +00:00
#include "progress.hpp"
#include "sqlitepp.hpp"
#include "tv_rename.hpp"
2019-02-04 16:39:48 +00:00
#ifdef _WIN32
2020-01-15 18:55:24 +00:00
#include <codecvt>
#include <shlobj.h>
2019-02-04 16:39:48 +00:00
#define cout std::wcout
#define cerr std::wcerr
#define cin std::wcin
constexpr const char_t *dir_divider = L"\\";
#else // UNIX
2019-02-04 16:39:48 +00:00
2020-01-15 18:55:24 +00:00
#include <pwd.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
2020-03-12 19:52:23 +00:00
#include "resources_linux.h"
2020-01-15 18:55:24 +00:00
2019-02-04 16:39:48 +00:00
#define cout std::cout
#define cerr std::cerr
#define cin std::cin
constexpr const char_t *dir_divider = "/";
#endif // UNIX
2019-01-23 19:46:03 +00:00
2019-02-04 16:39:48 +00:00
#ifdef _WIN32
// functions to convert between string and wstring
std::string wstring_to_utf8( const std::wstring &wstring ) {
std::wstring_convert< std::codecvt_utf8_utf16< wchar_t > > wconv;
return wconv.to_bytes( wstring );
}
std::wstring utf8_to_wstring( const std::string &utf8 ) {
std::wstring_convert< std::codecvt_utf8_utf16< wchar_t > > wconv;
return wconv.from_bytes( utf8 );
}
2020-03-12 19:52:23 +00:00
wchar_t *LMsg( int ID ) {
// TODO better max value perhaps?
static wchar_t local[MAX_PATH];
auto hInstance = GetModuleHandle( NULL );
LoadString( hInstance, ID, local, MAX_PATH );
// TODO add vargs
return local;
}
#else
std::string getlocalized( const char *id, ... ) {
const char *local = gettext( id );
va_list args;
va_start( args, id );
int count = 0;
const char *p;
while ( ( p = va_arg( args, const char * ) ) != NULL )
count++;
va_end( args );
if ( count == 0 )
return local;
va_start( args, id );
int len = vsnprintf( nullptr, 0, local, args );
va_end( args );
char *text = new char[len + 1];
va_start( args, id );
vsnprintf( text, len + 1, local, args );
std::string ret = text;
delete[] text;
return ret;
}
2019-02-04 16:39:48 +00:00
#endif // _WIN32
2019-01-23 19:46:03 +00:00
// encode url so it's valid even with UTF-8 characters
2019-02-04 16:39:48 +00:00
string encodeUrl( const string &url ) {
// stolen from here -
// https://stackoverflow.com/questions/154536/encode-decode-urls-in-c
#ifdef _WIN32
std::wstringstream encoded;
auto url_c = wstring_to_utf8( url );
#else
std::stringstream encoded;
const auto &url_c = url;
#endif
encoded.fill( '0' );
2019-01-23 19:46:03 +00:00
encoded << std::hex;
2019-02-04 16:39:48 +00:00
for ( const auto &x : url_c ) {
if ( isalnum( static_cast< unsigned char >( x ) ) || x == '-' ||
2019-06-04 19:54:00 +00:00
x == '_' || x == '.' || x == '~' || x == '+' ) {
2019-01-23 19:46:03 +00:00
encoded << x;
continue;
}
2019-02-04 16:39:48 +00:00
encoded << std::uppercase << '%' << std::setw( 2 );
encoded << int( static_cast< unsigned char >( x ) ) << std::nouppercase;
2019-01-23 19:46:03 +00:00
}
return encoded.str();
}
2020-03-12 19:52:23 +00:00
// return true if file contains S[0-9]+E[0-9]+, set
// season_pos to start of season number and ep_pos to start of episode number
2020-01-15 18:55:24 +00:00
bool searchSeason( const char_t *const path, size_t &season_pos,
size_t &ep_pos ) {
size_t cur_pos{};
2019-02-04 16:39:48 +00:00
while ( path[cur_pos] != '\0' ) {
if ( ( path[cur_pos] == 's' || path[cur_pos] == 'S' ) &&
iswdigit( path[cur_pos + 1] ) ) {
cur_pos++;
2019-02-04 16:39:48 +00:00
season_pos = cur_pos; // after ++ because we want the first pos to
// point to season's number
while ( iswdigit( path[cur_pos] ) )
cur_pos++;
2019-02-04 16:39:48 +00:00
if ( ( path[cur_pos] == 'e' || path[cur_pos] == 'E' ) &&
iswdigit( path[cur_pos + 1] ) ) {
2020-01-15 18:55:24 +00:00
ep_pos = cur_pos + 1;
2019-02-04 16:39:48 +00:00
return true;
}
}
cur_pos++;
}
2019-02-04 16:39:48 +00:00
return false;
}
2020-01-15 18:55:24 +00:00
bool searchSeason( const char_t *const path, size_t &season_pos ) {
size_t tmp;
return searchSeason( path, season_pos, tmp );
}
2020-01-15 18:55:24 +00:00
void iterateFS( std::map< int, std::map< int, string > > &seasons,
2019-02-04 16:39:48 +00:00
const string &path ) {
2020-01-18 21:18:58 +00:00
// season_pos - position of first digit of the season number
// ep_pos - position of first digit of the episode number
2019-02-04 16:39:48 +00:00
size_t season_pos{ string::npos };
2020-01-15 18:55:24 +00:00
size_t ep_pos{ string::npos };
2019-02-04 16:39:48 +00:00
for ( const auto p : FSLib::Directory( path ) ) {
// if p is directory, iterate through it
if ( FSLib::isDirectory( path + dir_divider + p ) ) {
iterateFS( seasons, path + dir_divider + p );
2019-01-23 19:46:03 +00:00
continue;
}
2019-02-04 16:39:48 +00:00
// if file is a correct format, add it to file list
// for its season
2020-01-15 18:55:24 +00:00
if ( searchSeason( p, season_pos, ep_pos ) )
seasons[std::stoi( p + season_pos )][std::stoi( p + ep_pos )] =
path + dir_divider + p;
}
}
2019-07-12 21:10:40 +00:00
#ifndef GUI
// following functions are only needed for CLI version
2018-09-22 22:50:42 +00:00
void printHelp() {
2020-03-12 19:52:23 +00:00
cout << _( HELP_USAGE ) << std::endl;
cout << " tv_rename [" << _( HELP_OPTIONS_SMALL ) << "] ["
<< _( HELP_PATH_SMALL ) << "]" << std::endl
2019-02-04 16:39:48 +00:00
<< std::endl;
2020-03-12 19:52:23 +00:00
cout << " -h, --help " << _( HELP_HELP ) << std::endl
2019-02-04 16:39:48 +00:00
<< std::endl;
2020-03-12 19:52:23 +00:00
cout << _( HELP_PATH_INFO ) << std::endl << std::endl;
cout << _( HELP_OPTIONS ) << std::endl;
cout << " -s, --show <string> " << _( HELP_SHOW ) << std::endl;
cout << " -n, --season <numbers> " << _( HELP_SEASON ) << std::endl;
cout << "" << std::endl;
cout << " -d, --dvd " << _( HELP_DVD ) << std::endl;
cout << " --name-pattern <string> " << _( HELP_PATTERN ) << std::endl;
cout << " --pattern-help " << _( HELP_PATTERN_HELP )
2020-01-15 18:55:24 +00:00
<< std::endl;
2020-03-12 19:52:23 +00:00
cout << " -c, --correct-path " << _( HELP_CORRECT_PATH )
2019-02-04 16:39:48 +00:00
<< std::endl;
2020-03-12 19:52:23 +00:00
cout << " -t, --trust " << _( HELP_TRUST ) << std::endl;
cout << " -x, --linux " << _( HELP_LINUX ) << std::endl;
cout << " -l, --lang <string> " << _( HELP_LANG ) << std::endl;
cout << " --print-langs " << _( HELP_LANGS ) << std::endl;
2019-06-04 19:54:00 +00:00
cout << std::endl;
2020-03-12 19:52:23 +00:00
cout << _( HELP_DATABASE_OPTS ) << std::endl;
cout << " --db-add " << _( HELP_ADD_DB ) << std::endl;
cout << " --db-refresh " << _( HELP_REFRESH_DB )
2019-06-04 19:54:00 +00:00
<< std::endl;
2020-03-12 19:52:23 +00:00
cout << " --db-update " << _( HELP_UPDATE_DB ) << std::endl;
cout << " --db-name-pattern <string> " << _( HELP_DB_PATTERN )
2019-06-04 19:54:00 +00:00
<< std::endl;
2020-03-12 19:52:23 +00:00
cout << " --db-clean " << _( HELP_CLEAN_DB ) << std::endl;
cout << " --db-remove " << _( HELP_REMOVE_DB ) << std::endl;
}
void printPatternHelp() {
cout << _( PATTERN_POSSIBLE ) << std::endl;
cout << " %filename - " << _( PATTERN_FILENAME ) << std::endl;
cout << " %show - " << _( PATTERN_SHOW ) << std::endl;
cout << " %epname - " << _( PATTERN_EPNAME ) << std::endl;
cout << " %season - " << _( PATTERN_SEASON ) << std::endl;
cout << " " << _( PATTERN_LEADING_ZERO ) << std::endl;
cout << " %2season " << _( PATTERN_LEADING_NUM ) << std::endl;
cout << " %episode - " << _( PATTERN_EPISODE ) << std::endl;
cout << " " << _( PATTERN_LEADING_ZERO ) << std::endl;
cout << " %2episode " << _( PATTERN_LEADING_NUM ) << std::endl;
cout << _( PATTERN_DEFAULT ) << " \"%filename - %epname\"" << std::endl;
2018-09-22 22:50:42 +00:00
}
2019-02-04 16:39:48 +00:00
// parse command line argument --seasons (e.g. '1 2 3 4 5')
// and store season numbers as integers in seasons_num
void parseSeasonNumbers( std::set< int > &seasons_num,
const char_t *argument ) {
size_t pos{ 0 };
2019-01-19 12:40:10 +00:00
2019-02-04 16:39:48 +00:00
while ( !iswdigit( argument[pos] ) && argument[pos] != '\0' )
2019-01-19 12:40:10 +00:00
pos++;
2019-02-04 16:39:48 +00:00
if ( argument[pos] == '\0' ) {
2019-01-19 12:40:10 +00:00
seasons_num.clear();
return;
}
int temp;
2019-02-04 16:39:48 +00:00
#ifdef _WIN32
std::wstringstream iss( argument + pos );
#else
std::stringstream iss( argument + pos );
#endif
while ( iss >> temp ) {
seasons_num.insert( temp );
2019-01-19 12:40:10 +00:00
}
}
2019-06-04 19:54:00 +00:00
#endif // ndef GUI
2020-01-15 18:55:24 +00:00
#ifdef _WIN32
// get user's %APPDATA% folder location
string userHome() {
wchar_t *dir = static_cast< wchar_t * >( CoTaskMemAlloc( MAX_PATH ) );
auto res = SHGetKnownFolderPath( FOLDERID_RoamingAppData, 0, NULL, &dir );
if ( res == S_OK ) {
string dir_s = dir;
CoTaskMemFree( dir );
return dir_s;
}
CoTaskMemFree( dir );
2020-03-12 19:52:23 +00:00
throw std::runtime_error( _( APPDATA_NOT_FOUND ) );
2020-01-15 18:55:24 +00:00
}
#else // UNIX
2019-02-04 16:39:48 +00:00
// get user's home directory
string userHome() {
uid_t user_uid; // current user's uid
2019-01-23 19:46:03 +00:00
{
uid_t eid;
uid_t sid;
getresuid( &user_uid, &eid, &sid ); // don't need eid and sid
}
2019-02-04 16:39:48 +00:00
// password file entry
2019-01-23 19:46:03 +00:00
auto user_passwd = getpwuid( user_uid );
2020-03-12 19:52:23 +00:00
if ( user_passwd == nullptr ) {
throw std::runtime_error( _( USER_NOT_EXIST, user_uid ) );
}
2019-01-23 19:46:03 +00:00
return user_passwd->pw_dir;
}
#endif // UNIX
2019-01-23 19:46:03 +00:00
2019-02-04 16:39:48 +00:00
// create file name based on given pattern
string compilePattern( const string &pattern, int season, int episode,
const string &filename, const string &episodeName,
const string &showName ) {
string output;
2019-01-23 13:08:40 +00:00
2019-02-04 16:39:48 +00:00
#ifdef _WIN32
auto season_num = std::to_wstring( season );
auto ep_num = std::to_wstring( episode );
#else
auto season_num = std::to_string( season );
auto ep_num = std::to_string( episode );
#endif
2019-01-23 13:08:40 +00:00
2019-02-04 16:39:48 +00:00
for ( size_t i = 0; i < pattern.size(); i++ ) {
// if current character is % check if a pattern follows, otherwise
// put %
if ( pattern[i] == '%' ) {
// check for numbers right after % indicating size of zero
// padding for numbers
auto pos = pattern.find_first_not_of( TEXT( "0123456789" ), i + 1 );
if ( pattern.find( TEXT( "season" ), pos - 1 ) == pos &&
pos != i + 1 ) {
2019-01-23 13:08:40 +00:00
// if season is AFTER numbers, put season number padded
// with zeros
// get number of leading zeros
2019-02-04 16:39:48 +00:00
auto leading = std::stoi( pattern.c_str() + i + 1 );
2019-01-23 13:08:40 +00:00
// move i to the last char of 'season'
i = pos + 5;
// get number of zeros to be put before the season number
leading -= season_num.size();
2019-02-04 16:39:48 +00:00
if ( leading < 0 )
2019-01-23 13:08:40 +00:00
leading = 0;
// add padded season to output
2019-02-04 16:39:48 +00:00
output += string( leading, '0' ) + season_num;
} else if ( pattern.find( TEXT( "season" ), i ) == i + 1 ) {
// if season isn't after numbers, just put season number to
// output
2019-01-23 13:08:40 +00:00
i += 6;
2019-02-04 16:39:48 +00:00
output += season_num;
} else if ( pattern.find( TEXT( "episode" ), pos - 1 ) == pos &&
pos != i + 1 ) {
2019-01-23 13:08:40 +00:00
// same principle as with season after number
2019-02-04 16:39:48 +00:00
auto leading = std::stoi( pattern.c_str() + i + 1 );
2019-01-23 13:08:40 +00:00
i = pos + 6;
leading -= ep_num.size();
2019-02-04 16:39:48 +00:00
if ( leading < 0 )
2019-01-23 13:08:40 +00:00
leading = 0;
2019-02-04 16:39:48 +00:00
output += string( leading, '0' ) + ep_num;
} else if ( pattern.find( TEXT( "episode" ), i ) == i + 1 ) {
// if episode isn't after number, just put the episode number to
// output
2019-01-23 13:08:40 +00:00
i += 7;
2019-02-04 16:39:48 +00:00
output += ep_num;
} else if ( pattern.find( TEXT( "epname" ), i ) == i + 1 ) {
2019-01-23 13:08:40 +00:00
// episode name from thetvdb
i += 6;
output += episodeName;
2019-02-04 16:39:48 +00:00
} else if ( pattern.find( TEXT( "show" ), i ) == i + 1 ) {
2019-01-23 13:08:40 +00:00
// show name from thetvdb
i += 4;
output += showName;
2019-02-04 16:39:48 +00:00
} else if ( pattern.find( TEXT( "filename" ), i ) == i + 1 ) {
2019-01-23 13:08:40 +00:00
// original file name
i += 8;
output += filename;
} else {
// output % if no escape sequence was found
output += '%';
}
} else if ( pattern[i] == '\\' ) {
// possibility to escape %
2019-02-04 16:39:48 +00:00
if ( pattern[i + 1] == '%' ) {
2019-01-23 13:08:40 +00:00
output += '%';
i++;
2019-02-04 16:39:48 +00:00
} else if ( pattern[i + 1] == '\\' ) {
2019-01-23 13:08:40 +00:00
output += '\\';
i++;
} else {
output += '\\';
}
} else {
// if char isn't % or / just add it to the output string
output += pattern[i];
}
}
return output;
}
2019-06-04 19:54:00 +00:00
2020-01-15 22:59:39 +00:00
#ifdef _WIN32
2019-06-04 19:54:00 +00:00
std::wstring getDBName() {
return userHome() + L"\\tv_rename\\database.db";
}
#else
std::string getDBName() {
return userHome() + "/.cache/tv_rename.db";
}
#endif
string sanitize( const string &str ) {
string ret;
size_t prev_pos{};
size_t pos = str.find_first_of( '\'' );
while ( pos != string::npos ) {
ret += str.substr( prev_pos, pos - prev_pos );
prev_pos = pos + 1;
pos = str.find_first_of( '\'', prev_pos );
ret += TEXT( "\'\'" );
}
ret += str.substr( prev_pos, pos );
return ret;
}
2020-01-15 22:59:39 +00:00
void prepareDB( const string &_pattern ) {
2019-06-04 19:54:00 +00:00
SQLite::Database db{};
try {
db.open( getDBName(), SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE );
} catch ( std::exception &e ) {
2020-03-12 19:52:23 +00:00
cerr << _( DB_NOT_CREATE ) << " " << getDBName() << std::endl;
2019-06-04 19:54:00 +00:00
throw e;
}
db.exec(
"CREATE TABLE IF NOT EXISTS SHOWS (ID INTEGER NOT NULL "
"PRIMARY KEY AUTOINCREMENT, TVID TEXT NOT NULL, SHOW TEXT NOT NULL,"
2020-01-15 18:55:24 +00:00
"PATH TEXT NOT NULL UNIQUE, LANGUAGE TEXT NOT NULL, DVD INTEGER NOT "
"NULL);" );
2019-06-04 19:54:00 +00:00
db.exec( "CREATE TABLE IF NOT EXISTS EPISODES (SHOWID INTEGER NOT NULL,"
"PATH TEXT NOT NULL UNIQUE, FOREIGN KEY(SHOWID) "
"REFERENCES SHOWS(ID));" );
2019-07-12 21:10:40 +00:00
const string *pattern;
if ( _pattern.empty() ) {
2020-03-12 19:52:23 +00:00
cout << _( DB_INSERT_PATTERN ) << std::endl;
2019-07-12 21:10:40 +00:00
auto *p = new string;
std::getline( cin, *p );
pattern = p;
} else {
pattern = &_pattern;
}
2020-01-15 18:55:24 +00:00
db.exec( TEXT( "INSERT INTO SHOWS ( TVID, SHOW, PATH, LANGUAGE, DVD ) "
"VALUES ( 'pattern', 'pattern', '" ) +
2020-01-15 18:55:24 +00:00
sanitize( *pattern ) + TEXT( "', 'pattern', 0 );" ) );
if ( pattern != &_pattern ) {
2019-07-12 21:10:40 +00:00
delete pattern;
}
2019-06-04 19:54:00 +00:00
}
2019-07-12 21:10:40 +00:00
#ifndef GUI
2020-01-18 21:18:58 +00:00
void addToDB( string &show, const string &path, const string &language,
2020-01-15 18:55:24 +00:00
bool linux, bool dvd ) {
2019-07-12 21:10:40 +00:00
#else
void addToDB( const string &show, const string &path, const string &language,
const string &id, const string &pattern, bool linux, bool dvd,
void *progress_ptr ) {
2019-07-12 21:10:40 +00:00
#endif
2020-01-18 21:18:58 +00:00
if ( !FSLib::exists( getDBName() ) )
prepareDB();
2019-06-04 19:54:00 +00:00
SQLite::Database db{};
auto absolute = FSLib::canonical( path );
try {
db.open( getDBName(), SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE );
} catch ( std::exception &e ) {
2020-03-12 19:52:23 +00:00
cerr << _( DB_CANT_OPEN ) << std::endl;
2019-06-04 19:54:00 +00:00
throw e;
}
2019-07-12 21:10:40 +00:00
#ifndef GUI
2020-01-15 18:55:24 +00:00
// gui gives id and correct show name
auto id = getShowId( show, language );
show = showNameFromId( id, language );
2019-07-12 21:10:40 +00:00
#endif
2020-01-15 18:55:24 +00:00
db.exec(
TEXT( "INSERT OR IGNORE INTO SHOWS ( TVID, SHOW, PATH, LANGUAGE, DVD ) "
"VALUES ( '" ) +
sanitize( id ) + TEXT( "', '" ) + sanitize( show ) + TEXT( "', '" ) +
sanitize( absolute ) + TEXT( "', '" ) + sanitize( language ) +
TEXT( "', " ) + ( dvd ? TEXT( "1" ) : TEXT( "0" ) ) + TEXT( " );" ) );
2020-01-15 22:59:39 +00:00
#ifdef _WIN32
2020-01-18 21:18:58 +00:00
string db_id = std::to_wstring( db.lastRowID() );
2020-01-15 22:59:39 +00:00
#else
2020-01-18 21:18:58 +00:00
string db_id = std::to_string( db.lastRowID() );
2020-01-15 22:59:39 +00:00
#endif
2019-07-12 21:10:40 +00:00
#ifndef GUI
string pattern{};
db.exec( TEXT( "SELECT PATH FROM SHOWS WHERE TVID == 'pattern';" ),
2019-06-04 19:54:00 +00:00
pattern );
2019-07-12 21:10:40 +00:00
#endif
2019-06-04 19:54:00 +00:00
2020-01-15 18:55:24 +00:00
std::map< int, std::map< int, string > > seasons;
2019-06-04 19:54:00 +00:00
// get all seasons and episodes
iterateFS( seasons, absolute );
2019-07-12 21:10:40 +00:00
#ifndef GUI
ProgressBar p;
#else
ProgressBar p( progress_ptr );
2019-07-12 21:10:40 +00:00
#endif
2020-03-12 19:52:23 +00:00
p.print( _( RENAMING ) );
p.print( 0 );
2020-01-18 21:18:58 +00:00
size_t i = 0;
size_t seasons_size = seasons.size();
2020-01-15 18:55:24 +00:00
for ( auto &x : seasons ) {
singleSeason( absolute, show, x.first, id, language, pattern, linux,
true, &x.second, false, dvd );
2019-06-04 19:54:00 +00:00
i++;
2020-01-18 21:18:58 +00:00
p.print( ( i * 100 ) / seasons_size );
2019-06-04 19:54:00 +00:00
}
2019-07-12 21:10:40 +00:00
#ifndef GUI
2019-06-04 19:54:00 +00:00
cout << std::endl;
2019-07-12 21:10:40 +00:00
#endif
2019-06-04 19:54:00 +00:00
2020-03-12 19:52:23 +00:00
p.print( _( ADDING_TO_DB ) );
p.print( 0 );
2020-01-18 21:18:58 +00:00
i = 0;
2019-06-04 19:54:00 +00:00
for ( auto &season : seasons ) {
for ( auto &episode : season.second ) {
db.exec( TEXT( "INSERT OR IGNORE INTO EPISODES ( SHOWID, PATH ) "
"VALUES ( " ) +
2020-01-18 21:18:58 +00:00
db_id + TEXT( ", '" ) + sanitize( episode.second ) +
2019-06-04 19:54:00 +00:00
TEXT( "' );" ) );
}
i++;
2020-01-18 21:18:58 +00:00
p.print( ( i * 100 ) / seasons_size );
2019-06-04 19:54:00 +00:00
}
2019-07-12 21:10:40 +00:00
#ifndef GUI
2019-06-04 19:54:00 +00:00
cout << std::endl;
2019-07-12 21:10:40 +00:00
#endif
2019-06-04 19:54:00 +00:00
}
2020-01-15 22:59:39 +00:00
#ifdef _WIN32
2020-01-15 18:55:24 +00:00
void cleanUpLine() {
2019-06-04 19:54:00 +00:00
CONSOLE_SCREEN_BUFFER_INFO csbi;
int width;
GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &csbi );
width = csbi.srWindow.Right - csbi.srWindow.Left + 1;
2020-01-18 21:18:58 +00:00
static HANDLE h = GetStdHandle( STD_OUTPUT_HANDLE );
2019-06-04 19:54:00 +00:00
CONSOLE_SCREEN_BUFFER_INFO info;
GetConsoleScreenBufferInfo( h, &info );
COORD c = { 0, info.dwCursorPosition.Y - 3 };
SetConsoleCursorPosition( h, c );
2020-01-18 21:18:58 +00:00
2020-01-15 18:55:24 +00:00
cout << string( width, ' ' ) << std::endl << std::endl;
SetConsoleCursorPosition( h, c );
}
2019-06-04 19:54:00 +00:00
#else
2020-01-15 18:55:24 +00:00
void cleanUpLine() {
2019-06-04 19:54:00 +00:00
struct winsize w;
ioctl( 0, TIOCGWINSZ, &w );
auto width = w.ws_col;
cout << "\x1b[2A";
cout << string( width, ' ' ) << std::endl << std::endl;
cout << "\x1b[2A";
}
2020-01-15 18:55:24 +00:00
#endif
2019-06-04 19:54:00 +00:00
#ifndef GUI
void refreshDB( bool linux ) {
#else
void refreshDB( bool linux, void *progress_ptr ) {
#endif
if ( !FSLib::exists( getDBName() ) )
return;
2019-06-04 19:54:00 +00:00
std::vector< std::unordered_map< string, string > > shows;
SQLite::Database db{};
try {
db.open( getDBName(), SQLite::OPEN_READWRITE );
} catch ( std::exception &e ) {
2020-03-12 19:52:23 +00:00
cerr << _( DB_CANT_OPEN ) << std::endl;
2019-06-04 19:54:00 +00:00
throw e;
}
2020-01-18 21:18:58 +00:00
2019-06-04 19:54:00 +00:00
db.exec( "DELETE FROM EPISODES;" );
db.exec(
2020-01-15 18:55:24 +00:00
TEXT(
"SELECT ID, TVID, SHOW, PATH, LANGUAGE, DVD FROM SHOWS WHERE TVID "
"!= 'pattern';" ),
shows );
2019-06-04 19:54:00 +00:00
string pattern{};
db.exec( TEXT( "SELECT PATH FROM SHOWS WHERE TVID == 'pattern';" ),
2019-06-04 19:54:00 +00:00
pattern );
#ifndef GUI
ProgressBar p;
2020-03-12 19:52:23 +00:00
p.print( _( REFRESHING_DB ) );
cout << std::endl << std::endl;
#else // GUI
ProgressBar p( progress_ptr );
#endif // GUI
2019-06-04 19:54:00 +00:00
for ( auto &show : shows ) {
if ( FSLib::exists( show[TEXT( "PATH" )] ) ) {
2020-01-19 11:03:38 +00:00
#ifndef GUI
2019-06-04 19:54:00 +00:00
cleanUpLine();
2020-01-19 11:03:38 +00:00
#endif
2020-03-12 19:52:23 +00:00
p.print( _( REFRESHING ) + string( " " ) + show[TEXT( "SHOW" )] );
p.print( 0 );
2020-01-15 18:55:24 +00:00
std::map< int, std::map< int, string > > seasons;
2020-01-18 21:18:58 +00:00
2019-06-04 19:54:00 +00:00
iterateFS( seasons, show[TEXT( "PATH" )] );
2020-01-18 21:18:58 +00:00
auto seasons_size = seasons.size();
2019-06-04 19:54:00 +00:00
size_t i{};
2020-01-15 18:55:24 +00:00
for ( auto &x : seasons ) {
2019-06-04 19:54:00 +00:00
singleSeason( show[TEXT( "PATH" )], show[TEXT( "SHOW" )],
x.first, show[TEXT( "TVID" )],
show[TEXT( "LANGUAGE" )], pattern, linux, true,
2020-01-15 18:55:24 +00:00
&x.second, false,
show[TEXT( "DVD" )] == TEXT( "1" ) );
2019-06-04 19:54:00 +00:00
i++;
2020-01-18 21:18:58 +00:00
p.print( ( i * 100 ) / seasons_size );
2019-06-04 19:54:00 +00:00
}
p.print( 100 );
2019-07-12 21:10:40 +00:00
#ifndef GUI
2019-06-04 19:54:00 +00:00
cout << std::endl;
cleanUpLine();
2019-07-12 21:10:40 +00:00
#endif
2020-03-12 19:52:23 +00:00
p.print( _( UPDATING_IN_DB, show[TEXT( "SHOW" )].c_str() ) );
p.print( 0 );
i = 0;
size_t j{};
2020-01-18 21:18:58 +00:00
size_t addition = 100 / seasons_size;
2019-06-04 19:54:00 +00:00
for ( auto &season : seasons ) {
j = 0;
size_t smalladdition = addition / season.second.size();
2019-06-04 19:54:00 +00:00
for ( auto &episode : season.second ) {
db.exec( TEXT( "INSERT INTO EPISODES ( SHOWID, PATH ) "
"VALUES ( " ) +
show[TEXT( "ID" )] + TEXT( ", '" ) +
2020-01-15 18:55:24 +00:00
sanitize( episode.second ) + TEXT( "' );" ) );
j++;
p.print( i * addition + j * smalladdition );
2019-06-04 19:54:00 +00:00
}
i++;
p.print( i * addition );
2019-06-04 19:54:00 +00:00
}
p.print( 100 );
#ifndef GUI
cout << std::endl;
#endif
2019-06-04 19:54:00 +00:00
}
}
}
#ifndef GUI
void updateDB( bool linux ) {
#else
void updateDB( bool linux, void *progress_ptr ) {
#endif
if ( !FSLib::exists( getDBName() ) )
return;
2019-06-04 19:54:00 +00:00
std::vector< std::unordered_map< string, string > > shows;
SQLite::Database db{};
try {
db.open( getDBName(), SQLite::OPEN_READWRITE );
} catch ( std::exception &e ) {
2020-03-12 19:52:23 +00:00
cerr << _( DB_CANT_OPEN ) << std::endl;
2019-06-04 19:54:00 +00:00
throw e;
}
2020-01-15 18:55:24 +00:00
db.exec(
TEXT( "SELECT ID, TVID, SHOW, PATH, LANGUAGE, DVD FROM SHOWS WHERE TVID"
" != 'pattern';" ),
shows );
2019-06-04 19:54:00 +00:00
string pattern{};
db.exec( TEXT( "SELECT PATH FROM SHOWS WHERE TVID == 'pattern';" ),
2019-06-04 19:54:00 +00:00
pattern );
#ifndef GUI
ProgressBar p;
#else // GUI
ProgressBar p( progress_ptr );
#endif
2020-03-12 19:52:23 +00:00
p.print( _( UPDATING_DB ) );
#ifndef GUI
cout << std::endl << std::endl;
2019-06-04 19:54:00 +00:00
#endif
for ( auto &show : shows ) {
if ( !FSLib::exists( show[TEXT( "PATH" )] ) ) {
continue;
}
2020-01-19 11:03:38 +00:00
#ifndef GUI
2019-06-04 19:54:00 +00:00
cleanUpLine();
2020-01-19 11:03:38 +00:00
#endif
2020-03-12 19:52:23 +00:00
p.print( _( UPDATING ) + string( ' ', 1 ) + show[TEXT( "SHOW" )] );
2019-06-04 19:54:00 +00:00
std::unordered_set< string > episodes;
db.exec( TEXT( "SELECT PATH FROM EPISODES WHERE SHOWID == " ) +
show[TEXT( "ID" )] + TEXT( ";" ),
episodes );
2020-01-15 18:55:24 +00:00
std::map< int, std::map< int, string > > seasons;
std::map< int, std::map< int, string > > new_eps;
// get all season numbers from this directory and subdirectories
2019-06-04 19:54:00 +00:00
iterateFS( seasons, show[TEXT( "PATH" )] );
for ( const auto &x : seasons ) {
for ( const auto &episode : x.second ) {
2020-01-15 18:55:24 +00:00
if ( episodes.find( episode.second ) == episodes.end() ) {
2019-06-04 19:54:00 +00:00
new_eps[x.first].insert( episode );
}
}
}
p.print( 0 );
2019-06-04 19:54:00 +00:00
if ( !new_eps.empty() ) {
auto size = new_eps.size();
size_t i{};
2020-01-15 18:55:24 +00:00
for ( auto &x : new_eps ) {
2019-06-04 19:54:00 +00:00
singleSeason( show[TEXT( "PATH" )], show[TEXT( "SHOW" )],
x.first, show[TEXT( "TVID" )],
show[TEXT( "LANGUAGE" )], pattern, linux, true,
2020-01-15 18:55:24 +00:00
&x.second, false,
show[TEXT( "DVD" )] == TEXT( "1" ) );
2019-06-04 19:54:00 +00:00
i++;
p.print( ( i * 100 ) / size );
2019-06-04 19:54:00 +00:00
}
2020-01-18 21:18:58 +00:00
for ( auto &season : new_eps ) {
2019-06-04 19:54:00 +00:00
for ( auto &episode : season.second ) {
db.exec( TEXT( "INSERT OR IGNORE INTO EPISODES ( SHOWID, "
"PATH ) VALUES ( " ) +
show[TEXT( "ID" )] + TEXT( ", '" ) +
2020-01-15 18:55:24 +00:00
sanitize( episode.second ) + TEXT( "' );" ) );
2019-06-04 19:54:00 +00:00
}
}
}
p.print( 100 );
2019-07-12 21:10:40 +00:00
#ifndef GUI
2019-06-04 19:54:00 +00:00
cout << std::endl;
2019-07-12 21:10:40 +00:00
#endif
2019-06-04 19:54:00 +00:00
}
}
void changeDBPattern( const string &pattern ) {
if ( !FSLib::exists( getDBName() ) )
return;
2019-06-04 19:54:00 +00:00
SQLite::Database db{};
try {
db.open( getDBName(), SQLite::OPEN_READWRITE );
} catch ( std::exception &e ) {
2020-03-12 19:52:23 +00:00
cerr << _( DB_CANT_OPEN ) << std::endl;
2019-06-04 19:54:00 +00:00
throw e;
}
db.exec( TEXT( "UPDATE SHOWS SET PATH = '" ) + pattern +
TEXT( "' WHERE TVID == 'pattern';" ) );
2019-06-04 19:54:00 +00:00
}
void cleanDB() {
if ( !FSLib::exists( getDBName() ) )
return;
2019-06-04 19:54:00 +00:00
std::vector< std::unordered_map< string, string > > shows;
SQLite::Database db{};
try {
db.open( getDBName(), SQLite::OPEN_READWRITE );
} catch ( std::exception &e ) {
2020-03-12 19:52:23 +00:00
cerr << _( DB_CANT_OPEN ) << std::endl;
2019-06-04 19:54:00 +00:00
throw e;
}
2020-01-15 18:55:24 +00:00
db.exec( TEXT( "SELECT ID, PATH FROM SHOWS WHERE TVID"
" != 'pattern';" ),
2019-06-04 19:54:00 +00:00
shows );
for ( auto &show : shows ) {
2020-01-15 18:55:24 +00:00
bool dir_exists = FSLib::exists( show[TEXT( "PATH" )] );
2019-06-04 19:54:00 +00:00
std::unordered_set< string > episodes;
db.exec( TEXT( "SELECT PATH FROM EPISODES WHERE SHOWID == " ) +
show[TEXT( "ID" )] + TEXT( ";" ),
episodes );
for ( const auto &episode : episodes ) {
2020-01-15 18:55:24 +00:00
if ( !dir_exists || !FSLib::exists( episode ) ) {
2019-06-04 19:54:00 +00:00
db.exec( TEXT( "DELETE FROM EPISODES WHERE PATH == '" ) +
sanitize( episode ) + TEXT( "';" ) );
}
}
episodes.clear();
db.exec( TEXT( "SELECT PATH FROM EPISODES WHERE SHOWID == " ) +
show[TEXT( "ID" )] + TEXT( ";" ),
episodes );
if ( episodes.empty() ) {
db.exec( TEXT( "DELETE FROM SHOWS WHERE ID == " ) +
show[TEXT( "ID" )] + TEXT( ";" ) );
}
}
}
void removeFromDB( const string &path ) {
if ( !FSLib::exists( getDBName() ) )
return;
2019-06-04 19:54:00 +00:00
SQLite::Database db{};
try {
db.open( getDBName(), SQLite::OPEN_READWRITE );
} catch ( std::exception &e ) {
2020-03-12 19:52:23 +00:00
cerr << _( DB_CANT_OPEN ) << std::endl;
2019-06-04 19:54:00 +00:00
throw e;
}
string show_id{};
db.exec( TEXT( "SELECT ID FROM SHOWS WHERE PATH == '" ) + sanitize( path ) +
TEXT( "';" ),
show_id );
db.exec( TEXT( "DELETE FROM EPISODES WHERE SHOWID == " ) + show_id +
TEXT( ";" ) );
db.exec( TEXT( "DELETE FROM SHOWS WHERE ID == " ) + show_id + TEXT( ";" ) );
}
2020-01-18 21:18:58 +00:00
#ifdef GUI
2020-01-15 22:59:39 +00:00
std::vector< std::unordered_map< string, string > > dbGetShows() {
if ( !FSLib::exists( getDBName() ) )
return {};
2019-07-12 21:10:40 +00:00
SQLite::Database db{};
try {
db.open( getDBName(), SQLite::OPEN_READWRITE );
} catch ( std::exception &e ) {
2020-03-12 19:52:23 +00:00
cerr << _( DB_CANT_OPEN ) << std::endl;
2019-07-12 21:10:40 +00:00
throw e;
}
2020-01-15 22:59:39 +00:00
std::vector< std::unordered_map< string, string > > ret;
db.exec( TEXT( "SELECT * FROM SHOWS;" ), ret );
2019-07-12 21:10:40 +00:00
return ret;
}
void changeDB( size_t index, const string &path, const string &language,
2020-01-15 18:55:24 +00:00
const string &id, bool dvd ) {
if ( !FSLib::exists( getDBName() ) )
return;
2019-07-12 21:10:40 +00:00
SQLite::Database db{};
auto absolute = FSLib::canonical( path );
try {
db.open( getDBName(), SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE );
} catch ( std::exception &e ) {
2020-03-12 19:52:23 +00:00
cerr << _( DB_CANT_OPEN ) << std::endl;
2019-07-12 21:10:40 +00:00
throw e;
}
2020-01-15 22:59:39 +00:00
#ifndef _WIN32
string show_id = std::to_string( index );
2019-07-12 21:10:40 +00:00
#else
string show_id = std::to_wstring( index );
2019-07-12 21:10:40 +00:00
#endif
auto real_show = showNameFromId( id, language );
2020-01-15 18:55:24 +00:00
db.exec( TEXT( "UPDATE SHOWS SET TVID = '" ) + sanitize( id ) +
TEXT( "', SHOW = '" ) + sanitize( real_show ) +
TEXT( "', PATH = '" ) + sanitize( absolute ) +
TEXT( "', LANGUAGE = '" ) + sanitize( language ) +
2020-01-15 22:59:39 +00:00
TEXT( "', DVD = " ) + ( dvd ? TEXT( "1" ) : TEXT( "0" ) ) +
TEXT( " WHERE ID == " ) + show_id + TEXT( ";" ) );
2019-07-12 21:10:40 +00:00
}
void refreshSelectDB( std::unordered_set< size_t > indexes, bool linux,
void *progress_ptr ) {
if ( !FSLib::exists( getDBName() ) )
return;
2019-07-12 21:10:40 +00:00
std::vector< std::unordered_map< string, string > > shows;
string index_list{ '(' };
for ( auto &index : indexes ) {
#ifndef WIN32
index_list += std::to_string( index );
2020-01-15 22:59:39 +00:00
#else
index_list += std::to_wstring( index );
2020-01-15 22:59:39 +00:00
#endif
index_list += ',';
}
index_list.back() = ')';
2019-07-12 21:10:40 +00:00
SQLite::Database db{};
try {
db.open( getDBName(), SQLite::OPEN_READWRITE );
} catch ( std::exception &e ) {
2020-03-12 19:52:23 +00:00
cerr << _( DB_CANT_OPEN ) << std::endl;
2019-07-12 21:10:40 +00:00
throw e;
}
db.exec( TEXT( "DELETE FROM EPISODES WHERE SHOWID IN " ) + index_list +
TEXT( ";" ) );
db.exec( TEXT( "SELECT ID, TVID, SHOW, PATH, LANGUAGE, DVD FROM SHOWS "
"WHERE ID IN " ) +
index_list + TEXT( ";" ),
shows );
2019-07-12 21:10:40 +00:00
string pattern{};
db.exec( TEXT( "SELECT PATH FROM SHOWS WHERE TVID == 'pattern';" ),
2019-07-12 21:10:40 +00:00
pattern );
ProgressBar p( progress_ptr );
for ( auto &show : shows ) {
2020-03-12 19:52:23 +00:00
p.print( _( REFRESHING ) + string( ' ', 1 ) + show[TEXT( "SHOW" )] );
if ( FSLib::exists( show[TEXT( "PATH" )] ) ) {
p.print( 0 );
std::map< int, std::map< int, string > > seasons;
// get all season number from this directory and subdirectories
iterateFS( seasons, show[TEXT( "PATH" )] );
auto size = seasons.size();
size_t i{};
for ( auto &x : seasons ) {
singleSeason( show[TEXT( "PATH" )], show[TEXT( "SHOW" )],
x.first, show[TEXT( "TVID" )],
show[TEXT( "LANGUAGE" )], pattern, linux, true,
&x.second, false,
show[TEXT( "DVD" )] == TEXT( "1" ) );
i++;
p.print( ( i * 100 ) / size );
}
p.print( 100 );
for ( auto &season : seasons ) {
for ( auto &episode : season.second ) {
db.exec( TEXT( "INSERT INTO EPISODES ( SHOWID, PATH ) "
"VALUES ( " ) +
show[TEXT( "ID" )] + TEXT( ", '" ) +
sanitize( episode.second ) + TEXT( "' );" ) );
}
2019-07-12 21:10:40 +00:00
}
}
}
}
#endif
string getDBPattern() {
if ( !FSLib::exists( getDBName() ) )
return TEXT( "" );
SQLite::Database db{};
try {
db.open( getDBName(), SQLite::OPEN_READONLY );
} catch ( std::exception &e ) {
2020-03-12 19:52:23 +00:00
cerr << _( DB_CANT_OPEN ) << std::endl;
throw e;
}
string pattern{};
db.exec( TEXT( "SELECT PATH FROM SHOWS WHERE TVID == 'pattern';" ),
pattern );
return pattern;
}
/* change names of original files to generated new names
* orig - original filenames
* renamed - renamed filenames (sorted in the same order as `orig`)
*/
void renameFiles( const std::vector< std::tuple< int, string, string, string > >
&renamed_files ) {
for ( const auto &renamed : renamed_files ) {
FSLib::rename(
std::get< 1 >( renamed ) + TEXT( "/" ) + std::get< 2 >( renamed ),
std::get< 1 >( renamed ) + TEXT( "/" ) + std::get< 3 >( renamed ) );
}
}