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