Page 1 of 2

Is there a way to make the scripts a bit more automated? :)

Posted: 2002-10-14 04:56:11
by SuperflyTNT
When I go to run the scripts I have found that the first thing i see is a prompt to hit "ok" if the correct title is in the box. Why? I have just grabbed this very nice looking Ant Movie Catalog, and I have imported my huge 1300+ movie database from Excel, which i had been using. I went to start to run a script to grab info from imdb, but then i had prompts for every little thing.

Is there a way that some of the prompting in the scripts can be automated out. Like for instance the description, can we set the script to just pick the longest one, or the top one or something. And I would see the toughest problems being with finding the correct movie, but most of the time it should be the most popular one with the title. Or is there a way that the script could do all the movies that it finds only one movie with that title and skip all the ones that need a prompt to go further, and come back to those movies all at once, so a person wouldn't have to sit there and micrmanage the script the whole time?

just some thoughts... ;) thanks everyone for these wonderful scriptZ ;)

Posted: 2002-10-14 06:52:13
by Twink
if you run scripts from in the tools menu you can select multiple movies. I think it asks you less crap then

Posted: 2002-10-14 07:24:01
by antp
It is not too difficult to modify the script for that. I should do that, if I've few time, but if somebody else wants to do it, he can :)

Posted: 2002-10-14 23:38:08
by phaelox
I'm working on something, will post results soon ;)

Posted: 2002-10-15 03:47:17
by SuperflyTNT
you guyz kick ass! ;) Thanks guyz, it iZ much appreciateD! ;) (to phae, SUz in the hizzow *L*)

Posted: 2002-10-15 08:22:51
by watchout
hehehe LOL.. indeed Superfly.. :hihi:

Posted: 2002-10-15 15:58:30
by phaelox
Lol Supe :hihi:

Well, here's my stuff...

