TheMovieDB: formatting

This commit is contained in:
zvon 2021-07-27 19:40:44 +02:00
parent b88d4c0e65
commit 1565aabf70

View File

@ -1,5 +1,6 @@
// API - 2ebc8e784a4072da457fae5c0d291e48 // API - 2ebc8e784a4072da457fae5c0d291e48
// API READ ONLY - eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIyZWJjOGU3ODRhNDA3MmRhNDU3ZmFlNWMwZDI5MWU0OCIsInN1YiI6IjYwZTJlNGI5MjJlNDgwMDA2MDJmZDMzMyIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.c0y7bTCI5KSsfQRw7igPx1FR40mbMF6hGTJTHn0HXH8 // API READ ONLY -
// eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIyZWJjOGU3ODRhNDA3MmRhNDU3ZmFlNWMwZDI5MWU0OCIsInN1YiI6IjYwZTJlNGI5MjJlNDgwMDA2MDJmZDMzMyIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.c0y7bTCI5KSsfQRw7igPx1FR40mbMF6hGTJTHn0HXH8
#include <algorithm> #include <algorithm>
#include <iostream> #include <iostream>
#include <map> #include <map>
@ -57,19 +58,24 @@ struct MovieInfo {
string id; string id;
}; };
bool init(const string &config_path) { bool init( const string &config_path ) {
Request &request = _moviedb_request; Request &request = _moviedb_request;
#ifdef _WIN32 #ifdef _WIN32
request.setServer( TEXT( "api.themoviedb.org/3" ) ); request.setServer( TEXT( "api.themoviedb.org/3" ) );
#else #else
request.setServer( "https://api.themoviedb.org/3" ); request.setServer( "https://api.themoviedb.org/3" );
#endif #endif
_moviedb_api_token = "eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIyZWJjOGU3ODRhNDA3MmRhNDU3ZmFlNWMwZDI5MWU0OCIsInN1YiI6IjYwZTJlNGI5MjJlNDgwMDA2MDJmZDMzMyIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.c0y7bTCI5KSsfQRw7igPx1FR40mbMF6hGTJTHn0HXH8"; _moviedb_api_token =
"eyJhbGciOiJIUzI1NiJ9."
"eyJhdWQiOiIyZWJjOGU3ODRhNDA3MmRhNDU3ZmFlNWMwZDI5MWU0OCIsInN1YiI6IjYwZT"
"JlNGI5MjJlNDgwMDA2MDJmZDMzMyIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9u"
"IjoxfQ.c0y7bTCI5KSsfQRw7igPx1FR40mbMF6hGTJTHn0HXH8";
return true; return true;
} }
bool hasKey(const rapidjson::GenericValue<rapidjson::UTF8<>> &object, const std::string &key) { bool hasKey( const rapidjson::GenericValue< rapidjson::UTF8<> > &object,
return object.FindMember(key.c_str()) != object.MemberEnd(); const std::string &key ) {
return object.FindMember( key.c_str() ) != object.MemberEnd();
} }
std::vector< MovieInfo > std::vector< MovieInfo >
@ -87,12 +93,13 @@ searchMovie( const string &movie, const string &language, const string &year ) {
do { do {
cur_page++; cur_page++;
rapidjson::Document json; rapidjson::Document json;
auto request_uri = TEXT( "/search/movie?query=" ) + encoded_show + TEXT("&language=" ) + language + TEXT("&page=") + toString(std::to_string(cur_page)); auto request_uri = TEXT( "/search/movie?query=" ) + encoded_show +
if(!year.empty()) { TEXT( "&language=" ) + language + TEXT( "&page=" ) +
request_uri += TEXT("&year=") + year; toString( std::to_string( cur_page ) );
if ( !year.empty() ) {
request_uri += TEXT( "&year=" ) + year;
} }
json.Parse( json.Parse( request.get( request_uri ).c_str() );
request.get( request_uri ).c_str() );
if ( json.HasParseError() ) if ( json.HasParseError() )
return {}; return {};
@ -105,33 +112,36 @@ searchMovie( const string &movie, const string &language, const string &year ) {
// find all possible movies // find all possible movies
for ( size_t i = 0; i < results.Size(); i++ ) { for ( size_t i = 0; i < results.Size(); i++ ) {
if(!hasKey(results[i], "title") || !hasKey(results[i], "id") || if ( !hasKey( results[i], "title" ) ||
!hasKey(results[i], "release_date") || !hasKey(results[i], "original_title")) { !hasKey( results[i], "id" ) ||
!hasKey( results[i], "release_date" ) ||
!hasKey( results[i], "original_title" ) ) {
continue; continue;
} }
auto movie = toString( results[i]["title"].GetString() ); auto movie = toString( results[i]["title"].GetString() );
auto id = toString( std::to_string( results[i]["id"].GetInt() ) ); auto id = toString( std::to_string( results[i]["id"].GetInt() ) );
string year = toString( results[i]["release_date"].GetString() ); string year = toString( results[i]["release_date"].GetString() );
string original = toString( results[i]["original_title"].GetString() ); string original =
if(year.empty() ) { toString( results[i]["original_title"].GetString() );
if ( year.empty() ) {
year = "0000"; year = "0000";
} else { } else {
year = year.substr(0, year.find('-')); year = year.substr( 0, year.find( '-' ) );
} }
MovieInfo tmp; MovieInfo tmp;
tmp.name = std::move(movie); tmp.name = std::move( movie );
tmp.id = std::move(id); tmp.id = std::move( id );
tmp.original_name = std::move(original); tmp.original_name = std::move( original );
tmp.year = std::move(year); tmp.year = std::move( year );
ret.push_back(std::move(tmp)); ret.push_back( std::move( tmp ) );
} }
} while(cur_page < pages && cur_page < 5); } while ( cur_page < pages && cur_page < 5 );
request.clearHeader(); request.clearHeader();
return ret; return ret;
} }
RenameObject movieToRenameObject( const MovieInfo &movie, RenameObject movieToRenameObject( const MovieInfo &movie,
const std::string &language ) { const std::string &language ) {
RenameObject result; RenameObject result;
result.setPresentedName( movie.name ); result.setPresentedName( movie.name );
result.addCustomField( "id", movie.id ); result.addCustomField( "id", movie.id );
@ -150,7 +160,7 @@ std::vector< RenameObject > getOptions( const RenameObject &search ) {
lang = search.getCustomFields().at( "language" ); lang = search.getCustomFields().at( "language" );
} }
if ( search.getCustomFields().find( "year" ) != if ( search.getCustomFields().find( "year" ) !=
search.getCustomFields().end() ) { search.getCustomFields().end() ) {
year = search.getCustomFields().at( "year" ); year = search.getCustomFields().at( "year" );
} }
string name = search.getPresentedName(); string name = search.getPresentedName();
@ -162,7 +172,7 @@ std::vector< RenameObject > getOptions( const RenameObject &search ) {
return result; return result;
} }
std::string removeIllegalCharacters(const std::string &input) { std::string removeIllegalCharacters( const std::string &input ) {
// replace '/' with '|' // replace '/' with '|'
std::string ret = input; std::string ret = input;
for ( size_t i = 0; i < ret.size(); i++ ) { for ( size_t i = 0; i < ret.size(); i++ ) {
@ -172,11 +182,11 @@ std::string removeIllegalCharacters(const std::string &input) {
} }
// replace characters illegal in windows // replace characters illegal in windows
ret.erase( std::remove_if( ret.begin(), ret.end(), ret.erase( std::remove_if( ret.begin(), ret.end(),
[]( char_t x ) { []( char_t x ) {
return x == '?' || x == '"' || return x == '?' || x == '"' || x == '\\' ||
x == '\\' || x == '*'; x == '*';
} ), } ),
ret.end() ); ret.end() );
for ( size_t i = 0; i < ret.size(); i++ ) { for ( size_t i = 0; i < ret.size(); i++ ) {
if ( ret[i] == '|' ) { if ( ret[i] == '|' ) {
ret[i] = '-'; ret[i] = '-';
@ -208,20 +218,23 @@ MovieNames movieFromId( const string &id, const string &language ) {
request.addHeader( TEXT( "Authorization: Bearer " ) + _moviedb_api_token ); request.addHeader( TEXT( "Authorization: Bearer " ) + _moviedb_api_token );
rapidjson::Document json; rapidjson::Document json;
json.Parse( request.get( uri ).c_str() ); json.Parse( request.get( uri ).c_str() );
if( json.HasParseError() ) { if ( json.HasParseError() ) {
return {"", ""}; return { "", "" };
} }
return {removeIllegalCharacters( json["title"].GetString() ), removeIllegalCharacters( json["original_title"].GetString() )}; return { removeIllegalCharacters( json["title"].GetString() ),
removeIllegalCharacters( json["original_title"].GetString() ) };
} }
bool renameMovie(const string &path, const string &name, const string &year) { bool renameMovie( const string &path, const string &name, const string &year ) {
return FSLib::rename(path, FSLib::canonical( FSLib::getContainingDirectory(path) ) + "/" + name + " (" + year + ")." + FSLib::getFileExtension(path) ); return FSLib::rename(
path, FSLib::canonical( FSLib::getContainingDirectory( path ) ) + "/" +
name + " (" + year + ")." + FSLib::getFileExtension( path ) );
} }
bool renamePath( const string &path, const RenameObject &renamer ) { bool renamePath( const string &path, const RenameObject &renamer ) {
string id = ""; string id = "";
string lang = "en-US"; string lang = "en-US";
MovieNames movie = {"",""}; MovieNames movie = { "", "" };
string year = ""; string year = "";
bool use_original = false; bool use_original = false;
@ -238,7 +251,7 @@ bool renamePath( const string &path, const RenameObject &renamer ) {
if ( renamer.getCustomFields().find( "use_original" ) != if ( renamer.getCustomFields().find( "use_original" ) !=
renamer.getCustomFields().end() ) { renamer.getCustomFields().end() ) {
auto use = renamer.getCustomFields().at( "use_original" ); auto use = renamer.getCustomFields().at( "use_original" );
use_original = (use == "true" || use == "True" || use == "TRUE"); use_original = ( use == "true" || use == "True" || use == "TRUE" );
} }
if ( renamer.getCustomFields().find( "id" ) == if ( renamer.getCustomFields().find( "id" ) ==
@ -248,17 +261,20 @@ bool renamePath( const string &path, const RenameObject &renamer ) {
if ( results.empty() ) if ( results.empty() )
return false; return false;
id = results[0].id; id = results[0].id;
movie = { removeIllegalCharacters( results[0].name ), removeIllegalCharacters( results[0].original_name ) }; movie = { removeIllegalCharacters( results[0].name ),
removeIllegalCharacters( results[0].original_name ) };
year = results[0].year; year = results[0].year;
} else { } else {
id = renamer.getCustomFields().at( "id" ); id = renamer.getCustomFields().at( "id" );
movie = movieFromId( id, lang ); movie = movieFromId( id, lang );
} }
if((!use_original && movie.name == "") || (use_original && movie.original_name == "")) if ( ( !use_original && movie.name == "" ) ||
( use_original && movie.original_name == "" ) )
return false; return false;
return renameMovie( path, use_original ? movie.original_name : movie.name, year ); return renameMovie( path, use_original ? movie.original_name : movie.name,
year );
} }
std::vector< string > getCustomKeys() { std::vector< string > getCustomKeys() {