Tv_rename: add validID, formatting

This commit is contained in:
zvon 2020-01-17 14:03:22 +01:00
parent a9802b4d47
commit 875541b67d
2 changed files with 78 additions and 52 deletions

View File

@ -45,7 +45,6 @@ constexpr const char_t *dir_divider = "/";
#endif #endif
string api_token; string api_token;
Request r; Request r;
@ -57,11 +56,11 @@ searchShow( const string &show, const string &language ) {
auto encoded_show = encodeUrl( show ); auto encoded_show = encodeUrl( show );
auto j = json::parse( auto j =
r.get( TEXT( "/search/series?name=" ) + encoded_show ) ); json::parse( r.get( TEXT( "/search/series?name=" ) + encoded_show ) );
std::vector< json > results; std::vector< json > results;
if( j["data"].is_array() ) { if ( j["data"].is_array() ) {
results = j["data"].get< std::vector< json > >(); results = j["data"].get< std::vector< json > >();
} else { } else {
cout << toString( j ) << std::endl; cout << toString( j ) << std::endl;
@ -85,14 +84,14 @@ searchShow( const string &show, const string &language ) {
string getShowId( string &show, const string &language ) { string getShowId( string &show, const string &language ) {
size_t order{}, pos{}; size_t order{}, pos{};
auto search_results = searchShow( show, language ); auto search_results = searchShow( show, language );
for( const auto &x : search_results ) { for ( const auto &x : search_results ) {
cout << ++order << ". " << x.first << std::endl; cout << ++order << ". " << x.first << std::endl;
} }
cout << "Which TV Show is the right one? " << std::flush; cout << "Which TV Show is the right one? " << std::flush;
cin >> pos; cin >> pos;
cin.clear(); cin.clear();
cin.ignore( 1, '\n' ); cin.ignore( 1, '\n' );
return search_results[pos-1].second; return search_results[pos - 1].second;
} }
#endif #endif
@ -104,65 +103,72 @@ string showNameFromId( const string &id, const string &language ) {
auto j = json::parse( r.get( TEXT( "/series/" ) + id ) ); auto j = json::parse( r.get( TEXT( "/series/" ) + id ) );
// TODO check if got json // TODO check if got json
std::string show = j["data"].get< json >()["seriesName"].get< std::string >(); std::string show =
j["data"].get< json >()["seriesName"].get< std::string >();
return toString( show ); return toString( show );
} }
// get names for all episodes for a given season // get names for all episodes for a given season
std::vector< string > getEpisodeNames( const string &id, const string &season, std::vector< string > getEpisodeNames( const string &id, const string &season,
const string &language, bool dvd = false ) { const string &language,
bool dvd = false ) {
r.addHeader( TEXT( "Accept: application/json" ) ); r.addHeader( TEXT( "Accept: application/json" ) );
r.addHeader( TEXT( "Authorization: Bearer " ) + api_token ); r.addHeader( TEXT( "Authorization: Bearer " ) + api_token );
r.addHeader( TEXT( "Accept-Language: " ) + language ); r.addHeader( TEXT( "Accept-Language: " ) + language );
string page = TEXT( "1" ); string page = TEXT( "1" );
string season_query = TEXT( "airedSeason=" ); string season_query = TEXT( "airedSeason=" );
if( dvd ) if ( dvd )
season_query = TEXT( "dvdSeason=" ); season_query = TEXT( "dvdSeason=" );
std::vector< string > episodes; std::vector< string > episodes;
do { do {
episodes.resize( episodes.size() * 2 ); episodes.resize( episodes.size() * 2 );
auto j = json::parse( r.get( TEXT( "/series/" ) + id + auto j = json::parse( r.get( TEXT( "/series/" ) + id +
TEXT( "/episodes/query?" ) + season_query + season TEXT( "/episodes/query?" ) + season_query +
+ TEXT( "&page=" ) + page ) ); season + TEXT( "&page=" ) + page ) );
if( j["data"].is_array() ) { if ( j["data"].is_array() ) {
auto epdata = j["data"].get< std::vector< json > >(); auto epdata = j["data"].get< std::vector< json > >();
if( episodes.size() < epdata.size() ) if ( episodes.size() < epdata.size() )
episodes.resize( epdata.size() ); episodes.resize( epdata.size() );
for ( auto &x : epdata ) { for ( auto &x : epdata ) {
if( x["episodeName"].is_string() ) { if ( x["episodeName"].is_string() ) {
if( dvd ) { if ( dvd ) {
size_t index = x["dvdEpisodeNumber"].get< size_t >(); size_t index = x["dvdEpisodeNumber"].get< size_t >();
if( index > episodes.size() ) if ( index > episodes.size() )
episodes.resize( index ); episodes.resize( index );
index--; index--;
episodes[index] = toString( x["episodeName"].get< std::string >() ); episodes[index] =
toString( x["episodeName"].get< std::string >() );
} else { } else {
size_t index = x["airedEpisodeNumber"].get< size_t >(); size_t index = x["airedEpisodeNumber"].get< size_t >();
if( index > episodes.size() ) if ( index > episodes.size() )
episodes.resize( index ); episodes.resize( index );
index--; index--;
episodes[index] = toString( x["episodeName"].get< std::string >() ); episodes[index] =
toString( x["episodeName"].get< std::string >() );
// some eps have whitespace at the end // some eps have whitespace at the end
while( isspace( episodes[index].back() ) ) while ( isspace( episodes[index].back() ) )
episodes[index].pop_back(); episodes[index].pop_back();
} }
} }
} }
} else { } else {
cerr << "Couldn't find episode names for season " << season << " of show " << showNameFromId( id, language ) << std::endl; cerr << "Couldn't find episode names for season " << season
<< " of show " << showNameFromId( id, language ) << std::endl;
} }
if( j["links"]["next"].is_null() ) if ( j["links"]["next"].is_null() )
break; break;
page = toString( std::to_string( j["links"]["next"].get< size_t >() ) ); page = toString( std::to_string( j["links"]["next"].get< size_t >() ) );
} while( 1 ); } while ( 1 );
r.clearHeader(); r.clearHeader();
return episodes; return episodes;
} }
std::vector< std::pair< std::pair< int, string >, std::pair< string, string > > > std::vector<
std::pair< std::pair< int, string >, std::pair< string, string > > >
getRenamedFiles( const string &show, int season, const string id, getRenamedFiles( const string &show, int season, const string id,
const string &language, const string &pattern, const string &language, const string &pattern,
const bool &linux, const std::map< int, string > &files, bool dvd ) { const bool &linux, const std::map< int, string > &files,
bool dvd ) {
auto season_num = toString( std::to_string( season ) ); auto season_num = toString( std::to_string( season ) );
auto episodes = getEpisodeNames( id, season_num, language, dvd ); auto episodes = getEpisodeNames( id, season_num, language, dvd );
@ -172,7 +178,8 @@ getRenamedFiles( const string &show, int season, const string id,
if ( files.empty() ) if ( files.empty() )
return {}; return {};
std::vector< std::pair< std::pair< int, string >, std::pair< string, string > > > std::vector<
std::pair< std::pair< int, string >, std::pair< string, string > > >
renamed_files; renamed_files;
for ( const auto &x : files ) { for ( const auto &x : files ) {
@ -192,8 +199,8 @@ getRenamedFiles( const string &show, int season, const string id,
auto pos = og_name.find_last_of( TEXT( "." ) ); auto pos = og_name.find_last_of( TEXT( "." ) );
// get desired filename // get desired filename
auto name = compilePattern( pattern, season, x.first, auto name = compilePattern( pattern, season, x.first,
og_name.substr( 0, pos ), episodes[ep_num], og_name.substr( 0, pos ),
show ) + episodes[ep_num], show ) +
og_name.substr( pos ); og_name.substr( pos );
// replace '/' with '|' // replace '/' with '|'
for ( size_t i = 0; i < name.size(); i++ ) { for ( size_t i = 0; i < name.size(); i++ ) {
@ -236,7 +243,8 @@ getRenamedFiles( const string &show, int season, const string id,
} }
} }
renamed_files.emplace_back( renamed_files.emplace_back(
std::pair< int, string >( x.first, dir ), std::pair< string, string >( og_name, name ) ); std::pair< int, string >( x.first, dir ),
std::pair< string, string >( og_name, name ) );
} }
} }
return renamed_files; return renamed_files;
@ -264,8 +272,8 @@ bool authenticate( const std::string &api_key ) {
#endif #endif
r.addHeader( TEXT( "Accept: application/json" ) ); r.addHeader( TEXT( "Accept: application/json" ) );
r.addHeader( TEXT( "Content-Type: application/json" ) ); r.addHeader( TEXT( "Content-Type: application/json" ) );
auto j = json::parse( r.post( TEXT( "/login" ), auto j = json::parse(
"{ \"apikey\": \"" + api_key + "\" }" ) ); r.post( TEXT( "/login" ), "{ \"apikey\": \"" + api_key + "\" }" ) );
api_token = toString( j["token"].get< std::string >() ); api_token = toString( j["token"].get< std::string >() );
r.clearHeader(); r.clearHeader();
// TODO check return code // TODO check return code
@ -286,11 +294,11 @@ void singleSeason( const string &path, string &show, int season, string id,
if ( files_ptr == nullptr ) { if ( files_ptr == nullptr ) {
found_files = new std::map< int, std::map< int, string > >; found_files = new std::map< int, std::map< int, string > >;
iterateFS( *found_files, path ); iterateFS( *found_files, path );
if( found_files->find( season ) != found_files->end() ) if ( found_files->find( season ) != found_files->end() )
files_ptr = &(*found_files)[season]; files_ptr = &( *found_files )[season];
} }
if( files_ptr == nullptr ) { if ( files_ptr == nullptr ) {
cerr << "Couldn't find episodes with season " << season << std::endl; cerr << "Couldn't find episodes with season " << season << std::endl;
return; return;
} }
@ -298,9 +306,9 @@ void singleSeason( const string &path, string &show, int season, string id,
auto renamed_files = getRenamedFiles( show, season, id, language, pattern, auto renamed_files = getRenamedFiles( show, season, id, language, pattern,
linux, *files_ptr, dvd ); linux, *files_ptr, dvd );
if( print || !trust ) { if ( print || !trust ) {
for ( auto renamed = renamed_files.begin(); renamed != renamed_files.end(); for ( auto renamed = renamed_files.begin();
++renamed ) { renamed != renamed_files.end(); ++renamed ) {
cout << renamed->second.first << " --> " << renamed->second.second cout << renamed->second.first << " --> " << renamed->second.second
<< std::endl; << std::endl;
} }
@ -318,10 +326,12 @@ void singleSeason( const string &path, string &show, int season, string id,
for ( auto renamed = renamed_files.begin(); renamed != renamed_files.end(); for ( auto renamed = renamed_files.begin(); renamed != renamed_files.end();
++renamed ) { ++renamed ) {
FSLib::rename( renamed->first.second + dir_divider + renamed->second.first, FSLib::rename(
renamed->first.second + dir_divider + renamed->second.second ); renamed->first.second + dir_divider + renamed->second.first,
if( found_files == nullptr ) { renamed->first.second + dir_divider + renamed->second.second );
files_ptr[0][renamed->first.first] = renamed->first.second + dir_divider + renamed->second.second; if ( found_files == nullptr ) {
files_ptr[0][renamed->first.first] =
renamed->first.second + dir_divider + renamed->second.second;
} }
} }
@ -346,10 +356,11 @@ void multipleSeasons( const string &path, string &show,
std::map< int, std::map< int, string > > season_map; std::map< int, std::map< int, string > > season_map;
iterateFS( season_map, path ); iterateFS( season_map, path );
auto id = getShowId( show, language ); auto id = getShowId( show, language );
for( auto &x : season_map ) { for ( auto &x : season_map ) {
if( seasons.find( x.first ) != seasons.end() ) { if ( seasons.find( x.first ) != seasons.end() ) {
singleSeason( path, show, x.first, id, language, pattern, flags & TV_LINUX, flags & TV_TRUST, singleSeason( path, show, x.first, id, language, pattern,
&x.second, flags & TV_DVD ); flags & TV_LINUX, flags & TV_TRUST, &x.second,
flags & TV_DVD );
} }
} }
} }
@ -360,9 +371,10 @@ void allSeasons( const string &path, string &show, const string &language,
// get all season number from this directory and subdirectories // get all season number from this directory and subdirectories
iterateFS( seasons, path ); iterateFS( seasons, path );
auto id = getShowId( show, language ); auto id = getShowId( show, language );
for( auto &x : seasons ) { for ( auto &x : seasons ) {
singleSeason( path, show, x.first, id, language, pattern, flags & TV_LINUX, flags & TV_TRUST, singleSeason( path, show, x.first, id, language, pattern,
&x.second, flags & TV_DVD ); flags & TV_LINUX, flags & TV_TRUST, &x.second,
flags & TV_DVD );
} }
} }
@ -371,6 +383,8 @@ void printLangs() {
cout << x.first << " - " << x.second << std::endl; cout << x.first << " - " << x.second << std::endl;
} }
#endif
bool findLanguage( const char_t *language ) { bool findLanguage( const char_t *language ) {
for ( auto &x : getLangs() ) { for ( auto &x : getLangs() ) {
if ( x.first == language ) if ( x.first == language )
@ -379,4 +393,11 @@ bool findLanguage( const char_t *language ) {
return false; return false;
} }
#endif bool validID( const string &id ) {
r.addHeader( TEXT( "Accept: application/json" ) );
r.addHeader( TEXT( "Authorization: Bearer " ) + api_token );
r.get( TEXT( "/series/" ) + id );
return r.lastResponseCode() == 200;
}

View File

@ -33,15 +33,18 @@ void singleSeason( const string &path, string &show, int season, string id,
void singleSeason( const string &path, string &show, int season, string id, void singleSeason( const string &path, string &show, int season, string id,
const string &language, const string &pattern, const string &language, const string &pattern,
const size_t &flags, std::map< int, string > *files_ptr = nullptr, const size_t &flags,
std::map< int, string > *files_ptr = nullptr,
bool print = true ); bool print = true );
#ifdef GUI #ifdef GUI
std::vector< std::pair< std::pair< int, string >, std::pair< string, string > > > std::vector<
std::pair< std::pair< int, string >, std::pair< string, string > > >
getRenamedFiles( const string &show, int season, const string id, getRenamedFiles( const string &show, int season, const string id,
const string &language, const string &pattern, const string &language, const string &pattern,
const bool &linux, const std::map< int, string > &files, bool dvd = false ); const bool &linux, const std::map< int, string > &files,
bool dvd = false );
std::vector< std::pair< string, string > > getLangs(); std::vector< std::pair< string, string > > getLangs();
@ -62,6 +65,8 @@ void printLangs();
bool findLanguage( const char_t *language ); bool findLanguage( const char_t *language );
bool validID( const string &id );
bool authenticate( const std::string &api_key ); bool authenticate( const std::string &api_key );
string showNameFromId( const string &id, const string &language ); string showNameFromId( const string &id, const string &language );