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