i think imdb script is broken
Posted: 2004-12-05 06:50:32
when there is two movies of the same name, there is a dialog for you to choose, before there is the year of movie is make now it is gone.
Indeed... (unless u had travelled back in time )antp wrote:If it was working yesterday, a script corrected two weeks ago cannot fix something that changed today
Code: Select all
// GETINFO SCRIPTING
// IMDB (US) import with small picture
(***************************************************
* Movie importation script for: *
* IMDB (US), http://us.imdb.com *
* *
* (c) 2002-2004 Antoine Potten *
* software@antp.be *
* Contributors : *
* Danny Falkov *
* Kai Blankenhorn *
* *
* For use with Ant Movie Catalog 3.4.0 *
* www.antp.be/software/moviecatalog *
* *
* This program is free software; you can *
* redistribute it and/or modify it under the *
* terms of the GNU General Public License as *
* published by the Free Software Foundation; *
* either version 2 of the License, or (at your *
* option) any later version. *
***************************************************)
program IMDb;
const
DescriptionToImport = 2;
{
2 = import longest
1 = import short (from main page, faster)
0 = display list to select a description
}
var
MovieName: string;
MovieURL: string;
function FindLine(Pattern: string; List: TStringList; StartAt: Integer): Integer;
var
i: Integer;
begin
result := -1;
if StartAt < 0 then
StartAt := 0;
for i := StartAt to List.Count-1 do
if Pos(Pattern, List.GetString(i)) <> 0 then
begin
result := i;
Break;
end;
end;
procedure 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;
// 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 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;
PickTreeAdd('Search results', '');
AddMoviesTitles(Page);
if PickTreeExec(Address) then
AnalyzePage(Address);
end;
Page.Free;
end;
procedure AnalyzeMoviePage(Page: TStringList);
var
Line, Value, Value2, FullValue: string;
LineNr: Integer;
BeginPos, EndPos, DescrImport: Integer;
begin
DescrImport := DescriptionToImport;
if (DescrImport <> 1) and (Pos('<a href="plotsummary">', Page.Text) = 0) then
DescrImport := 1;
MovieURL := 'http://imdb.com/title/tt' + Copy(Page.Text, Pos('<option>Your Vote</option>', Page.Text) - 9, 7);
// URL
SetField(fieldURL, MovieURL);
// Original Title & Year
LineNr := FindLine('<title>', Page, 0);
Line := Page.GetString(LineNr);
if LineNr > -1 then
begin
BeginPos := pos('<title>', Line);
if BeginPos > 0 then
BeginPos := BeginPos + 7;
EndPos := pos('(', Line);
if EndPos = 0 then
EndPos := Length(Line);
Value := copy(Line, BeginPos, EndPos - BeginPos - 1);
HTMLDecode(Value);
SetField(fieldOriginalTitle, Value);
BeginPos := pos('(', Line) + 1;
if BeginPos > 0 then
begin
EndPos := Pos('/I', Line);
if EndPos < BeginPos then
EndPos := Pos(')', Line);
Value := copy(Line, BeginPos, EndPos - BeginPos);
SetField(fieldYear, Value);
end;
end;
// Rating
LineNr := FindLine('User Rating:', Page, 0);
if LineNr > -1 then
begin
Line := Page.GetString(LineNr + 4);
if Pos('/10', Line) > 0 then
begin
BeginPos := pos('<b>', Line) + 3;
Value := IntToStr(Round(StrToInt(StrGet(Line, BeginPos), 0) + (StrToInt(StrGet(Line, BeginPos + 2), 0) / 10)));
SetField(fieldRating, Value);
end;
end;
// Picture
LineNr := FindLine('<img border="0"', 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);
GetPicture(Value, False); // False = do not store picture externally ; store it in the catalog file
end;
// Director
LineNr := FindLine('Directed by', Page, 0);
if LineNr > -1 then
begin
FullValue := '';
Line := Page.GetString(LineNr + 1);
repeat
BeginPos := pos('">', Line) + 2;
EndPos := pos('</a>', Line);
Value := copy(Line, BeginPos, EndPos - BeginPos);
if (Value <> '(more)') and (Value <> '') then
begin
if FullValue <> '' then
FullValue := FullValue + ', ';
FullValue := FullValue + Value;
end;
Delete(Line, 1, EndPos);
until Pos('</a>', Line) = 0;
HTMLDecode(FullValue);
SetField(fieldDirector, FullValue);
end;
// Actors
LineNr := FindLine('ast overview', Page, 0);
if LineNr = -1 then
LineNr := FindLine('redited cast', Page, 0);
if LineNr > -1 then
begin
FullValue := '';
Line := Page.GetString(LineNr);
repeat
BeginPos := Pos('<td valign="top">', Line);
if BeginPos > 0 then
begin
Delete(Line, 1, BeginPos);
Line := copy(Line, 25, Length(Line));
BeginPos := pos('">', Line) + 2;
EndPos := pos('</a>', Line);
if EndPos = 0 then
EndPos := Pos('</td>', Line);
Value := copy(Line, BeginPos, EndPos - BeginPos);
if (Value <> '(more)') and (Value <> '') then
begin
BeginPos := pos('.... </td><td valign="top">', Line);
if BeginPos > 0 then
begin
EndPos := pos('</td></tr>', Line);
BeginPos := BeginPos + 27;
Value2 := copy(Line, BeginPos, EndPos - BeginPos);
if Value2 <> '' then
begin
Value := Value + ' (as ' + Value2 + ')';
end;
end;
if FullValue <> '' then
FullValue := FullValue + ', ';
FullValue := FullValue + Value;
end;
EndPos := Pos('</td></tr>', Line);
Delete(Line, 1, EndPos);
end else
begin
Line := '';
end;
until Line = '';
HTMLDecode(FullValue);
SetField(fieldActors, FullValue);
end;
//Country
LineNr := FindLine('Country:', Page, 0);
if LineNr > -1 then
begin
Line := Page.GetString(LineNr + 1);
BeginPos := pos('/">', Line) + 3;
EndPos := pos('</a>', Line);
Value := copy(Line, BeginPos, EndPos - BeginPos);
HTMLDecode(Value);
SetField(fieldCountry, Value);
end;
//Category
LineNr := FindLine('Genre:', Page, 0);
if LineNr > -1 then
begin
Line := Page.GetString(LineNr + 1);
BeginPos := pos('/">', Line) + 3;
EndPos := pos('</a>', Line);
Value := copy(Line, BeginPos, EndPos - BeginPos);
HTMLDecode(Value);
SetField(fieldCategory, Value);
end;
//Description
LineNr := FindLine('Plot Summary:', Page, 0);
if LineNr < 1 then
LineNr := FindLine('Plot Outline:', Page, 0);
if LineNr > -1 then
begin
Line := Page.GetString(LineNr);
BeginPos := pos('</b>', Line) + 5;
EndPos := pos('<a href', Line);
if EndPos < 1 then
begin
Line := Line + Page.GetString(LineNr+1);
EndPos := pos('<br><br>', Line);
if EndPos < 1 then
EndPos := Length(Line);
end;
Value := copy(Line, BeginPos, EndPos - BeginPos);
HTMLDecode(Value);
case DescrImport of
0:
begin
PickListClear;
PickListAdd(Value);
GetDescriptions(GetField(fieldURL) + 'plotsummary');
if PickListExec('Select a description for "' + MovieName + '"', Value) then
SetField(fieldDescription, Value);
end;
1:
SetField(fieldDescription, Value);
2:
SetField(fieldDescription, GetDescriptions(MovieURL + 'plotsummary'));
end;
end;
// Comments
LineNr := FindLine('<b>Summary:</b>', Page, 0);
if LineNr > -1 then
begin
Value := '';
repeat
LineNr := LineNr + 1;
Line := Page.GetString(LineNr);
EndPos := Pos('</blockquote>', Line);
if EndPos = 0 then
EndPos := Length(Line)
else
EndPos := EndPos - 1;
Value := Value + Copy(Line, 1, EndPos) + ' ';
until Pos('</blockquote>', Line) > 0;
HTMLDecode(Value);
Value := StringReplace(Value, '<br>', #13#10);
Value := StringReplace(Value, #13#10+' ', #13#10);
SetField(fieldComments, Value);
end;
// Length
LineNr := FindLine('Runtime:', Page, 0);
if LineNr > -1 then
begin
Line := Page.GetString(LineNr + 1);
EndPos := pos(' min', Line);
if EndPos = 0 then
EndPos := pos(' /', Line);
if EndPos = 0 then
EndPos := Length(Line);
if Pos(':', Line) < EndPos then
BeginPos := Pos(':', Line) + 1
else
BeginPos := 1;
Value := copy(Line, BeginPos, EndPos - BeginPos);
SetField(fieldLength, Value);
end;
// 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);
SetField(fieldLanguages, Value);
end;
DisplayResults;
end;
function GetDescriptions(Address: string): string;
var
Line, Value: string;
LineNr: Integer;
BeginPos, EndPos,Longest: Integer;
Page: TStringList;
begin
Result := '';
Longest := 0;
Page := TStringList.Create;
Page.Text := GetPage(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(ResultsPage: TStringList);
var
Page: string;
MovieTitle, MovieAddress: string;
begin
Page := ResultsPage.Text;
while Pos('<a href="/title/tt', Page) > 0 do
begin
CutBefore(Page, '<ol><li><a href="/title/');
MovieAddress := 'http://imdb.com' + GetStringFromHTML(Page, '<li><a href="', '"', '?fr=');
MovieTitle := GetStringFromHTML(Page, 'fm=1">', '">', '</li>');
CutAfter(Page, '</li>');
PickTreeAdd(MovieTitle, MovieAddress);
end;
end;
begin
if CheckVersion(3,4,0) then
begin
MovieName := GetField(fieldOriginalTitle);
if MovieName = '' then
MovieName := GetField(fieldTranslatedTitle);
if Input('IMDb Import', 'Enter the title of the movie:', MovieName) then
begin
AnalyzePage('http://imdb.com/find?more=tt;q='+UrlEncode(MovieName));
end;
end
else
ShowMessage('This script requires a newer version of Ant Movie Catalog (at least the version 3.4.0)');
end.
Code: Select all
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;
// 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;
Code: Select all
procedure AddMoviesTitles(ResultsPage: TStringList);
var
Page: string;
MovieTitle, MovieAddress: string;
begin
Page := ResultsPage.Text;
while Pos('<a href="/title/tt', Page) > 0 do
begin
CutBefore(Page, '<ol><li><a href="/title/');
MovieAddress := 'http://imdb.com' + GetStringFromHTML(Page, '<li><a href="', '"', '?fr=');
MovieTitle := GetStringFromHTML(Page, 'fm=1">', '">', '</li>');
CutAfter(Page, '</li>');
PickTreeAdd(MovieTitle, MovieAddress);
end;
end;
Code: Select all
AnalyzePage('http://imdb.com/find?more=tt;q='+UrlEncode(MovieName));
Code: Select all
PickTreeClear;
PickTreeAdd('Search results', '');
AddMoviesTitles(Page);
Code: Select all
// Comments
LineNr := FindLine('/comments">', Page, 0);
if LineNr > -1 then
begin
Value := '';
repeat
LineNr := LineNr + 1;
Line := Page.GetString(LineNr);
EndPos := Pos('"', Line);
if EndPos = 0 then
EndPos := Length(Line)
else
EndPos := EndPos - 1;
Value := Value + Copy(Line, 1, EndPos) + ' ';
until Pos('"hidden"', Line) > 0;
HTMLDecode(Value);
Value := StringReplace(Value, '"', '"');
Value := StringReplace(Value, '&', '&');
Value := StringReplace(Value, '<br>', #13#10);
Value := StringReplace(Value, #13#10+' ', #13#10);
HTMLRemoveTags (Value);
Value := StringReplace(Value, ' ', '');
SetField(fieldComments, Value);
end;