2019-01-07 22:24:28 +00:00
# include <algorithm>
2019-02-04 16:39:48 +00:00
# include <array>
# include <cctype>
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
# ifdef _WIN32
2019-01-23 19:46:03 +00:00
2019-02-04 16:39:48 +00:00
# include <codecvt>
# include <shlobj.h>
2019-04-01 18:18:47 +00:00
# else // UNIX
2019-02-04 16:39:48 +00:00
2019-06-04 19:54:00 +00:00
# include <pwd.h>
# include <stdio.h>
# include <sys/ioctl.h>
# include <unistd.h>
2019-02-04 16:39:48 +00:00
# ifndef GUI
# include <iostream>
2018-09-22 22:50:42 +00:00
# include <stdlib.h>
2019-01-07 22:24:28 +00:00
# include <vector>
2018-09-22 22:50:42 +00:00
2019-02-04 16:39:48 +00:00
# endif // GUI
2019-04-01 18:18:47 +00:00
# endif // UNIX
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
# define cout std::wcout
# define cerr std::wcerr
# define cin std::wcin
constexpr const char_t * dir_divider = L " \\ " ;
2019-04-01 18:18:47 +00:00
# else // UNIX
2019-02-04 16:39:48 +00:00
# define TEXT( a ) a
# define cout std::cout
# define cerr std::cerr
# define cin std::cin
constexpr const char_t * dir_divider = " / " ;
2019-04-01 18:18:47 +00:00
# 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 ) ;
}
# endif // _WIN32
2019-01-23 19:46:03 +00:00
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 ( ) ;
}
2019-01-17 16:33:13 +00:00
// return true if filename has specified season
// set ep_pos to position where episode number starts
2019-02-04 16:39:48 +00:00
bool searchSpecificSeason ( const char_t * const path , size_t & ep_pos ,
const string & number ) {
2019-01-07 21:23:15 +00:00
size_t cur_pos { } ;
2019-02-07 18:36:36 +00:00
# ifdef _WIN32
auto ncompare = wcsncmp ;
# else
auto ncompare = strncmp ;
# endif
2019-02-04 16:39:48 +00:00
// search for S[0-9]+E[0-9]+
while ( path [ cur_pos ] ! = ' \0 ' ) {
if ( ( path [ cur_pos ] = = ' s ' | | path [ cur_pos ] = = ' S ' ) & &
iswdigit ( path [ cur_pos + 1 ] ) ) {
2019-01-07 21:23:15 +00:00
cur_pos + + ;
2019-02-04 16:39:48 +00:00
while ( path [ cur_pos ] = = ' 0 ' )
2019-01-07 21:23:15 +00:00
cur_pos + + ;
2019-06-01 19:37:58 +00:00
// if season number is 0, move back because previous loop skipped it
2019-06-01 19:54:45 +00:00
if ( number = = TEXT ( " 0 " ) )
2019-02-07 18:36:36 +00:00
cur_pos - - ;
2019-02-04 16:39:48 +00:00
// make sure season's number is the same as provided in argument
// `number`
2019-02-07 18:36:36 +00:00
if ( ! ncompare ( path + cur_pos , number . c_str ( ) , number . size ( ) ) ) {
cur_pos + = number . size ( ) ;
if ( ( path [ cur_pos ] = = ' e ' | | path [ cur_pos ] = = ' E ' ) & &
iswdigit ( path [ cur_pos + 1 ] ) ) {
ep_pos = cur_pos + 1 ;
return true ;
}
2019-01-07 21:23:15 +00:00
}
}
cur_pos + + ;
}
2019-02-04 16:39:48 +00:00
return false ;
2019-01-07 21:23:15 +00:00
}
2019-02-04 16:39:48 +00:00
bool searchSpecificSeason ( const char_t * const p , const string & number ) {
2019-01-07 21:23:15 +00:00
size_t tmp ;
2019-02-04 16:39:48 +00:00
return searchSpecificSeason ( p , tmp , number ) ;
2019-01-07 21:23:15 +00:00
}
2019-02-04 16:39:48 +00:00
// return true if file contains S[0-9]+E[0-9]+ nad set
// season_pos to start of season number
bool searchSeason ( const char_t * const path , size_t & season_pos ) {
2019-01-07 21:23:15 +00:00
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 ] ) ) {
2019-01-07 21:23:15 +00:00
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 ] ) )
2019-01-07 21:23:15 +00:00
cur_pos + + ;
2019-02-04 16:39:48 +00:00
if ( ( path [ cur_pos ] = = ' e ' | | path [ cur_pos ] = = ' E ' ) & &
iswdigit ( path [ cur_pos + 1 ] ) ) {
return true ;
2019-01-07 21:23:15 +00:00
}
}
cur_pos + + ;
}
2019-02-04 16:39:48 +00:00
return false ;
2019-01-07 21:23:15 +00:00
}
2019-02-04 16:39:48 +00:00
bool searchSeason ( const char_t * const path ) {
2019-01-07 21:23:15 +00:00
size_t tmp { } ;
2019-02-04 16:39:48 +00:00
return searchSeason ( path , tmp ) ;
2019-01-07 21:23:15 +00:00
}
2019-02-04 16:39:48 +00:00
void iterateFS ( std : : map < int , std : : set < string > > & seasons ,
const string & path ) {
// season_pos - position of first digit of the season
size_t season_pos { string : : npos } ;
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
if ( searchSeason ( p , season_pos ) )
2019-06-01 19:54:45 +00:00
seasons [ std : : stoi ( p + season_pos ) ] . insert ( path + dir_divider +
p ) ;
2019-01-23 19:46:03 +00:00
}
}
2019-02-04 16:39:48 +00:00
// find all files for provided season in `path` and store it in `files`
void findSeason ( std : : set < string > & files , int season , const string & path ) {
# ifdef _WIN32
auto number = std : : to_wstring ( season ) ;
# else
auto number = std : : to_string ( season ) ;
# endif
2019-01-07 21:23:15 +00:00
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 ) ) {
findSeason ( files , season , path + dir_divider + p ) ;
2019-01-04 19:19:08 +00:00
continue ;
}
2019-01-03 18:01:43 +00:00
2019-02-04 16:39:48 +00:00
if ( searchSpecificSeason ( p , number ) )
files . insert ( path + dir_divider + p ) ;
2019-01-04 19:19:08 +00:00
}
2018-09-22 22:50:42 +00:00
}
2019-02-04 16:39:48 +00:00
// find all files that comply with the S[0-9]+E[0-9]+ pattern
// and their season is in season_numbers and store tem in `seasons`
void findSeasons ( std : : map < int , std : : set < string > > & seasons ,
const string & path , const std : : set < int > & season_numbers ) {
// season_pos - position of first digit of the season
size_t season_pos { string : : npos } ;
for ( const auto p : FSLib : : Directory ( path ) ) {
// if p is directory, iterate through it
if ( FSLib : : isDirectory ( path + dir_divider + p ) ) {
findSeasons ( seasons , path + dir_divider + p , season_numbers ) ;
2019-01-17 16:23:15 +00:00
continue ;
}
2019-02-04 16:39:48 +00:00
if ( searchSeason ( p , season_pos ) ) {
auto num = std : : stoi ( p + season_pos ) ;
if ( season_numbers . find ( num ) ! = season_numbers . end ( ) )
seasons [ num ] . insert ( path + dir_divider + p ) ;
2019-01-17 16:23:15 +00:00
}
}
}
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 ( ) {
2019-06-01 19:37:58 +00:00
cout < < " Usage: " < < std : : endl ;
cout < < " tv_rename [options] [path] " < < std : : endl < < std : : endl ;
cout < < " -h, --help show this help message and exit "
2019-06-01 19:54:45 +00:00
< < std : : endl
< < std : : endl ;
2019-06-01 19:37:58 +00:00
cout < < " path can be either a file or a directory, if it's a directory "
2019-02-04 16:39:48 +00:00
< < std : : endl ;
2019-06-01 19:37:58 +00:00
cout < < " all files in it and its subdirectories will be renamed "
2019-06-01 19:54:45 +00:00
< < std : : endl
< < std : : endl ;
2019-06-01 19:37:58 +00:00
cout < < " OPTIONS " < < std : : endl ;
cout < < " -s, --show <string> TV show from which you want the " ;
cout < < " episode names " < < std : : endl ;
cout < < " -n, --season <numbers> Season number/s (if multiple seasons, "
2019-02-04 16:39:48 +00:00
< < std : : endl ;
2019-06-01 19:37:58 +00:00
cout < < " must be seperated by one space) "
< < " or 'all' " < < std : : endl ;
cout < < " for all seasons in selected directory "
2019-02-04 16:39:48 +00:00
< < std : : endl ;
2020-01-15 09:00:25 +00:00
cout < < " --dvd use dvd ordering instead of aired ordering " < < std : : endl ;
2019-06-01 19:37:58 +00:00
cout < < " --name-pattern <string> Pattern to which change the file name. "
2019-02-04 16:39:48 +00:00
< < std : : endl ;
2019-06-01 19:37:58 +00:00
cout < < " Possible sequences are: " < < std : : endl ;
cout < < " %filename - original filename "
2019-02-04 16:39:48 +00:00
< < std : : endl ;
2019-06-01 19:37:58 +00:00
cout < < " (without filetype "
< < " extension) " < < std : : endl ;
cout < < " %show - show name from thetvdb "
2019-02-04 16:39:48 +00:00
< < std : : endl ;
2019-06-01 19:37:58 +00:00
cout < < " %epname - episode name from thetvdb "
2019-02-04 16:39:48 +00:00
< < std : : endl ;
2019-06-01 19:37:58 +00:00
cout < < " %season - season number "
2019-02-04 16:39:48 +00:00
< < std : : endl ;
2019-06-01 19:37:58 +00:00
cout < < " it's possible to specify leading "
< < " 0 like this: " < < std : : endl ;
cout < < " %2season (number means how many "
< < " zeros) " < < std : : endl ;
cout < < " %episode - episode number "
2019-02-04 16:39:48 +00:00
< < std : : endl ;
2019-06-01 19:37:58 +00:00
cout < < " it's possible to specify leading "
< < " 0 like this: " < < std : : endl ;
cout < < " %2episode (number means how many "
< < " zeros) " < < std : : endl ;
cout < < " Default pattern is \" $filename - "
< < " $epname \" " < < std : : endl ;
cout < < " -c, --correct-path This is the correct path, "
< < " stop asking me! " < < std : : endl ;
cout < < " -t, --trust Don't ask whether the names are correct "
2019-02-04 16:39:48 +00:00
< < std : : endl ;
2019-06-01 19:37:58 +00:00
cout < < " -x, --linux Don't replace characters characters "
< < " that are " < < std : : endl ;
cout < < " illegal in Windows " < < std : : endl ;
cout < < " -l, --lang <string> Select which language the episode "
< < " names shoud be in " < < std : : endl ;
cout < < " --print-langs Pring available languages " < < std : : endl ;
2019-06-04 19:54:00 +00:00
cout < < std : : endl ;
cout < < " DATABASE OPTIONS " < < std : : endl ;
cout < < " --db-add Add path to the database "
< < std : : endl ;
cout < < " --db-refresh Refresh episode names for all paths "
< < " in the database " < < std : : endl ;
cout < < " --db-update Check all paths in the database, "
< < std : : endl ;
cout < < " if they contain new files, "
< < " rename them " < < std : : endl ;
cout < < " --db-name-pattern <string> Change name pattern used for files "
< < std : : endl
< < " managed by database " < < std : : endl ;
cout < < " --db-clean Remove deleted files from the "
< < " database " < < std : : endl ;
cout < < " --db-remove Remove path from the database "
< < 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
2019-02-04 16:39:48 +00:00
# ifndef _WIN32
// 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 ) ;
if ( user_passwd = = nullptr )
throw std : : runtime_error (
" User with uid " + std : : to_string ( user_uid ) + " doesn't exist! " ) ;
return user_passwd - > pw_dir ;
}
2019-04-01 18:18:47 +00:00
# else // UNIX
2019-02-04 16:39:48 +00:00
// 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 ;
}
return L " " ;
}
2019-04-01 18:18:47 +00:00
# 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
# ifdef WIN32
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 ;
}
2019-07-12 21:10:40 +00:00
void prepareDB ( const std : : 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 ) {
cerr < < " Couldn't create database, make sure you have "
< < " write permission for " < < getDBName ( ) < < std : : endl ;
throw e ;
}
2020-01-15 09:00:25 +00:00
db . exec (
" CREATE TABLE IF NOT EXISTS SHOWS (ID INTEGER NOT NULL "
" PRIMARY KEY AUTOINCREMENT, TVID TEXT NOT NULL, SHOW TEXT NOT NULL, "
" PATH TEXT NOT NULL UNIQUE, LANGUAGE TEXT 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 ;
2020-01-15 09:00:25 +00:00
if ( _pattern . empty ( ) ) {
2019-07-12 21:10:40 +00:00
cout < < " Insert name pattern for database: " < < std : : endl ;
auto * p = new string ;
std : : getline ( cin , * p ) ;
pattern = p ;
} else {
pattern = & _pattern ;
}
2020-01-15 09:00:25 +00:00
db . exec ( TEXT ( " INSERT INTO SHOWS ( TVID, SHOW, PATH, LANGUAGE ) "
" VALUES ( 'pattern', 'pattern', ' " ) +
sanitize ( * pattern ) + TEXT ( " ', 'pattern' ); " ) ) ;
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
2019-06-04 19:54:00 +00:00
void addToDB ( string & show , const string & path , const string & language ,
2020-01-15 09:00:25 +00:00
bool linux ) {
2019-06-04 19:54:00 +00:00
if ( ! FSLib : : exists ( getDBName ( ) ) )
prepareDB ( ) ;
2019-07-12 21:10:40 +00:00
# else
void addToDB ( string & show , const string & path , const string & language ,
2020-01-15 09:00:25 +00:00
const string & id , const string & pattern , bool linux ) {
2019-07-12 21:10:40 +00:00
if ( ! FSLib : : exists ( getDBName ( ) ) )
prepareDB ( pattern ) ;
# endif
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 ) {
cerr < < " Can't open database, make sure it exists " < < std : : endl ;
throw e ;
}
2019-07-12 21:10:40 +00:00
# ifndef GUI
2020-01-15 09:00:25 +00:00
auto id = getShowId ( show , language ) ;
show = showNameFromId ( id , language ) ;
2019-07-12 21:10:40 +00:00
# endif
2019-07-07 14:02:01 +00:00
string show_id { } ;
2020-01-15 09:00:25 +00:00
db . exec ( TEXT ( " INSERT OR IGNORE INTO SHOWS ( TVID, SHOW, PATH, LANGUAGE ) "
2019-06-04 19:54:00 +00:00
" VALUES ( ' " ) +
2020-01-15 09:00:25 +00:00
sanitize ( id ) + TEXT ( " ', ' " ) + sanitize ( show ) +
2019-06-04 19:54:00 +00:00
TEXT ( " ', ' " ) + sanitize ( absolute ) + TEXT ( " ', ' " ) +
sanitize ( language ) + TEXT ( " ' ); " ) ) ;
2019-07-07 14:02:01 +00:00
show_id = std : : to_string ( db . lastRowID ( ) ) ;
2019-07-12 21:10:40 +00:00
# ifndef GUI
string pattern { } ;
2020-01-15 09:00:25 +00:00
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
std : : map < int , std : : set < string > > seasons ;
// get all seasons and episodes
iterateFS ( seasons , absolute ) ;
auto size = seasons . size ( ) ;
size_t i = 0 ;
cout < < " Renaming " < < std : : endl ;
# ifdef WIN32
cout < < std : : endl ;
# endif
2019-07-12 21:10:40 +00:00
# ifndef GUI
2019-06-04 19:54:00 +00:00
ProgressBar : : print ( 0 ) ;
2019-07-12 21:10:40 +00:00
# endif
2019-06-04 19:54:00 +00:00
for ( const auto & x : seasons ) {
2020-01-15 09:00:25 +00:00
singleSeason ( absolute , show , x . first , id , language , pattern , linux ,
true , & x . second , false ) ;
2019-06-04 19:54:00 +00:00
i + + ;
2019-07-12 21:10:40 +00:00
# ifndef GUI
2019-06-04 19:54:00 +00:00
ProgressBar : : print ( ( i * 100 ) / size ) ;
2019-07-12 21:10:40 +00:00
# endif
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
seasons . clear ( ) ;
iterateFS ( seasons , absolute ) ;
size = seasons . size ( ) ;
i = 0 ;
cout < < " Adding to database " < < std : : endl ;
# ifdef WIN32
cout < < std : : endl ;
# endif
2019-07-12 21:10:40 +00:00
# ifndef GUI
2019-06-04 19:54:00 +00:00
ProgressBar : : print ( 0 ) ;
2019-07-12 21:10:40 +00:00
# endif
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 ( " ) +
2019-07-07 14:02:01 +00:00
show_id + TEXT ( " , ' " ) + sanitize ( episode ) +
2019-06-04 19:54:00 +00:00
TEXT ( " ' ); " ) ) ;
}
i + + ;
2019-07-12 21:10:40 +00:00
# ifndef GUI
2019-06-04 19:54:00 +00:00
ProgressBar : : print ( ( i * 100 ) / size ) ;
2019-07-12 21:10:40 +00:00
# endif
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
}
void cleanUpLine ( ) {
# ifdef WIN32
CONSOLE_SCREEN_BUFFER_INFO csbi ;
int width ;
GetConsoleScreenBufferInfo ( GetStdHandle ( STD_OUTPUT_HANDLE ) , & csbi ) ;
width = csbi . srWindow . Right - csbi . srWindow . Left + 1 ;
static HANDLE h = NULL ;
if ( ! h )
h = GetStdHandle ( STD_OUTPUT_HANDLE ) ;
CONSOLE_SCREEN_BUFFER_INFO info ;
GetConsoleScreenBufferInfo ( h , & info ) ;
COORD c = { 0 , info . dwCursorPosition . Y - 3 } ;
SetConsoleCursorPosition ( h , c ) ;
# else
struct winsize w ;
ioctl ( 0 , TIOCGWINSZ , & w ) ;
auto width = w . ws_col ;
cout < < " \x1b [2A " ;
# endif
cout < < string ( width , ' ' ) < < std : : endl < < std : : endl ;
# ifdef WIN32
SetConsoleCursorPosition ( h , c ) ;
# else
cout < < " \x1b [2A " ;
# endif
}
2020-01-15 09:00:25 +00:00
void refreshDB ( bool linux ) {
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 ) {
cerr < < " Can't open database, make sure it exists " < < std : : endl ;
throw e ;
}
db . exec ( " DELETE FROM EPISODES; " ) ;
2020-01-15 09:00:25 +00:00
db . exec (
TEXT ( " SELECT ID, TVID, SHOW, PATH, LANGUAGE FROM SHOWS WHERE TVID "
" != 'pattern'; " ) ,
shows ) ;
2019-06-04 19:54:00 +00:00
string pattern { } ;
2020-01-15 09:00:25 +00:00
db . exec ( TEXT ( " SELECT PATH FROM SHOWS WHERE TVID == 'pattern'; " ) ,
2019-06-04 19:54:00 +00:00
pattern ) ;
cout < < " Refreshing database " < < std : : endl < < std : : endl < < std : : endl ;
# ifdef WIN32
cout < < std : : endl ;
# endif
for ( auto & show : shows ) {
if ( FSLib : : exists ( show [ TEXT ( " PATH " ) ] ) ) {
cleanUpLine ( ) ;
cout < < " Refreshing " < < show [ TEXT ( " SHOW " ) ] < < std : : endl ;
# ifdef WIN32
cout < < std : : endl ;
# endif
2019-07-12 21:10:40 +00:00
# ifndef GUI
2019-06-04 19:54:00 +00:00
ProgressBar : : print ( 0 ) ;
2019-07-12 21:10:40 +00:00
# endif
2019-06-04 19:54:00 +00:00
std : : map < int , std : : set < string > > seasons ;
// get all season number from this directory and subdirectories
iterateFS ( seasons , show [ TEXT ( " PATH " ) ] ) ;
2019-07-12 21:10:40 +00:00
# ifndef GUI
2019-06-04 19:54:00 +00:00
auto size = seasons . size ( ) ;
2019-07-12 21:10:40 +00:00
# endif
2019-06-04 19:54:00 +00:00
size_t i { } ;
for ( const auto & x : seasons ) {
singleSeason ( show [ TEXT ( " PATH " ) ] , show [ TEXT ( " SHOW " ) ] ,
2020-01-15 09:00:25 +00:00
x . first , show [ TEXT ( " TVID " ) ] ,
show [ TEXT ( " LANGUAGE " ) ] , pattern , linux , true ,
2019-06-04 19:54:00 +00:00
& x . second , false ) ;
i + + ;
2019-07-12 21:10:40 +00:00
# ifndef GUI
2019-06-04 19:54:00 +00:00
ProgressBar : : print ( ( i * 100 ) / size ) ;
2019-07-12 21:10:40 +00:00
# endif
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
ProgressBar : : print ( 100 ) ;
cout < < std : : endl ;
2019-07-12 21:10:40 +00:00
# endif
2019-06-04 19:54:00 +00:00
seasons . clear ( ) ;
iterateFS ( seasons , show [ TEXT ( " PATH " ) ] ) ;
for ( auto & season : seasons ) {
for ( auto & episode : season . second ) {
db . exec ( TEXT ( " INSERT INTO EPISODES ( SHOWID, PATH ) "
" VALUES ( " ) +
show [ TEXT ( " ID " ) ] + TEXT ( " , ' " ) +
sanitize ( episode ) + TEXT ( " ' ); " ) ) ;
}
}
}
}
}
2020-01-15 09:00:25 +00:00
void updateDB ( bool linux ) {
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 ) {
cerr < < " Can't open database, make sure it exists " < < std : : endl ;
throw e ;
}
2020-01-15 09:00:25 +00:00
db . exec ( TEXT ( " SELECT ID, TVID, SHOW, PATH, LANGUAGE FROM SHOWS WHERE TVID "
" != 'pattern'; " ) ,
2019-06-04 19:54:00 +00:00
shows ) ;
string pattern { } ;
2020-01-15 09:00:25 +00:00
db . exec ( TEXT ( " SELECT PATH FROM SHOWS WHERE TVID == 'pattern'; " ) ,
2019-06-04 19:54:00 +00:00
pattern ) ;
cout < < " Updating database " < < std : : endl < < std : : endl < < std : : endl ;
# ifdef WIN32
cout < < std : : endl ;
# endif
for ( auto & show : shows ) {
if ( ! FSLib : : exists ( show [ TEXT ( " PATH " ) ] ) ) {
continue ;
}
cleanUpLine ( ) ;
cout < < " Updating " < < show [ TEXT ( " SHOW " ) ] < < std : : endl ;
# ifdef WIN32
cout < < std : : endl ;
# endif
std : : unordered_set < string > episodes ;
db . exec ( TEXT ( " SELECT PATH FROM EPISODES WHERE SHOWID == " ) +
show [ TEXT ( " ID " ) ] + TEXT ( " ; " ) ,
episodes ) ;
std : : map < int , std : : set < string > > seasons ;
std : : map < int , std : : set < string > > new_eps ;
// get all season number from this directory and subdirectories
iterateFS ( seasons , show [ TEXT ( " PATH " ) ] ) ;
for ( const auto & x : seasons ) {
for ( const auto & episode : x . second ) {
if ( episodes . find ( episode ) = = episodes . end ( ) ) {
new_eps [ x . first ] . insert ( episode ) ;
}
}
}
2019-07-12 21:10:40 +00:00
# ifndef GUI
2019-06-04 19:54:00 +00:00
ProgressBar : : print ( 0 ) ;
2019-07-12 21:10:40 +00:00
# endif
2019-06-04 19:54:00 +00:00
if ( ! new_eps . empty ( ) ) {
2019-07-12 21:10:40 +00:00
# ifndef GUI
2019-06-04 19:54:00 +00:00
auto size = new_eps . size ( ) ;
2019-07-12 21:10:40 +00:00
# endif
2019-06-04 19:54:00 +00:00
size_t i { } ;
for ( const auto & x : new_eps ) {
singleSeason ( show [ TEXT ( " PATH " ) ] , show [ TEXT ( " SHOW " ) ] ,
2020-01-15 09:00:25 +00:00
x . first , show [ TEXT ( " TVID " ) ] ,
show [ TEXT ( " LANGUAGE " ) ] , pattern , linux , true ,
2019-06-04 19:54:00 +00:00
& x . second , false ) ;
i + + ;
2019-07-12 21:10:40 +00:00
# ifndef GUI
2019-06-04 19:54:00 +00:00
ProgressBar : : print ( ( i * 100 ) / size ) ;
2019-07-12 21:10:40 +00:00
# endif
2019-06-04 19:54:00 +00:00
}
seasons . clear ( ) ;
iterateFS ( seasons , show [ TEXT ( " PATH " ) ] ) ;
for ( auto & season : seasons ) {
for ( auto & episode : season . second ) {
db . exec ( TEXT ( " INSERT OR IGNORE INTO EPISODES ( SHOWID, "
" PATH ) VALUES ( " ) +
show [ TEXT ( " ID " ) ] + TEXT ( " , ' " ) +
sanitize ( episode ) + TEXT ( " ' ); " ) ) ;
}
}
}
2019-07-12 21:10:40 +00:00
# ifndef GUI
2019-06-04 19:54:00 +00:00
ProgressBar : : print ( 100 ) ;
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 ) {
SQLite : : Database db { } ;
try {
db . open ( getDBName ( ) , SQLite : : OPEN_READWRITE ) ;
} catch ( std : : exception & e ) {
cerr < < " Can't open database, make sure it exists " < < std : : endl ;
throw e ;
}
db . exec ( TEXT ( " UPDATE SHOWS SET PATH = ' " ) + pattern +
2020-01-15 09:00:25 +00:00
TEXT ( " ' WHERE TVID == 'pattern'; " ) ) ;
2019-06-04 19:54:00 +00:00
}
void cleanDB ( ) {
std : : vector < std : : unordered_map < string , string > > shows ;
SQLite : : Database db { } ;
try {
db . open ( getDBName ( ) , SQLite : : OPEN_READWRITE ) ;
} catch ( std : : exception & e ) {
cerr < < " Can't open database, make sure it exists " < < std : : endl ;
throw e ;
}
2020-01-15 09:00:25 +00:00
db . exec ( TEXT ( " SELECT ID, TVID, SHOW, PATH, LANGUAGE FROM SHOWS WHERE TVID "
" != 'pattern'; " ) ,
2019-06-04 19:54:00 +00:00
shows ) ;
string pattern { } ;
2020-01-15 09:00:25 +00:00
db . exec ( TEXT ( " SELECT PATH FROM SHOWS WHERE TVID == 'pattern'; " ) ,
2019-06-04 19:54:00 +00:00
pattern ) ;
for ( auto & show : shows ) {
2019-07-12 21:10:40 +00:00
if ( FSLib : : exists ( show [ TEXT ( " PATH " ) ] ) ) {
2019-06-04 19:54:00 +00:00
continue ;
}
std : : unordered_set < string > episodes ;
db . exec ( TEXT ( " SELECT PATH FROM EPISODES WHERE SHOWID == " ) +
show [ TEXT ( " ID " ) ] + TEXT ( " ; " ) ,
episodes ) ;
for ( const auto & episode : episodes ) {
if ( ! FSLib : : exists ( episode ) ) {
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 ) {
SQLite : : Database db { } ;
try {
db . open ( getDBName ( ) , SQLite : : OPEN_READWRITE ) ;
} catch ( std : : exception & e ) {
cerr < < " Can't open database, make sure it exists " < < std : : endl ;
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 ( " ; " ) ) ;
}
2019-07-12 21:10:40 +00:00
std : : vector < std : : unordered_map < std : : string , std : : string > > dbGetShows ( ) {
SQLite : : Database db { } ;
try {
db . open ( getDBName ( ) , SQLite : : OPEN_READWRITE ) ;
} catch ( std : : exception & e ) {
cerr < < " Can't open database, make sure it exists " < < std : : endl ;
throw e ;
}
std : : vector < std : : unordered_map < std : : string , std : : string > > ret ;
2020-01-15 09:00:25 +00:00
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 09:00:25 +00:00
const string & id ) {
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 ) {
cerr < < " Can't open database, make sure it exists " < < std : : endl ;
throw e ;
}
# ifndef WIN32
2020-01-15 09:00:25 +00:00
string show_id = std : : to_string ( index ) ;
2019-07-12 21:10:40 +00:00
# else
2020-01-15 09:00:25 +00:00
string show_id = std : : to_wstring ( index ) ;
2019-07-12 21:10:40 +00:00
# endif
# ifdef _WIN32
string source_code = utf8_to_wstring (
c . execute ( url ) ;
# else
2020-01-15 09:00:25 +00:00
auto real_show = showNameFromId ( id , language ) ;
2019-07-12 21:10:40 +00:00
# endif
2020-01-15 09:00:25 +00:00
db . exec ( TEXT ( " UPDATE SHOWS SET TVID = ' " ) + sanitize ( id ) + TEXT ( " ', SHOW = ' " ) + sanitize ( real_show ) + TEXT ( " ', PATH = ' " ) + sanitize ( absolute ) + TEXT ( " ', LANGUAGE = ' " ) +
2019-07-12 21:10:40 +00:00
sanitize ( language ) + TEXT ( " ' WHERE ID == " + show_id + " ; " ) ) ;
}
2020-01-15 09:00:25 +00:00
void refreshSingleDB ( const size_t & index , bool linux ) {
2019-07-12 21:10:40 +00:00
std : : vector < std : : unordered_map < string , string > > shows ;
string show_id = std : : to_string ( index ) ;
SQLite : : Database db { } ;
try {
db . open ( getDBName ( ) , SQLite : : OPEN_READWRITE ) ;
} catch ( std : : exception & e ) {
cerr < < " Can't open database, make sure it exists " < < std : : endl ;
throw e ;
}
db . exec ( " DELETE FROM EPISODES WHERE SHOWID == " + show_id + " ; " ) ;
2020-01-15 09:00:25 +00:00
db . exec ( TEXT ( " SELECT TVID, SHOW, PATH, LANGUAGE FROM SHOWS WHERE ID == " +
show_id + " ; " ) ,
shows ) ;
2019-07-12 21:10:40 +00:00
std : : unordered_map < string , string > & show = shows [ 0 ] ;
string pattern { } ;
2020-01-15 09:00:25 +00:00
db . exec ( TEXT ( " SELECT PATH FROM SHOWS WHERE TVID == 'pattern'; " ) ,
2019-07-12 21:10:40 +00:00
pattern ) ;
2020-01-15 09:00:25 +00:00
cout < < " Refreshing " < < show [ TEXT ( " SHOW " ) ] < < std : : endl
< < std : : endl
< < std : : endl ;
2019-07-12 21:10:40 +00:00
# ifdef WIN32
cout < < std : : endl ;
2019-06-04 19:54:00 +00:00
# endif
2019-07-12 21:10:40 +00:00
if ( FSLib : : exists ( show [ TEXT ( " PATH " ) ] ) ) {
# ifdef WIN32
cout < < std : : endl ;
# endif
# ifndef GUI
ProgressBar : : print ( 0 ) ;
# endif
std : : map < int , std : : set < string > > seasons ;
// get all season number from this directory and subdirectories
iterateFS ( seasons , show [ TEXT ( " PATH " ) ] ) ;
# ifndef GUI
auto size = seasons . size ( ) ;
# endif
size_t i { } ;
for ( const auto & x : seasons ) {
2020-01-15 09:00:25 +00:00
singleSeason ( show [ TEXT ( " PATH " ) ] , show [ TEXT ( " SHOW " ) ] , x . first ,
show [ TEXT ( " TVID " ) ] , show [ TEXT ( " LANGUAGE " ) ] ,
pattern , linux , true , & x . second , false ) ;
2019-07-12 21:10:40 +00:00
i + + ;
# ifndef GUI
ProgressBar : : print ( ( i * 100 ) / size ) ;
# endif
}
# ifndef GUI
ProgressBar : : print ( 100 ) ;
cout < < std : : endl ;
# endif
seasons . clear ( ) ;
iterateFS ( seasons , show [ TEXT ( " PATH " ) ] ) ;
for ( auto & season : seasons ) {
for ( auto & episode : season . second ) {
db . exec ( TEXT ( " INSERT INTO EPISODES ( SHOWID, PATH ) "
" VALUES ( " ) +
2020-01-15 09:00:25 +00:00
show_id + TEXT ( " , ' " ) + sanitize ( episode ) +
TEXT ( " ' ); " ) ) ;
2019-07-12 21:10:40 +00:00
}
}
}
}