(*
 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)
*)

//Some useful constants
const
 //Script types
 stMovies = 0;
 stPeople = 1; 
 stPoster = 2;
  
 //Script modes
 smSearch = 0; 
 smNormal = 1;
  
 //Parse results
 prError    = 0; 
 prFinished = 1;
 prList     = 2;
 prDownload = 3;
  
 //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.0.0'; 
 SCRIPT_NAME    = 'Beyazperde.com';
 SCRIPT_DESC    = '[TR] Get movie information from Beyazperde.com';
 SCRIPT_LANG    = $1f; //English
 SCRIPT_TYPE    = stMovies;
 
 BASE_URL    = 'http://beyazperde.mynet.com';
 RATING_NAME = 'beyazperde';
 
 SEARCH_STR  = 'http://beyazperde.mynet.com/hizliarama.asp?keyword=%s';
 
 CODE_PAGE   = 0; //Use 0 for Autodetect 

 //OPTIONS
 GET_THEMES = True; //Set to False if you do not want Themes in Category field 

//Global variables 
var
 Mode : Byte;
 CreditsURL : 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
  Result := SEARCH_STR
end;

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

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


procedure ParseCredits(HTML : WideString);
var
 curPos, EndPos : Integer;
 Name, Role, URL : WideString;
begin
 curPos := Pos('<!--Begin Center Content-->', HTML);
 if curPos < 1 then
  Exit;
  
 //Actors              
 EndPos := curPos; 
 curPos := PosFrom('/cg/avg.dll?p=avg&sql=2:', HTML, curPos);
 while (curPos > 0) AND (curPos < PosFrom('</table>', HTML, EndPos)) do begin
  curPos := curPos + Length('/cg/avg.dll?p=avg&sql=2:');
  EndPos := PosFrom('">', HTML, curPos);
   
  URL := BASE_URL + '/cg/avg.dll?p=avg&sql=2:' + Copy(HTML, curPos, EndPos - curPos);
   
  curPos := EndPos + 2;
  EndPos := PosFrom('</A>', HTML, curPos);
   
  Name := Copy(HTML, curPos, EndPos - curPos); 
  
  curPos := PosFrom('<i>', HTML, EndPos) + 4;
  EndPos := PosFrom('</i>', HTML, curPos);
  
  Role := Trim(Copy(HTML, curPos, EndPos - curPos));
   
  AddMoviePerson(Name, '', Role, URL, ctActors);

  curPos := PosFrom('/cg/avg.dll?p=avg&sql=2:', HTML, curPos);
 end;
end;



procedure ParseMovie(MovieURL : WideString; HTML : WideString);
var
 curPos, EndPos : Integer;
 Name, URL  : WideString;
