(*
 Additional types and functions that can be used in scripts:
 
 Please see 'OPTIONS' in the body of the script.

 This version of script is for use with PVD versions 0.9.9.16 and above!!!
 ---------------------------------------------

 SCRIPT:		BING_PEOPLE SEARCH(Imdb)
 AUTHOR:	pra15 & Ivek23
 VERSION:		0.0.0.5
 DATE:			21/09/2014

 ---------------------------------------------

  //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 StringReplace(S, OldPattern, NewPattern: String; ReplaceAll : 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 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)
*)

const
 pauseBeforeLoad = 1000; // Pause before loading (in millisecond)
 SiteSearch = 'www.imdb.com/name/'; // Change the site as your choice
 
//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
 SCRIPT_VERSION = '0.0.0.5';
 SCRIPT_NAME    = 'BING_PEOPLE SEARCH(Imdb)';
 SCRIPT_DESC    = 'FUNCTION SEARCH WITH BING IN MULTI PEOPLE SITE(Imdb)';
 SCRIPT_LANG    = $09; //English
 SCRIPT_TYPE    = stPeople;
 
 BASE_URL    = '';
 RATING_NAME = 'ADE';
 SEARCH_STR  = '';
 CODE_PAGE   = 0; //Use 0 for Autodetect

//------------------------ Global variables ---------------------//

//Global variables
var
 Mode : Byte;
 PeopleName : String;
 TabPeopleName, TabSite : TwideArray;
 I : Integer;
//----------------------------- Functions ------------------------//
	
//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; 
Var
URLSearch : String;

begin
    //Get Name of people to search :                
    PeopleName := GetFieldValue(2);
    Explodestring(PeopleName,TabPeopleName,#32);
    PeopleName := #43;
    For I:=low(TabPeopleName) to High(TabPeopleName) do
    Begin
    PeopleName := PeopleName + TabPeopleName[I] + #43;
    end;
    
		URLSearch := 'www.bing.com/Search?q=site%3a' + SiteSearch + PeopleName +'%s';
    LogMessage('URL page recherche : '+ URLSearch);
		
		
		Result := URLSearch;
		
end;

function GetScriptType : Byte;
begin
 Result := SCRIPT_TYPE;
end;

function GetCurrentMode : Byte;
begin
 Result := Mode;
end;
 
//---------- Procedure ParsePeople ----------//

//  - after searchlist selection this function is called with the new html content

procedure ParsePeople(PeopleURL : String; HTML : String);

Begin

end;

//---------- Procedure SearchResults ----------//
// Create list of movies found during the search
procedure SearchResults (HTML : String);
Var
CurPos, PosStart, EndPos : integer;
URLposStart, URLPosEnd : integer;
StartURL, Title, URL : String;

Begin
  //List of other site where search page :
  TabSite := ['www.imdb.com/name/'];
  
  //Search Result in actual page :
  StartURL := '<h2><a href="http://' + SiteSearch;
   CurPos := Pos(StartURL, HTML);
   
   If Curpos > 0 then begin
      EndPos := Curpos;
      
      While (curpos > 0) AND (curpos < PosFrom('<![CDATA[', HTML, Endpos)) do begin
        EndPos:= Curpos;
        URLposStart := Curpos + 13;
        URLPosEnd := PosFrom('" h="', HTML, URLposStart);
        URL := Copy(HTML, URLPosStart, (URLposEnd-URLposStart));
	curPos	:= PosFrom('">', HTML, curPos)+2;
	endPos	:= PosFrom('</h2>', HTML, curPos);
	Title	:= RemoveTags(Trim(Copy(HTML, curPos, endPos - curPos)), false); 
        AddSearchResult(URL, Title, '', LowerCase(URL), '');    
        curpos := PosFrom(StartURL, HTML, URLPosEnd);
      End;
  //Add Links to Page with other sites in TabSite:
    For I := Low(TabSite) to High(TabSite) do
    Begin
    URL := 'www.bing.com/Search?q=site%3a' + TabSite[I] + PeopleName+'%s';
    AddSearchResult('Search in :', TabSite[I],'',LowerCase(URL),'');
    end;
   End;
   
End;

//---------- PARSE PAGE ----------//

function ParsePage(HTML : String; URL : AnsiString) : Cardinal;
Var
StartPos : integer;

begin
   Wait (pauseBeforeLoad);

   If Pos('Bing</title>', HTML) > 0 then begin
    SearchResults(HTML);
    Result := prList;
  end;
end;

begin
 Mode := smSearch;
end.