Batch-importing Features:
  • No more prompts (except when original title is empty)
  • When a search for a movie title leads to only one result on IMDb, IMDb will redirect you to that movie. The script would then assume the "http://us.imdb.com/Tsearch?title=Movie+Title" link as movie URL. This script will grab the real URL, no matter what. (IMHO, this should be in the regular IMDb script as well)
  • Choose whether to import a large picture, small picture or no picture by setting a simple True or False in the script Editor.
  • Choose whether to import any of the fields via the same way.
  • Choose to import the longest available description or the shortest
  • Import Translated Title (but only when it's actually a foreign movie, not regular alternate titles)
  • Choose to import Translated Title, but to not overwrite the Original Title you had filled in.
Bugs:
  • Probably, but none yet detected ;) (haven't had much time yet, to do extensive testing)
Note: Be sure to take a look at (and perhaps modify) the constants at the beginning of the script, as they control what gets imported and what does not.


IMDB (US) automated batch importing.ifs:

Code: Select all

// SCRIPTING
// IMDB (US) automated batch importing (use Editor to change options)
//
// Note(!): This script will take first found Movie title, which in some cases
// will mean it will take the wrong movie title. However, this script will
// save you a ton of time when importing info for a large list. Just correct
// the falsely imported infos manually later. Be sure to make a backup of your
// list beforehand.

(****************************************************
 *  Movie importation script for:                   *
 *      IMDB (US), http://us.imdb.com               *
 *                                                  *
 *  (c) 2002 Antoine Potten    antoine@buypin.com   *
 *  Improvements made by Danny Falkov               *
 *  Improvements made by Kai Blankenhorn            *
 *  Batch-import improvements made by YH            *
 *                                                  *
 *  For use with Ant Movie Catalog 3.3.0            *
 *  www.ant.be.tf/moviecatalog ··· www.buypin.com   *
 ****************************************************)

program IMDb;
const
  // Set the following constants to True to import field, or False to skip field
  ImportURL = True;
  ImportOriginalTitle = True;
    ImportTranslatedTitle = True;
      LeaveOriginalTitle = True; // True will get Translated Title, yet Original Title field will remain same
  ImportYear = True;
  ImportRating = True;
  ImportPicture = True;
    ImportLargePicture = False; // If set to False small pic will be imported
  ImportDirector = True;
  ImportActors = True;
  ImportCountry = True;
  ImportCategory = True;
  ImportDescription = True;
    UseLongestDescription = False; // If set to False shortest description available will be imported
  ImportComments = True;
  ImportLength = True;
  ImportLanguage = True;
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;
  TitleFound: Boolean;
  MovieURL: String;
begin
  Page := TStringList.Create;
  Page.Text := GetPage(Address);
  if pos('<TITLE>IMDb', Page.Text) = 0 then
  begin
    if ImportURL then
      SetField(fieldURL, Address);
    AnalyzeMoviePage(Page)
  end else
  begin
    TitleFound := False;
    LineNr := 0;
    LineNr := FindLine('<H2><A NAME="top">Most popular searches</A></H2>', Page, LineNr);
    if LineNr > -1 then
    begin
      MovieURL := AddMoviesTitles(Page, LineNr);
      TitleFound := True;
    end;
    LineNr := FindLine('<H2><A NAME="mov">Movies</A></H2>', Page, LineNr);
    if (LineNr > -1) And Not (TitleFound) then
    begin
      MovieURL := AddMoviesTitles(Page, LineNr);
      TitleFound := True;
    end;
    LineNr := FindLine('<H2><A NAME="tvm">TV-Movies</A></H2>', Page, LineNr);
    if (LineNr > -1) And Not (TitleFound) then
    begin
      MovieURL := AddMoviesTitles(Page, LineNr);
      TitleFound := True;
    end;
    LineNr := FindLine('<H2><A NAME="tvs">TV series</A></H2>', Page, LineNr);
    if (LineNr > -1) And Not (TitleFound) then
    begin
      MovieURL := AddMoviesTitles(Page, LineNr);
      TitleFound := True;
    end;
    LineNr := FindLine('<H2><A NAME="vid">Made for video</A></H2>', Page, LineNr);
    if (LineNr > -1) And Not (TitleFound) then
    begin
      MovieURL := AddMoviesTitles(Page, LineNr);
      TitleFound := True;
    end;
    if TitleFound then
      AnalyzePage(MovieURL);
  end;
  Page.Free;
end;

procedure AnalyzeMoviePage(Page: TStringList);
var
  Line, Value, Value2, FullValue, OldOriginalTitle: string;
  LineNr, Desc, i: Integer;
  BeginPos, EndPos: Integer;
  Descriptions, OldTitleParts: TStringList;
begin

  // Original Title & Year
  if (ImportOriginalTitle) or (ImportYear) then
  begin
    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 ImportOriginalTitle then
        OldOriginalTitle := GetField(fieldOriginalTitle);
      if (ImportTranslatedTitle) and Not (LeaveOriginalTitle) then
        SetField(fieldOriginalTitle, 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;
  end;

  // Translated Title
  if ImportTranslatedTitle then
  begin
    OldTitleParts := TStringList.Create;
    // Tokenize OldOriginalTitle while removing certain chars/common words ("the", "of")
    Value := AnsiUpperCase(OldOriginalTitle);
    Value := StringReplace(StringReplace(Value, ',', ' '), ':', ' ');
    Value := StringReplace(StringReplace(Value, '(', ' '), ')', ' ');
    Value := StringReplace(StringReplace(Value, 'OF', ' '), 'THE', ' ');
    repeat
      Value := StringReplace(Value, '  ', ' ');
    until Pos('  ', Value) = 0;
    Value := StringReplace(Trim(Value), ' ', ',');
    // Value now contains the original title (comma-separated) that was filled in before running the script
    Value2 := '';
    for i := 1 to Length(Value) do
    begin
      if Pos(',', Copy(Value, i, 1)) = 0 then
        Value2 := Value2 + Copy(Value, i, 1);
      if (Pos(',', Copy(Value, i, 1)) = 1) or (i = Length(Value)) then
      begin
        OldTitleParts.Add(Value2); // put each comma-separated value from Value into a separate string in TitleParts
        Value2 := '';
      end;
    end;
    for i := 0 to OldTitleParts.Count - 1 do
    // Begin comparing title parts (from the title originally filled in by moviedb owner) with
    // the 'true' Original Title (extracted from IMDb) to see if it's a foreign title and needs a Translated Title
    begin
      if Pos(OldTitleParts.GetString(i), AnsiUpperCase(GetField(fieldOriginalTitle))) <= 0 then
      begin // no match, must be a foreign title
        LineNr := FindLine('Also Known As', Page, 0);
        if LineNr > -1 then
        begin
          Line := Page.GetString(LineNr);
          if Pos('Also Known As', Line) > 0 then
          begin
            BeginPos := Pos('Also Known As', Line) + 26;
            Value := Copy(Line, BeginPos, Length(Line) - BeginPos - 4);
            Value := StringReplace(Value, '<br>', '/ ');
            SetField(fieldTranslatedTitle, Value);
          end;
        end;
        Break;
      end;
    end;
    OldTitleParts.Free;
  end;

  // Rating
  if ImportRating then
  begin
    LineNr := FindLine('User Rating:', Page, 0);
    if LineNr > -1 then
    begin
      Line := Page.GetString(LineNr + 4);
      if Pos('awaiting', 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;

  // Direct Link (FIX: when IMDb finds only one entry for the movie title, the script assumes the search URL as Movie URL)
  // Before Fix: http://us.imdb.com/Tsearch?title=Final Fantasy%3A+The+Spirits+Within 
  // After  Fix: http://us.imdb.com/Title?0173840
  if ImportURL then
  begin
    if Pos('Tsearch', GetField(fieldURL)) > 0 then
    begin
      LineNr := FindLine('User Rating:', Page, 0);
      if LineNr > -1 then
      begin
        Line := Page.GetString(LineNr + 2);
        if Pos('/Ratings?', Line) > 0 then
        begin
          BeginPos := pos('/Ratings?', Line) + 9;
          Value := 'http://us.imdb.com/Title?' + Copy(Line, BeginPos, 7);
          SetField(fieldURL, Value);
        end;
      end;
    end;
  end;

  // Picture
  if ImportPicture then
  begin
    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);
      if ImportLargePicture then
        Value := StringReplace(Value, 'MZZZZZZZ', 'LZZZZZZZ');
      GetPicture(Value, False); // False = do not store picture externally ; store it in the catalog file
    end;
  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('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 := '';
      Line := Page.GetString(LineNr);
      repeat
        BeginPos := Pos('<td valign="top">', Line);
        if BeginPos > 0 then
        begin
          Delete(Line, 1, BeginPos);
          Line := copy(Line, 25, Length(Line));
          BeginPos := pos('">', Line) + 2;
          EndPos := pos('</a>', Line);
          if EndPos = 0 then
            EndPos := Pos('</td>', Line);
          Value := copy(Line, BeginPos, EndPos - BeginPos);
          if (Value <> '(more)') and (Value <> '') then
          begin
            BeginPos := pos('.... </td><td valign="top">', Line);
            if BeginPos > 0 then
            begin
              EndPos := pos('</td></tr>', Line);
              BeginPos := BeginPos + 27;
              Value2 := copy(Line, BeginPos, EndPos - BeginPos);
              if Value2 <> '' then
              begin
                Value := Value + ' (as ' + Value2 + ')';
              end;
            end;
            if FullValue <> '' then
              FullValue := FullValue + ', ';
            FullValue := FullValue + Value;
          end;
          EndPos := Pos('</td></tr>', Line);
          Delete(Line, 1, EndPos);
        end else
        begin
          Line := '';
        end;
      until Line = '';
      HTMLDecode(FullValue);
      SetField(fieldActors, FullValue);
    end;
  end;

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

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

  // Description
  if ImportDescription then
  begin
    Descriptions := TStringList.Create;
    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);
      Value := copy(Line, BeginPos, EndPos - BeginPos);
      HTMLDecode(Value);
      Descriptions.Add(Value);
      BeginPos := pos('/Plot?', Line);
      EndPos := pos('">(more)', Line);
      Desc := 0;
      if (BeginPos <> 0) and (EndPos <> 0) then
      begin
        Value := copy(Line, BeginPos, EndPos - BeginPos);
        GetDescriptions(Value, Descriptions);
        For i := 0 to Descriptions.Count - 1 do
        begin
          if UseLongestDescription then
            if Length(Descriptions.GetString(i)) > Length(Descriptions.GetString(Desc)) then
              Desc := i
          else
            if Length(Descriptions.GetString(i)) < Length(Descriptions.GetString(Desc)) then
              Desc := i;
        end;
      end;
      Value := '';
      SetField(fieldDescription, Descriptions.GetString(Desc));
    end;
    Descriptions.Free;
  end;

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

  // Language
  if ImportLanguage then
  begin
    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);
      SetField(fieldLanguages, Value);
    end;
  end;

  DisplayResults;
end;

procedure GetDescriptions(Address: string; var Descriptions: TStringlist);
var
  Line, Value: string;
  LineNr: Integer;
  BeginPos, EndPos: Integer;
  Page: TStringList;
begin
  Page := TStringList.Create;
  Page.Text := GetPage('http://us.imdb.com' + Address);
  LineNr := FindLine('<P CLASS="plotpar">', Page, 0);
  while LineNr > -1 do
  begin
    Value := '';
    repeat
      Line := Page.GetString(LineNr);
      BeginPos := pos('"plotpar">', Line);
      if BeginPos > 0 then
        BeginPos := BeginPos + 10
      else
        BeginPos := 1;
      EndPos := pos('</P>', Line);
      if EndPos < 1 then
        EndPos := Length(Line) + 1;
      if Value <> '' then
        Value := Value + ' ';
      Value := Value + copy(Line, BeginPos, EndPos - BeginPos);
      LineNr := LineNr + 1;
    until (pos('</P>', Line) > 0) or (LineNr = Page.Count);
    Descriptions.Add(Value);
    LineNr := FindLine('<P CLASS="plotpar">', Page, LineNr);
  end;
  Page.Free;
end;

function AddMoviesTitles(Page: TStringList; var LineNr: Integer): String;
var
  Line: string;
  MovieTitle, MovieAddress: string;
  StartPos: Integer;
begin
  repeat
    LineNr := LineNr + 1;
    Line := Page.GetString(LineNr);
    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);
      if Length(Result) <= 0 then
        Result := 'http://us.imdb.com' + MovieAddress;
    end;
  until pos('</OL>', Line) > 0;