begin


    curPos := PosFrom('baslik_arama_sonuc.gif', HTML,1);
  //Title 

  curPos := PosFrom('"baslik_filmadi31">', HTML,1)+ Length('"baslik_filmadi31">'); 
  if curPos > 0 then begin
  EndPos := PosFrom('</span>', HTML, curPos);
  AddFieldValue(mfTitle, FloatToStr(CurPos)); 
  end else 
  EndPos:=curPos;

  //OrgTitle 
  curPos := PosFrom('"baslik_filmadi32">', HTML,EndPos) + Length('"baslik_filmadi32">'); 
  if curPos > 19 then begin
  EndPos := PosFrom('</span>', HTML, curPos);
  AddFieldValue(mfOrigTitle, Copy(HTML, curPos, EndPos - curPos));
  end else 
  EndPos:=curPos;

  //Get URL
  curPos := PosFrom('name="id" value="', HTML,EndPos)+ Length('name="id" value="'); 
  if curPos > 0 then begin
  EndPos := PosFrom('">', HTML, curPos);
  URL    := BASE_URL + '/film.asp?id=' +Copy(HTML, curPos, EndPos - curPos);
  AddFieldValue(mfURL,URL);
  end else 
  EndPos:=curPos;

  
 //Rating
  curPos := PosFrom('SinePuan:&nbsp;', HTML, EndPos)+Length('SinePuan:&nbsp;');
  if curPos > 0 then begin
  EndPos := PosFrom(' ', HTML, curPos);
    AddFieldValue(mfRating, FloatToStr(StrToFloat(Copy(HTML, curPos, EndPos - curPos))));  
  end else 
 EndPos:=curPos;

  //Genre
  curPos := PosFrom('<!-- TÜRÜ -->', HTML, EndPos);
  curPos := PosFrom('tur&keyword=', HTML, EndPos)+ Length('tur&keyword=');
  if curPos > 0 then begin
  curPos := PosFrom('" class=turunculine_11_px>', HTML, EndPos)+Length('" class=turunculine_11_px>');
  EndPos := PosFrom('</a>', HTML, curPos);
  AddFieldValue(mfGenre,  Copy(HTML, curPos, EndPos - curPos)); 
  end else 
 EndPos:=curPos;
  
 //Yönetmen
 curPos := PosFrom('<!-- YONETMEN -->', HTML, EndPos);
 if curPos > 0 then begin
  EndPos := curPos;
  curPos := PosFrom('/kisi/', HTML, curPos);
  while (curPos > 0) AND (curPos < PosFrom('<!-- SENARYO -->', HTML, EndPos)) do begin
   curPos := curPos + Length('/kisi/');
   EndPos := PosFrom('"', HTML, curPos);
   
   URL := BASE_URL + '/kisi/' + Copy(HTML, curPos, EndPos - curPos);
   
   curPos := PosFrom('" class=turunculine_11_px>', HTML, EndPos)+Length('" class=turunculine_11_px>');
   EndPos := PosFrom('</a>', HTML, curPos);
   
   Name := Copy(HTML, curPos, EndPos - curPos); 
   
   AddMoviePerson(Name, '', '', URL, ctDirectors);

   curPos := PosFrom('/kisi/', HTML, curPos);
  end;
 end else
EndPos:=curPos;

 //Senaryo
 curPos := PosFrom('<!-- SENARYO -->', HTML, EndPos);
 if curPos > 0 then begin
  EndPos := curPos;
  curPos := PosFrom('/kisi/', HTML, curPos);
  while (curPos > 0) AND (curPos < PosFrom('<!-- KİTAP -->', HTML, EndPos)) do begin
   curPos := curPos + Length('/kisi/');
   EndPos := PosFrom('"', HTML, curPos);
   
   URL := BASE_URL + '/kisi/' + Copy(HTML, curPos, EndPos - curPos);
   
   curPos := PosFrom('" class=turunculine_11_px>', HTML, EndPos)+Length('" class=turunculine_11_px>');
   EndPos := PosFrom('</a>', HTML, curPos);
   
   Name := Copy(HTML, curPos, EndPos - curPos); 
   
   AddMoviePerson(Name, '', '', URL, ctWriters);

   curPos := PosFrom('/kisi/', HTML, curPos);
  end;
 end else
EndPos:=curPos;

 //müzik
 curPos := PosFrom('<!-- MUZIK -->', HTML, EndPos);
 if curPos > 0 then begin
  EndPos := curPos;
  curPos := PosFrom('/kisi/', HTML, curPos);
  while (curPos > 0) AND (curPos < PosFrom('<!-- YAPIM YILI -->', HTML, EndPos)) do begin
   curPos := curPos + Length('/kisi/');
   EndPos := PosFrom('"', HTML, curPos);
   
   URL := BASE_URL + '/kisi/' + Copy(HTML, curPos, EndPos - curPos);
   
   curPos := PosFrom('" class=turunculine_11_px>', HTML, EndPos)+Length('" class=turunculine_11_px>');
   EndPos := PosFrom('</a>', HTML, curPos);
   
   Name := Copy(HTML, curPos, EndPos - curPos); 
   
   AddMoviePerson(Name, '', '', URL, ctComposers);

   curPos := PosFrom('/kisi/', HTML, curPos);
  end;
 end else
EndPos:=curPos;

  //yıl
  curPos := PosFrom('kat=yil&keyword=', HTML, EndPos)+ Length('kat=yil&keyword=');
  if curPos > 0 then begin
  curPos := PosFrom('" class=turunculine_11_px>', HTML, EndPos)+Length('" class=turunculine_11_px>');
  EndPos := PosFrom('</a>', HTML, curPos);
  AddFieldValue(mfYear,  Copy(HTML, curPos, EndPos - curPos)); 
  end else 
