 //This version of script is for use with PVD versions 0.9.9.5 and above!!!
 (*
 Additional types and functions that can be used in scripts:

  //Types
  TWIDEARRAY : array of WideString

  //Field functions
  procedure AddSearchResult(Title1, Title2, Year, URL, PreviewURL : WideString)
  procedure AddFieldValue(AField: Integer; AValue : WideString)
  procedure AddMoviePerson(Name, TransName, Role, URL : WideString; AType : Byte)
  procedure AddPersonMovie(Title, OrigTitle, Role, Year, URL : WideString; AType : Byte)
  procedure AddAward(Event, Award, Category, Recipient, Year: WideString; const Won : Boolean)
  procedure AddConnection(Title, OrigTitle, Category, URL, Year: WideString)
  procedure AddEpisode(Title, OrigTitle, Description, URL, Year, Season, Episode : WideString)
  
  //String functions
  function Pos(Substr : WideString; Str: WideString): Integer
  function PosFrom(const SubStr, Str : WideString; FromIndex : Integer) : Integer
  function LastPos(const SubStr, Str : WideString) : Integer
  function PrevPos(const SubStr, Str : WideString; APos : Integer) : Integer
  function RemoveTags(AText : WideString; doLineBreaks : Boolean) : WideString
  function ExplodeString(AText : WideString; var Items : TWideArray; Delimiters : WideString) : Integer
  function Copy(S: WideString; Index, Count: Integer): WideString
  procedure Delete(var S: WideString; Index, Count: Integer)
  procedure Insert(Source: WideString; var Dest: WideString; Index: Integer)
  function Length(S: WideString): Integer
  function Trim(S: WideString): WideString
  function CompareText(S1, S2: WideString): Integer
  function CompareStr(S1, S2: WideString): Integer
  function UpperCase(S: WideString): WideString
  function LowerCase(S: WideString): WideString
  function StringReplace(S, OldPattern, NewPattern: WideString; ReplaceAll : Boolean; IgnoreCase : Boolean; WholeWord: Boolean): WideString
  function StrToInt(const S: WideString): Integer
  function IntToStr(const Value: Integer): WideString
  function StrToFloat(const S: WideString): Extended
  function FloatToStr(const Value: Extended): WideString

  function HTMLValues(const HTML : WideString; ABegin, AEnd, ItemBegin, ItemEnd : WideString; ValDelim : WideString; var Pos : Integer) : WideString
  function HTMLValues2(const HTML : WideString; ABegin, AEnd, ItemBegin, ItemEnd : WideString; ValDelim : WideString; var Pos : Integer) : WideString
  function TextBetween(const HTML : WideString; ABegin, AEnd : WideString; doLineBreaks : Boolean; var Pos : Integer) : WideString
  function HTMLToText(const HTML : WideString) : WideString

  procedure ShowMessage(const Msg, Head : WideString)
*)

const
 pauseBeforeLoad = 0; // Pause before loading (in millisecond)

//Some useful constants
const
 //Script types
 stMovies = 0;
 stPeople = 1; 
 stPoster = 2;
  
 //Script modes
 smSearch = 0; 
 smNormal = 1;
 smPoster = 2;
 
 //Parse results
 prError     = 0; 
 prFinished  = 1;
 prList      = 2;
 prListImage = 3;
 prDownload  = 4;
  
 //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 
 //This version of script is for use with PVD versions 0.9.9.5 and above!!!
 //Fixed: "Production Year" changed to "Year" - AimHere 01/15/2009
 //Added: Movie Category field - AimHere 01/25/2009
 //Fixed: Format of URL links to movies within search results changed - Aimhere 03/06/2009
 //Fixed: Do sanity check on presence of production year in search results - AimHere 03/06/2009
 //Added: Search results include name of studio, in addition to title and year - AimHere 03/06/2009
 //Fixed: Properly (?) extract actor names/URLs from new Adult DVD Empire format - AimHere 03/06/2009
 //Fixed: Extracting name of studio wasn't working due to change in ADE HTML - AimHere 3/30/2009
 //Fixed: Yet another change to the way ADE's HTML specifies the studio name - AimHere 4/23/2009
 //Fixed: Also had to change extraction of studio name from search results - AimHere 4/23/2009
 SCRIPT_VERSION = '0.1.0.5';
 SCRIPT_NAME    = 'AdultDVDEmpire.com';
 SCRIPT_DESC    = '[EN] Get movie information from AdultDVDEmpire.com';
 SCRIPT_LANG    = $09; //English
 SCRIPT_TYPE    = stMovies;
 
 BASE_URL    = 'http://www.adultdvdempire.com';
 RATING_NAME = 'ADE';
 
 SEARCH_STR  = 'http://www.adultdvdempire.com/SearchTitlesPage.aspx?SearchString=%s&media_id=2';
 
 CODE_PAGE   = 0; //Use 0 for Autodetect 

 
