My Script (IMDB + Allmovie). Some changes that may interest

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.
Curna

My Script (IMDB + Allmovie). Some changes that may interest

Post by Curna »

Well, first of all I must say I'm not a programmer but I wanted to have some more information, so I changed a little bit a Script to fit my needs.

I've taken the IMDB(large picture) and added some changes. Afterwards I merged some things from the Allmovie script.

Thanks to the people that made thos two Scripts.

Note: There may be some errors (I hope not too many). Try it and take it at your own risk.


CHANGES IN "MY SCRIPT":

- Support for Translated title (it loads "akas.imdb.com" to find all titles)
(to fit your needs you'll have to change the word 'Spain' that appears in the Script for that one you want:

Code: Select all

if (pos('Spain',Value) <> 0) Then
Change it to

Code: Select all

if (pos('France',Value) <> 0) Then
)

- Support for Writing Credits
(They are placed in the "producer" field. You can edit your language file to display "Writing credits" at the place of "Producer")

- Support for the tagline
(It is added at the end of the description. There is the possibility to download that one of the main page or to choose a new one)

- Adds all countries
(All countries are added, not just the first one)

- Adds all categories
(All categories are added, not just the first one)

- Changed the "actors" display
(each actor+role is displayed in a new one, to make it more readable)

- Added the "allmovieguide"(very good) to the "comments field".

