Functions: slight refactor

This commit is contained in:
zvon 2020-01-15 19:55:24 +01:00
parent 1036431f2e
commit 5b38547fe3
2 changed files with 105 additions and 208 deletions

View File

@ -7,28 +7,6 @@
#include <unordered_set> #include <unordered_set>
#include <vector> #include <vector>
#ifdef _WIN32
#include <codecvt>
#include <shlobj.h>
#else // UNIX
#include <pwd.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#ifndef GUI
#include <iostream>
#include <stdlib.h>
#include <vector>
#endif // GUI
#endif // UNIX
#include "filesystem.hpp" #include "filesystem.hpp"
#include "functions.hpp" #include "functions.hpp"
#include "progress.hpp" #include "progress.hpp"
@ -37,6 +15,9 @@
#ifdef _WIN32 #ifdef _WIN32
#include <codecvt>
#include <shlobj.h>
#define cout std::wcout #define cout std::wcout
#define cerr std::wcerr #define cerr std::wcerr
#define cin std::wcin #define cin std::wcin
@ -45,6 +26,11 @@ constexpr const char_t *dir_divider = L"\\";
#else // UNIX #else // UNIX
#include <pwd.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#define TEXT( a ) a #define TEXT( a ) a
#define cout std::cout #define cout std::cout
@ -95,50 +81,10 @@ string encodeUrl( const string &url ) {
return encoded.str(); return encoded.str();
} }
// return true if filename has specified season // return true if file contains S[0-9]+E[0-9]+ and set
// set ep_pos to position where episode number starts
bool searchSpecificSeason( const char_t *const path, size_t &ep_pos,
const string &number ) {
size_t cur_pos{};
#ifdef _WIN32
auto ncompare = wcsncmp;
#else
auto ncompare = strncmp;
#endif
// 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] ) ) {
cur_pos++;
while ( path[cur_pos] == '0' )
cur_pos++;
// if season number is 0, move back because previous loop skipped it
if ( number == TEXT( "0" ) )
cur_pos--;
// make sure season's number is the same as provided in argument
// `number`
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;
}
}
}
cur_pos++;
}
return false;
}
bool searchSpecificSeason( const char_t *const p, const string &number ) {
size_t tmp;
return searchSpecificSeason( p, tmp, number );
}
// return true if file contains S[0-9]+E[0-9]+ nad set
// season_pos to start of season number // season_pos to start of season number
bool searchSeason( const char_t *const path, size_t &season_pos ) { bool searchSeason( const char_t *const path, size_t &season_pos,
size_t &ep_pos ) {
size_t cur_pos{}; size_t cur_pos{};
while ( path[cur_pos] != '\0' ) { while ( path[cur_pos] != '\0' ) {
if ( ( path[cur_pos] == 's' || path[cur_pos] == 'S' ) && if ( ( path[cur_pos] == 's' || path[cur_pos] == 'S' ) &&
@ -150,6 +96,7 @@ bool searchSeason( const char_t *const path, size_t &season_pos ) {
cur_pos++; cur_pos++;
if ( ( path[cur_pos] == 'e' || path[cur_pos] == 'E' ) && if ( ( path[cur_pos] == 'e' || path[cur_pos] == 'E' ) &&
iswdigit( path[cur_pos + 1] ) ) { iswdigit( path[cur_pos + 1] ) ) {
ep_pos = cur_pos + 1;
return true; return true;
} }
} }
@ -158,15 +105,16 @@ bool searchSeason( const char_t *const path, size_t &season_pos ) {
return false; return false;
} }
bool searchSeason( const char_t *const path ) { bool searchSeason( const char_t *const path, size_t &season_pos ) {
size_t tmp{}; size_t tmp;
return searchSeason( path, tmp ); return searchSeason( path, season_pos, tmp );
} }
void iterateFS( std::map< int, std::set< string > > &seasons, void iterateFS( std::map< int, std::map< int, string > > &seasons,
const string &path ) { const string &path ) {
// season_pos - position of first digit of the season // season_pos - position of first digit of the season
size_t season_pos{ string::npos }; size_t season_pos{ string::npos };
size_t ep_pos{ string::npos };
for ( const auto p : FSLib::Directory( path ) ) { for ( const auto p : FSLib::Directory( path ) ) {
// if p is directory, iterate through it // if p is directory, iterate through it
if ( FSLib::isDirectory( path + dir_divider + p ) ) { if ( FSLib::isDirectory( path + dir_divider + p ) ) {
@ -176,50 +124,9 @@ void iterateFS( std::map< int, std::set< string > > &seasons,
// if file is a correct format, add it to file list // if file is a correct format, add it to file list
// for its season // for its season
if ( searchSeason( p, season_pos ) ) if ( searchSeason( p, season_pos, ep_pos ) )
seasons[std::stoi( p + season_pos )].insert( path + dir_divider + seasons[std::stoi( p + season_pos )][std::stoi( p + ep_pos )] =
p ); path + dir_divider + p;
}
}
// 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
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 );
continue;
}
if ( searchSpecificSeason( p, number ) )
files.insert( path + dir_divider + p );
}
}
// 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 );
continue;
}
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 );
}
} }
} }
@ -246,7 +153,9 @@ void printHelp() {
<< " or 'all'" << std::endl; << " or 'all'" << std::endl;
cout << " for all seasons in selected directory" cout << " for all seasons in selected directory"
<< std::endl; << std::endl;
cout << " --dvd use dvd ordering instead of aired ordering" << std::endl; cout << " -d, --dvd use dvd ordering instead of aired "
"ordering"
<< std::endl;
cout << " --name-pattern <string> Pattern to which change the file name." cout << " --name-pattern <string> Pattern to which change the file name."
<< std::endl; << std::endl;
cout << " Possible sequences are:" << std::endl; cout << " Possible sequences are:" << std::endl;
@ -330,7 +239,22 @@ void parseSeasonNumbers( std::set< int > &seasons_num,
#endif // ndef GUI #endif // ndef GUI
#ifndef _WIN32 #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 );
return L"";
}
#else // UNIX
// get user's home directory // get user's home directory
string userHome() { string userHome() {
@ -349,20 +273,6 @@ string userHome() {
return user_passwd->pw_dir; return user_passwd->pw_dir;
} }
#else // UNIX
// 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"";
}
#endif // UNIX #endif // UNIX
// create file name based on given pattern // create file name based on given pattern
@ -502,7 +412,8 @@ void prepareDB( const std::string &_pattern ) {
db.exec( db.exec(
"CREATE TABLE IF NOT EXISTS SHOWS (ID INTEGER NOT NULL " "CREATE TABLE IF NOT EXISTS SHOWS (ID INTEGER NOT NULL "
"PRIMARY KEY AUTOINCREMENT, TVID TEXT NOT NULL, SHOW TEXT NOT NULL," "PRIMARY KEY AUTOINCREMENT, TVID TEXT NOT NULL, SHOW TEXT NOT NULL,"
"PATH TEXT NOT NULL UNIQUE, LANGUAGE TEXT NOT NULL);" ); "PATH TEXT NOT NULL UNIQUE, LANGUAGE TEXT NOT NULL, DVD INTEGER NOT "
"NULL);" );
db.exec( "CREATE TABLE IF NOT EXISTS EPISODES (SHOWID INTEGER NOT NULL," db.exec( "CREATE TABLE IF NOT EXISTS EPISODES (SHOWID INTEGER NOT NULL,"
"PATH TEXT NOT NULL UNIQUE, FOREIGN KEY(SHOWID) " "PATH TEXT NOT NULL UNIQUE, FOREIGN KEY(SHOWID) "
"REFERENCES SHOWS(ID));" ); "REFERENCES SHOWS(ID));" );
@ -515,9 +426,9 @@ void prepareDB( const std::string &_pattern ) {
} else { } else {
pattern = &_pattern; pattern = &_pattern;
} }
db.exec( TEXT( "INSERT INTO SHOWS ( TVID, SHOW, PATH, LANGUAGE ) " db.exec( TEXT( "INSERT INTO SHOWS ( TVID, SHOW, PATH, LANGUAGE, DVD ) "
"VALUES ( 'pattern', 'pattern', '" ) + "VALUES ( 'pattern', 'pattern', '" ) +
sanitize( *pattern ) + TEXT( "', 'pattern' );" ) ); sanitize( *pattern ) + TEXT( "', 'pattern', 0 );" ) );
if ( pattern != &_pattern ) { if ( pattern != &_pattern ) {
delete pattern; delete pattern;
} }
@ -525,12 +436,12 @@ void prepareDB( const std::string &_pattern ) {
#ifndef GUI #ifndef GUI
void addToDB( string &show, const string &path, const string &language, void addToDB( string &show, const string &path, const string &language,
bool linux ) { bool linux, bool dvd ) {
if ( !FSLib::exists( getDBName() ) ) if ( !FSLib::exists( getDBName() ) )
prepareDB(); prepareDB();
#else #else
void addToDB( string &show, const string &path, const string &language, void addToDB( string &show, const string &path, const string &language,
const string &id, const string &pattern, bool linux ) { const string &id, const string &pattern, bool linux, bool dvd ) {
if ( !FSLib::exists( getDBName() ) ) if ( !FSLib::exists( getDBName() ) )
prepareDB( pattern ); prepareDB( pattern );
#endif #endif
@ -543,23 +454,24 @@ void addToDB( string &show, const string &path, const string &language,
throw e; throw e;
} }
#ifndef GUI #ifndef GUI
// gui gives id and correct show name
auto id = getShowId( show, language ); auto id = getShowId( show, language );
show = showNameFromId( id, language ); show = showNameFromId( id, language );
#endif #endif
string show_id{}; db.exec(
db.exec( TEXT( "INSERT OR IGNORE INTO SHOWS ( TVID, SHOW, PATH, LANGUAGE ) " TEXT( "INSERT OR IGNORE INTO SHOWS ( TVID, SHOW, PATH, LANGUAGE, DVD ) "
"VALUES ( '" ) + "VALUES ( '" ) +
sanitize( id ) + TEXT( "', '" ) + sanitize( show ) + sanitize( id ) + TEXT( "', '" ) + sanitize( show ) + TEXT( "', '" ) +
TEXT( "', '" ) + sanitize( absolute ) + TEXT( "', '" ) + sanitize( absolute ) + TEXT( "', '" ) + sanitize( language ) + "', " +
sanitize( language ) + TEXT( "' );" ) ); ( dvd ? TEXT( "1" ) : TEXT( "0" ) ) + TEXT( " );" ) );
show_id = std::to_string( db.lastRowID() ); string show_id = std::to_string( db.lastRowID() );
#ifndef GUI #ifndef GUI
string pattern{}; string pattern{};
db.exec( TEXT( "SELECT PATH FROM SHOWS WHERE TVID == 'pattern';" ), db.exec( TEXT( "SELECT PATH FROM SHOWS WHERE TVID == 'pattern';" ),
pattern ); pattern );
#endif #endif
std::map< int, std::set< string > > seasons; std::map< int, std::map< int, string > > seasons;
// get all seasons and episodes // get all seasons and episodes
iterateFS( seasons, absolute ); iterateFS( seasons, absolute );
auto size = seasons.size(); auto size = seasons.size();
@ -571,9 +483,9 @@ void addToDB( string &show, const string &path, const string &language,
#ifndef GUI #ifndef GUI
ProgressBar::print( 0 ); ProgressBar::print( 0 );
#endif #endif
for ( const auto &x : seasons ) { for ( auto &x : seasons ) {
singleSeason( absolute, show, x.first, id, language, pattern, linux, singleSeason( absolute, show, x.first, id, language, pattern, linux,
true, &x.second, false ); true, &x.second, true, dvd );
i++; i++;
#ifndef GUI #ifndef GUI
ProgressBar::print( ( i * 100 ) / size ); ProgressBar::print( ( i * 100 ) / size );
@ -582,8 +494,6 @@ void addToDB( string &show, const string &path, const string &language,
#ifndef GUI #ifndef GUI
cout << std::endl; cout << std::endl;
#endif #endif
seasons.clear();
iterateFS( seasons, absolute );
size = seasons.size(); size = seasons.size();
i = 0; i = 0;
@ -598,7 +508,7 @@ void addToDB( string &show, const string &path, const string &language,
for ( auto &episode : season.second ) { for ( auto &episode : season.second ) {
db.exec( TEXT( "INSERT OR IGNORE INTO EPISODES ( SHOWID, PATH ) " db.exec( TEXT( "INSERT OR IGNORE INTO EPISODES ( SHOWID, PATH ) "
"VALUES ( " ) + "VALUES ( " ) +
show_id + TEXT( ", '" ) + sanitize( episode ) + show_id + TEXT( ", '" ) + sanitize( episode.second ) +
TEXT( "' );" ) ); TEXT( "' );" ) );
} }
i++; i++;
@ -611,8 +521,8 @@ void addToDB( string &show, const string &path, const string &language,
#endif #endif
} }
void cleanUpLine() {
#ifdef WIN32 #ifdef WIN32
void cleanUpLine() {
CONSOLE_SCREEN_BUFFER_INFO csbi; CONSOLE_SCREEN_BUFFER_INFO csbi;
int width; int width;
@ -626,21 +536,21 @@ void cleanUpLine() {
GetConsoleScreenBufferInfo( h, &info ); GetConsoleScreenBufferInfo( h, &info );
COORD c = { 0, info.dwCursorPosition.Y - 3 }; COORD c = { 0, info.dwCursorPosition.Y - 3 };
SetConsoleCursorPosition( h, c ); SetConsoleCursorPosition( h, c );
cout << string( width, ' ' ) << std::endl << std::endl;
SetConsoleCursorPosition( h, c );
}
#else #else
void cleanUpLine() {
struct winsize w; struct winsize w;
ioctl( 0, TIOCGWINSZ, &w ); ioctl( 0, TIOCGWINSZ, &w );
auto width = w.ws_col; auto width = w.ws_col;
cout << "\x1b[2A"; cout << "\x1b[2A";
#endif
cout << string( width, ' ' ) << std::endl << std::endl; cout << string( width, ' ' ) << std::endl << std::endl;
#ifdef WIN32
SetConsoleCursorPosition( h, c );
#else
cout << "\x1b[2A"; cout << "\x1b[2A";
#endif
} }
#endif
void refreshDB( bool linux ) { void refreshDB( bool linux ) {
std::vector< std::unordered_map< string, string > > shows; std::vector< std::unordered_map< string, string > > shows;
@ -653,8 +563,9 @@ void refreshDB( bool linux ) {
} }
db.exec( "DELETE FROM EPISODES;" ); db.exec( "DELETE FROM EPISODES;" );
db.exec( db.exec(
TEXT( "SELECT ID, TVID, SHOW, PATH, LANGUAGE FROM SHOWS WHERE TVID " TEXT(
"!= 'pattern';" ), "SELECT ID, TVID, SHOW, PATH, LANGUAGE, DVD FROM SHOWS WHERE TVID "
"!= 'pattern';" ),
shows ); shows );
string pattern{}; string pattern{};
@ -675,18 +586,19 @@ void refreshDB( bool linux ) {
#ifndef GUI #ifndef GUI
ProgressBar::print( 0 ); ProgressBar::print( 0 );
#endif #endif
std::map< int, std::set< string > > seasons; std::map< int, std::map< int, string > > seasons;
// get all season number from this directory and subdirectories // get all season number from this directory and subdirectories
iterateFS( seasons, show[TEXT( "PATH" )] ); iterateFS( seasons, show[TEXT( "PATH" )] );
#ifndef GUI #ifndef GUI
auto size = seasons.size(); auto size = seasons.size();
#endif #endif
size_t i{}; size_t i{};
for ( const auto &x : seasons ) { for ( auto &x : seasons ) {
singleSeason( show[TEXT( "PATH" )], show[TEXT( "SHOW" )], singleSeason( show[TEXT( "PATH" )], show[TEXT( "SHOW" )],
x.first, show[TEXT( "TVID" )], x.first, show[TEXT( "TVID" )],
show[TEXT( "LANGUAGE" )], pattern, linux, true, show[TEXT( "LANGUAGE" )], pattern, linux, true,
&x.second, false ); &x.second, false,
show[TEXT( "DVD" )] == TEXT( "1" ) );
i++; i++;
#ifndef GUI #ifndef GUI
ProgressBar::print( ( i * 100 ) / size ); ProgressBar::print( ( i * 100 ) / size );
@ -696,14 +608,12 @@ void refreshDB( bool linux ) {
ProgressBar::print( 100 ); ProgressBar::print( 100 );
cout << std::endl; cout << std::endl;
#endif #endif
seasons.clear();
iterateFS( seasons, show[TEXT( "PATH" )] );
for ( auto &season : seasons ) { for ( auto &season : seasons ) {
for ( auto &episode : season.second ) { for ( auto &episode : season.second ) {
db.exec( TEXT( "INSERT INTO EPISODES ( SHOWID, PATH ) " db.exec( TEXT( "INSERT INTO EPISODES ( SHOWID, PATH ) "
"VALUES ( " ) + "VALUES ( " ) +
show[TEXT( "ID" )] + TEXT( ", '" ) + show[TEXT( "ID" )] + TEXT( ", '" ) +
sanitize( episode ) + TEXT( "' );" ) ); sanitize( episode.second ) + TEXT( "' );" ) );
} }
} }
} }
@ -719,9 +629,10 @@ void updateDB( bool linux ) {
cerr << "Can't open database, make sure it exists" << std::endl; cerr << "Can't open database, make sure it exists" << std::endl;
throw e; throw e;
} }
db.exec( TEXT( "SELECT ID, TVID, SHOW, PATH, LANGUAGE FROM SHOWS WHERE TVID" db.exec(
" != 'pattern';" ), TEXT( "SELECT ID, TVID, SHOW, PATH, LANGUAGE, DVD FROM SHOWS WHERE TVID"
shows ); " != 'pattern';" ),
shows );
string pattern{}; string pattern{};
db.exec( TEXT( "SELECT PATH FROM SHOWS WHERE TVID == 'pattern';" ), db.exec( TEXT( "SELECT PATH FROM SHOWS WHERE TVID == 'pattern';" ),
@ -744,13 +655,13 @@ void updateDB( bool linux ) {
db.exec( TEXT( "SELECT PATH FROM EPISODES WHERE SHOWID == " ) + db.exec( TEXT( "SELECT PATH FROM EPISODES WHERE SHOWID == " ) +
show[TEXT( "ID" )] + TEXT( ";" ), show[TEXT( "ID" )] + TEXT( ";" ),
episodes ); episodes );
std::map< int, std::set< string > > seasons; std::map< int, std::map< int, string > > seasons;
std::map< int, std::set< string > > new_eps; std::map< int, std::map< int, string > > new_eps;
// get all season number from this directory and subdirectories // get all season numbers from this directory and subdirectories
iterateFS( seasons, show[TEXT( "PATH" )] ); iterateFS( seasons, show[TEXT( "PATH" )] );
for ( const auto &x : seasons ) { for ( const auto &x : seasons ) {
for ( const auto &episode : x.second ) { for ( const auto &episode : x.second ) {
if ( episodes.find( episode ) == episodes.end() ) { if ( episodes.find( episode.second ) == episodes.end() ) {
new_eps[x.first].insert( episode ); new_eps[x.first].insert( episode );
} }
} }
@ -763,11 +674,12 @@ void updateDB( bool linux ) {
auto size = new_eps.size(); auto size = new_eps.size();
#endif #endif
size_t i{}; size_t i{};
for ( const auto &x : new_eps ) { for ( auto &x : new_eps ) {
singleSeason( show[TEXT( "PATH" )], show[TEXT( "SHOW" )], singleSeason( show[TEXT( "PATH" )], show[TEXT( "SHOW" )],
x.first, show[TEXT( "TVID" )], x.first, show[TEXT( "TVID" )],
show[TEXT( "LANGUAGE" )], pattern, linux, true, show[TEXT( "LANGUAGE" )], pattern, linux, true,
&x.second, false ); &x.second, false,
show[TEXT( "DVD" )] == TEXT( "1" ) );
i++; i++;
#ifndef GUI #ifndef GUI
ProgressBar::print( ( i * 100 ) / size ); ProgressBar::print( ( i * 100 ) / size );
@ -780,7 +692,7 @@ void updateDB( bool linux ) {
db.exec( TEXT( "INSERT OR IGNORE INTO EPISODES ( SHOWID, " db.exec( TEXT( "INSERT OR IGNORE INTO EPISODES ( SHOWID, "
"PATH ) VALUES ( " ) + "PATH ) VALUES ( " ) +
show[TEXT( "ID" )] + TEXT( ", '" ) + show[TEXT( "ID" )] + TEXT( ", '" ) +
sanitize( episode ) + TEXT( "' );" ) ); sanitize( episode.second ) + TEXT( "' );" ) );
} }
} }
} }
@ -812,24 +724,18 @@ void cleanDB() {
cerr << "Can't open database, make sure it exists" << std::endl; cerr << "Can't open database, make sure it exists" << std::endl;
throw e; throw e;
} }
db.exec( TEXT( "SELECT ID, TVID, SHOW, PATH, LANGUAGE FROM SHOWS WHERE TVID" db.exec( TEXT( "SELECT ID, PATH FROM SHOWS WHERE TVID"
" != 'pattern';" ), " != 'pattern';" ),
shows ); shows );
string pattern{};
db.exec( TEXT( "SELECT PATH FROM SHOWS WHERE TVID == 'pattern';" ),
pattern );
for ( auto &show : shows ) { for ( auto &show : shows ) {
if ( FSLib::exists( show[TEXT( "PATH" )] ) ) { bool dir_exists = FSLib::exists( show[TEXT( "PATH" )] );
continue;
}
std::unordered_set< string > episodes; std::unordered_set< string > episodes;
db.exec( TEXT( "SELECT PATH FROM EPISODES WHERE SHOWID == " ) + db.exec( TEXT( "SELECT PATH FROM EPISODES WHERE SHOWID == " ) +
show[TEXT( "ID" )] + TEXT( ";" ), show[TEXT( "ID" )] + TEXT( ";" ),
episodes ); episodes );
for ( const auto &episode : episodes ) { for ( const auto &episode : episodes ) {
if ( !FSLib::exists( episode ) ) { if ( !dir_exists || !FSLib::exists( episode ) ) {
db.exec( TEXT( "DELETE FROM EPISODES WHERE PATH == '" ) + db.exec( TEXT( "DELETE FROM EPISODES WHERE PATH == '" ) +
sanitize( episode ) + TEXT( "';" ) ); sanitize( episode ) + TEXT( "';" ) );
} }
@ -876,7 +782,7 @@ std::vector< std::unordered_map< std::string, std::string > > dbGetShows() {
} }
void changeDB( size_t index, const string &path, const string &language, void changeDB( size_t index, const string &path, const string &language,
const string &id ) { const string &id, bool dvd ) {
SQLite::Database db{}; SQLite::Database db{};
auto absolute = FSLib::canonical( path ); auto absolute = FSLib::canonical( path );
try { try {
@ -890,14 +796,13 @@ void changeDB( size_t index, const string &path, const string &language,
#else #else
string show_id = std::to_wstring( index ); string show_id = std::to_wstring( index );
#endif #endif
#ifdef _WIN32
string source_code = utf8_to_wstring(
c.execute( url );
#else
auto real_show = showNameFromId( id, language ); auto real_show = showNameFromId( id, language );
#endif db.exec( TEXT( "UPDATE SHOWS SET TVID = '" ) + sanitize( id ) +
db.exec( TEXT( "UPDATE SHOWS SET TVID = '" ) + sanitize( id ) + TEXT("', SHOW = '") + sanitize(real_show) + TEXT("', PATH = '") + sanitize(absolute) + TEXT("', LANGUAGE = '") + TEXT( "', SHOW = '" ) + sanitize( real_show ) +
sanitize( language ) + TEXT( "' WHERE ID == " + show_id + ";" ) ); TEXT( "', PATH = '" ) + sanitize( absolute ) +
TEXT( "', LANGUAGE = '" ) + sanitize( language ) +
TEXT( "', DVD = " + ( dvd ? TEXT( "1" ) : TEXT( "0" ) ) +
" WHERE ID == " + show_id + ";" ) );
} }
void refreshSingleDB( const size_t &index, bool linux ) { void refreshSingleDB( const size_t &index, bool linux ) {
@ -911,9 +816,10 @@ void refreshSingleDB( const size_t &index, bool linux ) {
throw e; throw e;
} }
db.exec( "DELETE FROM EPISODES WHERE SHOWID == " + show_id + ";" ); db.exec( "DELETE FROM EPISODES WHERE SHOWID == " + show_id + ";" );
db.exec( TEXT( "SELECT TVID, SHOW, PATH, LANGUAGE FROM SHOWS WHERE ID == " + db.exec(
show_id + ";" ), TEXT( "SELECT TVID, SHOW, PATH, LANGUAGE, DVD FROM SHOWS WHERE ID == " +
shows ); show_id + ";" ),
shows );
std::unordered_map< string, string > &show = shows[0]; std::unordered_map< string, string > &show = shows[0];
@ -934,17 +840,18 @@ void refreshSingleDB( const size_t &index, bool linux ) {
#ifndef GUI #ifndef GUI
ProgressBar::print( 0 ); ProgressBar::print( 0 );
#endif #endif
std::map< int, std::set< string > > seasons; std::map< int, std::map< int, string > > seasons;
// get all season number from this directory and subdirectories // get all season number from this directory and subdirectories
iterateFS( seasons, show[TEXT( "PATH" )] ); iterateFS( seasons, show[TEXT( "PATH" )] );
#ifndef GUI #ifndef GUI
auto size = seasons.size(); auto size = seasons.size();
#endif #endif
size_t i{}; size_t i{};
for ( const auto &x : seasons ) { for ( auto &x : seasons ) {
singleSeason( show[TEXT( "PATH" )], show[TEXT( "SHOW" )], x.first, singleSeason( show[TEXT( "PATH" )], show[TEXT( "SHOW" )], x.first,
show[TEXT( "TVID" )], show[TEXT( "LANGUAGE" )], show[TEXT( "TVID" )], show[TEXT( "LANGUAGE" )],
pattern, linux, true, &x.second, false ); pattern, linux, true, &x.second, false,
show[TEXT( "DVD" )] == TEXT( "1" ) );
i++; i++;
#ifndef GUI #ifndef GUI
ProgressBar::print( ( i * 100 ) / size ); ProgressBar::print( ( i * 100 ) / size );
@ -960,7 +867,7 @@ void refreshSingleDB( const size_t &index, bool linux ) {
for ( auto &episode : season.second ) { for ( auto &episode : season.second ) {
db.exec( TEXT( "INSERT INTO EPISODES ( SHOWID, PATH ) " db.exec( TEXT( "INSERT INTO EPISODES ( SHOWID, PATH ) "
"VALUES ( " ) + "VALUES ( " ) +
show_id + TEXT( ", '" ) + sanitize( episode ) + show_id + TEXT( ", '" ) + sanitize( episode.second ) +
TEXT( "' );" ) ); TEXT( "' );" ) );
} }
} }

View File

@ -1,18 +1,12 @@
#ifndef TV_FUNCTIONS_H #ifndef TV_FUNCTIONS_H
#define TV_FUNCTIONS_H #define TV_FUNCTIONS_H
#include <iostream>
#include <map> #include <map>
#include <set> #include <set>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#ifdef GUI
#include <vector> #include <vector>
#endif
#include "network.hpp" #include "network.hpp"
#ifdef _WIN32 #ifdef _WIN32
@ -38,9 +32,8 @@ void findSeasons( std::map< int, std::set< string > > &seasons,
void printHelp(); void printHelp();
string encodeUrl( const string &url ); string encodeUrl( const string &url );
bool searchSpecificSeason( const char_t *const path, const string &number );
bool searchSeason( const char_t *const path, size_t &season_pos ); bool searchSeason( const char_t *const path, size_t &season_pos );
bool searchSeason( const char_t *const path ); bool searchSeason( const char_t *const path, size_t &season_pos, size_t &ep_pos );
void parseSeasonNumbers( std::set< int > &seasons_num, const char_t *argument ); void parseSeasonNumbers( std::set< int > &seasons_num, const char_t *argument );
@ -71,15 +64,12 @@ void refreshDB( bool linux );
void updateDB( bool linux ); void updateDB( bool linux );
void cleanDB(); void cleanDB();
void changeDB( size_t index, const string &path, const string &language, void changeDB( size_t index, const string &path, const string &language,
const string &url ); const string &id, bool dvd );
void refreshSingleDB( const size_t &index, bool linux ); void refreshSingleDB( const size_t &index, bool linux );
void iterateFS( std::map< int, std::set< string > > &seasons, void iterateFS( std::map< int, std::map< int, string > > &seasons,
const string &path ); const string &path );
bool searchSpecificSeason( const char_t *const path, size_t &ep_pos,
const string &number );
string compilePattern( const string &pattern, int season, int episode, string compilePattern( const string &pattern, int season, int episode,
const string &filename, const string &episodeName, const string &filename, const string &episodeName,
const string &showName ); const string &showName );