// CUSTOM AND NON-STANDARD FIELD MAPPINGS

// All custom fields are optional. All standard fields can be suppressed using
// Preferences-Plugins-Overwrite fields. If custom names are changed in PVD, they
// must also be changed where they occur in the body of this script. The type of
// custom field used is also somewhat a matter of personal preference. A list which
// is for viewing only will do just as well in a long text as a multiselect field.

//  ALLMOVIE CAPTION			PVD FIELD				FIELD TYPE

//  Cast Tab					Cast					memo
//  Production Credits Tab		Production			memo
//  Review Tab					Review				memo
//  Run time					Original duration		number
//  Work rating				AllMovie rating		rating
//  Attributes					Attributes			multiselect list
//  MPAA + MPAA Reasons			MPAA					standard (IMDb is primary source)
//  Category					Work type				select list
//  Color type					Color type			select list
//  Types						Types				multiselect list
//  Flags						Flags				multiselect list
//  Keywords					Keywords				multiselect list
//  Themes					Category				standard  (sole source)
//  Tones						Tags					standard  (sole source)
//  Moods						Moods				multiselect list
//  From book (or story)			Book					short text
//  Set in					Set in				multiselect list
//  Box office					Box office			standard (IMDb is primary source)
//  Produced by				Studio				standard (IMDb is secondary source)
//  Release (or Premiere)		Released				short text
//  Released by				Released by			short text
//  	   	 			(Date)	Updated				short text
//
// The script starts here:

(*
 Additional types and functions that can be used in scripts:

  //Types
  TWIDEARRAY : array of String

  //Field functions
  procedure AddSearchResult(Title1, Title2, Year, URL, PreviewURL : String)
  procedure AddFieldValue(AField: Integer; AValue : String)
  procedure AddMoviePerson(Name, TransName, Role, URL : String; AType : Byte)
  procedure AddPersonMovie(Title, OrigTitle, Role, Year, URL : String; AType : Byte)
  procedure AddAward(Event, Award, Category, Recipient, Year: String; const Won : Boolean)
  procedure AddConnection(Title, OrigTitle, Category, URL, Year: String)
  procedure AddEpisode(Title, OrigTitle, Description, URL, Year, Season, Episode : String)
  
  //String functions
  function Pos(Substr : String; Str: String): Integer
  function PosFrom(const SubStr, Str : String; FromIndex : Integer) : Integer
  function LastPos(const SubStr, Str : String) : Integer
  function PrevPos(const SubStr, Str : String; APos : Integer) : Integer
  function RemoveTags(AText : String; doLineBreaks : Boolean) : String
  function ExplodeString(AText : String; var Items : TWideArray; Delimiters : String) : Integer
  function Copy(S: String; Index, Count: Integer): String
  procedure Delete(var S: String; Index, Count: Integer)
  procedure Insert(Source: String; var Dest: String; Index: Integer)
  function Length(S: String): Integer
  function Trim(S: String): String
  function CompareText(S1, S2: String): Integer
  function CompareStr(S1, S2: String): Integer
  function UpperCase(S: String): String
  function LowerCase(S: String): String
  function AnsiString(S, OldPattern, NewPattern: String; All : Boolean; IgnoreCase : Boolean; WholeWord: Boolean): String
  function StrToInt(const S: String): Integer
  function IntToStr(const Value: Integer): String
  function StrToFloat(const S: String): Extended
  function FloatToStr(const Value: Extended): String
  function CurrentDateTime : Extended
  function DateToStr(Value : Extended) : String
  function TimeToStr(Value : Extended) : String
  function HTMLValues(const HTML : String; ABegin, AEnd, ItemBegin, ItemEnd : String; ValDelim : String; var Pos : Integer) : String
  function HTMLValues2(const HTML : String; ABegin, AEnd, ItemBegin, ItemEnd : String; ValDelim : String; var Pos : Integer) : String
  function TextBetween(const HTML : String; ABegin, AEnd : String; doLineBreaks : Boolean; var Pos : Integer) : String
  function HTMLToText(const HTML : String) : String

  procedure ShowMessage(const Msg, Head : String)
*)

