(*
 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 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)
*)

//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 : 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
  Result := SEARCH_STR
end;

function GetScriptType : Byte; 
begin
 Result := SCRIPT_TYPE;
end;

function GetCurrentMode : Byte; 
begin
 Result := Mode;
end;


procedure ParseCredits(HTML : String);
var
 curPos, EndPos : Integer;
 Name, Role, URL : String;
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 : String; HTML : String);
var
 curPos, EndPos : Integer;
 Name, URL  : String;
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 : String);
var
 curPos, EndPos : Integer;
 Title, Year, URL : String;
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 : String; URL : AnsiString) : 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.