//Global variables 
var
 Mode : Byte;
 PosterURL : WideString;

//Functions 
function GetScriptVersion : WideString;
begin
 Result := SCRIPT_VERSION;
end; 
 
function GetScriptName : WideString;
begin
 Result :=  SCRIPT_NAME;
end;

function GetScriptDesc : WideString;
begin
 Result := SCRIPT_DESC;
end;

function GetRatingName : WideString;
begin
 Result := RATING_NAME;
end;

function GetScriptLang: Cardinal;
begin
 Result := SCRIPT_LANG;
end;

function GetCodePage : Cardinal;
begin
 Result := CODE_PAGE;
end;

function GetBaseURL : String;
begin
 Result := BASE_URL;
end;

function GetDownloadURL : String;
begin
 if PosterURL = '' then
  Result := SEARCH_STR
 else
  Result := PosterURL; 
end;

function GetScriptType : Byte; 
begin
 Result := SCRIPT_TYPE;
end;

function GetCurrentMode : Byte; 
begin
 Result := Mode;
end;

procedure FindPoster(HTML : WideString);
var
 curPos, EndPos : Integer;
begin
 curPos := Pos('<span class="Item_ViewTitle">Box Cover</span>', HTML);
 curPos := PosFrom('<img src="', HTML, curPos);
 curPos := curPos + Length('<img src="');
 EndPos := PosFrom('"', HTML, curPos);
 
 PosterURL := Copy(HTML, curPos, EndPos - curPos);
end;
  
procedure ParseMovie(MovieURL : WideString; HTML : WideString);
var
 curPos, EndPos, P, L : Integer;
 Tmp, URL, Name : WideString;