end;

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

Posted: 2002-10-15 16:01:23
by phaelox
The Translated Title import is prolly not done the neatest way possible, but what the hell, it's scripting for a scripting component anyway and it does work (for me anyway *L*).. Hope it works for you guys.

Posted: 2002-10-15 16:05:42
by antp
Thanks for your script, I will probably put it with the other scripts ;)
phaelox wrote: [*]When a search for a movie title leads to only one result on IMDb, IMDb will redirect you to that movie. The script would then assume the "http://us.imdb.com/Tsearch?title=Movie+Title" link as movie URL. This script will grab the real URL, no matter what. (IMHO, this should be in the regular IMDb script as well)
Sure, I will add that :)

Posted: 2002-10-15 18:27:25
by antp
Just a remark : you kept "// GETINFO SCRIPTING" at the top of the file, you should only keep "// SCRIPTING" since this script is not made for the classic information importation (where you can import for only one movie)

Posted: 2002-10-15 19:42:57
by phaelox
Ah, right you are. (Hadn't really looked into that yet.)

Edited it out of my post above. ;)

Posted: 2002-10-15 19:53:51
by phaelox
Another thing.

The way I scripted it, when it needs a translated title (w/ foreign movies), it takes all the translated/alternate titles in one string, separated by a forward slash. Personally I like it that way, but if preferred I could further split it up and take only the "English title" alternate Title or if not there, the "(UK)" title or if also not there, the first alternate title.

Example bit from IMDb movie page for the movie "Gwan geun shut daam":

Code: Select all

<b class="ch">Also Known As</a>:</b><br>And Now You're Dead (1998) (UK) <br>Enter the Eagles (1998) (Hong Kong: English title) <br>
I'll probably enhance this later.
phaelox

Posted: 2002-10-15 22:12:54
by SuperflyTNT
i'm running it right now! ;) been ten minutes of running and no prompts yet *L* ;)

oop, thereZ one..."IMDB service temporarily unavailable" then i clicked ok and it went back to what it was doing ;) Guess thatz a good sign ;)

Thanks man, this looks to be just what i needed!!! ;) I'll test it out and try a few different constants or whateverZ ;) Muchos graciaZ ;)


The only LIL BITTY THing I can see so far that seems like it would be easy enough to fix is all the popupZ saying something bout an IMDB error (connection refuses, connection reset, gracefully disconnected, service temporarily unavailable). If there is a way to automate the script to hit "ok" or enter on these prompts then it would be perfect ;) otherwise you still have to kinda oversee the whole thing, because one of these usually pops up on average every 5-10 minutes ;)