//Some useful constants
const
 //Script types
 stMovies = 0;
 stPeople = 1; 
 stPoster = 2;
  
 //Script modes
 smSearch   = 0; 
 smNormal   = 1;
 smCast     = 2;
 smReview   = 3;
 smCredits  = 4;
 smPoster   = 5;
 smFinished = 6;
  
 //Parse results
 prError     = 0; 
 prFinished  = 1;
 prList      = 2;
 prListImage = 3;
 prDownload  = 4;
 
 //Prefix modes
 pmNone   = 0;
 pmEnd    = 1;
 pmBegin  = 2;
 pmRemove = 3; 
 
 //Download methods
 dmGET  = 0;
 dmPOST = 1;
  
 //Movie fields
 mfURL         = 0;
 mfTitle       = 1;
 mfOrigTitle   = 2;
 mfAka         = 3;
 mfYear        = 4;
 mfGenre       = 5;
 mfCategory    = 6;
 mfCountry     = 7;
 mfStudio      = 8;
 mfMPAA        = 9;
 mfRating      = 10;
 mfTags        = 11;
 mfTagline     = 12;
 mfDescription = 13;
 mfDuration    = 14;
 mfFeatures    = 15;
  
 //People fields
 pfURL        = 0;
 pfName       = 1;
 pfTransName  = 2;
 pfAltNames   = 3;
 pfBirthday   = 4;
 pfBirthplace = 5;
 pfGenre      = 6;
 pfBio        = 7;
 pfDeathDate  = 8;
  
 //Credits types
 ctActors    = 0;
 ctDirectors = 1;
 ctWriters   = 2;
 ctComposers = 3;
 ctProducers = 4;
  
 //Script data 
 //SCRIPT_VERSION = '0.1.1.2'; //Added (current date) Updated
 //SCRIPT_VERSION = '0.1.1.3'; //Added Reset's fix re "www" in URL (AnsiString in ParsePage)
 //SCRIPT_VERSION = '0.1.1.4'; //Updated to conform to version provided with 0.9.9.5
 //SCRIPT_VERSION = '0.1.1.6'; //Run time saved to custom field "Original Duration" instead of mfDuration
 //SCRIPT_VERSION = '0.1.1.7'; //Modified 'Flags' to replace malformed data '(Alcohol' and 'Drugs)'
 //SCRIPT_VERSION = '0.1.1.8'; //Modified 'Description' and 'Review' to replace ' --- ', ' -- ' and '--' with '—' (a proper em dash).
 //SCRIPT_VERSION = '0.1.1.9'; //Converted to use Unicode strings effective 0.9.9.15
 //SCRIPT_VERSION = '0.1.2.1'; //Fix to properly convert paragraph breaks in Review (thanks nostra)
 SCRIPT_VERSION = '0.1.2.2'; //Modified ParseCast procedure to save entire Cast tab to custom memo field, as well as individual actors.
 SCRIPT_NAME    = 'AllMovie+';
 SCRIPT_DESC    = '[EN] Get Movie Information from Allmovie';
 SCRIPT_LANG    = $09; //English
 SCRIPT_TYPE    = stMovies;
 
 BASE_URL    = 'http://www.allmovie.com';
 RATING_NAME = 'AllMovie';
 
 SEARCH_STR  = 'http://www.allmovie.com/search/work/%s/results';
 
 CODE_PAGE   = 65001; //Use 0 for Autodetect 

 //OPTIONS
 GET_THEMES  = True; //Set to False if you do not want Themes in Category field 
 GET_REVIEW  = True; 
 GET_CREDITS = True;


//Global variables 
var
 ELI : Integer;
 Mode : Byte;
 ExtraLinks : array [smCast..smPoster] of String;

//Functions 
function GetScriptVersion : String;
begin
 Result := SCRIPT_VERSION;
end; 
 
function GetScriptName : String;
begin
 Result :=  SCRIPT_NAME;
end;

function GetScriptDesc : String;
begin
 Result := SCRIPT_DESC;
end;

function GetRatingName : String;
begin
 Result := RATING_NAME;
end;

function GetScriptLang: Cardinal;
begin
 Result := SCRIPT_LANG;
end;

function GetCodePage : Cardinal;
begin
 Result := CODE_PAGE;
end;

