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,8 +56,8 @@ 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() ) {
@ -104,13 +103,15 @@ 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 );
@ -122,8 +123,8 @@ std::vector< string > getEpisodeNames( const string &id, const string &season,
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() )
@ -135,13 +136,15 @@ std::vector< string > getEpisodeNames( const string &id, const string &season,
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();
@ -149,7 +152,8 @@ std::vector< string > getEpisodeNames( const string &id, const string &season,
} }
} }
} 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;
@ -159,10 +163,12 @@ std::vector< string > getEpisodeNames( const string &id, const string &season,
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
@ -299,8 +307,8 @@ void singleSeason( const string &path, string &show, int season, string id,
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.first,
renamed->first.second + dir_divider + renamed->second.second ); renamed->first.second + dir_divider + renamed->second.second );
if ( found_files == nullptr ) { if ( found_files == nullptr ) {
files_ptr[0][renamed->first.first] = renamed->first.second + dir_divider + renamed->second.second; files_ptr[0][renamed->first.first] =
renamed->first.second + dir_divider + renamed->second.second;
} }
} }
@ -348,8 +358,9 @@ void multipleSeasons( const string &path, string &show,
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 );
} }
} }
} }
@ -361,8 +372,9 @@ void allSeasons( const string &path, string &show, const string &language,
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 );