IMDB script with support for alternative titles (translated

If you made a script you can offer it to the others here, or ask help to improve it. You can also report here bugs & problems with existing scripts.
Post Reply
Willspo
Posts: 61
Joined: 2002-08-14 15:50:23

IMDB script with support for alternative titles (translated

Post by Willspo »

Hello!

I've made a little addition to the IMDB script with support for translated title. The script let you choose a title from the "also known as" list in IMDB as translated title.

It's really usefull for Hongkong movies!

The script is pretty sloppy (never tried pascal before) but worked for all my movies.

Code: Select all

  // Alternative title
  LineNr := FindLine('Also Known As:', Page, 0);
  if LineNr > -1 then
  begin
    LineNr := LineNr + 1;
    Line := Page.GetString(LineNr);
    PickListClear;
    repeat
    BeginPos := pos('>', Line) + 1;
    EndPos := pos('(', Line) - 1;
    Value := copy(Line, BeginPos, EndPos - BeginPos);
    HTMLDecode(Value);
    PickListAdd(Value);
    EndPos := pos(')<', Line) + 1;
    Line := copy(Line, EndPos, Length(Line) - 1);
    until Line = '</TD></TR>';
    Value := '';
    if PickListExec('Select an alternative title for "' + MovieName + '"', Value) then
    setField(fieldTranslatedTitle, Value);
  end;
Place this code inside AnalyzeMoviePage(Page: TStringList)

/w
Guest

some improvements

Post by Guest »

Thank you for this! I've been thinking of this some time ago but I was too lazy to do it :)
I made some improvements to your script: It loads from akas.imdb.com, which knows all international alternative titles. I had to change some things in other parts of the script, so I'll post the whole thing.
I also include some changes I made a while ago to include actors' role names (Ex: "Arnold Schwarzenegger (as The Terminator)").

OK, here comes the script:

Code: Select all

// GETINFO SCRIPTING
// IMDB (US) import with large pictures whenever possible

(***************************************************
 *  Movie importation script for:                  *
 *      IMDB (US), http://akas.imdb.com            *
 *                                                 *
 *  (c) 2002 Antoine Potten    antoine@buypin.com  *
 *  Improvements by Danny Falkov, Kai Blankenhorn  *
 *  and Willspo                                    *
 *                                                 *
 *  For use with Ant Movie Catalog 3.3.1           *
 *  www.ant.be.tf/moviecatalog ··· www.buypin.com  *
 ***************************************************)

program IMDb;
var
  MovieName: 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);
//  Page.Text := Page2;
  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, LineNr);
    if LineNr > -1 then
    begin
      PickTreeAdd('Made for video', '');
      AddMoviesTitles(Page, LineNr);
    end;
    if PickTreeExec(Address) then
      AnalyzePage(Address);
  end;
  Page.Free;
end;

procedure AnalyzeMoviePage(Page: TStringList);
var
  Line, Value, TempCopy, FullValue, Value2: string;
  LineNr: Integer;
  BeginPos, EndPos: Integer;
