The Movies DB - Fanart import script

If you made a script you can offer it to the others here, or ask help to improve it. You can also report here bugs & problems with existing scripts.
Post Reply
boyerf
Posts: 46
Joined: 2009-07-20 18:24:33

The Movies DB - Fanart import script

Post by boyerf »

Hello,

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.
boyerf
Posts: 46
Joined: 2009-07-20 18:24:33

Post by boyerf »

The difficulty is to code/decode the JSON format (http://docs.themoviedb.apiary.io/). I didn't find a way do to it properly with the script language of AMC. If you have any idea... let me know.! ;)
boyerf
Posts: 46
Joined: 2009-07-20 18:24:33

Post by boyerf »

For exemple, how to decode this page properly in a TString List ?
There is Backdrop and Poster. It's important to keep the name of the image, the width and the type (backdrop or poster).

Code: Select all

{"id":23051,"backdrops":[{"file_path":"/mR6WnXFvwStdUCSStSFsDKOUECs.jpg","width":1920,"height":1080,"iso_639_1":null,"aspect_ratio":1.78,"vote_average":5.3125,"vote_count":1},{"file_path":"/guEfavznWgbPADTj1hJaQKynRqV.jpg","width":1280,"height":720,"iso_639_1":null,"aspect_ratio":1.78,"vote_average":0.0,"vote_count":0}],"posters":[{"file_path":"/oFQxR31sRVZ5JtNZopkFb8A9rbX.jpg","width":1100,"height":1665,"iso_639_1":null,"aspect_ratio":0.66,"vote_average":5.28273809523809,"vote_count":1},{"file_path":"/jwtiSDSxsS763epDciTs517wELg.jpg","width":1000,"height":1500,"iso_639_1":null,"aspect_ratio":0.67,"vote_average":0.0,"vote_count":0}]}
J
Posts: 224
Joined: 2008-02-17 17:09:26

Post by J »

Hello boyerf,

I´m not sure to fully understand the problem, but to get the backdrops from the moviepage why don´t you just use the given links?

for example:
http://www.themoviedb.org/movie/109439- ... r-part-iii

in the HTML you´ll easily find the part

Code: Select all

<div id="images">
      <img class="lightbox" id="517aed16760ee31cd81c0ad2" src="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w300/xIrtFqXc62VGcWKHOsQfCq4j3x0.jpg" width="300" height="169" />
      <img class="lightbox" id="516d49b419c295628314e7a3" src="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w300/rEXYi7F1Cm4aoYEc9OKrF29gOu4.jpg" width="300" height="169" />
      <img class="lightbox" id="513a4769760ee32030061a82" src="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w300/zMR2TQ6kH0taKaE4hK82OxBROml.jpg" width="300" height="169" />
      <img class="lightbox" id="51a714d7760ee367f91d522a" src="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w300/ddqGKr0uDf5Sz4Y9SGI60sL1Uyg.jpg" width="300" height="169" />
    </div>
so you just have to cut the picture links and change "w300" to "original" like
http://d3gtl9l2a4fn1j.cloudfront.net/t/ ... q4j3x0.jpg

No need to use an API-key or JSON.
boyerf
Posts: 46
Joined: 2009-07-20 18:24:33

Post by boyerf »

Hello J,

Thank for your help ! You're right, maybe it would be more simple to do my script request with html, but I'm the way with API and I found some solutions.

Now, the script is functionnal. He researches a Movie with the Title and the product date. If there is no result, he researches without the date. If there is a lot of response, he opens a PickList with the list of Movies.
The script catch all the Fanart with a resolution >= 1280.

If somebody want to test it, improve it, let's go.

Best regards,
François

The new version :

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.5 (alpha)
Requires=4.2.0
Comments=Script en construction...
License=
GetInfo=1
RequiresMovies=1

[Options]
Debug=1|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, idFilmTrouve: string;
  Nbr_ligne_page : integer;
Const
  BASE_URL = 'http://api.themoviedb.org/3/';
	CRLF = #13#10;                        // carriage return/line feed

(* Enter here the api key !!!! *)
//  Example APIKEY = '?api_key=546545658564645646545465';
  APIKEY = '?api_key=';
(* Enter here the api key !!!! *)

function RecupImage(FilmID: string): Integer;
var
i : integer;
out : Boolean;
WidthFlag : string;
ImageFlag : string;
begin
// Récupération des image - Nom du fichier et résolution
URL := BASE_URL + 'movie/' + FilmID + '/images' + APIKEY;
Page := TStringList.Create;
Page.Text := GetPage(URL);
// Découpage de la page à la hache...
Page.Text := StringReplace(Page.Text, '{', CRLF);
for i:=0 to Page.Count-1 do
begin
  Line := Page.GetString(i);
  if debug = 1 then ShowMessage('Ligne_split-->'+Line+'<--');
  if pos('backdrop', Line) <> 0 then ImageFlag := 'FanArt';
  if pos('"/', Line)  <> 0 then
  begin
    Img_Value := TextBetween(Line, 'file_path":"', '"');
    Img_Width := TextBetween(Line, 'width":', ',');
    WidthFlag := 'w'+Img_Width;
    if debug = 1 then ShowMessage('Width-->'+WidthFlag+'<--');
    if (Img_Width <> '1280') and (strtofloat(Img_Width) > 1280) then WidthFlag := 'original';
    if debug = 1 then ShowMessage('Appel_Image-->'+ConfigPath+WidthFlag+Img_Value+'<--');
    if (strtofloat(Img_Width) >= 1280) then
    begin
      // Creation d'un extrafield et sauvegarde de l'image
      NumExtra := GetExtraCount;
      ReturnExtra := AddExtra;
      SetExtraField(ReturnExtra, extraFieldCategory, ImageFlag);
      GetExtraPicture(ReturnExtra, ConfigPath+WidthFlag+Img_Value);
    end;
  end;
//  if pos('posters', Line)  <> 0 then ImageFlag := 'Poster';
  if pos('posters', Line)  <> 0 then exit;
end;
end;

function RechercheFilm(TitreFilm, DateFilm: string): string;
var
i : integer;
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;

function SelectMultiFilm(TitreFilm, DateFilm: string): string;
var
i : integer;
Rech_Title, Rech_OTitle, Rech_Date, Rech_ID, Pick_Line, Pick_Return : string;
begin
  // Découpage de la page à la hache...
  Page.Text := StringReplace(Page.Text, '{', CRLF);
  PickTreeClear;
  for i:=2 to Page.Count-1 do
  begin
    Line := Page.GetString(i);
    if debug = 1 then ShowMessage('Ligne_split-->'+Line+'<--');
    if pos('title', Line) <> 0 then
    begin
      Rech_Title := TextBetween(Line, 'title":"', '"');
      Rech_OTitle := TextBetween(Line, 'original_title":"', '"');
      Rech_Date := TextBetween(Line, 'release_date":"', '"');
      Rech_ID := TextBetween(Line, 'id":', ',');
      Pick_Line := '"'+Rech_Title+'" --- "'+Rech_OTitle+'" --- '+Rech_Date+' --- '+Rech_ID;
      if debug = 1 then ShowMessage('Pick_Line-->'+Pick_Line+'<--');
      PickTreeAdd(Pick_Line,Rech_ID);
    end;
  end;
  if PickTreeExec(Pick_Return) then RechResult:=Pick_Return;
  if debug = 1 then ShowMessage('Pick_Return-->'+Pick_Return+'<--');
Result := Pick_Return;
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
begin
  xDateFilm := '';
  nbFilmTrouve := RechercheFilm(xTitreFilm, xDateFilm);
  if strtofloat(nbFilmTrouve) = 0 then exit;
end;
if strtofloat(nbFilmTrouve) > 1 then idFilmTrouve := SelectMultiFilm(xTitreFilm, xDateFilm);
if strtofloat(nbFilmTrouve) = 1 then idFilmTrouve := TextBetween(Line, 'id":', ',');
RecupImage(idFilmTrouve);

end.
Teebee
Posts: 111
Joined: 2005-06-04 10:46:56

Post by Teebee »

I get a 'HTTP /1.1 404 Not Found' eery movie i try to check.

Have latest version of antp and script
boyerf
Posts: 46
Joined: 2009-07-20 18:24:33

Post by boyerf »

Teebee wrote:I get a 'HTTP /1.1 404 Not Found' eery movie i try to check.

Have latest version of antp and script
Hello,

Do you have an APIKEY ? (http://www.themoviedb.org/documentation/api).
After requested a KEY on the TMDB webside, you need to put the KEY in the script.

(* Enter here the api key !!!! *)
// Example APIKEY = '?api_key=546545658564645646545465';
APIKEY = '?api_key=';
(* Enter here the api key !!!! *)
Teebee
Posts: 111
Joined: 2005-06-04 10:46:56

Post by Teebee »

Yes, have an APIKEY, and put it at the right spot.

[/img]
boyerf
Posts: 46
Joined: 2009-07-20 18:24:33

Post by boyerf »

It's strange... I test my script with 800 différent movies...

For your movies, do you have something in the "Translated Title" field ? This is the field used by the script. And do you try to activate the debug mode in options ?
Teebee
Posts: 111
Joined: 2005-06-04 10:46:56

Post by Teebee »

oh, that was the issue.

Almost none of my movies have Translated titles :grinking:

When i added one it found the movie immediately!
boyerf
Posts: 46
Joined: 2009-07-20 18:24:33

Post by boyerf »

Hello Teebee !
You can modify the script for your configuration.
just change the line :

xTitreFilm := UrlEncode(getfield(fieldTranslatedTitle));

by

xTitreFilm := UrlEncode(getfield(fieldOriginalTitle));

I must improve the script and add some options and verification....
Tell me if you find it usefull !

Enjoy !
fagnerpc
Posts: 40
Joined: 2015-04-23 05:07:08

Post by fagnerpc »

Thx for the amazing code, boyerf :grinking:
I've made some simple modifications and the code is fully functional with 4.2 and the newest beta release.
Tested on +100 different movies with success (the script is incredibly fast)

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.5 (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, idFilmTrouve: string;
  Nbr_ligne_page : integer;
Const
  BASE_URL = 'http://api.themoviedb.org/3/';
   CRLF = #13#10;                        // carriage return/line feed

(* Enter here the api key !!!! *)
//  Example APIKEY = '?api_key=546545658564645646545465';
  APIKEY = '?api_key='; 
(* Enter here the api key !!!! *)

function RecupImage(FilmID: string): Integer;
var
i : integer;
out : Boolean;
WidthFlag : string;
ImageFlag : string;
begin
// Récupération des image - Nom du fichier et résolution
URL := BASE_URL + 'movie/' + FilmID + '/images' + APIKEY;
Page := TStringList.Create;
Page.Text := GetPage(URL);
// Découpage de la page à la hache...
Page.Text := StringReplace(Page.Text, '{', CRLF);
for i:=0 to Page.Count-1 do
begin
  Line := Page.GetString(i);
  if debug = 1 then ShowMessage('Ligne_split-->'+Line+'<--');
  if pos('backdrop', Line) <> 0 then ImageFlag := 'Backgrounds';
  if pos('"/', Line)  <> 0 then
  begin
    Img_Value := TextBetween(Line, 'file_path":"', '"');
    Img_Width := TextBetween(Line, 'width":', ',');
    WidthFlag := 'w'+Img_Width;
    if debug = 1 then ShowMessage('Width-->'+WidthFlag+'<--');
    if (Img_Width <> '1920') and (strtofloat(Img_Width) > 1920) then WidthFlag := 'original';
    if debug = 0 then
              begin
      // Creation d'un extrafield et sauvegarde de l'image
      NumExtra := GetExtraCount;
      ReturnExtra := AddExtra;
      SetExtraField(ReturnExtra, extraFieldCategory, ImageFlag);
      GetExtraPicture(ReturnExtra, ConfigPath+'original'+Img_Value);
    end;
  end;
//  if pos('posters', Line)  <> 0 then ImageFlag := 'Poster';
  if pos('posters', Line)  <> 0 then exit;
end;
end;

function RechercheFilm(TitreFilm, DateFilm: string): string;
var
i : integer;
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;

function SelectMultiFilm(TitreFilm, DateFilm: string): string;
var
i : integer;
Rech_Title, Rech_OTitle, Rech_Date, Rech_ID, Pick_Line, Pick_Return : string;
begin
  // Découpage de la page à la hache...
  Page.Text := StringReplace(Page.Text, '{', CRLF);
  PickTreeClear;
  for i:=2 to Page.Count-1 do
  begin
    Line := Page.GetString(i);
    if debug = 1 then ShowMessage('Ligne_split-->'+Line+'<--');
    if pos('title', Line) <> 0 then
    begin
      Rech_Title := TextBetween(Line, 'title":"', '"');
      Rech_OTitle := TextBetween(Line, 'original_title":"', '"');
      Rech_Date := TextBetween(Line, 'release_date":"', '"');
      Rech_ID := TextBetween(Line, 'id":', ',');
      Pick_Line := '"'+Rech_Title+'" --- "'+Rech_OTitle+'" --- '+Rech_Date+' --- '+Rech_ID;
      if debug = 1 then ShowMessage('Pick_Line-->'+Pick_Line+'<--');
      PickTreeAdd(Pick_Line,Rech_ID);
    end;
  end;
  if PickTreeExec(Pick_Return) then RechResult:=Pick_Return;
  if debug = 1 then ShowMessage('Pick_Return-->'+Pick_Return+'<--');
Result := Pick_Return;
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(fieldOriginalTitle));
xDateFilm := UrlEncode(getfield(fieldYear));
nbFilmTrouve := RechercheFilm(xTitreFilm, xDateFilm);
if strtofloat(nbFilmTrouve) = 0 then
begin
  xDateFilm := '';
  nbFilmTrouve := RechercheFilm(xTitreFilm, xDateFilm);
  if strtofloat(nbFilmTrouve) = 0 then exit;
end;
if strtofloat(nbFilmTrouve) > 1 then idFilmTrouve := SelectMultiFilm(xTitreFilm, xDateFilm);
if strtofloat(nbFilmTrouve) = 1 then idFilmTrouve := TextBetween(Line, 'id":', ',');
RecupImage(idFilmTrouve);

end.
antp
Site Admin
Posts: 9629
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Post by antp »

Should it replace the current TMDB script? Or (I suppose) it is rather just an additional script, to put as extra beside the one already included?
fagnerpc
Posts: 40
Joined: 2015-04-23 05:07:08

Post by fagnerpc »

antp wrote:Should it replace the current TMDB script? Or (I suppose) it is rather just an additional script, to put as extra beside the one already included?
Yep, kind of a additional script.
This script only imports backdrops (backgrounds), posters and fanarts in general to extra fields. Probably, it's the only (functional) script that import fanart from TMDb right now.

Could import others fields in a further improvement, but I think it's kind of pointless, since the very TMDb exchange information with IMDb.
An combo IMDb + TMDb Fanart is more appropriate...
KRiSZK0
Posts: 19
Joined: 2008-04-05 15:45:32

Post by KRiSZK0 »

Great script, working fast :grinking:
Will you guys add posters to the script? Now it only download backgrouds.
pacomania2005
Posts: 5
Joined: 2016-05-04 21:24:46

Post by pacomania2005 »

Hello boyerf,
I'm using Your script for TMDBFARNAT, when I'm searching the title (in english or italian) I can't download any image.

I tried to modified the script, in original language or translated but I don't have any result.

How can I do?

Thanks
Giulio
boyerf
Posts: 46
Joined: 2009-07-20 18:24:33

Post by boyerf »

pacomania2005 wrote:Hello boyerf,
I'm using Your script for TMDBFARNAT, when I'm searching the title (in english or italian) I can't download any image.

I tried to modified the script, in original language or translated but I don't have any result.

How can I do?

Thanks
Giulio
Hi, new version with some corrects.

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.1
Requires=4.2.0
Comments=Beware, this script use fieldOriginalTitle for research !||And in the script, you need to modify the API Key !||To obtain an APIKey, visit the TMDB site and create an account :|https://www.themoviedb.org/documentation/api
License=
GetInfo=1
RequiresMovies=1

[Options]

[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, idFilmTrouve: string;
  Nbr_ligne_page : integer;
Const
  BASE_URL = 'http://api.themoviedb.org/3/';
	CRLF = #13#10;                        // carriage return/line feed

(* Enter here the api key !!!! *)
//  Example APIKEY = '?api_key=546545658564645646545465';
  APIKEY = '?api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
(* Enter here the api key !!!! *)

function RecupImage(FilmID: string): Integer;
var
i : integer;
out : Boolean;
WidthFlag : string;
ImageFlag : string;
begin
// Récupération des image - Nom du fichier et résolution
URL := BASE_URL + 'movie/' + FilmID + '/images' + APIKEY;
Page := TStringList.Create;
Page.Text := GetPage(URL);
// Découpage de la page à la hache...
Page.Text := StringReplace(Page.Text, '{', CRLF);
for i:=0 to Page.Count-1 do
begin
  Line := Page.GetString(i);
  if debug = 1 then ShowMessage('Ligne_split-->'+Line+'<--');
  if pos('backdrop', Line) <> 0 then ImageFlag := 'FanArt';
  if pos('"/', Line)  <> 0 then
  begin
    Img_Value := TextBetween(Line, 'file_path":"', '"');
    Img_Width := TextBetween(Line, 'width":', ',');
    Img_Width := StringReplace(Img_Width, '}', '');
    WidthFlag := 'w'+Img_Width;
    if debug = 1 then ShowMessage('Width-->'+WidthFlag+'<--');
    if (Img_Width <> '1280') and (strtofloat(Img_Width) > 1280) then WidthFlag := 'original';
    if debug = 1 then ShowMessage('Appel_Image-->'+ConfigPath+WidthFlag+Img_Value+'<--');
    if (strtofloat(Img_Width) >= 1280) then
    begin
      // Creation d'un extrafield et sauvegarde de l'image
      NumExtra := GetExtraCount;
      ReturnExtra := AddExtra;
      SetExtraField(ReturnExtra, extraFieldCategory, ImageFlag);
      GetExtraPicture(ReturnExtra, ConfigPath+WidthFlag+Img_Value);
    end;
  end;
//  if pos('posters', Line)  <> 0 then ImageFlag := 'Poster';
  if pos('posters', Line)  <> 0 then exit;
end;
end;

function RechercheFilm(TitreFilm, DateFilm: string): string;
var
i : integer;
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;
if debug = 1 then ShowMessage('RechercheFilm - URL-->'+URL);
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,'}','');
RechResult := StringReplace(RechResult,',','');
if debug = 1 then ShowMessage('RechercheFilm - NB de Films trouvés-->'+RechResult);
Result := RechResult;
end;

function SelectMultiFilm(TitreFilm, DateFilm: string): string;
var
i : integer;
Rech_Title, Rech_OTitle, Rech_Date, Rech_ID, Pick_Line, Pick_Return : string;
begin
  // Découpage de la page à la hache...
  Page.Text := StringReplace(Page.Text, '{', CRLF);
  PickTreeClear;
  PickTreeAdd('Film --> ' + UrlDecode(TitreFilm) + '[' + DateFilm + ']','');
  for i:=2 to Page.Count-1 do
  begin
    Line := Page.GetString(i);
    if debug = 1 then ShowMessage('Ligne_split-->'+Line+'<--');
    if pos('title', Line) <> 0 then
    begin
      Rech_Title := TextBetween(Line, 'title":"', '"');
      Rech_OTitle := TextBetween(Line, 'original_title":"', '"');
      Rech_Date := TextBetween(Line, 'release_date":"', '"');
      Rech_ID := TextBetween(Line, 'id":', ',');
      Pick_Line := '"'+Rech_Title+'" --- "'+Rech_OTitle+'" --- '+Rech_Date+' --- '+Rech_ID;
      if debug = 1 then ShowMessage('Pick_Line-->'+Pick_Line+'<--');
      PickTreeAdd(Pick_Line,Rech_ID);
    end;
  end;
  if PickTreeExec(Pick_Return) then RechResult:=Pick_Return;
  if debug = 1 then ShowMessage('Pick_Return-->'+Pick_Return+'<--');
Result := Pick_Return;
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
if getfield(fieldOriginalTitle)<>'' then xTitreFilm := UrlEncode(Cp1252ToASCII(getfield(fieldOriginalTitle)))
  else xTitreFilm := UrlEncode(Cp1252ToASCII(getfield(fieldTranslatedTitle)));
xDateFilm := UrlEncode(getfield(fieldYear));
nbFilmTrouve := RechercheFilm(xTitreFilm, xDateFilm);
if strtofloat(nbFilmTrouve) = 0 then
begin
  xDateFilm := '';
  nbFilmTrouve := RechercheFilm(xTitreFilm, xDateFilm);
  if strtofloat(nbFilmTrouve) = 0 then exit;
end;
if strtofloat(nbFilmTrouve) > 1 then idFilmTrouve := SelectMultiFilm(xTitreFilm, xDateFilm);
if strtofloat(nbFilmTrouve) = 1 then idFilmTrouve := TextBetween(Line, 'id":', ',');
RecupImage(idFilmTrouve);

end.
pacomania2005
Posts: 5
Joined: 2016-05-04 21:24:46

Post by pacomania2005 »

Hello boyerf,

thanks so much
Giulio
Post Reply