Thanks again, this script is still running strong and its been over an hour *L* (lot of movies to grab shit for) but it seems to be running very fast ;)

All finished! ;) and it worked Great! only found a few movies with wrong info in 'em and I don't care *L* this script rocks for mass importation!!!!! ;)

oh, but by the way, It didn't import the ratingZ, but still, itZ almost perfect ;)

Posted: 2002-10-16 00:50:59
by SuperflyTNT
hey wow, i fixed the Rating part of the script ;)

i just compared it to another script where the rating worked and cut and pasted the right shit, and after a few tries i got it to work ;)

and thanks to your very intuitive addition of True/False on/off switches, I'm gonna go ahead and run your mass script to only import Rating ;)

this would kinda be useful anyways to update your ratings every few months ;) so hereZ Your script with all falses except for ratings, and the modified rating part ;)

EDIT *L* I just tried running this and it gives me a Script error every few movies "IMDB position at 7575 out of range" *L* so well, ok i guess i did it "almost" right *L* well anywayzz this might help ya so i'll post it, but it doesn't completely work... *L*

OK, I checked some more, and this only seems to be giving that error on movies with "awaiting 5 votes" under user rating. like:
http://amazon.imdb.com/Title?0189066

so it must need a line like:

"Line := Page.GetString(LineNr + 4);
if Pos('awaiting', Line) > 0 then"

like Phaelox had, but then when you add that it will no longer grab the rating at all. So the values must be off on that line, but i have no idea what most of it means *L* so I just figured this might help pinpoint the problem to make it easier to fix *L*

THANKS again ;)


welp, hereZ the one that works at getting ratings only, mass import for all, except it gives that error when it comes to a movie without a user rating ;)

Code: Select all

// SCRIPTING 
// IMDB (US) automated batch importing (use Editor to change options) 
// 
// Note(!): This script will take first found Movie title, which in some cases 
// will mean it will take the wrong movie title. However, this script will 
// save you a ton of time when importing info for a large list. Just correct 
// the falsely imported infos manually later. Be sure to make a backup of your 
// list beforehand. 

(**************************************************** 
*  Movie importation script for:                   * 
*      IMDB (US), http://us.imdb.com               * 
*                                                  * 
*  (c) 2002 Antoine Potten    antoine@buypin.com   * 
*  Improvements made by Danny Falkov               * 
*  Improvements made by Kai Blankenhorn            * 
*  Batch-import improvements made by Youri Heijnen * 
*                                                  * 
*  For use with Ant Movie Catalog 3.3.0            * 
*  www.ant.be.tf/moviecatalog ··· www.buypin.com   * 
****************************************************) 

program IMDb; 
const 
  // Set the following constants to True to import field, or False to skip field 
  ImportURL = False; 
  ImportOriginalTitle = False; 
    ImportTranslatedTitle = False; 
      LeaveOriginalTitle = False; // True will get Translated Title, yet Original Title field will remain same 
  ImportYear = False; 
  ImportRating = True; 
  ImportPicture = False; 
    ImportLargePicture = False; // If set to False small pic will be imported 
  ImportDirector = False; 
  ImportActors = False; 
  ImportCountry = False; 
  ImportCategory = False; 
  ImportDescription = False; 
    UseLongestDescription = False; // If set to False shortest description available will be imported 
  ImportComments = False; 
  ImportLength = False; 
  ImportLanguage = False; 
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; 
  TitleFound: Boolean; 
  MovieURL: String; 
begin 
  Page := TStringList.Create; 
  Page.Text := GetPage(Address); 
  if pos('<TITLE>IMDb', Page.Text) = 0 then 
  begin 
    if ImportURL then 
      SetField(fieldURL, Address); 
    AnalyzeMoviePage(Page) 
  end else 
  begin 
    TitleFound := False; 
    LineNr := 0; 
    LineNr := FindLine('<H2><A NAME="top">Most popular searches</A></H2>', Page, LineNr); 
    if LineNr > -1 then 
    begin 
      MovieURL := AddMoviesTitles(Page, LineNr); 
      TitleFound := True; 
    end; 
    LineNr := FindLine('<H2><A NAME="mov">Movies</A></H2>', Page, LineNr); 
    if (LineNr > -1) And Not (TitleFound) then 
    begin 
      MovieURL := AddMoviesTitles(Page, LineNr); 
      TitleFound := True; 
    end; 
    LineNr := FindLine('<H2><A NAME="tvm">TV-Movies</A></H2>', Page, LineNr); 
    if (LineNr > -1) And Not (TitleFound) then 
    begin 
      MovieURL := AddMoviesTitles(Page, LineNr); 
      TitleFound := True; 
    end; 
    LineNr := FindLine('<H2><A NAME="tvs">TV series</A></H2>', Page, LineNr); 
    if (LineNr > -1) And Not (TitleFound) then 
    begin 
      MovieURL := AddMoviesTitles(Page, LineNr); 
      TitleFound := True; 
    end; 
    LineNr := FindLine('<H2><A NAME="vid">Made for video</A></H2>', Page, LineNr); 
    if (LineNr > -1) And Not (TitleFound) then 
    begin 
      MovieURL := AddMoviesTitles(Page, LineNr); 
      TitleFound := True; 
    end; 
    if TitleFound then 
      AnalyzePage(MovieURL); 
  end; 
  Page.Free; 
end; 

procedure AnalyzeMoviePage(Page: TStringList); 
var 
  Line, Value, Value2, FullValue, OldOriginalTitle: string; 
  LineNr, Desc, i: Integer; 
  BeginPos, EndPos: Integer; 
  Descriptions, OldTitleParts: TStringList; 
