Page 1 of 1

help with old IMDb-script

Posted: 2004-03-28 19:58:05
by kinetix
a long time ago, on a page not so far away...
there was an IMDb-script which was designed to get the correct poster...

I've tried to edit the code so it would fit the today's version of IMDb, but with minimal succes...
so I'm now asking for help on editing the code so it will work...

beneath is the original code (not the one I've edited)

Code: Select all

// GETINFO SCRIPTING 
// IMDB (US) import with large picture (usually from Amazon.com) 

(*************************************************** 
*  Movie importation script for:                  * 
*      IMDB (US), http://us.imdb.com              * 
*                                                 * 
*  (c) 2002 Antoine Potten    antoine@buypin.com  * 
*  Improvements made by:                          * 
*       Danny Falkov                              * 
*       Kai Blankenhorn                           * 
*  2002-12-18 : IMDB/Amazon large images fix      * 
*               made by lboregard                 * 
*  2003-01-03 : Remove duplicates in PickTree     * 
*               HTMDDecode in GetDescriptions     * 
*               by Ork <ork@everydayangels.net>   * 
*                                                 * 
*  Improvements made by Trekkie Asimov@hotmail.com*                    
*                                                 * 
*  For use with Ant Movie Catalog 3.4.0           * 
*  www.ant.be.tf/moviecatalog ยทยทยท www.buypin.com  * 
*                                                 * 
*  The source code of the script can be used in   * 
*  another program only if full credits to        * 
*  script author and a link to Ant Movie Catalog  * 
*  website are given in the About box or in       * 
*  the documentation of the program               * 
***************************************************) 

    
program IMDb; 

const
  ExternalPictures = False;
    { True: Pictures will be stored as external files in the folder of the
            catalog
      False: Pictures will be stored inside the catalog (only for .amc files) }
  ManualPictureSelect = True;
    { True: If no Title Match found a picture selection window appears
      False: Revert to IMDB picture }
  ImportLanguage = True,
    { True: Import value of Language field }
  DescriptionToImport = 1;
   {
      2 = import longest
      1 = import short (from main page, faster)
      0 = display list to select a description
   }

var
  MovieName: string;
  MovieURL: string;

function FindLine(Pattern: string; List: TStringList; StartAt: Integer): Integer; 
var 
  i: Integer; 
begin 
  result := -1; 
  if StartAt < 0 then 
    StartAt := 0; 
  for i := StartAt to List.Count-1 do 
    if Pos(Pattern, List.GetString(i)) <> 0 then 
    begin 
      result := i; 
      Break; 
    end; 
end; 

procedure AnalyzePage(Address: string);
var
  Page: TStringList;
begin
  Page := TStringList.Create;
  Page.Text := GetPage(Address);
  if pos('<title>IMDb', Page.Text) = 0 then
  begin
    AnalyzeMoviePage(Page)
  end else
  begin
    PickTreeClear;
    AddMoviesTitles(Page, '<b>Exact Matches</b>');
    AddMoviesTitles(Page, '<b>Partial Matches</b>');
    AddMoviesTitles(Page, '<b>Approximate Matches</b>');
    if PickTreeExec(Address) then
      AnalyzePage(Address);
  end;
  Page.Free;
end;

function FindValue(BeginTag, EndTag: string; Page: TStringList; var LineNr: Integer; var Line: string): string;
var
  BeginPos, EndPos: Integer;
  Value: string;
begin
  Result := '';
  Value := '';
  BeginPos := Pos(BeginTag, Line);
  if BeginPos > 0 then
  begin
    BeginPos := BeginPos + Length(BeginTag);
    if BeginTag = EndTag then
    begin
      Delete(Line,1,BeginPos-1);
      BeginPos := 1;
    end;
    EndPos := pos(EndTag, Line);
    while ((EndPos = 0) and (LineNr < Page.Count-1 )) do
    begin
      Value := Value + copy(Line, BeginPos, Length(Line) - BeginPos);
      // Next Line
      BeginPos := 1;
      LineNr := LineNr + 1;
      Line := Page.GetString(LineNr);
      if Value = '' then
        Exit;
      EndPos := Pos(EndTag, Line);
    end;
    Value := Value + copy(Line, BeginPos, EndPos - BeginPos);
   end;
  Result := Value;
end;

procedure AnalyzeMoviePage(Page: TStringList);
var
  Line, Value, Value2, FullValue: string;
  LineNr, BeginPos, EndPos, DescrImport: Integer;
  AllTitles: TStringList;
begin
  DescrImport := DescriptionToImport;
  if (DescrImport <> 1) and (Pos('<a href="plotsummary">', Page.Text) = 0) then
    DescrImport := 1;

  MovieURL := 'http://imdb.com/title/tt' + copy(Page.Text, pos('<a href="/title/tt',Page.Text)+19, 7);

  // URL
  SetField(fieldURL, MovieURL);

  AllTitles := TStringList.Create;

  // Original Title & Year
  LineNr := FindLine('<title>', Page, 0);
  Line := Page.GetString(LineNr);
  if LineNr > -1 then
  begin
    BeginPos := pos('<title>', Line);
    if BeginPos > 0 then
      BeginPos := BeginPos + 7;
    EndPos := pos('(', Line);
    if EndPos = 0 then
      EndPos := Length(Line);
    Value := copy(Line, BeginPos, EndPos - BeginPos - 1);
    HTMLDecode(Value);
    SetField(fieldOriginalTitle, Value);
    BeginPos := pos('(', Line) + 1;
    if BeginPos > 0 then
    begin
      EndPos := Pos('/I', Line);
      if EndPos < BeginPos then
        EndPos := Pos(')', Line);
      Value := copy(Line, BeginPos, EndPos - BeginPos);
      SetField(fieldYear, Value);
    end;
  end;

  // Rating
  LineNr := FindLine('User Rating:', Page, 0);
  if LineNr > -1 then
  begin
    Line := Page.GetString(LineNr + 4);
    if Pos('/10', Line) > 0 then
    begin
      BeginPos := pos('<b>', Line) + 3;
      Value := IntToStr(Round(StrToInt(StrGet(Line, BeginPos), 0) + (StrToInt(StrGet(Line, BeginPos + 2), 0) / 10)));
      SetField(fieldRating, Value);
    end;
  end;

  // Language
  LineNr := FindLine('Language:', Page, 0);
  if LineNr > -1 then
  begin
    Line := Page.GetString(LineNr + 1);
    BeginPos := pos('/">', Line) + 3;
    EndPos := pos('</a>', Line);
    if EndPos = 0 then
      EndPos := Length(Line);
    Value := copy(Line, BeginPos, EndPos - BeginPos);
    if ImportLanguage then
      SetField(fieldLanguages, Value);
  end;

  // Picture 
  FoundOnAmazon := False; 
  LineNr := FindLine('title="DVD available at Amazon.com"', Page, 0); 
  if LineNr > -1 then 
  begin 
    Line := Page.GetString(LineNr); 
    BeginPos := Pos('href="', Line) + 5; 
    Delete(Line, 1, BeginPos); 
    EndPos := Pos('"', Line); 
    Value := Copy(Line, 1, EndPos - 1); 
    AmazonPage := TStringList.Create; 
    AmazonPage.Text := GetPage('http://us.imdb.com' + Value); 
    

    // Original Title 
    Value2:=AllTitles.GetString(Index); 
    Value2:=TransFormIMDBTitle(Value2); 
    
    PickTreeClear;    
    PickTreeCount:=0; 
    PickTreeAdd('Available Titles for matching a picture to: ' + Value2,''); 

    ParagraphIndex:=1; 
    LineNr:=0; 
    Repeat 
       LineNr := FindLine('<b>'+IntToStr(ParagraphIndex)+'.', AmazonPage, LineNr); 
      
       if LineNr > -1 then 
       Begin 
           TitleLine:=LineNr;          
           Value:=''; 
           PictureAvailable:=False; 
           Repeat 
              TitleLine:=TitleLine +1; 
              Line:= AmazonPage.GetString(TitleLine); 
              BeginPos:=0; 
              if Pos('dvd>',Line) > 0 then 
              Begin 
                  if Pos('dvd><img',Line) = 0 then 
                  Begin 
                     For Index:=0 to AllTitles.Count -1 do 
                     Begin 
                       Value2:=AllTitles.GetString(Index); 
                  
                       BeginPos:=Pos(Value2,Line); 
                       if BeginPos > 0 Then 
                            Break; 
                     End; 
                     // Match not found 
                     If BeginPos = 0 Then 
                     Begin 
                       BeginPos:=Pos('dvd>',Line)+4; 
                       EndPos:=Pos('</a>',Line); 
                       Value:=Copy(Line,BeginPos,EndPos-BeginPos); 
                     End; 
                  End 
                  Else 
                  Begin 
                      PictureAvailable:=(Pos('dvd/icons/dvd-no-image.gif',Line) = 0); 
                      PictureAddress:=IntToStr(TitleLine); 
                  End; 
             End; 
             if BeginPos > 0 Then  Break; 
                        
           Until (Pos('</table>',Line ) > 0); 

           // Try to Find a Title Match 
           if Pos(Value2,Line) > 0 Then 
           begin 
               // Compare Current Title to Original 
               BeginPos := Pos('dvd>', Line) + 3; 
               Delete(Line, 1, BeginPos); 
               EndPos:= Pos('(',Line); 
               if EndPos = 0 Then 
                   EndPos := Pos('</a>', Line); 
               Value := Copy(Line, 1, EndPos - 1); 
               Value:= Trim(Value); 
               if Value = Value2 then 
               begin 
                 If PictureAvailable then 
                     Break 
               end; 
        
           End; 
        
           if PictureAvailable Then 
           Begin 
             PickTreeAdd(Value,PictureAddress); 
             PickTreeCount:=PickTreeCount+1; 
           End; 

       End; 
       ParagraphIndex:=ParagraphIndex+1;    
  
    Until (LineNr = -1); 


    if (LineNr = -1) then 
    begin 

      // Handle Amazon Page Redirection(s) 
      LineNr:= FindLine('You clicked on this item',AmazonPage,0); 
      if (LineNr = -1) then 
        LineNr:=FindLine('Customers who bought',AmazonPage,0); 

      // Display the Picture Selection Window    
      if (LineNr = -1) and ManualPictureSelect and (PickTreeCount > 0) Then 
      begin 
        PickTreeSelected:=PickTreeExec(PictureAddress); 
        if PickTreeSelected then 
          LineNr:=StrToInt(PictureAddress,0); 
      End; 

      if (LineNr > -1 ) Then 
      begin 
           LineNr := FindLine('src="http://images.amazon.com/images/P/',AmazonPage, LineNr); 

           if PickTreeSelected and (LineNr > TitleLine) Then LineNr:=-1; 

           if LineNr > -1 then 
           begin 
             Line := AmazonPage.GetString(LineNr); 
             BeginPos := Pos('src="http://images.amazon.com/images/P/', Line) + 4; 
             Delete(Line, 1, BeginPos); 
             EndPos := Pos('"', Line); 
             Value := Copy(Line, 1, EndPos - 1); 
             Value := StringReplace(Value, 'TZZZZZZZ', 'LZZZZZZZ'); 
             Value := StringReplace(Value, 'THUMBZZZ', 'LZZZZZZZ'); 
             GetPicture(Value, True); // False = do not store picture externally ; store it in the catalog file 
             FoundOnAmazon := True; 
           end; 
      end; 
    end else 
    begin 
      LineNr := FindLine('http://images.amazon.com/images/P/', AmazonPage, LineNr); 
      if LineNr < TitleLine Then 
      Begin 
         Line := AmazonPage.GetString(LineNr); 
         BeginPos := Pos('src="', Line) + 4; 
         Delete(Line, 1, BeginPos); 
         EndPos := Pos('"', Line); 
         Value := Copy(Line, 1, EndPos - 1); 
         Value := StringReplace(Value, 'THUMBZZZ', 'LZZZZZZZ'); 
         GetPicture(Value, True); 
         FoundOnAmazon := True; 
      End; 
    end; 
    AmazonPage.Free; 
  end; 
  
  if not FoundOnAmazon then 
  begin 
    { 
       not found on Amazon, so taking what's available directly on IMDB. 
       if we are lucky, a picture from amazon but directly linked in the page 
    } 
    LineNr := FindLine('<img alt="cover" align="left" src="http://ia.imdb.com/media/imdb/', Page, 0); 
    if LineNr < 0 then 
      LineNr := FindLine('<img alt="cover" align="left" src="http://posters.imdb.com/', Page, 0); 
    if LineNr < 0 then 
      LineNr := FindLine('<img alt="cover" align="left" src="http://images.amazon.com/', Page, 0); 
    if LineNr > -1 then 
    begin 
      Line := Page.GetString(LineNr); 
      BeginPos := pos('src="', Line) + 4; 
      Delete(Line, 1, BeginPos); 
      EndPos := pos('"', Line); 
      Value := copy(Line, 1, EndPos - 1); 
      Value := StringReplace(Value, 'MZZZZZZZ', 'LZZZZZZZ'); // change URL to get the Large instead of Small image 
      GetPicture(Value, True); // False = do not store picture externally ; store it in the catalog file 
    end; 
  end; 
  
  GetMoviePicture(Value, Page, AllTitles);
  AllTitles.Free;

  // Director
  LineNr := FindLine('Directed by', Page, 0);
  if LineNr > -1 then
  begin
    FullValue := '';
    Line := Page.GetString(LineNr + 1);
    repeat
      BeginPos := pos('">', Line) + 2;
      EndPos := pos('</a>', Line);
      Value := copy(Line, BeginPos, EndPos - BeginPos);
      if (Value <> '(more)') and (Value <> '') then
      begin
        if FullValue <> '' then
          FullValue := FullValue + ', ';
        FullValue := FullValue + Value;
      end;
      Delete(Line, 1, EndPos);
    until Pos('</a>', Line) = 0;
    HTMLDecode(FullValue);
    SetField(fieldDirector, FullValue);
  end;

  // Actors
  LineNr := FindLine('ast overview', Page, 0);
  if LineNr = -1 then
    LineNr := FindLine('redited cast', Page, 0);
  if LineNr > -1 then
  begin
    FullValue := '';
    Line := Page.GetString(LineNr);
    repeat
      BeginPos := Pos('<td valign="top">', Line);
      if BeginPos > 0 then
      begin
        Delete(Line, 1, BeginPos);
        Line := copy(Line, 25, Length(Line));
        BeginPos := pos('">', Line) + 2;
        EndPos := pos('</a>', Line);
        if EndPos = 0 then
          EndPos := Pos('</td>', Line);
        Value := copy(Line, BeginPos, EndPos - BeginPos);
        if (Value <> '(more)') and (Value <> '') then
        begin
          BeginPos := pos('.... </td><td valign="top">', Line);
          if BeginPos > 0 then
          begin
            EndPos := pos('</td></tr>', Line);
            BeginPos := BeginPos + 27;
            Value2 := copy(Line, BeginPos, EndPos - BeginPos);
            if Value2 <> '' then
            begin
              Value := Value + ' (as ' + Value2 + ')';
            end;
          end;
          if FullValue <> '' then
            FullValue := FullValue + ', ';
          FullValue := FullValue + Value;
        end;
        EndPos := Pos('</td></tr>', Line);
        Delete(Line, 1, EndPos);
      end else
      begin
        Line := '';
      end;
    until Line = '';
    HTMLDecode(FullValue);
    SetField(fieldActors, FullValue);
  end;

  //Country
  LineNr := FindLine('Country:', Page, 0);
  if LineNr > -1 then
  begin
    Line := Page.GetString(LineNr + 1);
    BeginPos := pos('/">', Line) + 3;
    EndPos := pos('</a>', Line);
    Value := copy(Line, BeginPos, EndPos - BeginPos);
    HTMLDecode(Value);
    SetField(fieldCountry, Value);
  end;

  //Category
  LineNr := FindLine('Genre:', Page, 0);
  if LineNr > -1 then
  begin
    Line := Page.GetString(LineNr + 1);
    BeginPos := pos('/">', Line) + 3;
    EndPos := pos('</a>', Line);
    Value := copy(Line, BeginPos, EndPos - BeginPos);
    HTMLDecode(Value);
    SetField(fieldCategory, Value);
  end;

  //Description
  LineNr := FindLine('Plot Summary:', Page, 0);
  if LineNr < 1 then
    LineNr := FindLine('Plot Outline:', Page, 0);
  if LineNr > -1 then
  begin
    Line := Page.GetString(LineNr);
    BeginPos := pos('</b>', Line) + 5;
    EndPos := pos('<a href', Line);
    if EndPos < 1 then
    begin
      Line := Line + Page.GetString(LineNr+1);
      EndPos := pos('<br><br>', Line);
      if EndPos < 1 then
        EndPos := Length(Line);
    end;
    Value := copy(Line, BeginPos, EndPos - BeginPos);
    HTMLDecode(Value);
    case DescrImport of
      0:
        begin
          PickListClear;
          PickListAdd(Value);
          GetDescriptions(GetField(fieldURL) + 'plotsummary');
          if PickListExec('Select a description for "' + MovieName + '"', Value) then
            SetField(fieldDescription, Value);
        end;
      1:
        SetField(fieldDescription, Value);
      2:
        SetField(fieldDescription, GetDescriptions(MovieURL + 'plotsummary'));
    end;
  end;

  // Comments
  LineNr := FindLine('<b>Summary:</b>', Page, 0);
  if LineNr > -1 then
  begin
    Value := '';
    repeat
      LineNr := LineNr + 1;
      Line := Page.GetString(LineNr);
      EndPos := Pos('</blockquote>', Line);
      if EndPos = 0 then
        EndPos := Length(Line)
      else
        EndPos := EndPos - 1;
      Value := Value + Copy(Line, 1, EndPos) + ' ';
    until Pos('</blockquote>', Line) > 0;
    HTMLDecode(Value);
    Value := StringReplace(Value, '<br>', #13#10);
    Value := StringReplace(Value, #13#10+' ', #13#10);
    SetField(fieldComments, Value);
  end;

  // Length
  LineNr := FindLine('Runtime:', Page, 0);
  if LineNr > -1 then
  begin
    Line := Page.GetString(LineNr + 1);
    EndPos := pos(' min', Line);
    if EndPos = 0 then
      EndPos := pos('  /', Line);
    if EndPos = 0 then
      EndPos := Length(Line);
    if Pos(':', Line) < EndPos then
      BeginPos := Pos(':', Line) + 1
    else
      BeginPos := 1;
    Value := copy(Line, BeginPos, EndPos - BeginPos);
    SetField(fieldLength, Value);
  end;

procedure GetMoviePicture(Language: string; Page, AllTitles: TStringList);
var
  Line, Value, Value2, Aka, PictureAddress: string;
  AmazonPage: TStringList;
  FoundOnAmazon, PickTreeSelected, PictureAvailable: Boolean;
  TitleRef, ImgRef, NoImage: string;
  LineNr, BeginPos, EndPos, PickTreeCount, ParagraphIndex, Index, TitleLine, LastMatch: Integer;
begin
  FoundOnAmazon := False;

  AllTitles.Free; 

  DisplayResults; 
end; 

function GetDescriptions(Address: string):String; 
var 
  Line, Value: string; 
  LineNr: Integer; 
  BeginPos, EndPos,Longest: Integer; 
  Page: TStringList; 
begin 
  Result:=''; 
  Longest:=0; 
  Page := TStringList.Create; 
  Page.Text := GetPage('http://us.imdb.com' + Address); 
  LineNr := FindLine('<p class="plotpar">', Page, 0); 
  while LineNr > -1 do 
  begin 
    Value := ''; 
    repeat 
      Line := Page.GetString(LineNr); 
      BeginPos := pos('"plotpar">', Line); 
      if BeginPos > 0 then 
        BeginPos := BeginPos + 10 
      else 
        BeginPos := 1; 
      EndPos := pos('</p>', Line); 
      if EndPos < 1 then 
        EndPos := Length(Line) + 1; 
      if Value <> '' then 
        Value := Value + ' '; 
      Value := Value + copy(Line, BeginPos, EndPos - BeginPos); 
      LineNr := LineNr + 1; 
    until (pos('</p>', Line) > 0) or (LineNr = Page.Count); 
    HTMLDecode(Value); 
    PickListAdd(Value); 

    if Length(Value) > Longest then 
    begin 
      Result:=Value; 
      Longest:= Length(Value); 
    end; 

    LineNr := FindLine('<p class="plotpar">', Page, LineNr); 
  end; 
  Page.Free; 
end; 

procedure AddMoviesTitles(Page: TStringList; Tag: string);
var
  Line: string;
  LineNr: Integer;
  MovieTitle, MovieAddress: string;
  StartPos: Integer;
begin
  LineNr := FindLine(tag, Page, 0);
  if LineNr > -1 then
  begin
    Line := Page.GetString(LineNr);
    HTMLRemoveTags(Line);
    PickTreeAdd(Trim(Line), '');
    LineNr := LineNr + 5;
    Line := Page.GetString(LineNr);
    repeat
      StartPos := pos('href="', Line) + 5;
      Delete(Line, 1, StartPos);
      MovieAddress := Copy(Line, 1, pos('">', Line) - 1);
      StartPos := Pos('">', Line) + 2;
      MovieTitle := Copy(Line, StartPos, Pos('</a>', Line) - StartPos);
      HTMLDecode(Movietitle);
      PickTreeAdd(MovieTitle, 'http://us.imdb.com' + MovieAddress);
      LineNr := LineNr + 2;
      Line := Page.GetString(LineNr);
    until Pos('</table>', Line) > 0;
  end;
end;

begin
  if CheckVersion(3,4,0) then
  begin
    MovieName := GetField(fieldOriginalTitle);
    if MovieName = '' then
      MovieName := GetField(fieldTranslatedTitle);
    if Input('IMDb Import', 'Enter the title of the movie:', MovieName) then
    begin
      AnalyzePage('http://us.imdb.com/Tsearch?title='+UrlEncode(MovieName));
    end;
  end
  else
    ShowMessage('This script requires a newer version of Ant Movie Catalog (at least the version 3.4.0)');
end.

Posted: 2004-03-28 20:51:41
by antp
What do you mean by "correct poster" ?
Long time ago, IMDB had a direct link to Amazon picture.
Now it doesn't. It only has a link to a search on Amazon.

Posted: 2004-03-29 06:50:12
by kinetix
sorry for not being clear enough...
I mean, just like you enter a word in the movie title, you get a window which displays all titles with that word...

this script displays a window, with the movie you selected, for different movie covers (i.e. Single Disc Edition, Special Edition...)

to be extra clear about this, if I search for 'Lord of the Rings Two Towers', this script displays a window where I can select either the 2-disc edition cover or the 4-disc edition cover...

Posted: 2004-03-29 10:17:25
by antp
ho ok, well it would be nice. When I'll have the time for that, I will probably do that in the current IMDB script, since letting the user chose the right movie is probably the best way to get the right movie picture.

Posted: 2004-03-29 18:31:50
by kinetix
terrific!!
I'm looking forward to the finished result!

IMDB script with picture selection Window

Posted: 2004-04-08 09:27:12
by trekkie
Hi

Made some changes to the GetMoviePicture function as
follows:

Code: Select all


procedure GetMoviePicture(Language: string; Page, AllTitles: TStringList);
var
  Line, Value, Value2, Aka, PictureAddress: string;
  AmazonPage: TStringList;
  FoundOnAmazon, PickTreeSelected, PictureAvailable,AlternateRef: Boolean;
  TitleRef, ImgRef, NoImage,AltTitleRef: string;
  LineNr, BeginPos, EndPos, PickTreeCount, ParagraphIndex, Index, TitleLine, LastMatch: Integer;
begin
  FoundOnAmazon := False;
  AlternateRef:=False;
  

  // Find Alternate Titles for Movies which are not in English
  Aka := '';
  if Language <> 'English' Then
  begin
    LineNr:= FindLine('Also Known As',Page,0);
    EndPos:=0;
    if LineNr > -1 then
    begin
      Line := Page.GetString(LineNr);
      repeat
        Aka:=FindValue('<br>','<br>',Page,LineNr,Line);
        if Aka <> '' then
        begin
          BeginPos:=1;
          EndPos:=Pos('(',Line);
          if EndPos = 0 then
            EndPos := Length(Aka);
          Value := copy(Aka, BeginPos, EndPos - BeginPos - 1);
          Value:=TransFormIMDBTitle(Value);
          AllTitles.Add(Value);
        end;
      until (Pos('Runtime',Line) > 0) or (Pos('MPAA',Line) > 0 ) or (Pos('Country', Line) > 0);
    end;
  end;

  TitleRef:='dvd>';
  AltTitleRef:='dvd">';
  ImgRef:='dvd><img';
  NoImage:='/icons/dvd-no-image.gif';
  LineNr := FindLine('title="DVD available at Amazon.com"', Page, 0);
  if LineNr = -1 then
  begin
    LineNr := FindLine('title="VHS available at Amazon.com"', Page, 0);
    if LineNr > -1 then
    begin
      TitleRef:='video>';
      AltTitleRef:='video">';
      ImgRef:='video><img';
      NoImage:='/icons/video-no-image.gif';
    end;
  end;

  if LineNr > -1 then
  begin
    Line := Page.GetString(LineNr);
    if(TitleRef='dvd>') then
    begin
      EndPos := pos('title="DVD', Line);
      BeginPos := pos('title="VHS', Line);
      while (BeginPos > 0) and (BeginPos<EndPos) do
      begin
        Delete(Line, 1, BeginPos+1);
        BeginPos := pos('title="VHS', Line);
      end;
    end;
    BeginPos := Pos('href="', Line) + 5;
    Delete(Line, 1, BeginPos);
    EndPos := Pos('"', Line);
    Value := Copy(Line, 1, EndPos - 1);
    AmazonPage := TStringList.Create;
    AmazonPage.Text := GetPage('http://us.imdb.com' + Value);
      
    // Original Title
    Value2 := AllTitles.GetString(0);
    Value2 := TransFormIMDBTitle(Value2);
  
    PickTreeClear;
    PickTreeCount := 0;
    PickTreeAdd('Available Titles for matching a picture to: ' + Value2, '');

    ParagraphIndex := 1;
    LineNr := 0;
    LastMatch := -1;
    TitleLine := -1;
    repeat
      LineNr := FindLine('<b>'+IntToStr(ParagraphIndex)+'.', AmazonPage, LineNr);

      if LineNr > -1 then
      begin
        TitleLine:=LineNr;
        Value:='';
        PictureAvailable:=False;
        repeat
          TitleLine:=TitleLine +1;
          Line:= AmazonPage.GetString(TitleLine);
          BeginPos:=0;
          if (Pos(TitleRef,Line) > 0) or (Pos(AltTitleRef,Line) > 0 ) then
          begin
            AlternateRef:=Pos(AltTitleRef,Line) > 0 ;
            if Pos(ImgRef,Line) = 0 then
            begin
              for Index:=0 to AllTitles.Count -1 do
              begin
                Value2:=AllTitles.GetString(Index);
                BeginPos:=Pos(Value2,Line);
                if BeginPos > 0 then
                  Break;
              end;
              // Match not found
              if BeginPos = 0 then
              begin
                BeginPos:=Pos(TitleRef,Line)+Length(TitleRef);
                EndPos:=Pos('</a>',Line);
                Value:=Copy(Line,BeginPos,EndPos-BeginPos);
              end;
            end
            else
            begin
              PictureAvailable:=(Pos(NoImage,Line) = 0);
              PictureAddress:=IntToStr(TitleLine);
            end;
          end;
          if BeginPos > 0 then
            Break;
       
        until (Pos('</table>',Line ) > 0);

        // Try to Find a Title Match
        if Pos(Value2,Line) > 0 then
        begin
          // Compare Current Title to Original
          if not AlternateRef then
              BeginPos := Pos(TitleRef, Line) + Length(TitleRef) -1
          else
              BeginPos := Pos(AltTitleRef, Line) + Length(AltTitleRef) -1;
          Delete(Line, 1, BeginPos);
          EndPos := Pos('</a>', Line);
          Value := Copy(Line, 1, EndPos - 1);
          Value:= Trim(Value);
          HTMLDecode(Value);
          if Value = Value2 then
          begin
            if PictureAvailable then
              LastMatch:=LineNr;
              //Break
          end;
        end;

        if PictureAvailable then
        begin
          PickTreeAdd(Value,PictureAddress);
          PickTreeCount:=PickTreeCount+1;
        end;
      end;
      ParagraphIndex:=ParagraphIndex+1;
    until (LineNr = -1);

    LineNr:=LastMatch;
  
    if (LineNr = -1) then
    begin
      // Handle Amazon Page Redirection(s)
      LineNr:= FindLine('You clicked on this item',AmazonPage,0);
      if (LineNr = -1) then
        LineNr:=FindLine('Customers who bought',AmazonPage,0);
      // Display the Picture Selection Window
      if (LineNr = -1) and ManualPictureSelect and (PickTreeCount > 0) then
      begin
        PickTreeSelected:=PickTreeExec(PictureAddress);
        if PickTreeSelected then
          LineNr:=StrToInt(PictureAddress,0);
      end;
      if (LineNr > -1 ) then
      begin
        LineNr := FindLine('src="http://images.amazon.com/images/P/',AmazonPage, LineNr);
        if not PickTreeSelected  Then 
          TitleLine:= FindLine('/exec/obidos/ASIN/',AmazonPage, 0);
        if (LineNr > TitleLine) then
          LineNr:=-1;


        if LineNr > -1 then
        begin
          Line := AmazonPage.GetString(LineNr);
          BeginPos := Pos('src="http://images.amazon.com/images/P/', Line) + 4;
          Delete(Line, 1, BeginPos);
          EndPos := Pos('"', Line);
          Value := Copy(Line, 1, EndPos - 1);
          Value := StringReplace(Value, 'TZZZZZZZ', 'LZZZZZZZ');
          Value := StringReplace(Value, 'THUMBZZZ', 'LZZZZZZZ');
          GetPicture(Value, ExternalPictures);
          FoundOnAmazon := True;
        end;
      end;
    end
    else
    begin
      if ManualPictureSelect and (PickTreeCount > 0) then
      begin
         PickTreeSelected:=PickTreeExec(PictureAddress);
         if PickTreeSelected then
             LineNr:=StrToInt(PictureAddress,0);
         end;
      LineNr := FindLine('http://images.amazon.com/images/P/', AmazonPage, LineNr);
      if LineNr < TitleLine then
      begin
        Line := AmazonPage.GetString(LineNr);
        BeginPos := Pos('src="', Line) + 4;
        Delete(Line, 1, BeginPos);
        EndPos := Pos('"', Line);
        Value := Copy(Line, 1, EndPos - 1);
        Value := StringReplace(Value, 'THUMBZZZ', 'LZZZZZZZ');
        GetPicture(Value, ExternalPictures);
        FoundOnAmazon := True;
      end;
    end;
    AmazonPage.Free;
  end;

  if not FoundOnAmazon then
  begin
      {  not found on Amazon, so taking what's available directly on IMDB.
       if we are lucky, a picture from amazon but directly linked in the page  }
    LineNr := FindLine('<img alt="cover" align="left" src="http://ia.imdb.com/media/imdb/', Page, 0);
    if LineNr < 0 then
      LineNr := FindLine('<img alt="cover" align="left" src="http://posters.imdb.com/', Page, 0);
    if LineNr < 0 then
      LineNr := FindLine('<img alt="cover" align="left" src="http://images.amazon.com/', Page, 0);
    if LineNr > -1 then
    begin
      Line := Page.GetString(LineNr);
      BeginPos := pos('src="', Line) + 4;
      Delete(Line, 1, BeginPos);
      EndPos := pos('"', Line);
      Value := copy(Line, 1, EndPos - 1);
      Value := StringReplace(Value, 'MZZZZZZZ', 'LZZZZZZZ'); // change URL to get the Large instead of Small image
       GetPicture(Value, ExternalPictures);
    end;
  end;
end;

Edit the IMDB script and replace the Old "GetMoviePicture" function with this code

Make Sure you set "ManualPictureSelect = True;" on top of script
otherwise picture selection window will NOT appear

Posted: 2004-04-08 11:29:29
by kinetix
Hmmmm....
I've inserted the code, but as I try to run it, I get an error that says:
---
Script error : MAIN at position 18477 (string error)
---

and the marker is then placed at this position.

Code: Select all

  // Find Alternate Titles for Movies which are not in English
....
....
    begin
      TitleRef:='video>';
      AltTitleRef:{the marker is placed here}='video">";
      ImgRef:='video><img';
      NoImage:='/icons/video-no-image.gif';
    end;
got any ideas why this happens???

Correction

Posted: 2004-04-08 11:46:19
by trekkie
oops my mistakte :/

should be

'video"> '


instead of 'video"> "

Posted: 2004-04-08 13:09:16
by kinetix
LOL!!!

man, you have no idea of how happy I am right now!
terrific work, trekkie! :grinking:

Posted: 2004-04-09 09:52:08
by frog
:wink: still a smaal bug though: try running it with "Cruel intentions"