function GetBaseURL : AnsiString;
begin
 Result := BASE_URL;
end;

function GetDownloadURL : AnsiString;
begin
 if (Mode = smSearch) then
  Result := SEARCH_STR
 else
  Result := ExtraLinks[Mode]; 
end;

function GetDownloadMethod : Byte;
begin
  Result := dmGET;  
end;

function GetPrefixMode : Byte;
begin
 Result := pmBegin;  
end;
function GetScriptType : Byte; 
begin
 Result := SCRIPT_TYPE;
end;

function GetCurrentMode : Byte; 
begin
 Result := Mode;
end;

procedure ParseCredits(HTML : String);
var
 curPos, EndPos : Integer;
 TmpStr : String;
 Name, Role, URL : String;
begin
 curPos := Pos('<div id="results-table">', HTML);
 if curPos < 1 then
  Exit;
  
 TmpStr := '';
  
 EndPos := curPos; 
 curPos := PosFrom('http://www.allmovie.com/artist/', HTML, curPos);
 while (curPos > 0) AND (curPos < PosFrom('</table>', HTML, EndPos)) do begin
  EndPos := PosFrom('">', HTML, curPos);
   
  URL := Copy(HTML, curPos, EndPos - curPos);
   
  curPos := EndPos + 2;
  EndPos := PosFrom('</a>', HTML, curPos);
   
  Name := Copy(HTML, curPos, EndPos - curPos); 
  
  curPos := PosFrom('<em>', HTML, EndPos);
  if curPos > 0 then begin 
   curPos := curPos + 4;
   EndPos := PosFrom('</em>', HTML, curPos);
  
   Role := Trim(Copy(HTML, curPos, EndPos - curPos));
  end else begin
   Role := '';  
   curPos := EndPos;
  end; 
   
  if TmpStr <> '' then
   TmpStr := TmpStr + #13;  
  if URL <> '' then 
   TmpStr := TmpStr + '<link url="' + URL + '">';
  TmpStr := TmpStr + Name;
  if Role <> '' then
   TmpStr := TmpStr + ' - ' + Role;
  if URL <> '' then 
   TmpStr := TmpStr + '</link>';

  if curPos > 0 then 
   curPos := PosFrom('http://www.allmovie.com/artist/', HTML, curPos)
  else
   Exit;  
 end;
  
 AddCustomFieldValueByName('Production', TmpStr);  //Memo field for entire list
end;

function RemoveTagsEx(AText : String) : String;
var
 B, E : Integer;
begin
 Result := AText;

 B := PosFrom('<', Result, 1);
 E := PosFrom('>', Result, B);

 while (B > 0) AND (B < E) do begin
  Delete(Result, B, E - B + 1);

  B := Pos('<', Result);
  E := Pos('>', Result);
 end;
end;

procedure ParseReview(HTML : String);
var
 curPos, endPos : Integer;
 TmpStr, TmpStr1, TmpStr2 : String;
