(*
 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;
 stScreen = 3;
 stCovers = 4;  
  
 //Script modes
 smSearch = 0; 
 smNormal = 1;
  
 //Parse results
 prError    = 0; 
 prFinished = 1;
 prList     = 2;
 prDownload = 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.0.0'; 
 SCRIPT_NAME    = 'Anime.gen.tr';
 SCRIPT_DESC    = '[TR] Get movie information from Anime.gen.tr';
 SCRIPT_LANG    = $1f; //English
 SCRIPT_TYPE    = stMovies;
 
 BASE_URL    = 'http://www.anime.gen.tr';
 RATING_NAME = 'Anime.gen.tr';
 
 SEARCH_STR  = 'http://www.anime.gen.tr/ara.php?aranacak=%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 GetDownloadMethod : Byte;
begin
 if Mode = smSearch then
  Result := dmPOST
 else
  Result := dmGET;  
end;

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

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


procedure ParseMovie(MovieURL : WideString; HTML : WideString);
var
 curPos, EndPos : Integer;
 Name, URL : WideString;
begin
 //Get URL
  AddFieldValue(mfURL, '');
 
 //Get Cast URL

  
 //Title 
 
 AddFieldValue(mfTitle, ''); 
 
 //Year
  AddFieldValue(mfYear, ''); 
  
 //Duration
 
 AddFieldValue(mfDuration, ''); 
 
 //Rating
  
  AddFieldValue(mfRating, '');  

 
 //Country
 AddFieldValue(mfCountry, '');
 
 //MPAA
 AddFieldValue(mfMPAA,''); 
 
 //Aka
 AddFieldValue(mfAka,'');							 
  
 //Director
  
   AddMoviePerson('', '', '', '', ctDirectors);

//Genre
 AddFieldValue(mfGenre, '');							 							 
  
 //Tags 
 AddFieldValue(mfTags, '');
							 
 //Themes (Category) 
  AddFieldValue(mfCategory,'');							 
  
 //Studio
 AddFieldValue(mfStudio,''); 
  
 //Description
 AddFieldValue(mfDescription, ''); 
end;


procedure ParseSearchResults(HTML : WideString);
var
 curPos, EndPos : Integer;
 Title, Year, URL : WideString;
begin
 curPos := Pos('Arama Sonuçları</p>', HTML);
 if curPos < 1 then
  Exit;
  curPos := PosFrom('animetanitim.php?id=', HTML, curPos); 
 // while (curPos > 0) AND (curPos < Pos('dikkatli olun', HTML)) do begin
  EndPos := PosFrom('">', HTML, curPos);
  URL := BASE_URL + '/'+Copy(HTML, curPos, EndPos - curPos);
  curPos := EndPos + 2;
  EndPos := PosFrom('</a>', HTML, curPos);
  Title := Copy(HTML, curPos, EndPos - curPos);
  Year := '';
  AddSearchResult(Title, '', Year, URL, '');
  curPos := PosFrom('animetanitim.php?id=', HTML, curPos); 
// end;
end;

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


if Pos('<b>Animeler</b>', HTML) > 0 then begin
  ParseSearchResults(HTML);
  Result := prList; //search results retrieved
end else
  if Pos('<b>Animeler</b>', HTML) <= 0 then
  Result := prError //error (movie not found)
else
  if (Pos('font: bold 18px arial;color: #FF0000">', 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.
