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,8 +56,8 @@ 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() ) {
@ -104,13 +103,15 @@ 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 );
@ -122,8 +123,8 @@ std::vector< string > getEpisodeNames( const string &id, const string &season,
do {
episodes.resize( episodes.size() * 2 );
auto j = json::parse( r.get( TEXT( "/series/" ) + id +
TEXT( "/episodes/query?" ) + season_query + season
+ TEXT( "&page=" ) + page ) );
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() )
@ -135,13 +136,15 @@ std::vector< string > getEpisodeNames( const string &id, const string &season,
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() )
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() ) )
episodes[index].pop_back();
@ -149,7 +152,8 @@ std::vector< string > getEpisodeNames( const string &id, const string &season,
}
}
} 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() )
break;
@ -159,10 +163,12 @@ std::vector< string > getEpisodeNames( const string &id, const string &season,
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
@ -299,8 +307,8 @@ void singleSeason( const string &path, string &show, int season, string id,
linux, *files_ptr, dvd );
if ( print || !trust ) {
for ( auto renamed = renamed_files.begin(); renamed != renamed_files.end();
++renamed ) {
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,
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;
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 );
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 );
singleSeason( path, show, x.first, id, language, pattern,
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 );
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 );
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 );