begin
 curPos := Pos('<div id="results-table">', HTML);
 endPos := curPos
 if curPos < 1 then
  Exit;
   
  
 //Review
 curPos := PosFrom('<td colspan="2">', HTML, curPos);
 endPos := PosFrom('</p></td>', HTML, curPos);
 TmpStr := Copy(HTML, curPos, EndPos - curPos);
 TmpStr1 := StringReplace(TmpStr, ' -- ', '—', True, True, False);
 TmpStr2 := StringReplace(TmpStr1, ' --- ', '—', True, True, False);
 TmpStr := StringReplace(TmpStr2, '--', '—', True, True, False);
 TmpStr1 := StringReplace(TmpStr, #13#13#13#13, #13#10#13#10, True, True, False);
 TmpStr2 := RemoveTagsEx(TmpStr1);
 AddCustomFieldValueByName('Review', TmpStr2);
end;

//Parse Cast
//Modified 09/24/2010 to save entire Cast tab to custom memo field, as well as individual actors.
procedure ParseCast(HTML : String);
var
 curPos, EndPos : Integer;
 TmpStr : String;
 Name, Role, URL : String;
begin
 curPos := Pos('<div id="results-table">', HTML);
 if curPos < 1 then
  Exit;
  
 TmpStr := '';  
             
 EndPos := curPos; 
 curPos := PosFrom('http://www.allmovie.com/artist/', HTML, curPos);
 while (curPos > 0) AND (curPos < PosFrom('</table>', HTML, EndPos)) do begin
  EndPos := PosFrom('">', HTML, curPos);
   
  URL := Copy(HTML, curPos, EndPos - curPos);
   
  curPos := EndPos + 2;
  EndPos := PosFrom('</a>', HTML, curPos);
   
  Name := Copy(HTML, curPos, EndPos - curPos); 
  
  curPos := PosFrom('<em>', HTML, EndPos);
  if curPos > 0 then begin 
   curPos := curPos + 4;
   EndPos := PosFrom('</em>', HTML, curPos);
  
   Role := Trim(Copy(HTML, curPos, EndPos - curPos));
  end else begin
   Role := '';  
   curPos := EndPos;
  end; 
   
  AddMoviePerson(Name, '', Role, URL, ctActors);
  
  if TmpStr <> '' then
   TmpStr := TmpStr + #13;  
  if URL <> '' then 
   TmpStr := TmpStr + '<link url="' + URL + '">';
  TmpStr := TmpStr + Name;
  if Role <> '' then
   TmpStr := TmpStr + ' - ' + Role;
  if URL <> '' then 
   TmpStr := TmpStr + '</link>';

  if curPos > 0 then 
   curPos := PosFrom('http://www.allmovie.com/artist/', HTML, curPos)
  else
   Exit;  
 end;
 
 AddCustomFieldValueByName('Cast', TmpStr); 
end;

procedure ParseMovie(MovieURL : String; HTML : String);
var
 curPos, EndPos : Integer;
 Name, URL, TmpStr, TmpStr1, TmpStr2 : String;
begin
  

//Date Updated (chose simple or verbose version)
  AddCustomFieldValueByName('Updated', DateToStr(CurrentDateTime)); // Simple
//AddCustomFieldValueByName('Updated', DateToStr(CurrentDateTime) + ' at ' + TimeToStr(CurrentDateTime) + ' by AllMovie+'); // Verbose

 
 //Get URL
 EndPos := Pos('">Overview</a>', HTML);
 if EndPos > 0 then begin
  curPos := PrevPos('<a href="', HTML, EndPos);
  AddFieldValue(mfURL, Copy(HTML, curPos + 9, EndPos - curPos - 9));
 end else
  AddFieldValue(mfURL, MovieURL);
  
 //Get Review URL
 if GET_REVIEW then begin
  EndPos := Pos('">Review</a>', HTML);
  if EndPos > 0 then begin
   curPos := PrevPos('<a href="', HTML, EndPos);
   ExtraLinks[smReview] := Copy(HTML, curPos + 9, EndPos - curPos - 9);
  end;  
 end; 
 
 //Get Cast URL
 EndPos := Pos('">Cast</a>', HTML);
 if EndPos > 0 then begin
  curPos := PrevPos('<a href="', HTML, EndPos);
  ExtraLinks[smCast] := Copy(HTML, curPos + 9, EndPos - curPos - 9);
 end; 
 
 //Get Credits URL
 if GET_CREDITS then begin
  EndPos := Pos('">Production Credits</a>', HTML);
  if EndPos > 0 then begin
   curPos := PrevPos('<a href="', HTML, EndPos);
   ExtraLinks[smCredits] := Copy(HTML, curPos + 9, EndPos - curPos - 9);
  end; 
 end; 

  
 //Title 
 curPos := Pos('<span class="title">', HTML) + Length('<span class="title">'); 
 EndPos := PosFrom('</span>', HTML, curPos);
 
 AddFieldValue(mfTitle, Copy(HTML, curPos, EndPos - curPos)); 
 
 
 //Poster
 curPos := PosFrom('<tr><td valign="top"><img src="', HTML, EndPos);
 if curPos > 0 then begin
  curPos := curPos + Length('<tr><td valign="top"><img src="');
  EndPos := PosFrom('"', HTML, curPos);
  
  ExtraLinks[smPoster] := Copy(HTML, curPos, EndPos - curPos);
  if Pos('noimage', ExtraLinks[smPoster]) > 0 then
   ExtraLinks[smPoster] := '';
 end;
 

 //Year
 curPos := PosFrom('allmovie.com/explore/year/', HTML, EndPos); 
 if curPos < PosFrom('min.', HTML, EndPos) then begin
  EndPos := PosFrom('">', HTML, curPos);
 
  AddFieldValue(mfYear, Copy(HTML, curPos + Length('allmovie.com/explore/year/'), 4)); 
  
  EndPos := EndPos + 1;
 end else
  EndPos := PosFrom('">', HTML, curPos) + 1;

 
 //Duration
 curPos := PosFrom('">', HTML, EndPos) + 2;
 EndPos := PosFrom(' ', HTML, curPos);
 
// AddFieldValue(mfDuration, Copy(HTML, curPos, EndPos - curPos)); // Changed to:
AddCustomFieldValueByName('Original Duration', Copy(HTML, curPos, EndPos - curPos));  
 
 
 //Rating
 AddCustomFieldValueByName('AllMovie rating', '0');
 curPos := PosFrom('<img src="/img/st_r', HTML, EndPos);
 if curPos > 0 then begin
  curPos := PosFrom('alt="', HTML, curPos) + 5;
  EndPos := PosFrom(' ', HTML, curPos);
  
  AddCustomFieldValueByName('AllMovie rating', FloatToStr(StrToFloat(Copy(HTML, curPos, EndPos - curPos)) * 2));  
   
 end else 
  curPos := EndPos;


 //Attributes
 AddCustomFieldValueByName('Attributes', HTMLValues(HTML, 
                             '<span>Attributes</span>', '</table>',
			                 '<li>', '</li>', 
			                 ', ', EndPos)); 
  
  curPos := EndPos;

 
 //Country
 AddFieldValue(mfCountry, HTMLValues2(HTML, 
                             '<span>Countries</span>', '</table>',
			                 'allmovie.com/explore/country/', '</a>', 
			                 ', ', EndPos));
 

 //MPAA
 EndPos := PosFrom('style="width: 86px;', HTML, EndPos); //if no countries found set EndPos to the right position
 curPos := PosFrom('">', HTML, EndPos) + 2;
 EndPos := PosFrom('</td>', HTML, curPos);
 
 TmpStr2 := Copy(HTML, curPos, EndPos - curPos);


 //Work Type ("Category")
 AddCustomFieldValueByName('Work type', HTMLValues2(HTML, 
                             '<span>Category</span>', '</table>',
			                 '<a href="http://www.allmovie.com/explore/category/', '</a>', 
			                 ', ', EndPos));
 

 //Color type
 EndPos := PosFrom('style="width: 86px;', HTML, EndPos); //if no countries found set EndPos to the right position
 curPos := PosFrom('">', HTML, EndPos) + 2;
 EndPos := PosFrom('</td>', HTML, curPos);
 AddCustomFieldValueByName('Color type', Copy(HTML, curPos, EndPos - curPos));
  
 
 //Aka
 AddFieldValue(mfAka, TextBetween(HTML, '<span>AKA</span>', '</table>', True, curPos)); 
 
 
 //Director
 curPos := PosFrom('<span>Director</span>', HTML, EndPos);
 if curPos > 0 then begin
  EndPos := curPos;
  curPos := PosFrom('allmovie.com/artist/', HTML, curPos);
  while (curPos > 0) AND (curPos < PosFrom('</table>', HTML, EndPos)) do begin
   curPos := curPos + Length('allmovie.com/artist/');
   EndPos := PosFrom('">', HTML, curPos);
   
   URL := 'http://www.allmovie.com/artist/' + Copy(HTML, curPos, EndPos - curPos);
   
   curPos := EndPos + 2;
   EndPos := PosFrom('</a>', HTML, curPos);
   
   Name := Copy(HTML, curPos, EndPos - curPos); 
   
   AddMoviePerson(Name, '', '', URL, ctDirectors);

   curPos := PosFrom('allmovie.com/artist/', HTML, curPos);
  end;
 end else
 curPos := EndPos; 


 //Genre
 AddFieldValue(mfGenre, HTMLValues(HTML, 
                             '<span>Genres</span>', '</table>',
			                 '<li>', '</li>', 
			                 ', ', EndPos)); 


 //Types 
 AddCustomFieldValueByName('Types', HTMLValues(HTML,
                             '<span>Types</span>', '</table>',
			                 '<li>', '</li>', 
			                 ', ', EndPos));


 //Flags 
 TmpStr := HTMLValues(HTML, 
			     '<span>Flags</span>', '</table>',
			                 '<li>', '</li>', 
			                 ', ', EndPos);
 TmpStr1 := StringReplace(TmpStr, ' (Alcohol', '', True, True, False)
 TmpStr2 := StringReplace(TmpStr1, 'Drugs)', '', True, True, False)
 AddCustomFieldValueByName('Flags', TmpStr2)


 //Keywords
 AddCustomFieldValueByName('Keywords', HTMLValues(HTML,
                             '<span>Keywords</span>', '</table>',
			                 '<li>', '</li>', 
			                 ', ', EndPos));


 //Themes (Category) 
 if GET_THEMES then
  AddFieldValue(mfCategory, HTMLValues(HTML, 
                             '<span>Themes</span>', '</table>',
			                 '<li>', '</li>', 
			                 ', ', EndPos));

  
 //Tones (using Tags field)
 AddFieldValue(mfTags, HTMLValues(HTML, 
                             '<span>Tones</span>', '</table>',
			                 '<li>', '</li>', 
			                 ', ', EndPos));


 //Moods
 AddCustomFieldValueByName('Moods', HTMLValues(HTML, 
                             '<span>Moods</span>', '</table>',
			                 '<li>', '</li>', 
			                 ', ', EndPos)); 


 //From book
 AddCustomFieldValueByName('Book', HTMLValues(HTML, 
                             '<span>From book</span>', '</table>',
			                 '<li>', '</li>', 
			                 ', ', EndPos)); 


 //From story
 AddCustomFieldValueByName('Book', HTMLValues(HTML, 
                             '<span>From story</span>', '</table>',
			                 '<li>', '</li>', 
			                 ', ', EndPos)); 


 //Set in
 AddCustomFieldValueByName('Set in', HTMLValues2(HTML,
                             '<span>Set In</span>', '</table>',
			                 'http://www.allmovie.com/search/tag', '</a>', 
			                 ', ', EndPos)); 


 //Box office 
 curPos := PosFrom('<span>Box office</span>', HTML, EndPos);
 if curPos > 0 then begin
  curPos := PosFrom('<li>', HTML, curPos) + 5;
  EndPos := PosFrom('</li>', HTML, curPos); 
  
  TmpStr := Copy(HTML, curPos, EndPos - curPos);
  curPos := Pos('/', TmpStr);
  if curPos > 0 then
   Delete(TmpStr, curPos, Length(TmpStr) - curPos + 1);
  curPos := Pos(':', TmpStr);
  if curPos > 0 then
   Delete(TmpStr, curPos, Length(TmpStr) - curPos + 1); 
  AddFieldValueXML('money', TmpStr); 
 end; 
 

 //MPAA Reasons
 TmpStr1 := HTMLValues(HTML, '<span>MPAA Reasons</span>', '</table>', '<li>', '</li>', ', ', EndPos);
 if (TmpStr1 = '') AND (TmpStr2 <> '') then
 AddFieldValue(mfMPAA, 'Rated ' + TmpStr2);
 if (TmpStr1 <> '') AND (TmpStr2 <> '') then
 AddFieldValue(mfMPAA, 'Rated ' + TmpStr2 + ' ' + TmpStr1 + '.');
 

 //Premiere (overwritten by Release, if that also exists)
 AddCustomFieldValueByName('Released', HTMLValues2(HTML,
                             '<span>Premiere</span>', '</table>',
			                 '<a href="', '</a>', 
			                 ', ', EndPos));


 //Produced by (using Studio field)
 AddFieldValue(mfStudio, HTMLValues2(HTML, 
                             '<span>Produced by</span>', '</table>',
			                 '<a href="', '</a>', 
			                 ', ', EndPos)); 


 //Released
 AddCustomFieldValueByName('Released', HTMLValues2(HTML,
                             '<span>Release</span>', '</table>',
                 'http://www.allmovie.com/search/tag/', '</a>',
                 ', ', EndPos));


 //Released by
 AddCustomFieldValueByName('Released by', HTMLValues2(HTML,
                             '<span>Released by</span>', '</table>',
                 'http://www.allmovie.com/search/tag/', '</a>',
                 ', ', EndPos));

				 
 //Description
 curPos := PosFrom('<td align="left" class="title">Plot Synopsis</td>', HTML, EndPos);
 TmpStr := TextBetween(HTML, '<td colspan="2"><p>', '</p></td>', True, curPos); 
 TmpStr1 := StringReplace(TmpStr, ' -- ', '—', True, True, False);
 TmpStr2 := StringReplace(TmpStr1, ' --- ', '—', True, True, False);
 TmpStr := StringReplace(TmpStr2, '--', '—', True, True, False);
 AddFieldValue(mfDescription, TmpStr); 

 
 //Features (DVD releases)
 curPos := PosFrom('<td class="large-list-title" style="padding-top: 13px; padding-bottom: 1px;">', HTML, EndPos);
 TmpStr := TextBetween(HTML, '<td class="large-list-title" style="padding-top: 13px; padding-bottom: 1px;">', '>View DVD Releases</a></td>', False, curPos); 
 TmpStr1 := StringReplace(TmpStr, '» <a href=', '', True, True, False);
 If TmpStr1 <> '' then
 AddFieldValue(mfFeatures, '<link url=' + TmpStr1 + '>DVD Releases</link>'); 

end;


procedure ParseSearchResults(HTML : String);
var
 curPos, EndPos : Integer;
 Title, Year, URL : String;
begin
 curPos := Pos('<div id="results-table">', HTML);
 if curPos < 1 then
  Exit;
  
  
 curPos := PosFrom('<a href="http://www.allmovie.com/work/', HTML, curPos); 
 while curPos > 0 do begin
  EndPos := PosFrom('">', HTML, curPos);
  URL := Copy(HTML, curPos + 9, EndPos - curPos - 9);
  
  curPos := EndPos + 2;
  
  EndPos := PosFrom('</a>', HTML, curPos);
  Title := Copy(HTML, curPos, EndPos - curPos);
  
  curPos := PosFrom('style="width: 70px;">', HTML, curPos);
  curPos := curPos + Length('style="width: 70px;">');
  EndPos := PosFrom('</td>', HTML, curPos);
  
  Year := Copy(HTML, curPos, EndPos - curPos);
  
  AddSearchResult(Title, '', Year, URL, '');
 
  curPos := PosFrom('<a href="http://www.allmovie.com/work/', HTML, curPos); 
 end;
end;

function NextMode(curMode : Integer) : Integer;
var
 I : Integer;
begin
 Result := smFinished;
 
 if curMode < Low(ExtraLinks) - 1 then
  curMode := Low(ExtraLinks) - 1;

 for I := curMode + 1 to High(ExtraLinks) do
  if ExtraLinks[I] <> '' then begin
   Result := I;
   Break;
  end;
end;

function ParsePage(HTML : String; URL : AnsiString) : Cardinal;
begin
 HTML := HTMLToText(HTML);
 HTML := StringReplace (HTML, 'http://allmovie.com', 'http://www.allmovie.com', True, True, False);


 if Pos('Search Results for:', HTML) > 0 then begin
  ParseSearchResults(HTML);
  Result := prList; 
  Exit;
 end else
 if Pos('> Overview - AllMovie</title>', HTML) > 0 then
  ParseMovie(URL, HTML)
 else
 if Pos('> Review - AllMovie</title>', HTML) > 0 then
  ParseReview(HTML)
 else 
 if Pos('> Cast - AllMovie</title>', HTML) > 0 then
  ParseCast(HTML)
 else
 if Pos('> Production Credits - AllMovie</title>', HTML) > 0 then
  ParseCredits(HTML);
  
 Mode := NextMode(Mode);
 if Mode <> smFinished then
  Result := prDownload
 else
  Result := prFinished;  
end;

begin
 Mode := smSearch;

 for ELI := Low(ExtraLinks) to High(ExtraLinks) do
  ExtraLinks[ELI] := '';
end.