- Added the "allmovieguide" awards.
(It is placed on the Description field, but it is bvery easy to change it to display it in the comments field if you want to.Just do the following change:

Code: Select all

SetField(fieldDescription, GetField(fieldDescription)+#13#10+#13#10+#13#10+'AWARDS:'+#13#10+#13#10+Value);
Change it to

Code: Select all

SetField(fieldComments, GetField(fieldComments)+#13#10+#13#10+#13#10+'AWARDS:'+#13#10+#13#10+Value);
I've also added some variables at the beginning of the script to be able to select which fields do you want (and do not have to download so many pages always)

Well, and now the (long) Script:

Code: Select all

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

(***************************************************
*  Movie importation script for:                  *
*      IMDB (US), http://akas.imdb.com              *
*                                                 *
*  (c) 2002 Antoine Potten    antoine@buypin.com  *
*  Contributors :                                 *
*    Danny Falkov                                 *
*    Kai Blankenhorn                              *
*    lboregard                                    *
*    Ork <ork@everydayangels.net>                 *
*    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 }
  ImportLongDescription = False;
    { True: Automatically import the longest description
      False: Description selection window appears }
  ImportTitle = True;
  ImportYear = True;
  ImportTranslatedTitle = True;
  ImportRating = True;
  ImportLanguage = False;
  ImportDirector = True;
  ImportActors = True;
  ImportCountry = True;
  ImportWritingCredits = True;
  ImportCategory = True;
  ImportComments = True;
  ImportLength = True;

  ImportDescription = True;
  
  ImportPicture = False;

  ImportAllMovie = False;
  ImportAwards = False;
  ImportAMGreview = False;
   {Select the fields you want to download
      True: The field will be filled
      False: The field will be empty

      Note: This section is divided by blocks. Each block requires to download a new page.
       If you want to go faster just put to "false" a complete block you do not want. }

  
  ImportTagline = True;
    { True: You add the tagline
      False: You do not add the tagline}
  ImportPageTag = True;
    { True: Automatically import the tag line from the current page
      False: Description selection window appears for all tag lines.You need to download
             another page (slower) }


var
  MovieName: string;
  TheMovieTitle: string;
  TheMovieAddress: 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;
  LineNr: Integer;
begin
  Page := TStringList.Create;
  Page.Text := GetPage(Address);
 
  if pos('<TITLE>IMDb', Page.Text) = 0 then
  begin
    AnalyzeMoviePage(Page)
  end else
  begin
    PickTreeClear;
    LineNr := 0;
    LineNr := FindLine('<H2><A NAME="top">Most popular searches</A></H2>', Page, LineNr);
    if LineNr > -1 then
    begin
      PickTreeAdd('Most popular searches', '');
      AddMoviesTitles(Page, LineNr);
    end;
    LineNr := FindLine('<H2><A NAME="mov">Movies</A></H2>', Page, LineNr);
    if LineNr > -1 then
    begin
      PickTreeAdd('Movies', '');
      AddMoviesTitles(Page, LineNr);
    end;
    LineNr := FindLine('<H2><A NAME="tvm">TV-Movies</A></H2>', Page, LineNr);
    if LineNr > -1 then
    begin
      PickTreeAdd('TV-Movies', '');
      AddMoviesTitles(Page, LineNr);
    end;
    LineNr := FindLine('<H2><A NAME="tvs">TV series</A></H2>', Page, LineNr);
    if LineNr > -1 then
    begin
      PickTreeAdd('TV Series', '');
      AddMoviesTitles(Page, LineNr);
    end;
    LineNr := FindLine('<H2><A NAME="vid">Made for video</A></H2>', Page, 0);
    if LineNr > -1 then
    begin
      PickTreeAdd('Made for video', '');
      AddMoviesTitles(Page, LineNr);
    end;
 
    //Sometimes, the IMDb sends a title in Most Popular Searches
    // and the same title in Movies.
    //TheMovieAddress and TheMovieTitle are used to choose directly
    // that one movie instead of asking the user.
    if TheMovieAddress = '' then
    begin
      if PickTreeExec(Address) then
        AnalyzePage(Address);
    end
    else
      AnalyzePage(TheMovieAddress);
  end;
  Page.Free;
end;

// Loads and analyses page from internet (list of movies or direct hit)
procedure AnalyzePage2(Address: string);
var
  Page: TStringList;
begin
  Page := TStringList.Create;
//  Input('Debug message', 'Search string: ', Address);
//  Page.LoadFromFile('c:\entrapment.html');
//  ShowMessage('DEBUG: AnalyzePage GetPage('+Address+')');
  Page.Text := GetPage(Address);
  // movie list
  if Pos('movie titles like: ', Page.Text) > 0 then begin
    PickTreeClear;
    PickTreeAdd('Search results', '');
    AddMoviesTitles2(Page);
    if PickTreeExec(Address) then AnalyzePage2(Address);
  // refine search
  end else if Pos('Sorry, there is too many possible matches, please adjust your search.', Page.Text) > 0 then begin
    ShowMessage('Sorry, there is too many possible matches, please adjust your search.');
    if Input('All Movie Import', 'Enter the title of the movie:', MovieName) then
      AnalyzePage2('http://allmovie.com/cg/avg.dll?p=avg&type=2&srch=' + URLEncode(MovieName));
  // direct hit
  end else begin
    AnalyzeMoviePage2(Page);
  end;
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;

// simple string procedures
function StringReplaceAll(S, Old, New: string): string;
begin
  while Pos(Old, S) > 0 do
    S := StringReplace(S, Old, New);
  Result := S;
end;
procedure CutAfter(var Str: string; Pattern: string);
begin
  Str := Copy(str, Pos(Pattern, Str) + Length(Pattern), Length(Str));
end;
procedure CutBefore(var Str: string; Pattern: string);
begin
  Str := Copy(Str, Pos(Pattern, Str), Length(Str));
end;

procedure AnalyzeMoviePage(Page: TStringList);
var
  Line, Value, Value2, FullValue: string;
  LineNr, BeginPos, EndPos: Integer;
  AllTitles: TStringList;
begin
  // URL
  SetField(fieldURL, 'http://imdb.com/Title' + copy(Page.Text, pos('href="/Title?',Page.Text)+12, 8));

  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);
    if ImportTitle then
      SetField(fieldOriginalTitle, Value);
    // IMDB original Title
    AllTitles.Add(Value);
    // IMDB Corrected Title
    Value:=TransformIMDBTitle(Value);
    AllTitles.Add(Value);
    BeginPos := pos('(', Line) + 1;
    if BeginPos > 0 then
    begin
      EndPos := pos(')', Line);
      Value := copy(Line, BeginPos, EndPos - BeginPos);
      if ImportYear then
        SetField(fieldYear, Value);
    end;
  end;
  
  // Translated title
  if ImportTranslatedTitle then
  begin
    LineNr := FindLine('Also Known', Page, 0);
    if LineNr > -1 then
    begin
      PickListClear;
      Line := Page.GetString(LineNr);
      Line:=copy(line, pos('<br>', Line) + 4, length(Line)-1);
      repeat
        EndPos := pos('<br>',Line)-2;
        Value := Copy(Line, 0, EndPos);
        HTMLDecode(Value);
        HTMLRemoveTags(Value);
        // It searches for titles in Spanish. CHANGE TO YOUR LANGUAGE !!!
        if (pos('Spain',Value) <> 0) Then
        begin
          EndPos := pos('(',Value)-1;
          Value := Copy(Value,0,EndPos);
          PickListAdd(Value);
        end;
        EndPos := pos('<br>',Line)+3;
        Delete(Line, 1, EndPos);
      until length(line)=0;
      if Picklistexec('Select the desired translated title for "' + MovieName + '"',value) Then
      SetField(fieldTranslatedTitle, Value);
    end;
  end;

  // Rating
  if ImportRating then
  begin
    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;
  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
  if ImportPicture then
  begin
    GetMoviePicture(Value, Page, AllTitles);
    AllTitles.Free;
  end;

  // Director
  if ImportDirector then
  begin
    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;
  end;

  // Actors
  if ImportActors then
  begin
    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 + ' ... ' + Value2;
              end;
            end;
            if FullValue <> '' then
              FullValue := FullValue + #13#10;
            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;
  end;

  //Country
  if ImportCountry then
  begin
    LineNr := FindLine('Country:', 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(fieldCountry, FullValue);
    end;
  end;
  
  //Writing Credits
  if ImportWritingCredits then
  begin
    LineNr := FindLine('Writing credits', Page, 0);
    if LineNr > -1 then
    begin
      FullValue := '';
      LineNr:=LineNr+1;
      Line := Page.GetString(LineNr);
      Repeat
        LineNr := LineNr + 1;
        Line := Page.GetString(LineNr);
      Until (Pos('Name', Line)<>0);
      repeat
        BeginPos := pos('">', Line) + 2;
        EndPos := pos('<br>', Line);
        Value := copy(Line, BeginPos, EndPos - BeginPos);
        HTMLDecode(Value);
        HTMLRemoveTags(Value);
        if (Value <> '(more)') and (Value <> '') then
        begin
          if FullValue <> '' then
            FullValue := FullValue + ' ';
          FullValue := FullValue + Value;
        end;
        Delete(Line, 1, EndPos);
      until (Pos('">', Line) = 0);
      SetField(fieldProducer, FullValue);
    end;
  end;

  //Category
  if ImportCategory then
  begin
    LineNr := FindLine('Genre:', 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(fieldCategory, FullValue);
    end;
  end;
  
  //Description
  if ImportDescription then
  begin
    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
        Line := Line + Page.GetString(LineNr+1);
      EndPos := pos('<a href="/Plot?', Line);
      if EndPos < 1 then
        EndPos := pos('<br><br>', Line);
      if EndPos < 1 then
        EndPos := Length(Line);
      PickListClear;
      Value := copy(Line, BeginPos, EndPos - BeginPos);
      HTMLDecode(Value);
      PickListAdd(Value);
      BeginPos := pos('/Plot?', Line);
      EndPos := pos('">(more)', Line);
      if (BeginPos <> 0) and (EndPos <> 0) then
      begin
        Value := copy(Line, BeginPos, EndPos - BeginPos);
        Value := GetDescriptions(Value);
      end;
      if not ImportLongDescription then
      begin
        Value := '';
        if PickListExec('Select a description for "' + MovieName + '"', Value) then
          SetField(fieldDescription, 'IMDB Summary: '+#13#10+#13#10+ Value+ #13#10 + #13#10);
      end
      else
        SetField(fieldDescription,'IMDB Summary: '+#13#10+#13#10+ Value+ #13#10 + #13#10);
    end;
  end;
  
  //Tagline
  if ImportTagline then
  begin
    FullValue := GetField(fieldDescription);
    LineNr := FindLine('Tagline:', Page, 0);
    if LineNr > -1 then
    begin
      Line := Page.GetString(LineNr);
      BeginPos := pos('</b>', Line) + 5;
      EndPos := pos('<a href="/Taglines?', Line);
      if EndPos < 1 then
        EndPos := Length(Line)+1;
      Value := copy(Line, BeginPos, EndPos - BeginPos);
      HTMLDecode(Value);
      SetField(fieldDescription, FullValue + Value);
      if (not ImportPageTag) Then
      begin
        BeginPos := pos('/Taglines?', Line);
        EndPos := pos('">(more)', Line);
        if (BeginPos <> 0) and (EndPos <> 0) then
        begin
          Value := copy(Line, BeginPos, EndPos - BeginPos);
          GetTaglines(Value);
          if PickListExec('Select a tagline for "' + MovieName + '"', Value) then
            SetField(fieldDescription, FullValue + Value);
        end;
      end;
    end;
  end;
  
  // Comments
  if ImportComments then
  begin
    LineNr := FindLine('<b>Summary:</b>', Page, 0);
    Line := Page.GetString(LineNr);
    BeginPos := Pos('<b>Summary:</b>',line)+16;
    if LineNr > -1 then
    begin
      Value := '';
      repeat
        EndPos := Pos('</blockquote>', Line);
        if EndPos = 0 then
          EndPos := Length(Line)
        else
          EndPos := EndPos -2;
        if Beginpos = 1 Then
          Value := Value + Copy(Line, BeginPos, EndPos) + ' '
        else
          Value := Value + Copy(Line, BeginPos, EndPos);
        LineNr := LineNr + 1;
        Line := Page.GetString(LineNr);
        BeginPos :=1;
      until Pos('</blockquote>', Line) > 0;
      Value := StringReplace(Value, '<br>', #13#10);
      Value := StringReplace(Value, '</p>', #13#10 #13#10);
      Value := StringReplace(Value, #13#10+' ', #13#10);
      HTMLDecode(Value);
      HTMLRemoveTags(Value);
      SetField(fieldComments, 'IMDB comments: '+#13#10+#13#10+Value);
    end;
  end;

  // Length
  if ImportLength then
  begin
    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;
  end;
end;

// Extracts movie details from page
procedure AnalyzeMoviePage2(MoviePage: TStringList);
var
  Page: string;
  Value: string;
begin
  Page := MoviePage.Text;

  if ImportAMGreview then
  begin
    // Review -> description
    Value := GetStringFromHTML(Page, '<A Name="REVIEW">', '</table>', '</table>');
    if Length(Value) > 0 then
    begin
      SetField(fieldComments, GetField(fieldComments)+#13#10+#13#10++#13#10'AMG REVIEW:'+#13#10+#13#10+Value);
    end;
  end;
  
  if ImportAwards then
  begin
    // Awards -> description
    // adjust spaces and line feeds
    Value := StringReplaceAll(Page, '> <FONT', ''); // space before title
    Value := StringReplaceAll(Value, '</FONT> </td><td WIDTH=209>', ' - '); // minus before name
    Value := StringReplaceAll(Value, ' </A></FONT></td>', ' - '); // minus after name (1)
    Value := StringReplaceAll(Value, ' </FONT></td>', ' - '); // minus after name (2)
    Value := StringReplaceAll(Value, '</FONT> </td></tr>', + #13#10); // newline after academy name
    Value := GetStringFromHTML(Value, '<A Name="AWRD">', '</td></tr>', '</TABLE>');
    Value := StringReplaceAll(Value, '  ', ' ');
    Value := StringReplaceAll(Value, ' - - ', ' - ');
    if Length(Value) > 0 then
    begin
      SetField(fieldDescription, GetField(fieldDescription)+#13#10+#13#10+#13#10+'AWARDS:'+#13#10+#13#10+Value);
    end;
  end;
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;

  // 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 );
    end;
  end;

  TitleRef:='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>';
      ImgRef:='video><img';
      NoImage:='/icons/video-no-image.gif';
    end;
  end;

  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://akas.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 then
          begin
            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
          BeginPos := Pos(TitleRef, Line) + Length(TitleRef) -1;
          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
              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
      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;

function TransformIMDBTitle(Title: string): string;
var
  BeginPos, EndPos: Integer;
  Value: string;
  Words: array of string;
  Articles: array of string;
  Replace,Original: string;
  Index, CommaCount: Integer;
Begin
  // Original Title
  Result:=Title;

  Setarraylength(Words,11);
  Words[0]:=' In ';
  Words[1]:=' On ';
  Words[2]:=' Of ';
  Words[3]:=' As ';
  Words[4]:=' The ';
  Words[5]:=' At ';
  Words[6]:=' And A ';
  Words[7]:=' And ';
  Words[8]:=' An ';
  Words[9]:=' To ';
  Words[10]:=' For ';

  SetArrayLength(Articles,35);
  Articles[0]:=' The';
  Articles[1]:=' a';
  Articles[2]:=' An';
  Articles[3]:=' Le';
  Articles[4]:=' L''';
  Articles[5]:=' Les';
  Articles[6]:=' Der';
  Articles[7]:=' Das';
  Articles[8]:=' Die';
  Articles[9]:=' Des';
  Articles[10]:=' Dem';
  Articles[11]:=' Den';
  Articles[12]:=' Ein';
  Articles[13]:=' Eine';
  Articles[14]:=' Einen';
  Articles[15]:=' Einer';
  Articles[16]:=' Eines';
  Articles[17]:=' Einem';
  Articles[18]:=' Il';
  Articles[19]:=' Lo';
  Articles[20]:=' La';
  Articles[21]:=' I';
  Articles[22]:=' Gli';
  Articles[23]:=' Le';
  Articles[24]:=' Uno';
  Articles[25]:=' Una';
  Articles[26]:=' Un''';
  Articles[27]:=' O';
  Articles[28]:=' Os';
  Articles[29]:=' As';
  Articles[30]:=' El';
  Articles[31]:=' Los';
  Articles[32]:=' Las';
  Articles[33]:=' Unos';
  Articles[34]:=' Unas';

  // Count the Comma in The Title
  CommaCount := 0;
  EndPos := 0;
  Value := Title;
  repeat
     BeginPos := Pos(',', Value);
     if BeginPos > 0 then
     begin
       Delete(Value, 1, BeginPos);
       CommaCount := CommaCount + 1;
       EndPos := EndPos + BeginPos;
     end;
  until( Pos(',',Value) = 0);

  // Compare the Article to a list of known ones
  for Index := 0 to 34 do
  begin
    if Pos(Articles[Index], Value) <> 0 then
    begin
       CommaCount := 1;
       BeginPos := EndPos;
       Break;
    end;
  end;

  if (BeginPos > 0) and (CommaCount = 1) then
  begin
    Value := Copy(Title, BeginPos + 1, Length(Title));
    Value := Trim(Value);
    Result := Value + ' ' + Copy(Title, 1, BeginPos - 1);
  end;

  BeginPos := Pos(': ', Result);
  if BeginPos > 0 then
    Result := StringReplace(Result, ': ', ' - ');

  Result := AnsiMixedCase(Result, ' ');

  for Index := 0 to 10 do
  begin
    if Pos(Words[Index],Result) <> 0 then
    begin
      Original := Words[Index];
      Replace := AnsiLowerCase(Original);
      Result := StringReplace(Result, Original, Replace);
    end;
  end;

  Result := StringReplace(Result, ' - the ', ' - The ');
  Result := Trim(Result);
end;

procedure GetTaglines(Address: string);
var
  Line, Value: string;
  LineNr: Integer;
  BeginPos, EndPos,Longest: Integer;
  Page: TStringList;
begin
  Page := TStringList.Create;
  Page.Text := GetPage('http://akas.imdb.com' + Address);
  LineNr := FindLine('<p>', Page, 0);
  Line := Page.GetString(LineNr);
  PickListClear;
  while Length(line) > 5 do
  begin
    Value := '';
    BeginPos := pos('<p>', Line)+3;
    EndPos := pos('</p>', Line);
    Value := copy(Line, BeginPos, EndPos - BeginPos);
    HTMLDecode(Value);
    PickListAdd(Value);
    Delete(Line, 1, EndPos+4);
//    Line:=copy(Line, EndPos+4, length(Line)-1);
  end;
  Page.Free;
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://akas.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; var LineNr: Integer);
var
  Line: string;
  MovieTitle, MovieAddress: string;
  StartPos: Integer;
begin
  repeat
    LineNr := LineNr + 1;
    Line := Page.GetString(LineNr);
    StartPos := pos('HREF="', Line);
    if StartPos > 0 then
    begin
      Startpos := Startpos + 6;
      MovieAddress := copy(Line, StartPos, pos('">', Line) - StartPos);
      StartPos := pos('">', Line) + 2;
      MovieTitle := copy(Line, StartPos, pos('</A>', Line) - StartPos);
      HTMLDecode(Movietitle);
     
      //Remove duplicates
      if TheMovieTitle='' then
        begin
          TheMovieTitle:=MovieTitle;
          TheMovieAddress:='http://akas.imdb.com' + MovieAddress;
        end
      else
        begin
          if TheMovieTitle<>'*' then
            if TheMovieTitle<>MovieTitle then
              begin
                TheMovieTitle:='*';
                TheMovieAddress:='';
              end;
        end;
      PickTreeAdd(MovieTitle, 'http://akas.imdb.com' + MovieAddress);
    end;
  until pos('</OL>', Line) > 0;
end;

// Adds movie titles from search results to tree
procedure AddMoviesTitles2(ResultsPage: TStringList);
var
  Page: string;
  MovieTitle, MovieAddress: string;
begin
  Page := ResultsPage.Text;
  // Every movie entry begins with string "<A HREF='/cg/avg.dll?"
  while Pos('<A HREF=''/cg/avg.dll?', Page) > 0 do begin
    CutBefore(Page, '<A HREF=''/cg/avg.dll?');
    MovieAddress := 'http://allmovie.com' + GetStringFromHTML(Page, '<A', '''', '''>');
    MovieTitle := GetStringFromHTML(Page, '<A', '', '</tr>');
    MovieTitle := StringReplace(MovieTitle, ')', '),  ');
    CutAfter(Page, '</tr>');
    // add movie to list
    PickTreeAdd(MovieTitle, MovieAddress);
  end;
end;

// Extracts single movie detail (like director, genre) from page
function GetStringFromHTML(Page, StartTag, CutTag, EndTag: string): string;
begin
  Result := '';
  // recognition tag - if present, extract detail from page, otherwise assume detail is not present
  if Pos(StartTag, Page) > 0 then begin
    CutBefore(Page, StartTag);
    // optional cut tag helps finding right string in html page
    if Length(CutTag) > 0 then
      CutAfter(Page, CutTag);
    // movie detail copied with html tags up to end string
    Result := Copy(Page, 0, Pos(EndTag, Page) - 1);
    // remove html tags and decode html string
    HTMLRemoveTags(Result);
    HTMLDecode(Result);
//  ShowMessage('DEBUG: GetStringFromHTML - StartTag "'+StartTag+'", CutTag "'+CutTag+'", EndTag "'+EndTag+'", Result "'+Result+'" ___ '+Page);
  end;
end;

procedure RemovePronoun(var Str: string);
var
  i: Integer;
  s: string;
  c: char;
begin
  // remove pronouns
  if (Copy(Str, 0, 2) = 'L ') or (Copy(Str, 0, 2) = 'A ') then
    Str := Copy(Str, 3, Length(Str) - 2)
  else if (Copy(Str, 0, 3) = 'Le ') or (Copy(Str, 0, 3) = 'La ') or (Copy(Str, 0, 3) = 'Un ') then
    Str := Copy(Str, 4, Length(Str) - 3)
  else if (Copy(Str, 0, 4) = 'Les ') or (Copy(Str, 0, 4) = 'Une ') or (Copy(Str, 0, 4) = 'The ') then
    Str := Copy(Str, 5, Length(Str) - 4);

  // remove non-letters, non-digits and non-spaces
  s := '';
  for i := 1 to Length(Str) do begin
  c := StrGet(Str, i);
    if ((c<'a') or (c>'z')) and
       ((c<'A') or (c>'Z')) and
       ((c<'0') or (c>'9')) and
       (c<>' ') then
    else
      s := s + Copy(Str, i, 1);
  end;
  Str := s;
end;

begin
  if CheckVersion(3,4,0) then
  begin
    TheMovieTitle:='';
    TheMovieAddress:='';
    MovieName := GetField(fieldOriginalTitle);
    if MovieName = '' then
      MovieName := GetField(fieldTranslatedTitle);

    if Input('IMDb Import', 'Enter the title of the movie:', MovieName) then
    begin
//      AnalyzePage('http://akas.imdb.com/Tsearch?title='+UrlEncode(MovieName)+'&restrict=Movies+only');
      AnalyzePage('http://akas.imdb.com/Tsearch?title='+UrlEncode(MovieName));
      if ImportAllmovie Then
      begin
        TheMovieTitle := GetField(fieldOriginalTitle);
        RemovePronoun(TheMovieTitle);
        AnalyzePage2('http://allmovie.com/cg/avg.dll?p=avg&type=2&srch=' + URLEncode(TheMovieTitle));
      end;
      DisplayResults;
    end;
  end
  else
    ShowMessage('This script requires a newer version of Ant Movie Catalog (at least the version 3.4.0)');
end.
antp
Site Admin
Posts: 9630
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Post by antp »

Thanks ;)

You could modify the comments at the top of the sript, and also the description of the script ;)
curna

Post by curna »

antp wrote:Thanks ;)

You could modify the comments at the top of the sript, and also the description of the script ;)
Well, I would like to do it. However, as I am not registered, I cannot change it.

P.S: I think that some things could be merged to the official "IMDB Script", as these 2:

- Adds all countries
(All countries are added, not just the first one)

- Adds all categories
(All categories are added, not just the first one)

And maybe the option to add the tagline.
palantir
Posts: 14
Joined: 2003-04-06 08:10:49
Location: Porto

Post by palantir »

Hi

Congratulations on this script. It helped a lot creating my DB.

I've been using it for some time now, but since yesterday I haven't been able to get information from allmovie.com namely the awards and the AMG Review :(

I don't have experience in programing, but I have looked the script back and forth and tried different version, but nothing seems to work. it doesn't display any error message, but that info simply doesn´t appear.

I have put all the const in the beginning to true, I have checked that allmovie.com site works, but the info is just not sownloaded as it used to be:(

Can you help me with this?

Thanks a lot!!
palantir
Posts: 14
Joined: 2003-04-06 08:10:49
Location: Porto

Post by palantir »

Well, now it works and I haven't changed a comma!

I don't understand... does anyone have any idea why this may happen?
antp
Site Admin
Posts: 9630
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Post by antp »

maybe that allmovie site and/or imdb had a problem yesterday, that can happen :D
palantir
Posts: 14
Joined: 2003-04-06 08:10:49
Location: Porto

Post by palantir »

It may be that. But I tried to connect to allmovie.com and it worked fine, so I thought it could be something else... but maybe not... :/

thanks anyway
Smoked Caramel

Post by Smoked Caramel »

With the recent changes at IMDB, this script hangs. To fix it, in addition to this changes, find this fragment and change 'Name' to 'name' in line 13.

Code: Select all

[1]    //Writing Credits 
[2]    if ImportWritingCredits then 
[3]    begin 
[4]      LineNr := FindLine('Writing credits', Page, 0); 
[5]      if LineNr > -1 then 
[6]      begin 
[7]        FullValue := ''; 
[8]        LineNr:=LineNr+1; 
[9]        Line := Page.GetString(LineNr); 
[10]       Repeat 
[11]         LineNr := LineNr + 1; 
[12]         Line := Page.GetString(LineNr); 
[13]       Until (Pos('Name', Line)<>0);
[14]       repeat 
[15]         BeginPos := pos('">', Line) + 2; 
[16]         EndPos := pos('<br>', Line); 
[17]         Value := copy(Line, BeginPos, EndPos - BeginPos); 
[18]         HTMLDecode(Value); 
[19]         HTMLRemoveTags(Value); 
[20]         if (Value <> '(more)') and (Value <> '') then 
[21]         begin 
[22]           if FullValue <> '' then 
[23]             FullValue := FullValue + ' '; 
[24]           FullValue := FullValue + Value; 
[25]         end; 
[26]         Delete(Line, 1, EndPos); 
[27]       until (Pos('">', Line) = 0); 
[28]       SetField(fieldProducer, FullValue); 
[29]     end; 
[30]   end;
Guest

Script HELP!!!

Post by Guest »

Thank you very much for you script.

For GetPicture, I want to grab the picture from AMG first, if it is not available then continute your procedure GetMoviePicture().

So how do I incorporate the following code into the script?
:??: :??: :??:

Code: Select all

program AllMovie;
var
  MovieName: string;

// simple string procedures
function StringReplaceAll(S, Old, New: string): string;
begin
  while Pos(Old, S) > 0 do
    S := StringReplace(S, Old, New);
  Result := S;
end;
procedure CutAfter(var Str: string; Pattern: string);
begin
  Str := Copy(str, Pos(Pattern, Str) + Length(Pattern), Length(Str));
end;
procedure CutBefore(var Str: string; Pattern: string);
begin
  Str := Copy(Str, Pos(Pattern, Str), Length(Str));
end;

// Loads and analyses page from internet (list of movies or direct hit)
procedure AnalyzePage(Address: string);
var
  Page: TStringList;
begin
  Page := TStringList.Create;
  Page.Text := GetPage(Address);
  // movie list
  if Pos('movie titles like: ', Page.Text) > 0 then
  begin
    PickTreeClear;
    PickTreeAdd('Search results', '');
    AddMoviesTitles(Page);
    if PickTreeExec(Address) then
      AnalyzePage(Address);
  // refine search
  end
  else
  if Pos('Sorry, there is too many possible matches, please adjust your search.', Page.Text) > 0 then
  begin
    ShowMessage('Sorry, there is too many possible matches, please adjust your search.');
    if Input('All Movie Import', 'Enter the title of the movie:', MovieName) then
      AnalyzePage('http://allmovie.com/cg/avg.dll?p=avg&type=2&srch=' + URLEncode(MovieName));
  // direct hit
  end
  else
  begin
    SetField(FieldURL, Address);
    AnalyzeMoviePage(Page)
  end;
end;

// Extracts movie details from page
procedure AnalyzeMoviePage(MoviePage: TStringList);
var
  Page: string;
  Value: string;
begin
  Page := MoviePage.Text;
  // Image
  Value := GetStringFromHTML(Page, 'http://image.allmusic.com', '', '"');
  if Length(Value) > 0 then GetPicture(Value, False);
 DisplayResults;
end;

// Extracts single movie detail (like director, genre) from page
function GetStringFromHTML(Page, StartTag, CutTag, EndTag: string): string;
begin
  Result := '';
  // recognition tag - if present, extract detail from page, otherwise assume detail is not present
  if Pos(StartTag, Page) > 0 then begin
    CutBefore(Page, StartTag);
    // optional cut tag helps finding right string in html page
    if Length(CutTag) > 0 then
      CutAfter(Page, CutTag);
    // movie detail copied with html tags up to end string
    Result := Copy(Page, 0, Pos(EndTag, Page) - 1);
    // remove html tags and decode html string
    HTMLRemoveTags(Result);
    HTMLDecode(Result);
//  ShowMessage('DEBUG: GetStringFromHTML - StartTag "'+StartTag+'", CutTag "'+CutTag+'", EndTag "'+EndTag+'", Result "'+Result+'" ___ '+Page);
  end;
end;

procedure RemovePronoun(var Str: string);
var
  i: Integer;
  s: string;
  c: char;
begin
  // remove pronouns
  if (Copy(Str, 0, 2) = 'L ') or (Copy(Str, 0, 2) = 'A ') then
    Str := Copy(Str, 3, Length(Str) - 2)
  else if (Copy(Str, 0, 3) = 'Le ') or (Copy(Str, 0, 3) = 'La ') or (Copy(Str, 0, 3) = 'Un ') then
    Str := Copy(Str, 4, Length(Str) - 3)
  else if (Copy(Str, 0, 4) = 'Les ') or (Copy(Str, 0, 4) = 'Une ') or (Copy(Str, 0, 4) = 'The ') then
    Str := Copy(Str, 5, Length(Str) - 4);

  // remove non-letters, non-digits and non-spaces
  s := '';
  for i := 1 to Length(Str) do begin
  c := StrGet(Str, i);
    if ((c<'a') or (c>'z')) and
       ((c<'A') or (c>'Z')) and
       ((c<'0') or (c>'9')) and
       (c<>' ') then
    else
      s := s + Copy(Str, i, 1);
  end;
  Str := s;
end;

begin
  if CheckVersion(3,4,1) then begin
    MovieName := GetField(fieldOriginalTitle);
    if MovieName = '' then MovieName := GetField(fieldTranslatedTitle);
    RemovePronoun(MovieName);
    if Input('All Movie Import', 'Enter the title of the movie (only letters, digits and spaces):', MovieName) then
      AnalyzePage('http://allmovie.com/cg/avg.dll?p=avg&type=2&srch=' + URLEncode(MovieName));
  end else ShowMessage('This script requires a newer version of Ant Movie Catalog (at least the version 3.4.1)');
end.
Guest

Post by Guest »

any help above is appreciated.
Guest

Post by Guest »

This is a really good script,
Antp can you please modify this to the recent change of imdb?

Also if you can answer my question back in Sept 2003, I would appreciate it. :)
For GetPicture, I want to grab the picture from AMG first, if it is not available then continute your procedure GetMoviePicture().
antp
Site Admin
Posts: 9630
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Post by antp »

Anonymous wrote: Antp can you please modify this to the recent change of imdb?
For the moment I do not really have time for that. I'll do this later if I do not forget, but it would be better if someone else could do it :D
Most of the changes were in the AnalyzePage procedure if I remember well.
Curna

Post by Curna »

This is the script I'm using right now. However, I haven't tested it too much, so it could have some mistakes.

For me, it works.

Code: Select all

// GETINFO SCRIPTING
// IMDB (Us/akas) import with large picture (usually from Amazon.com)
// All movie guide (US) import of awards and comments

(***************************************************
*  Movie importation script for:                  *
*      IMDB (US), http://akas.imdb.com            *
*      All Movie Guide (US), http://allmovie.com  *
*                                                 *
*  (IMDB)                                         *
*  (c) 2002 Antoine Potten    antoine@buypin.com  *
*  Contributors :                                 *
*    Danny Falkov                                 *
*    Kai Blankenhorn                              *
*    lboregard                                    *
*    Ork <ork@everydayangels.net>                 *
*    Trekkie <Asimov@hotmail.com>                 *   
*                                                 *
*  (All movie guide)	                          *
*  (c) 2003 Hubert Kosior                         *
*       send bugs and reports to: hubert@tm1.net  *
*                                                 *
*  For use with Ant Movie Catalog 3.4.1           *
*  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) }

  ImportPicture = True;
  ManualPictureSelect = True;
    { True: If no Title Match found a picture selection window appears
      False: Revert to IMDB picture }

  ImportDescription = True;
  DescriptionToImport = 0;
   {
      2 = import longest
      1 = import short (from main page, faster)
      0 = display list to select a description
   }
  ImportTitle = True;
  ImportYear = True;
  ImportTranslatedTitle = True;
  ImportRating = True;
  ImportLanguage = False;
  ImportDirector = True;
  ImportActors = True;
  ImportCountry = True;
  ImportWritingCredits = True;
  ImportCategory = True;
  ImportComments = true;
  ImportLength = False;

  ImportAllMovie = true;
  ImportAwards = True;
  ImportAMGreview = True;
   {Select the fields you want to download
      True: The field will be filled
      False: The field will be empty

      Note: This section is divided by blocks. Each block requires to download a new page.
       If you want to go faster just put to "false" a complete block you do not want. }

  
  ImportTagline = True;
    { True: You add the tagline
      False: You do not add the tagline}
  ImportPageTag = true;
    { True: Automatically import the tag line from the current page
      False: Description selection window appears for all tag lines.You need to download
             another page (slower) }


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;

// Loads and analyses page from internet (list of movies or direct hit)
procedure AnalyzePage2(Address: string);
var
  Page: TStringList;
begin
  Page := TStringList.Create;
//  Input('Debug message', 'Search string: ', Address);
//  Page.LoadFromFile('c:\entrapment.html');
//  ShowMessage('DEBUG: AnalyzePage GetPage('+Address+')');
  Page.Text := GetPage(Address);
  // movie list
  if Pos('movie titles like', Page.Text) > 0 then begin
    PickTreeClear;
    PickTreeAdd('Search results', '');
    AddMoviesTitles2(Page);
    if PickTreeExec(Address) then AnalyzePage2(Address);
  // refine search
  end else if Pos('Sorry, there is too many possible matches, please adjust your search.', Page.Text) > 0 then begin
    ShowMessage('Sorry, there is too many possible matches, please adjust your search.');
    if Input('All Movie Import', 'Enter the title of the movie:', MovieName) then
      AnalyzePage2('http://allmovie.com/cg/avg.dll?p=avg&type=2&srch=' + URLEncode(MovieName));
  // direct hit
  end else begin
    AnalyzeMoviePage2(Page);
  end;
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;

// simple string procedures
function StringReplaceAll(S, Old, New: string): string;
begin
  while Pos(Old, S) > 0 do
    S := StringReplace(S, Old, New);
  Result := S;
end;

procedure CutAfter(var Str: string; Pattern: string);
begin
  Str := Copy(str, Pos(Pattern, Str) + Length(Pattern), Length(Str));
end;

procedure CutBefore(var Str: string; Pattern: string);
begin
  Str := Copy(Str, Pos(Pattern, Str), Length(Str));
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://akas.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);
    if ImportTitle then
      SetField(fieldOriginalTitle, Value);
    // IMDB original Title
    AllTitles.Add(Value);
    // IMDB Corrected Title
    Value:=TransformIMDBTitle(Value);
    AllTitles.Add(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);
      if ImportYear then
        SetField(fieldYear, Value);
    end;
  end;

  // Translated title
  if ImportTranslatedTitle then
  begin
    LineNr := FindLine('Also Known', Page, 0);
    if LineNr > -1 then
    begin
      PickListClear;
      Line := Page.GetString(LineNr);
      Line:=copy(line, pos('<br>', Line) + 4, length(Line)-1);
      repeat
        EndPos := pos('<br>',Line)-2;
        Value := Copy(Line, 0, EndPos);
        HTMLDecode(Value);
        HTMLRemoveTags(Value);
        // It searches for titles in Spanish. CHANGE TO YOUR LANGUAGE !!!
        if (pos('Spain',Value) <> 0) Then
        begin
          EndPos := pos('(',Value)-1;
          Value := Copy(Value,0,EndPos);
          PickListAdd(Value);
        end;
        EndPos := pos('<br>',Line)+3;
        Delete(Line, 1, EndPos);
      until length(line)=0;
      if Picklistexec('Select the desired translated title for "' + MovieName + '"',value) Then
      SetField(fieldTranslatedTitle, Value);
    end;
  end;

  // Rating
  if ImportRating then
  begin
    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;
  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
  if ImportPicture then
  begin
    GetMoviePicture(Value, Page, AllTitles);
    AllTitles.Free;
  end;

  // Director
  if ImportDirector then
  begin
    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;
  end;

  // Actors
  if ImportActors then
  begin
    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 + ' ... ' + Value2;
              end;
            end;
            if FullValue <> '' then
              FullValue := FullValue + #13#10;
            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;
  end;

  //Country
  if ImportCountry then
  begin
    LineNr := FindLine('Country:', Page, 0);
    if LineNr > -1 then
    begin
      FullValue := '';
      Line := Page.GetString(LineNr + 1);
      repeat
    BeginPos := pos('/">', Line) + 3;
    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(fieldCountry, FullValue);
    end;
  end;

  //Writing Credits
  if ImportWritingCredits then
  begin
    LineNr := FindLine('Writing credits', Page, 0);
    if LineNr > -1 then
    begin
      FullValue := '';
      Repeat
        LineNr := LineNr + 1;
        Line := Page.GetString(LineNr);
      Until (Pos('name', Line)<>0);
      repeat
        BeginPos := pos('">', Line) + 2;
        EndPos := pos('<br>', Line);
        Value := copy(Line, BeginPos, EndPos - BeginPos);
        HTMLDecode(Value);
        HTMLRemoveTags(Value);
        if (Value <> '(more)') and (Value <> '') then
        begin
          if FullValue <> '' then
            FullValue := FullValue + ' ';
          FullValue := FullValue + Value;
        end;
        Delete(Line, 1, EndPos);
      until (Pos('">', Line) = 0);
      SetField(fieldProducer, FullValue);
    end;
  end;

  //Category
  if ImportCategory then
  begin
    LineNr := FindLine('Genre:', 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);
      HTMLRemoveTags(Value);
      SetField(fieldCategory, FullValue);
    end;
  end;

  //Tagline
  Value:='';
  if ImportTagline then
  begin
    LineNr := FindLine('Tagline:', Page, 0);
    if LineNr > -1 then
    begin
      Line := Page.GetString(LineNr);
      BeginPos := pos('</b>', Line) + 5;
      EndPos := pos('<a href="taglines"', Line);
      if EndPos < 1 then
        EndPos := Length(Line)+1;
      Value := copy(Line, BeginPos, EndPos - BeginPos);
      HTMLDecode(Value);
      SetField(fieldDescription,'IMDB SUMMARY: '+#13#10+#13#10+ Value+ #13#10 + #13#10);
      if (not ImportPageTag) Then
      begin
        BeginPos := pos('taglines"', Line);
        EndPos := pos('">(more)', Line);
        if (BeginPos <> 0) and (EndPos <> 0) then
        begin
          Value := copy(Line, BeginPos, EndPos - BeginPos);
          GetTaglines(Value);
          if PickListExec('Select a tagline for "' + MovieName + '"', Value) then
            SetField(fieldDescription,'IMDB SUMMARY: '+#13#10+#13#10+ Value+ #13#10 + #13#10);
        end;
      end;
    end;
  end;

  //Description
  if ImportDescription then
  begin
    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, GetField(fieldDescription)+ Value);
          end;
        1:
          SetField(fieldDescription, GetField(fieldDescription)+ Value);
        2:
          SetField(fieldDescription, GetField(fieldDescription)+ GetDescriptions(MovieURL + 'plotsummary'));
      end;
    end;
  end;

  // Comments
  if ImportComments then
  begin
    LineNr := FindLine('<b>Summary:</b>', Page, 0);
    if LineNr > -1 then
    begin
      Value := '';
      Line := Page.GetString(LineNr);
      BeginPos := Pos('<b>Summary:</b>',line)+16;
      EndPos := Pos('<blockquote>', Line);
      Value := Value + Copy(Line,BeginPos, EndPos);
      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;
      Value := StringReplace(Value, '<br>', #13#10);
      Value := StringReplace(Value, '</p>', #13#10 #13#10);
      Value := StringReplace(Value, #13#10+' ', #13#10);
      HTMLDecode(Value);
      HTMLRemoveTags(Value);
      SetField(fieldComments, 'IMDB COMMENTS: '+#13#10+#13#10+Value);
    end;
  end;

  // Length
  if ImportLength then
  begin
    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;
  end;
end;
    
// Extracts movie details from page
procedure AnalyzeMoviePage2(MoviePage: TStringList);
var
  Page: string;
  Value: string;
begin
  Page := MoviePage.Text;

  if ImportAMGreview then
  begin
    // Review -> description
    Value := GetStringFromHTML(Page, 'A Name="REVIEW">', '</table>', '</table>');
    if Length(Value) > 0 then
    begin
      SetField(fieldComments, 'AMG REVIEW:'+#13#10+#13#10+Value+#13#10+#13#10++#13#10+GetField(fieldComments));
    end;
  end;

  if ImportAwards then
  begin
    // Awards -> description
    // adjust spaces and line feeds
    Value := StringReplaceAll(Page, '> <FONT', ''); // space before title
    Value := StringReplaceAll(Value, '</FONT> </td><td WIDTH=209>', ' - '); // minus before name
    Value := StringReplaceAll(Value, ' </A></FONT></td>', ' - '); // minus after name (1)
    Value := StringReplaceAll(Value, ' </FONT></td>', ' - '); // minus after name (2)
    Value := StringReplaceAll(Value, '</FONT> </td></tr>', + #13#10); // newline after academy name
    Value := GetStringFromHTML(Value, 'A Name="AWRD">', '</td></tr>', '</TABLE>');
    Value := StringReplaceAll(Value, '  ', ' ');
    Value := StringReplaceAll(Value, ' - - ', ' - ');
    if Length(Value) > 0 then
    begin
      SetField(fieldDescription, GetField(fieldDescription)+#13#10+#13#10+#13#10+'AWARDS:'+#13#10+#13#10+Value);
    end;
  end;
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;

  // 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
        ShowMessage(Line);
        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>';
  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>';
      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://akas.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 then
          begin
            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
          BeginPos := Pos(TitleRef, Line) + Length(TitleRef) -1;
          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
              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
      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;

function TransformIMDBTitle(Title: string): string;
var
  BeginPos, EndPos: Integer;
  Value: string;
  Words: array of string;
  Articles: array of string;
  Replace,Original: string;
  Index, CommaCount: Integer;
Begin
  // Original Title
  Result:=Title;

  Setarraylength(Words,11);
  Words[0]:=' In ';
  Words[1]:=' On ';
  Words[2]:=' Of ';
  Words[3]:=' As ';
  Words[4]:=' The ';
  Words[5]:=' At ';
  Words[6]:=' And A ';
  Words[7]:=' And ';
  Words[8]:=' An ';
  Words[9]:=' To ';
  Words[10]:=' For ';

  SetArrayLength(Articles,35);
  Articles[0]:=' The';
  Articles[1]:=' a';
  Articles[2]:=' An';
  Articles[3]:=' Le';
  Articles[4]:=' L''';
  Articles[5]:=' Les';
  Articles[6]:=' Der';
  Articles[7]:=' Das';
  Articles[8]:=' Die';
  Articles[9]:=' Des';
  Articles[10]:=' Dem';
  Articles[11]:=' Den';
  Articles[12]:=' Ein';
  Articles[13]:=' Eine';
  Articles[14]:=' Einen';
  Articles[15]:=' Einer';
  Articles[16]:=' Eines';
  Articles[17]:=' Einem';
  Articles[18]:=' Il';
  Articles[19]:=' Lo';
  Articles[20]:=' La';
  Articles[21]:=' I';
  Articles[22]:=' Gli';
  Articles[23]:=' Le';
  Articles[24]:=' Uno';
  Articles[25]:=' Una';
  Articles[26]:=' Un''';
  Articles[27]:=' O';
  Articles[28]:=' Os';
  Articles[29]:=' As';
  Articles[30]:=' El';
  Articles[31]:=' Los';
  Articles[32]:=' Las';
  Articles[33]:=' Unos';
  Articles[34]:=' Unas';

  // Count the Comma in The Title
  CommaCount := 0;
  EndPos := 0;
  Value := Title;
  repeat
     BeginPos := Pos(',', Value);
     if BeginPos > 0 then
     begin
       Delete(Value, 1, BeginPos);
       CommaCount := CommaCount + 1;
       EndPos := EndPos + BeginPos;
     end;
  until( Pos(',',Value) = 0);

  // Compare the Article to a list of known ones
  for Index := 0 to 34 do
  begin
    if Pos(Articles[Index], Value) <> 0 then
    begin
       CommaCount := 1;
       BeginPos := EndPos;
       Break;
    end;
  end;

  if (BeginPos > 0) and (CommaCount = 1) then
  begin
    Value := Copy(Title, BeginPos + 1, Length(Title));
    Value := Trim(Value);
    Result := Value + ' ' + Copy(Title, 1, BeginPos - 1);
  end;

  BeginPos := Pos(': ', Result);
  if BeginPos > 0 then
    Result := StringReplace(Result, ': ', ' - ');

  Result := AnsiMixedCase(Result, ' ');

  for Index := 0 to 10 do
  begin
    if Pos(Words[Index],Result) <> 0 then
    begin
      Original := Words[Index];
      Replace := AnsiLowerCase(Original);
      Result := StringReplace(Result, Original, Replace);
    end;
  end;

  Result := StringReplace(Result, ' - the ', ' - The ');
  Result := Trim(Result);
end;

procedure GetTaglines(Address: string);
var
  Line, Value: string;
  LineNr: Integer;
  BeginPos, EndPos,Longest: Integer;
  Page: TStringList;
begin
  Page := TStringList.Create;
  Page.Text := GetPage(Address);
  LineNr := FindLine('<p>', Page, 0);
  Line := Page.GetString(LineNr);
  PickListClear;
  while Length(line) > 5 do
  begin
    Value := '';
    BeginPos := pos('<p>', Line)+3;
    EndPos := pos('</p>', Line);
    Value := copy(Line, BeginPos, EndPos - BeginPos);
    HTMLDecode(Value);
    PickListAdd(Value);
    Delete(Line, 1, EndPos+4);
  end;
  Page.Free;
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(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;

// Adds movie titles from search results to tree
procedure AddMoviesTitles2(ResultsPage: TStringList);
var
  Page: string;
  MovieTitle, MovieAddress: string;
begin
  Page := ResultsPage.Text;
  // Every movie entry begins with string "<A HREF='/cg/avg.dll?"
  while Pos('<A HREF="/cg/avg.dll?', Page) > 0 do begin
    CutBefore(Page, '<A HREF="/cg/avg.dll?');
    MovieAddress := 'http://allmovie.com' + GetStringFromHTML(Page, '<A', '"', '">');
    MovieTitle := GetStringFromHTML(Page, '<A','', '</tr>');
    MovieTitle := StringReplace(MovieTitle, ')', '),  ');
    CutAfter(Page, '</tr>');
    // add movie to list
    PickTreeAdd(MovieTitle, MovieAddress);
  end;
end;

// Extracts single movie detail (like director, genre) from page
function GetStringFromHTML(Page, StartTag, CutTag, EndTag: string): string;
begin
  Result := '';
  // recognition tag - if present, extract detail from page, otherwise assume detail is not present
  if Pos(StartTag, Page) > 0 then begin
    CutBefore(Page, StartTag);
    // optional cut tag helps finding right string in html page
    if Length(CutTag) > 0 then
      CutAfter(Page, CutTag);
    // movie detail copied with html tags up to end string
    Result := Copy(Page, 0, Pos(EndTag, Page) - 1);
    // remove html tags and decode html string
    HTMLRemoveTags(Result);
    HTMLDecode(Result);
//  ShowMessage('DEBUG: GetStringFromHTML - StartTag "'+StartTag+'", CutTag "'+CutTag+'", EndTag "'+EndTag+'", Result "'+Result+'" ___ '+Page);
  end;
end;

procedure RemovePronoun(var Str: string);
var
  i: Integer;
  s: string;
  c: char;
begin
  // remove pronouns
  if (Copy(Str, 0, 2) = 'L ') or (Copy(Str, 0, 2) = 'A ') then
    Str := Copy(Str, 3, Length(Str) - 2)
  else if (Copy(Str, 0, 3) = 'Le ') or (Copy(Str, 0, 3) = 'La ') or (Copy(Str, 0, 3) = 'Un ') then
    Str := Copy(Str, 4, Length(Str) - 3)
  else if (Copy(Str, 0, 4) = 'Les ') or (Copy(Str, 0, 4) = 'Une ') or (Copy(Str, 0, 4) = 'The ') then
    Str := Copy(Str, 5, Length(Str) - 4);

  // remove pronouns form the end
  if (Copy(Str,Length(Str)-2, 2) = ' L') or (Copy(Str,Length(Str)-2, 2) = ' A') then
    Str := Copy(Str, 0, Length(Str) - 3)
  else if (Copy(Str,Length(Str)-3,3) = ' Le') or (Copy(Str,Length(Str)-3,3) = ' La') or (Copy(Str,Length(Str)-3,3) = ' Un') then
    Str := Copy(Str, 0, Length(Str) - 4)
  else if (Copy(Str,Length(Str)-3,4) = ' Les') or (Copy(Str,Length(Str)-3,4) = ' Une') or (Copy(Str,Length(Str)-3,4) = ' The') then
    Str := Copy(Str, 0, Length(Str) - 5);
    
  // change accents
  s := '';
  for i := 1 to Length(Str) do begin
  c := StrGet(Str, i);
    if (c='í') Then s := s + 'i'
    else if (c='á') Then s := s + 'a'
    else if (c='é') Then s := s + 'e'
    else if (c='ó') Then s := s + 'o'
    else if (c='ú') Then s := s + 'u'
    else s := s + Copy(Str, i, 1);
  end;
  Str := s;

  // remove non-letters, non-digits and non-spaces
  s := '';
  for i := 1 to Length(Str) do begin
  c := StrGet(Str, i);
    if ((c<'a') or (c>'z')) and
       ((c<'A') or (c>'Z')) and
       ((c<'0') or (c>'9')) and
       (c<>' ') then
    else
      s := s + Copy(Str, i, 1);
  end;
  Str := s;
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://akas.imdb.com/Tsearch?title='+UrlEncode(MovieName));
      if ImportAllmovie Then
      begin
        MovieName := GetField(fieldOriginalTitle);
        RemovePronoun(MovieName);
//        Setfield(fieldcategory,MovieName);
//        Setfield(fieldproducer,'http://allmovie.com/cg/avg.dll?p=avg&type=2&srch=' + URLEncode(MovieName));
        AnalyzePage2('http://allmovie.com/cg/avg.dll?p=avg&type=2&srch=' + URLEncode(MovieName));
      end;
      DisplayResults;
    end;
  end
  else
    ShowMessage('This script requires a newer version of Ant Movie Catalog (at least the version 3.4.0)');
end.
Guest

Post by Guest »

Curna wrote:This is the script I'm using right now. However, I haven't tested it too much, so it could have some mistakes.

For me, it works.
Thanks, Curna.

Can you modify your script to allow getting the small picture from AMG first if not then small pic from IMDB then amazon?

Also if the selection such as Country and Catalog that has multiple descritption, give us an option of selecting the first one only. Example: Canada / USA / Germany, select Canada only.
Guest

Post by Guest »

This script works!!! Thanks Curna, is there any way to contact you via email?
Guest

Post by Guest »

Script hangs on this movie:
Pavement
Guest

Post by Guest »

Not working anymore :( , help please....
KaraGarga
Posts: 50
Joined: 2004-04-03 03:33:22
Location: Turkey
Contact:

Post by KaraGarga »

Nice script! Thanx :grinking:

It works in my AMC database.
Guest

Help with translated title

Post by Guest »

Truly beautiful script, exactly what I want! One question, though:

How do I get a prompt so I can pick a translated title I prefer? I have a lot of HK/Japanese/Korean movies with several translated titles. I would like to be able to choose the title from a list of titles for that movie. Right now the above script shows the lists in prompt boxes with no possibility to choose (only "OK" button).

Currently I use the "International English title" as the value in for the country (instead of Spain, Italy, etc.). It works sometimes, but sometimes the phrase is "International Title: English" or "Asia: English Title" or something else. Instead of trying to guess all the possibilities, a list to choose from would be the best for me.

Any help greatly appreciated!
Guest

Strange conflict with URL and Plot Summary import

Post by Guest »

I occasionally experience some strange behavior with this script: the script picks up all information for the movie perfectly, but imports the wrong IMDb URL and plot summary. The strange part is, the wrong URL and plot summary are for a movie that's somehow related to the movie I'm actually looking for.

For example, for "Another You" it found the URL and plot summary for "See No Evil, Hear No Evil." In all these cases when the prompt to choose the plot summary from the list appears, the correct one always appears first. and it's usually cut at the point of where the IMDb main page for the movie cuts off. So it doesn't find the correct plot summary from the link. I believe here is the problem. The wrong movie's plot summary/ies follow. Even if I choose the partial correct plot summary, it'll still import the wrong URL for the movie.

Any idea how to fix this? Thanks!
Post Reply