I try to program a script to import fanart from http://www.themoviedb.org/.
Here, you can find an alpha version. To test it you need to register on the TMDB website and request an API key. You need to provide this API Key in the script.
This script needs the last version of Ant Movie Catalog 4.2.0 Beta.
I need your help to improve it, because I'm not a good programmer...
Best regard,
François
Code: Select all
(***************************************************
Ant Movie Catalog importation script
www.antp.be/software/moviecatalog/
[Infos]
Authors=Boyerf
Title=TMDB_FanArt
Description=Importation de FanArt en provenance de TMDB
Site=http://www.themoviedb.org/
Language=FR
Version=1.0.0 (alpha)
Requires=4.2.0
Comments=Script en construction...
License=
GetInfo=1
RequiresMovies=1
[Options]
Debug=0|0|0=Non|1=Mode Debug
[Parameters]
***************************************************)
program XBMCFanart;
uses
StringUtils1;
var
URL, GuestID, Img_Value, Img_Width, NewURL, Line, ConfigPath: string;
Page: TStringList;
Debug, StartPos, BeginPos, EndPos, LineNr: Integer;
ReturnExtra, NumExtra : Integer;
RechResult, xFilmID, xTitreFilm, xDateFilm, nbFilmTrouve: string;
Nbr_ligne_page : integer;
Const
BASE_URL = 'http://api.themoviedb.org/3/';
(* Enter here the api key !!!! *)
// Example APIKEY = '?api_key=546545658564645646545465';
APIKEY = '?api_key=';
(* Enter here the api key !!!! *)
function RecupImage(FilmID: string): Integer;
begin
// Récupération de la 1ere image - Nom du fichier et résolution
URL := BASE_URL + 'movie/' + FilmID + '/images' + APIKEY;
Page := TStringList.Create;
Page.Text := GetPage(URL);
LineNr := FindLine('file_path', Page, 0);
if LineNr > -1 then
begin
Line := Page.GetString(LineNr);
BeginPos := pos('file_path', Line) + 12;
Img_Value := TextBetween(Line, 'file_path":"', '"');
if debug = 1 then ShowMessage('Nom_image-->'+Img_Value+'<--');
BeginPos := pos('width', Line) + 7;
Img_Width := TextBetween(Line, 'width":', ',');
if debug = 1 then ShowMessage('Taille_image-->'+Img_Width+'<--');
if debug = 1 then ShowMessage('Chemin complet : '+ConfigPath+'w'+Img_Width+Img_Value);
// Creation d'un extrafield et sauvegarde de l'image
NumExtra := GetExtraCount;
if debug = 1 then ShowMessage('Nbr Extra-->'+FloattoStr(NumExtra)+'<--');
ReturnExtra := AddExtra;
if debug = 1 then ShowMessage('No Extra-->'+FloattoStr(ReturnExtra)+'<--');
SetExtraField(ReturnExtra, extraFieldCategory, 'FanArt');
GetExtraPicture(ReturnExtra, ConfigPath+'w'+Img_Width+Img_Value);
end;
end;
function RechercheFilm(TitreFilm, DateFilm: string): string;
begin
if DateFilm = '' then URL := BASE_URL + 'search/movie' + APIKEY + '&query=' + TitreFilm
Else URL := BASE_URL + 'search/movie' + APIKEY + '&query=' + TitreFilm +'&primary_release_year=' + DateFilm;
Page := TStringList.Create;
Page.Text := GetPage(URL);
Nbr_ligne_page := Page.Count;
if debug = 1 then ShowMessage('NB de lignes-->'+floattostr(Nbr_ligne_page));
LineNr := FindLine('total_results', Page, 0);
Line := Page.GetString(LineNr);
StartPos := pos('total_results', Line);
RechResult := copy(Line, Startpos+15, 2);
RechResult := StringReplace(RechResult,'}','');
if debug = 1 then ShowMessage('NB de resultats-->'+RechResult);
Result := RechResult;
end;
// ---------------------------------------------------------------------------------------------------------------------
begin
debug :=0;
If (GetOption('debug') = 1) then debug :=1;
// Configuration de la connection à l'API
URL := BASE_URL + 'configuration' + APIKEY;
Page := TStringList.Create;
Page.Text := GetPage(URL);
LineNr := FindLine('base_url', Page, 0);
Line := Page.GetString(LineNr);
ConfigPath := TextBetween(Line, 'base_url":"', '"');
if debug = 1 then showMessage('ConfigPath-->'+ConfigPath);
// Ouverture d'une session Invité
URL := BASE_URL + 'authentication/guest_session/new' + APIKEY;
Page := TStringList.Create;
Page.Text := GetPage(URL);
LineNr := FindLine('guest_session_id', Page, 0);
Line := Page.GetString(LineNr);
GuestID := TextBetween(Line, 'guest_session_id":"', '"');
if debug = 1 then ShowMessage('GuestID-->'+GuestID);
// Recherche de film
xTitreFilm := UrlEncode(getfield(fieldTranslatedTitle));
xDateFilm := UrlEncode(getfield(fieldYear));
nbFilmTrouve := RechercheFilm(xTitreFilm, xDateFilm);
if strtofloat(nbFilmTrouve) = 0 then exit;
if strtofloat(nbFilmTrouve) = 1 then
begin
// recup info film
xFilmID := TextBetween(Line, 'id":', ',');
if debug = 1 then ShowMessage('FilmID-->'+xFilmID);
// recuperation image
RecupImage(xFilmID);
end
else
begin
exit
// recup info chaque film
// affichage liste
// recuperation image
end;
end.