begin 

  // Original Title & Year 
  if (ImportOriginalTitle) or (ImportYear) then 
  begin 
    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 ImportOriginalTitle then 
        OldOriginalTitle := GetField(fieldOriginalTitle); 
      if (ImportTranslatedTitle) and Not (LeaveOriginalTitle) then 
        SetField(fieldOriginalTitle, 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; 
  end; 

  // Translated Title 
  if ImportTranslatedTitle then 
  begin 
    OldTitleParts := TStringList.Create; 
    // Tokenize OldOriginalTitle while removing certain chars/common words ("the", "of") 
    Value := AnsiUpperCase(OldOriginalTitle); 
    Value := StringReplace(StringReplace(Value, ',', ' '), ':', ' '); 
    Value := StringReplace(StringReplace(Value, '(', ' '), ')', ' '); 
    Value := StringReplace(StringReplace(Value, 'OF', ' '), 'THE', ' '); 
    repeat 
      Value := StringReplace(Value, '  ', ' '); 
    until Pos('  ', Value) = 0; 
    Value := StringReplace(Trim(Value), ' ', ','); 
    // Value now contains the original title (comma-separated) that was filled in before running the script 
    Value2 := ''; 
    for i := 1 to Length(Value) do 
    begin 
      if Pos(',', Copy(Value, i, 1)) = 0 then 
        Value2 := Value2 + Copy(Value, i, 1); 
      if (Pos(',', Copy(Value, i, 1)) = 1) or (i = Length(Value)) then 
      begin 
        OldTitleParts.Add(Value2); // put each comma-separated value from Value into a separate string in TitleParts 
        Value2 := ''; 
      end; 
    end; 
    for i := 0 to OldTitleParts.Count - 1 do 
    // Begin comparing title parts (from the title originally filled in by moviedb owner) with 
    // the 'true' Original Title (extracted from IMDb) to see if it's a foreign title and needs a Translated Title 
    begin 
      if Pos(OldTitleParts.GetString(i), AnsiUpperCase(GetField(fieldOriginalTitle))) <= 0 then 
      begin // no match, must be a foreign title 
        LineNr := FindLine('Also Known As', Page, 0); 
        if LineNr > -1 then 
        begin 
          Line := Page.GetString(LineNr); 
          if Pos('Also Known As', Line) > 0 then 
          begin 
            BeginPos := Pos('Also Known As', Line) + 26; 
            Value := Copy(Line, BeginPos, Length(Line) - BeginPos - 4); 
            Value := StringReplace(Value, '<br>', '/ '); 
            SetField(fieldTranslatedTitle, Value); 
          end; 
        end; 
        Break; 
      end; 
    end; 
    OldTitleParts.Free; 
  end; 

  // Rating 
  if ImportRating then 
  begin 
    LineNr := FindLine('User Rating:', Page, 0); 
    if LineNr > -1 then
    begin
      Line := Page.GetString(LineNr + 4);
      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;

  // Direct Link (FIX: when IMDb finds only one entry for the movie title, the script assumes the search URL as Movie URL) 
  // Before Fix: http://us.imdb.com/Tsearch?title=Final Fantasy%3A+The+Spirits+Within 
  // After  Fix: http://us.imdb.com/Title?0173840 
  if ImportURL then 
  begin 
    if Pos('Tsearch', GetField(fieldURL)) > 0 then 
    begin 
      LineNr := FindLine('User Rating:', Page, 0); 
      if LineNr > -1 then 
      begin 
        Line := Page.GetString(LineNr + 2); 
        if Pos('/Ratings?', Line) > 0 then 
        begin 
          BeginPos := pos('/Ratings?', Line) + 9; 
          Value := 'http://us.imdb.com/Title?' + Copy(Line, BeginPos, 7); 
          SetField(fieldURL, Value); 
        end; 
      end; 
    end; 
  end; 

  // Picture 
  if ImportPicture then 
  begin 
    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); 
      if ImportLargePicture then 
        Value := StringReplace(Value, 'MZZZZZZZ', 'LZZZZZZZ'); 
      GetPicture(Value, False); // False = do not store picture externally ; store it in the catalog file 
    end; 
  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('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 := ''; 
      Line := Page.GetString(LineNr); 
      repeat 
        BeginPos := Pos('<td valign="top">', Line); 
        if BeginPos > 0 then 
        begin 
          Delete(Line, 1, BeginPos); 
          Line := copy(Line, 25, Length(Line)); 
          BeginPos := pos('">', Line) + 2; 
          EndPos := pos('</a>', Line); 
          if EndPos = 0 then 
            EndPos := Pos('</td>', Line); 
          Value := copy(Line, BeginPos, EndPos - BeginPos); 
          if (Value <> '(more)') and (Value <> '') then 
          begin 
            BeginPos := pos('.... </td><td valign="top">', Line); 
            if BeginPos > 0 then 
            begin 
              EndPos := pos('</td></tr>', Line); 
              BeginPos := BeginPos + 27; 
              Value2 := copy(Line, BeginPos, EndPos - BeginPos); 
              if Value2 <> '' then 
              begin 
                Value := Value + ' (as ' + Value2 + ')'; 
              end; 
            end; 
            if FullValue <> '' then 
              FullValue := FullValue + ', '; 
            FullValue := FullValue + Value; 
          end; 
          EndPos := Pos('</td></tr>', Line); 
          Delete(Line, 1, EndPos); 
        end else 
        begin 
          Line := ''; 
        end; 
      until Line = ''; 
      HTMLDecode(FullValue); 
      SetField(fieldActors, FullValue); 
    end; 
  end; 

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

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

  // Description 
  if ImportDescription then 
  begin 
    Descriptions := TStringList.Create; 
    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); 
      Value := copy(Line, BeginPos, EndPos - BeginPos); 
      HTMLDecode(Value); 
      Descriptions.Add(Value); 
      BeginPos := pos('/Plot?', Line); 
      EndPos := pos('">(more)', Line); 
      Desc := 0; 
      if (BeginPos <> 0) and (EndPos <> 0) then 
      begin 
        Value := copy(Line, BeginPos, EndPos - BeginPos); 
        GetDescriptions(Value, Descriptions); 
        For i := 0 to Descriptions.Count - 1 do 
        begin 
          if UseLongestDescription then 
            if Length(Descriptions.GetString(i)) > Length(Descriptions.GetString(Desc)) then 
              Desc := i 
          else 
            if Length(Descriptions.GetString(i)) < Length(Descriptions.GetString(Desc)) then 
              Desc := i; 
        end; 
      end; 
      Value := ''; 
      SetField(fieldDescription, Descriptions.GetString(Desc)); 
    end; 
    Descriptions.Free; 
  end; 

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

  // Language 
  if ImportLanguage then 
  begin 
    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); 
      SetField(fieldLanguages, Value); 
    end; 
  end; 

  DisplayResults; 