begin
 AddFieldValue(mfURL, MovieURL);
 
 EndPos := 1;
 //Title
 AddFieldValue(mfOrigTitle, TextBetween(HTML, '<div class="Item_Title">', '</div>', False, EndPos)); 
 
 //Studio
 // ADE formatting of StudioPage link changed *again* 3/30/2009
 // was:     curPos := PosFrom('<a href="../StudioPage.aspx?', HTML, EndPos);
 // ... and again around 4.20/2009!
 // was:     curPos := PosFrom('<a href="../StudioItemsPage.aspx?', HTML, EndPos);
 // The following fix may not be bulletproof. *Assumes* the first instance of "Item_StudioProductionRating"
 // after "Item_StudioProductionRatingContainer" is the name of the studio.
 curPos := PosFrom('Item_StudioProductionRating_Container', HTML, EndPos);
 if curPos > 0 then
  AddFieldValue(mfStudio, TextBetween(HTML, 'class="Item_StudioProductionRating">', '</a>', False, curPos))
 else
  curPos := EndPos; 
 
 //Year
 AddFieldValue(mfYear, Trim(TextBetween(HTML, '/ Year:', '</span>', False, curPos)));  
 if curPos < 1 then
  curPos := EndPos;  
  
 //Poster
 curPos := PosFrom('>Box Cover</a>', HTML, curPos); 
 if curPos > 0 then begin
  curPos := PrevPos('<a href="', HTML, curPos);
  curPos := curPos + Length('<a href="');
  EndPos := PosFrom('"', HTML, curPos);
  
  PosterURL := BASE_URL + HTMLToText(Copy(HTML, curPos, EndPos - curPos));
 end else
  curPos := EndPos;
 
 //Description
 curPos := PosFrom('<div class="Item_Info_SectionHeader">Synopsis</div>', HTML, curPos);
 if curPos > 0 then
  AddFieldValue(mfDescription, Trim(TextBetween(HTML, '<div class="Item_InfoContainer">', '</div>', True, curPos)))
 else
  curPos := EndPos;
  
 //Rating
 curPos := PosFrom('<span class="Item_RankRating">', HTML, curPos); 
 if curPos > 0 then begin
  curPos := curPos + Length('<span class="Item_RankRating">');
  EndPos := PosFrom(' ', HTML, curPos);
  
  AddFieldValue(mfRating, FloatToStr(StrToFloat(Trim(Copy(HTML, curPos, EndPos - curPos))) * 2));
 end else
  curPos := EndPos;

 //Features
 curPos := PosFrom('<div class="Item_Info_SectionHeaderAlt">Features:</div>', HTML, curPos);
 if curPos > 0 then
  AddFieldValue(mfFeatures, Trim(TextBetween(HTML, '<div class="Item_ProductInfoSectionConatiner">', '</div>', True, curPos)))
 else
  curPos := EndPos;    
  
 //Length
 curPos := PosFrom('<div>Length: ', HTML, curPos);
 if curPos > 0 then begin
  curPos := curPos + Length('<div>Length: ');
  EndPos := PosFrom('</div>', HTML, curPos);
  Tmp := Copy(HTML, curPos, EndPos - curPos);
  P := Pos(' hrs.', Tmp);
  L := StrToInt(Trim(Copy(Tmp, 1, P - 1))) * 60;
  Delete(Tmp, 1, P + 5);  
  P := Pos(' mins.', Tmp);
  L := L + StrToInt(Trim(Copy(Tmp, 1, P - 1)));
  
  AddFieldValue(mfDuration, IntToStr(L));
 end else
  curPos := EndPos;
  
 //Cast
 //***This part is still a little messed up - Aimhere 03/06/2009***
 curPos := PosFrom('<div class="Item_Info_SectionHeader">Cast', HTML, curPos); 
 if curPos > 0 then begin
  EndPos := curPos;
  curPos := PosFrom('<td class="Item_CastListSecondaryName">', HTML, curPos);
  while (curPos > 0) AND (curPos < PosFrom('<div class="Section_Separator"></div>', HTML, EndPos)) do begin
   EndPos := curPos;

   //AdultDVDEmpire changed format of URLs for cast members, had to modify code - AimHere 03/06/2009
   curPos := PosFrom('.html', HTML, EndPos);  
   P := PosFrom('<td class="Item_CastListSecondaryName">', HTML, EndPos + 1);
   if (curPos < 1) OR ((P > 0) AND (curPos > P)) then begin
    //Actor has no associated URL
    curPos := EndPos + Length('<td class="Item_CastListSecondaryName">');
	EndPos := PosFrom('<', HTML, curPos);
	
	Name := Trim(Copy(HTML, curPos, EndPos - curPos));
	URL := '';
   end else begin
    //Actor does have linked URL
    curPos := PrevPos('<a href="', HTML, curPos) + Length('<a href="');
    EndPos := PosFrom('"', HTML, curPos);
   
    P := PosFrom('class="Item_BioInterviewLink"', HTML, curPos); //Skip (bio)
    if (P < 1) OR (P > PosFrom('>', HTML, EndPos)) then begin
     //Modified URL - Aimhere 03/06/2009
     URL := BASE_URL + HTMLToText(Copy(HTML, curPos, EndPos - curPos));
    
     curPos := PosFrom('>', HTML, curPos) + 1;
     EndPos := PosFrom('</a>', HTML, curPos);   
	
     Name := Trim(Copy(HTML, curPos, EndPos - curPos));
    end; 
   end;	
   
   P := PosFrom('_pn_directors">', HTML, EndPos);
   curPos := PosFrom('</div>', HTML, EndPos);
   
   if (P > 0) AND (P < curPos) then begin
	P := P + Length('_pn_directors">');
	Tmp := Trim(HTMLToText(Copy(HTML, P, curPos - P)));
	
	if Tmp = 'Director' then
     AddMoviePerson(Name, '', '', URL, ctDirectors)	
	else
	if Tmp = 'Writer' then
     AddMoviePerson(Name, '', '', URL, ctWriters)	
	else
	if Tmp = 'Producer' then
     AddMoviePerson(Name, '', '', URL, ctProducers);	
   end else
    AddMoviePerson(Name, '', '', URL, ctActors);	

   curPos := PosFrom('<td class="Item_CastListSecondaryName">', HTML, EndPos);
  end; 
 end;
 
 if curPos < 1 then
  curPos := EndPos;

 //Category
 curPos := PosFrom('<div class="Item_BrowseSectionTitle">Category:&nbsp;&nbsp;</div>', HTML, curPos);
 if curPos > 0 then begin
  curPos := curPos + Length('<div class="Item_BrowseSectionTitle">Category:&nbsp;&nbsp;</div>');
  EndPos := PosFrom('</div>', HTML, curPos);
  Tmp := Trim(RemoveTags(Copy(HTML, curPos, EndPos - curPos), False));
  
  AddFieldValue(mfCategory, Tmp);
 end;
 
 //Genre
 curPos := PosFrom('<div class="Item_BrowseSectionTitle">Genre:&nbsp;&nbsp;</div>', HTML, curPos);
 if curPos > 0 then begin
  curPos := curPos + Length('<div class="Item_BrowseSectionTitle">Genre:&nbsp;&nbsp;</div>');
  EndPos := PosFrom('</div>', HTML, curPos);
  Tmp := Trim(RemoveTags(Copy(HTML, curPos, EndPos - curPos), False));
  
  AddFieldValue(mfGenre, Tmp);
 end;  