begin

  // Alternative title
  LineNr := FindLine('Also Known As:', Page, 0);
  if LineNr > -1 then
  begin
    LineNr := LineNr + 1;
    Line := Page.GetString(LineNr);
    PickListClear;
    LineNr := 0;
    while (pos('<BR>', Line)>0) AND (LineNr<100) do begin
      while copy(Line, 1, 1)='<' do begin
        BeginPos := pos('<', Line);
        EndPos := pos('>', Line);
        Delete(Line, BeginPos, EndPos);
      end;
      EndPos := pos('(', Line) - 1;
      Value := copy(Line, 1, EndPos);
      HTMLDecode(Value);
      PickListAdd(Value);
      BeginPos := pos('<BR>', Line);
      Line := copy(Line, BeginPos, Length(Line)-BeginPos);

      LineNr := LineNr + 1;
    end;
    Value := '';
    if PickListExec('Select an alternative title for "' + MovieName + '"', Value) then
    setField(fieldTranslatedTitle, Value);
  end;

  // 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(')', Line);
      Value := copy(Line, BeginPos, EndPos - BeginPos);
      SetField(fieldYear, Value);
    end;
  end;

  // Picture
  LineNr := FindLine('<IMG SRC="http://posters.imdb.com/', Page, 0);
  if LineNr < 0 then
    LineNr := FindLine('<A HREF="/ImageView?', Page, 0);
  if LineNr > -1 then
  begin
    Line := Page.GetString(LineNr);
    BeginPos := pos('<A HREF="/ImageView?', Line) + 29;
    EndPos := pos('"><IMG SRC', Line);
    if (BeginPos > 10) and (EndPos > 0) then
    begin
      Value := 'http:' + copy(Line, BeginPos, EndPos - BeginPos);
      //in case there are multiple pics, replace Value with small pic
      if ((pos('.jpg', Value) = 0) and (pos('.gif', Value) = 0)) then
      begin
        BeginPos := pos('<IMG SRC="', Line) + 10;
        EndPos := pos('ALT="cover" ', Line);
        if (BeginPos > 10) and (EndPos > 0) then
          Value := copy(Line, BeginPos, EndPos - BeginPos - 2);
      end;
      GetPicture(Value, False); // False = do not store picture externally ; store it in the catalog file
    end;
  end;

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

  // Director
  LineNr := FindLine('Directed by', Page, 0);
  if LineNr > -1 then
  begin
    FullValue := '';
    repeat
      LineNr := LineNr + 1;
      Line := Page.GetString(LineNr);
      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;
    until (pos('<BR><BR>', Line) > 0) or (LineNr >= Page.Count-1) or (Line = '<BR>');
    HTMLDecode(FullValue);
    SetField(fieldDirector, FullValue);
  end;

  // Actors
  LineNr := FindLine('Cast overview', Page, 0);
  if LineNr = -1 then
    LineNr := FindLine('cast overview', Page, 0);
  if LineNr = -1 then
    LineNr := FindLine('Complete credited cast', Page, 0);
  if LineNr > -1 then
  begin
    FullValue := '';
    repeat
      LineNr := LineNr + 1;
      Line := Page.GetString(LineNr);
      if pos('<TD VALIGN="TOP">', Line) > 0 then
      begin
        Line := copy(Line, 25, Length(Line));
        BeginPos := pos('">', Line) + 2;
        EndPos := pos('</A>', 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;
      end else
      begin
        Line := '';
      end;
    until Line = '';
    HTMLDecode(FullValue);
    SetField(fieldActors, FullValue);
  end;

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

  //Category
  LineNr := FindLine('Genre</B>', Page, 0);
  if LineNr > -1 then
  begin
    Line := Page.GetString(LineNr);
    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) + 4;
    EndPos := pos('<A HREF', Line);
    if EndPos < 1 then
      Line := Line + Page.GetString(LineNr+1);
    EndPos := pos('<A HREF', Line);
    PickListClear;
    Value := copy(Line, BeginPos, EndPos - BeginPos);
    HTMLDecode(Value);
    PickListAdd(Value);
    BeginPos := pos('/Plot?', Line);
    EndPos := pos('"> (more)', Line);
    Value := copy(Line, BeginPos, EndPos - BeginPos);
    GetDescriptions(Value);
    Value := '';
    if PickListExec('Select a description for "' + MovieName + '"', Value) then
      SetField(fieldDescription, Value);
  end;

  // Comments
  LineNr := FindLine('Summary:</B>', Page, 0);
  if LineNr > -1 then
  begin
      BeginPos := 0;
      repeat
         LineNr := LineNr + 1;
         Line := Page.GetString(LineNr);
         BeginPos := pos('<P>', Line);
      until BeginPos > 0;
      BeginPos := BeginPos + 3;
      repeat
         LineNr := LineNr + 1;
         if (copy(line, Length(Line), 1)=' ') OR (Line = '<BLOCKQUOTE><P>') then
         begin
           Line := Line + Page.GetString(LineNr);
         end else
         begin
           Line := Line + ' ' + Page.GetString(LineNr);
         end;
         EndPos := pos('</P>', Line);
      until EndPos > 0;
    Value := copy(Line, BeginPos, EndPos - BeginPos);
    HTMLDecode(Value);
    Value := StringReplace(Value, '<BR>', #13#10);
    Value := StringReplace(Value, #13#10+' ', #13#10);
    SetField(fieldComments, Value);
  end;

  //URL
  LineNr := FindLine('<BASE HREF="', Page, 0);
  if LineNr > -1 then
  begin
    Line := Page.GetString(LineNr);
    BeginPos := pos('<BASE HREF="', Line) + 12;
    EndPos := pos('">', Line);
    Value := copy(Line, BeginPos, EndPos - BeginPos);
    SetField(fieldURL, Value);
  end;

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

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

  DisplayResults;
end;

procedure GetDescriptions(Address: string);
var
  Line, Value: string;
  LineNr: Integer;
  BeginPos, EndPos: Integer;
  Page: TStringList;
begin
  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);
    PickListAdd(Value);
    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);
    if pos('<LI>', Line)>0 then begin
      StartPos := pos('="', Line);
      if StartPos > 0 then
      begin
        Startpos := Startpos + 2;
        MovieAddress := copy(Line, StartPos, pos('">', Line) - StartPos);
        StartPos := pos('">', Line) + 2;
        MovieTitle := copy(Line, StartPos, pos('</A>', Line) - StartPos);
        HTMLDecode(Movietitle);
        PickTreeAdd(MovieTitle, 'http://akas.imdb.com' + MovieAddress);
      end;
    end;
  until pos('</OL>', Line) > 0;
end;

begin
  if CheckVersion(3,3,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)+'&restrict=Movies+only');
      AnalyzePage('http://akas.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.3.0)');
end.

Kai
Willspo
Posts: 61
Joined: 2002-08-14 15:50:23

new version

Post by Willspo »

New version for new IMDB script.

Code: Select all

  // Alternative title
  LineNr := FindLine('Also Known As', 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('(', Line) - 1;
      Value := copy(Line, 0, EndPos);
      HTMLDecode(Value);
      PickListAdd(Value);
      EndPos := pos('>', Line) + 1;
      Line := copy(Line, EndPos, Length(Line) - 1);
    until Length(Line) = 0;
    Value := '';
    if PickListExec('Select an alternative title for "' + MovieName + '"', Value) then
    setField(fieldTranslatedTitle, Value);
  end;
antp
Site Admin
Posts: 9651
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Post by antp »

Thanks :)
Post Reply