end; 

procedure GetDescriptions(Address: string; var Descriptions: TStringlist); 
var 
  Line, Value: string; 
  LineNr: Integer; 
  BeginPos, EndPos: Integer; 
  Page: TStringList; 
begin 
  Page := TStringList.Create; 
  Page.Text := GetPage('http://us.imdb.com' + Address); 
  LineNr := FindLine('<P CLASS="plotpar">', Page, 0); 
  while LineNr > -1 do 
  begin 
    Value := ''; 
    repeat 
      Line := Page.GetString(LineNr); 
      BeginPos := pos('"plotpar">', Line); 
      if BeginPos > 0 then 
        BeginPos := BeginPos + 10 
      else 
        BeginPos := 1; 
      EndPos := pos('</P>', Line); 
      if EndPos < 1 then 
        EndPos := Length(Line) + 1; 
      if Value <> '' then 
        Value := Value + ' '; 
      Value := Value + copy(Line, BeginPos, EndPos - BeginPos); 
      LineNr := LineNr + 1; 
    until (pos('</P>', Line) > 0) or (LineNr = Page.Count); 
    Descriptions.Add(Value); 
    LineNr := FindLine('<P CLASS="plotpar">', Page, LineNr); 
  end; 
  Page.Free; 
end; 

function AddMoviesTitles(Page: TStringList; var LineNr: Integer): String; 
var 
  Line: string; 
  MovieTitle, MovieAddress: string; 
  StartPos: Integer; 
begin 
  repeat 
    LineNr := LineNr + 1; 
    Line := Page.GetString(LineNr); 
    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); 
      if Length(Result) <= 0 then 
        Result := 'http://us.imdb.com' + MovieAddress; 
    end; 
  until pos('</OL>', Line) > 0; 
end; 

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

Posted: 2002-10-16 07:40:07
by antp
SuperflyTNT wrote: so it must need a line like:

"Line := Page.GetString(LineNr + 4);
if Pos('awaiting', Line) > 0 then"

Actually this line is there to avoid the error when it is awaiting 5 ratings, but I made a small mistake : you have to replace the ">" by a "="

I never get errors while connecting to IMDB. If you get so many it is either that you have a poor connection, or that you make too many request to their server, and that's not a so good idea since they may notify it and block your IP or block Ant Movie Catalog (that for the moment uses its own agent identifier)