end;

procedure ParseSearchResults(HTML : WideString);
var
 curPos, EndPos, P : Integer;
 Title, Year, URL, Studio : WideString;
begin
 EndPos := 1;
 
 curPos := PosFrom('<td class="ListItem_Num">', HTML, EndPos); 
 while curPos > 0 do begin
  EndPos := curPos + Length('<td class="ListItem_Num">');

// formatting of ADE search result URLs changed - take URL from first link following ListItem_Num  
  curPos := PosFrom('<a href="', HTML, EndPos); 
  if curPos > 0 then begin
   curPos := curPos + Length('<a href="');
   EndPos := PosFrom('"', HTML, curPos);
   URL := BASE_URL + Copy(HTML, curPos, EndPos - curPos);
  end; 
  
  Title := TextBetween(HTML, '<div class="ListItem_ItemTitle">', '</a>', False, EndPos);

// Extract Studio name from search results - AimHere 03/06/2009
// ADE changed HTML, studio name follows first URL after ListItem_InfoContainer - Aimhere 4/23/2009
// old script:  curPos := PosFrom('<a href="StudioPage.aspx?', HTML, EndPos);
//              if curPos > 0 then begin
//                curPos := PosFrom('>', HTML, curPos) + 1;

  curPos := PosFrom('<div class="ListItem_InfoContainer">', HTML, EndPos);
  if curPos > 0 then begin
    curPos := PosFrom('>', HTML, curPos + Length('<div class="ListItem_InfoContainer">')) + 1;
    EndPos := PosFrom('</a>', HTML, curPos);
    
    Studio := Copy(HTML, curPos, EndPos - curPos);
  end;


// Not currently implemented:
// Revised to search for/extract Release Date year, which is always present, vs: production year, which is not! - Aimhere 03/06/2009 
//  curPos := PosFrom('Release Date: ', HTML, EndPos);
//  if curPos > 0 then begin  
//   EndPos := PosFrom('</span>', HTML, curPos);
//     
//   Year := Trim(Copy(HTML, EndPos-4, 4));
//  end; 

// Sanity check: Production Year, if it exists, will be BEFORE the next "<div class="ListItem_Info">" tag - Aimhere 03/06/2009
  P := PosFrom('<div class="ListItem_Info">', HTML, EndPos);
  curPos := PosFrom(' Year: ', HTML, EndPos);
  if (curPos > 0) AND (curPos < P) then begin  
   curPos := curPos + Length(' Year: ');
   EndPos := PosFrom('</span>', HTML, curPos);
  
   Year := Trim(Copy(HTML, curPos, EndPos - curPos));
  end else
   Year := ''; 

  
  AddSearchResult(Title, Studio, Year, URL, '');
 
  curPos := PosFrom('<td class="ListItem_Num">', HTML, EndPos); 
 end;
end;

function ParsePage(HTML : WideString; URL : String) : Cardinal;
begin
 Wait (pauseBeforeLoad);

 if Pos('>0</span> Matches Found', HTML) > 0 then 
  Result := prError
 else 
 if Pos('</span> Matches Found', HTML) > 0 then begin
  ParseSearchResults(HTML);
  Result := prList; 
 end else 
 if Pos('<span class="Item_ViewTitle">Box Cover</span>', HTML) > 0 then begin
  FindPoster(HTML);
  Mode := smPoster;
  if PosterURL <> '' then
   Result := prDownload
  else
   Result := prFinished;  
 end else begin
  ParseMovie(URL, HTML);
  Mode := smNormal;
  if PosterURL = '' then
   Result := prFinished
  else
   Result := prDownload;  
 end; 
end;

begin
 Mode := smSearch; 
end.