EndPos:=curPos;

  //ülke
  curPos := PosFrom('ulke&keyword=', HTML, EndPos)+ Length('ulke&keyword=');
  if curPos > 0 then begin
  curPos := PosFrom('" class=turunculine_11_px>', HTML,  curPos)+Length('" class=turunculine_11_px>');
  EndPos := PosFrom('</a>', HTML, curPos);
  AddFieldValue(mfCountry,  Copy(HTML, curPos, EndPos - curPos)); 
  end else 

///////////////////////////////////////////////////
 
//süre
  curPos := PosFrom('<!-- SURESI -->', HTML, EndPos)+ Length('<!-- SURESI -->');
  if curPos > 0 then begin
  curPos := PosFrom(',', HTML, EndPos)+ Length(',');
  EndPos := PosFrom(' dk', HTML, curPos);
  AddFieldValue(mfDuration,  Copy(HTML, curPos, EndPos - curPos)); 
  end else 
EndPos:=curPos;


 curPos := PosFrom('<!-- OYUNCULAR -->', HTML, EndPos);
 if curPos > 0 then begin
  EndPos := curPos;
  curPos := PosFrom('/kisi/', HTML, curPos);
  while (curPos > 0) AND (curPos < PosFrom('</td>', HTML, EndPos)) do begin
   curPos := curPos + Length('/kisi/');
   EndPos := PosFrom('"', HTML, curPos);
   
   URL := BASE_URL + '/kisi/' + Copy(HTML, curPos, EndPos - curPos);
   
   curPos := PosFrom('style="line-height:15px;">', HTML, EndPos)+Length('style="line-height:15px;">');
   EndPos := PosFrom('</a>', HTML, curPos);
   
   Name := Copy(HTML, curPos, EndPos - curPos); 
   
   AddMoviePerson(Name, '', '', URL, ctActors);

   curPos := PosFrom('/kisi/', HTML, curPos);
  end;
 end else



//Açıklama   
EndPos:=1;
curPos := PosFrom('class=metin align=left', HTML, EndPos);
 AddFieldValue(mfDescription, TextBetween(HTML, 'valign="middle">', '</td>', True, curPos)); 
end;


procedure ParseSearchResults(HTML : WideString);
var
 curPos, EndPos : Integer;
 Title, Year, URL : WideString;
begin
 //curPos := PosFrom('/film.asp?id=', HTML, curPos); 

  curPos := PosFrom('/film.asp?id', HTML,1); 
  while curPos > 0 do begin
  EndPos := PosFrom('&kat=arama">', HTML, curPos);
  URL    := BASE_URL + Copy(HTML, curPos, EndPos - curPos);
  curPos := PosFrom('class="turuncucizgisiz_11_px"><b>', HTML, EndPos) + Length('class="turuncucizgisiz_11_px"><b>');
  EndPos := PosFrom('</b> (', HTML, curPos);
  Title  := Copy(HTML, curPos, EndPos - curPos);
  curPos := PosFrom('(', HTML, curPos) + Length('(') ;
  EndPos := PosFrom(')', HTML, curPos) - Length(')') ;
  Year   := Copy(HTML, curPos, EndPos);
  AddSearchResult(Title, '', Year, URL, '');
 curPos := PosFrom('/film.asp?id', HTML, curPos); 
 end;
end;





function ParsePage(HTML : WideString; URL : String) : Cardinal;
begin
 if Pos('Bulunan toplam', HTML) > 0 then begin
  ParseSearchResults(HTML);
  Result := prList; //search results retrieved
end else
 if Pos('kritere uygun.', HTML) > 0 then
  Result := prError //error (movie not found)
else
 if (Pos('baslik_filmadi31', HTML) > 0) then begin
 Mode := smNormal;
ParseMovie(URL, HTML);
  Result := prFinished;
  //script has finished it's job
end else
  Result := prError; //error (unknown page retrieved)
end;


begin
 Mode := smSearch; 
 CreditsURL := '';
end.