Posted: 2002-10-16 09:37:39
by phaelox
Hehehe, nice to hear it worked so good for ya ('cept for the ratings). And like antp says, IMDb connection errors... whew.. you must be hammering the server with your 1300+ movies :lol: ;)

antp, it can't be a poor connection, he's got 1300+ movies in his list. A list like that you don't get on a poor connection. :P I think his connection is so fast, that he can make a *lot* of requests in a *very* short period of time. But if ppl are going to use batch import for large lists, perhaps better to remove the agent identifier then or at least include an option to not use it (like most ripping progs do)?

Btw, I actually knew of that rating fix, read about it on this forum somewhere, but forgot to include it :P

Bugfixes:
  • Rating import fixed
IMDB (US) automated batch importing.ifs:

Code: Select all

// SCRIPTING
// IMDB (US) automated batch importing (use Editor to change options)
//
// Note(!): This script will take first found Movie title, which in some cases
// will mean it will take the wrong movie title. However, this script will
// save you a ton of time when importing info for a large list. Just correct
// the falsely imported infos manually later. Be sure to make a backup of your
// list beforehand.

(****************************************************
 *  Movie importation script for:                   *
 *      IMDB (US), http://us.imdb.com               *
 *                                                  *
 *  (c) 2002 Antoine Potten    antoine@buypin.com   *
 *  Improvements made by Danny Falkov               *
 *  Improvements made by Kai Blankenhorn            *
 *  Batch-import improvements made by YH            *
 *                                                  *
 *  For use with Ant Movie Catalog 3.3.0            *
 *  www.ant.be.tf/moviecatalog ··· www.buypin.com   *
 ****************************************************)

program IMDb;
const
  // Set the following constants to True to import field, or False to skip field
  ImportURL = True;
  ImportOriginalTitle = True;
    ImportTranslatedTitle = True;
      LeaveOriginalTitle = True; // True will get Translated Title, yet Original Title field will remain same
  ImportYear = True;
  ImportRating = True;
  ImportPicture = True;
    ImportLargePicture = False; // If set to False small pic will be imported
  ImportDirector = True;
  ImportActors = True;
  ImportCountry = True;
  ImportCategory = True;
  ImportDescription = True;
    UseLongestDescription = False; // If set to False shortest description available will be imported
  ImportComments = True;
  ImportLength = True;
  ImportLanguage = True;
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;
  TitleFound: Boolean;
  MovieURL: String;
begin
  Page := TStringList.Create;
  Page.Text := GetPage(Address);
  if pos('<TITLE>IMDb', Page.Text) = 0 then
  begin
    if ImportURL then
      SetField(fieldURL, Address);
    AnalyzeMoviePage(Page)
  end else
  begin
    TitleFound := False;
    LineNr := 0;
    LineNr := FindLine('<H2><A NAME="top">Most popular searches</A></H2>', Page, LineNr);
    if LineNr > -1 then
    begin
      MovieURL := AddMoviesTitles(Page, LineNr);
      TitleFound := True;
    end;
    LineNr := FindLine('<H2><A NAME="mov">Movies</A></H2>', Page, LineNr);
    if (LineNr > -1) And Not (TitleFound) then
    begin
      MovieURL := AddMoviesTitles(Page, LineNr);
      TitleFound := True;
    end;
    LineNr := FindLine('<H2><A NAME="tvm">TV-Movies</A></H2>', Page, LineNr);
    if (LineNr > -1) And Not (TitleFound) then
    begin
      MovieURL := AddMoviesTitles(Page, LineNr);
      TitleFound := True;
    end;
    LineNr := FindLine('<H2><A NAME="tvs">TV series</A></H2>', Page, LineNr);
    if (LineNr > -1) And Not (TitleFound) then
    begin
      MovieURL := AddMoviesTitles(Page, LineNr);
      TitleFound := True;
    end;
    LineNr := FindLine('<H2><A NAME="vid">Made for video</A></H2>', Page, LineNr);
    if (LineNr > -1) And Not (TitleFound) then
    begin
      MovieURL := AddMoviesTitles(Page, LineNr);
      TitleFound := True;
    end;
    if TitleFound then
      AnalyzePage(MovieURL);
  end;
  Page.Free;
end;

procedure AnalyzeMoviePage(Page: TStringList);
var
  Line, Value, Value2, FullValue, OldOriginalTitle: string;
  LineNr, Desc, i: Integer;
  BeginPos, EndPos: Integer;
  Descriptions, OldTitleParts: TStringList;
begin

  // Original Title & Year
  if (ImportOriginalTitle) or (ImportYear) then
  begin
    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 ImportOriginalTitle then
        OldOriginalTitle := GetField(fieldOriginalTitle);
      if (ImportTranslatedTitle) and Not (LeaveOriginalTitle) then
        SetField(fieldOriginalTitle, 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;
  end;

  // Translated Title
  if ImportTranslatedTitle then
  begin
    OldTitleParts := TStringList.Create;
    // Tokenize OldOriginalTitle while removing certain chars/common words ("the", "of")
    Value := AnsiUpperCase(OldOriginalTitle);
    Value := StringReplace(StringReplace(Value, ',', ' '), ':', ' ');
    Value := StringReplace(StringReplace(Value, '(', ' '), ')', ' ');
    Value := StringReplace(StringReplace(Value, 'OF', ' '), 'THE', ' ');
    repeat
      Value := StringReplace(Value, '  ', ' ');
    until Pos('  ', Value) = 0;
    Value := StringReplace(Trim(Value), ' ', ',');
    // Value now contains the original title (comma-separated) that was filled in before running the script
    Value2 := '';
    for i := 1 to Length(Value) do
    begin
      if Pos(',', Copy(Value, i, 1)) = 0 then
        Value2 := Value2 + Copy(Value, i, 1);
      if (Pos(',', Copy(Value, i, 1)) = 1) or (i = Length(Value)) then
      begin
        OldTitleParts.Add(Value2); // put each comma-separated value from Value into a separate string in TitleParts
        Value2 := '';
      end;
    end;
    for i := 0 to OldTitleParts.Count - 1 do
    // Begin comparing title parts (from the title originally filled in by moviedb owner) with
    // the 'true' Original Title (extracted from IMDb) to see if it's a foreign title and needs a Translated Title
    begin
      if Pos(OldTitleParts.GetString(i), AnsiUpperCase(GetField(fieldOriginalTitle))) <= 0 then
      begin // no match, must be a foreign title
        LineNr := FindLine('Also Known As', Page, 0);
        if LineNr > -1 then
        begin
          Line := Page.GetString(LineNr);
          if Pos('Also Known As', Line) > 0 then
          begin
            BeginPos := Pos('Also Known As', Line) + 26;
            Value := Copy(Line, BeginPos, Length(Line) - BeginPos - 4);
            Value := StringReplace(Value, '<br>', '/ ');
            SetField(fieldTranslatedTitle, Value);
          end;
        end;
        Break;
      end;
    end;
    OldTitleParts.Free;
  end;

  // Rating
  if ImportRating then
  begin
    LineNr := FindLine('User Rating:', Page, 0);
    if LineNr > -1 then
    begin
      Line := Page.GetString(LineNr + 4);
      if Pos('awaiting', 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;

  // Direct Link (FIX: when IMDb finds only one entry for the movie title, the script assumes the search URL as Movie URL)
  // Before Fix: http://us.imdb.com/Tsearch?title=Final Fantasy%3A+The+Spirits+Within 
  // After  Fix: http://us.imdb.com/Title?0173840
  if ImportURL then
  begin
    if Pos('Tsearch', GetField(fieldURL)) > 0 then
    begin
      LineNr := FindLine('User Rating:', Page, 0);
      if LineNr > -1 then
      begin
        Line := Page.GetString(LineNr + 2);
        if Pos('/Ratings?', Line) > 0 then
        begin
          BeginPos := pos('/Ratings?', Line) + 9;
          Value := 'http://us.imdb.com/Title?' + Copy(Line, BeginPos, 7);
          SetField(fieldURL, Value);
        end;
      end;
    end;
  end;

  // Picture
  if ImportPicture then
  begin
    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);
      if ImportLargePicture then
        Value := StringReplace(Value, 'MZZZZZZZ', 'LZZZZZZZ');
      GetPicture(Value, False); // False = do not store picture externally ; store it in the catalog file
    end;
  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('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 := '';
      Line := Page.GetString(LineNr);
      repeat
        BeginPos := Pos('<td valign="top">', Line);
        if BeginPos > 0 then
        begin
          Delete(Line, 1, BeginPos);
          Line := copy(Line, 25, Length(Line));
          BeginPos := pos('">', Line) + 2;
          EndPos := pos('</a>', Line);
          if EndPos = 0 then
            EndPos := Pos('</td>', Line);
          Value := copy(Line, BeginPos, EndPos - BeginPos);
          if (Value <> '(more)') and (Value <> '') then
          begin
            BeginPos := pos('.... </td><td valign="top">', Line);
            if BeginPos > 0 then
            begin
              EndPos := pos('</td></tr>', Line);
              BeginPos := BeginPos + 27;
              Value2 := copy(Line, BeginPos, EndPos - BeginPos);
              if Value2 <> '' then
              begin
                Value := Value + ' (as ' + Value2 + ')';
              end;
            end;
            if FullValue <> '' then
              FullValue := FullValue + ', ';
            FullValue := FullValue + Value;
          end;
          EndPos := Pos('</td></tr>', Line);
          Delete(Line, 1, EndPos);
        end else
        begin
          Line := '';
        end;
      until Line = '';
      HTMLDecode(FullValue);
      SetField(fieldActors, FullValue);
    end;
  end;

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

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

  // Description
  if ImportDescription then
  begin
    Descriptions := TStringList.Create;
    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);
      Value := copy(Line, BeginPos, EndPos - BeginPos);
      HTMLDecode(Value);
      Descriptions.Add(Value);
      BeginPos := pos('/Plot?', Line);
      EndPos := pos('">(more)', Line);
      Desc := 0;
      if (BeginPos <> 0) and (EndPos <> 0) then
      begin
        Value := copy(Line, BeginPos, EndPos - BeginPos);
        GetDescriptions(Value, Descriptions);
        For i := 0 to Descriptions.Count - 1 do
        begin
          if UseLongestDescription then
            if Length(Descriptions.GetString(i)) > Length(Descriptions.GetString(Desc)) then
              Desc := i
          else
            if Length(Descriptions.GetString(i)) < Length(Descriptions.GetString(Desc)) then
              Desc := i;
        end;
      end;
      Value := '';
      SetField(fieldDescription, Descriptions.GetString(Desc));
    end;
    Descriptions.Free;
  end;

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

  // Language
  if ImportLanguage then
  begin
    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);
      SetField(fieldLanguages, Value);
    end;
  end;

  DisplayResults;
end;

procedure GetDescriptions(Address: string; var Descriptions: TStringlist);
var
  Line, Value: string;
  LineNr: Integer;
  BeginPos, EndPos: Integer;
  Page: TStringList;
begin
  Page := TStringList.Create;
  Page.Text := GetPage('http://us.imdb.com' + Address);
  LineNr := FindLine('<P CLASS="plotpar">', Page, 0);
  while LineNr > -1 do
  begin
    Value := '';
    repeat
      Line := Page.GetString(LineNr);
      BeginPos := pos('"plotpar">', Line);
      if BeginPos > 0 then
        BeginPos := BeginPos + 10
      else
        BeginPos := 1;
      EndPos := pos('</P>', Line);
      if EndPos < 1 then
        EndPos := Length(Line) + 1;
      if Value <> '' then
        Value := Value + ' ';
      Value := Value + copy(Line, BeginPos, EndPos - BeginPos);
      LineNr := LineNr + 1;
    until (pos('</P>', Line) > 0) or (LineNr = Page.Count);
    Descriptions.Add(Value);
    LineNr := FindLine('<P CLASS="plotpar">', Page, LineNr);
  end;
  Page.Free;
end;

function AddMoviesTitles(Page: TStringList; var LineNr: Integer): String;
var
  Line: string;
  MovieTitle, MovieAddress: string;
  StartPos: Integer;
begin
  repeat
    LineNr := LineNr + 1;
    Line := Page.GetString(LineNr);
    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);
      if Length(Result) <= 0 then
        Result := 'http://us.imdb.com' + MovieAddress;
    end;
  until pos('</OL>', Line) > 0;
end;

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

Posted: 2002-10-16 10:38:03
by antp
I think that what I should make is a pause of 1 or 2 seconds before connecting the server.
Or maybe put it in the script : see if Sleep(1000); is accepted (pause of 1000 milliseconds)

Posted: 2002-10-16 12:06:03
by SuperflyTNT
yeah the pause idea sounds like a winner ;) It definately semeed like a hammering problem.

how do ya put the sleep 1000 in the script? (this is the first time i have ever dealt with scripts for the most part) *L* ;)

Thanks guyz for your help, this catalog is really becoming one hell of a handy tool! ;)

Posted: 2002-10-16 12:19:45
by antp
before each "GetPage" line, add a line with :

Code: Select all

Sleep(1000);
If it does not work or causes an error, I'll have to add that into the program itself.

Unknown identifier

Posted: 2002-10-16 15:11:54
by phaelox
Sleep is not a supported procedure apparently. Sounds like a plan to add it though. The way it is now, the programs pops up a msgbox when it can't connect, but how about not popping up a msgbox when a page can't be loaded and a retry to get that page (maybe max. 2 or 3 tries and then pop up a showmessage if still no go)? (only on scripting, not on getinfo)

phaelox