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

  //Types
  TWIDEARRAY : array of String

  //Field functions
  procedure AddSearchResult(Title1, Title2, Year, URL, PreviewUR: 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
//riga 50
 //Script types
 stMovies = 0;
 stPeople = 1; 
 stPoster = 2;
  
 //Script modes
 smSearch     = 0; 
 smNormal     = 1;
 //smCast       = 2;
 //smReview     = 3;
 //smCredits    = 4;
 smAward      = 2;
 smPrePoster  = 3;
 smPoster     = 4;
 smFinished   = 5;
 
 //Parse results
 prError     = 0; 
 prFinished  = 1;
 prList      = 2;
 prListImage = 3;
 prDownload  = 4;
 
 //Download methods
 dmGET  = 0;
 dmPOST = 1;
 
 //Prefix modes
 pmNone   = 0;
 pmEnd    = 1;
 pmBegin  = 2;
 pmRemove = 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;
//riga 100
 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.6'; 
 SCRIPT_NAME    = 'sedicifilm';
 SCRIPT_DESC    = '[IT] Get movie information from Sedicifilm.it';
 SCRIPT_LANG    = $09; //English
 SCRIPT_TYPE    = stMovies;
 
 BASE_URL    = 'http://www.sedicifilm.it';
 RATING_NAME = 'Sedicifilm';
 
 SEARCH_STR  = 'http://www.sedicifilm.it/vendita.php?cat=&q=%s';

 
 CODE_PAGE   = 65001; //Use 0 for Autodetect 

 //OPTIONS
 GET_THEMES  = True; //Set to False if you do not want Themes in Category field 
 GET_REVIEW  = True; 
 GET_CREDITS = True;
 
 //Custom field names to use
 IMPORTANCE_FIELD     = 'High Historical Importance'; 
 ARTQUALITY_FIELD     = 'High Artistic Quality';
 CREDITS_FIELD        = 'Production Credits'; 
 REVIEW_FIELD         = 'Review';
 HIGHBUDGET_FIELD     = 'High Budget';
 HIGHPRODUCTION_FIELD = 'High Production Value';
 CULT_FIELD           = 'Cult Film';
//riga 150
//Global variables 
var
 ELI, intParsed : Integer;
 Mode : Byte;
 ExtraLinks : array [smAward..smPoster] of 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
 if (Mode = smSearch) then
  Result := SEARCH_STR
 else
  Result := ExtraLinks[Mode]; 
end;
//riga 200
function GetDownloadMethod : Byte;
begin
 //if Mode = smSearch then
 // Result := dmPOST
 //else
  Result := dmGET;  
end;

function GetPrefixMode : Byte;
begin
 Result := pmBegin;  
end;

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

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

procedure ParseReview(HTML : String);
var
 curPos, EndPos : Integer;
 strDescription : String;
begin
 curPos := Pos('trama:', HTML);
 LogMessage('parsereview1: curpos='+ IntToStr(curPos)+' endpos='+IntToStr(EndPos));
 
 if curPos < 1 then
  Exit;
  
 LogMessage('Parsing review...');  
  
 //Review          
  
 EndPos := PosFrom('</td></tr>', HTML, curPos);
 curPos := PrevPos('>', HTML, EndPos)+1;
 
 strDescription := Trim(HTMLToText(Copy(HTML, curPos, EndPos - curPos)));
 
 if Trim(strDescription) <> '' then begin
	 AddFieldValue(mfDescription, Trim(strDescription)); 
	 LogMessage('Recensione: '+  strDescription);  
 end;
end;
 
//riga 250
procedure ParseCast(HTML : String);
var
 curPos, EndPos, limitePos : Integer;
 Name : String;
begin
 curPos :=0;
 curPos := Pos('cast ', HTML)+5;
 LogMessage('cast1: curpos='+ IntToStr(curPos)+' endpos='+IntToStr(EndPos));
 if curPos < 1 then
   Exit;
 LogMessage('Parsing cast...'); 
  
 //Actors              
 
 limitePos := PosFrom('. SE-DICI', HTML, curPos);
 while (curPos > 0) AND (curPos < limitePos)  do begin
   EndPos := PosFrom(',', HTML, curPos);
   if endPos > limitePos then
     EndPos := PosFrom('.', HTML, curPos);
   Name := Copy(HTML, curPos, EndPos - curPos); 
   LogMessage('cast2: curpos='+ IntToStr(curPos)+' endpos='+IntToStr(EndPos)+' Name='+ Name);
   AddMoviePerson(Name, '', '', '', ctActors);
   LogMessage('Actor:'+ Name);
   curPos := EndPos +1;
 end;
end;

procedure ParseMovie(MovieURL : String; HTML : String);
var
 curPos, EndPos : Integer;
// Name, URL, Title  : String;
 Name  : String;
begin
 LogMessage('Parsing movie...'); 
 intParsed := 0;

//Director
 curPos := Pos('Regia:', HTML);
 LogMessage('movie Director1: curpos='+ IntToStr(curPos)+' endpos='+IntToStr(EndPos));
 if curPos > 0 then begin
   EndPos := PosFrom('</td></tr>', HTML, curPos)-4;
   LogMessage('movie Director2: curpos='+ IntToStr(curPos)+' endpos='+IntToStr(EndPos));
   curPos := PrevPos('>', HTML, EndPos)+2;
   LogMessage('movie Director3: curpos='+ IntToStr(curPos)+' endpos='+IntToStr(EndPos));
   Name := Trim(Copy(HTML, curPos, EndPos - curPos)); 
   AddMoviePerson(Name, '', '', '', ctDirectors);
   LogMessage('Director:' + Name);
 end;
 curPos := EndPos;      
//riga300
 AddFieldValue(mfURL, MovieURL);
 LogMessage('Url='+ MovieURL);

 //Poster
 curPos := Pos('LOCANDINA:',HTML);
 curPos := PosFrom('src="', HTML, curPos)+5;
 EndPos := PosFrom('"', HTML, curPos);
 LogMessage('poster0: curpos='+ IntToStr(curPos)+' endpos='+IntToStr(EndPos));
 if curPos > 0 then begin
    ExtraLinks[smPoster] := Trim(Copy(HTML, curPos, EndPos - curPos));
    LogMessage('Poster:' + ExtraLinks[smPoster] );
 end;

 //Genere
 curPos := Pos('Genere:', HTML);
 if curPos > 0 then begin
	 CurPos := PosFrom('strong>', HTML, curPos)+7;
	 EndPos := PosFrom('<', HTML, curPos);
	 AddFieldValue(mfGenre, Copy(HTML, curPos, EndPos - curPos) ); 
	 LogMessage('Genre:' + Copy(HTML, curPos, EndPos - curPos) );  
 end ;

 //Duration 
 curPos := Pos('Durata', HTML);
 if curPos > 0 then begin
	 curPos := PosFrom('corpo', HTML, curPos)+7;
         EndPos := PosFrom('</td></tr>',HTML,curPos)-1;
	 AddFieldValue(mfDuration, Trim(Copy(HTML, curPos, EndPos - curPos))); 
	 LogMessage('Duration:' + Trim(Copy(HTML, curPos, EndPos - curPos))); 
 end ;
 
 //Year
 curPos := Pos('Anno', HTML); 
 if curPos > 0 then begin
  EndPos := PosFrom(' </td></tr>', HTML, curPos);
  curPos := EndPos -4;
  AddFieldValue(mfYear, Trim(Copy(HTML, curPos , Endpos - curPos ))); 
  LogMessage('Anno: ' + Trim(Copy(HTML, curPos , Endpos - curPos )));
  end;
end;

procedure ParseSearchResults(HTML : String);
var
 intInitPos, curPos, EndPos : Integer;
 Title, Year, URL : String;
begin
 curPos := Pos('prodotti selezionati', HTML);
  if curPos < 1 then
  Exit;
//riga 350 
 LogMessage('Parsing search results...');  
 intInitPos := curPos
 curPos := PosFrom('title="', HTML, curPos)+7; 
 while curPos > 0 do begin
 if (curPos > Pos('catalogo film', HTML))  OR (curPos < Pos('prodotti selezionati', HTML)) then
    exit;
  EndPos := PosFrom('"', HTML, curPos);
  Title := Copy(HTML, curPos, EndPos - curPos);
  LogMessage('search1: curpos='+ IntToStr(curPos)+' endpos='+IntToStr(EndPos)+ ' Title='+Title);
   if Length(Title) > 80 then Title := Copy(Title, 1, 80);
  curPos := PosFrom('href=', HTML, curPos);
  EndPos := PosFrom('><', HTML, curPos) - 1;
  URL := BASE_URL + Copy(HTML, curPos + 6, EndPos - curPos - 6);	
  LogMessage('search2: curpos='+ IntToStr(curPos)+' endpos='+IntToStr(EndPos)+ ' Url='+URL);
  Year := ''  
  AddSearchResult(Title, '', Year, URL, '');
  curPos := PosFrom('title="', HTML, curPos)+7; 
 end;
end;

function NextMode(curMode : Integer) : Integer;
var
 I : Integer;
begin
 Result := smFinished;

 if curMode < Low(ExtraLinks) - 1 then
  curMode := Low(ExtraLinks) - 1;

 for I := curMode + 1 to High(ExtraLinks) do
  if ExtraLinks[I] <> '' then begin
   LogMessage('Extralinks:' + IntToStr(I) );
   Result := I;
   Break;
  end;
end;

function ParsePage(HTML : String; URL : AnsiString) : Cardinal;
begin
 HTML := HTMLToText(HTML);

 if Pos('prodotti selezionati', HTML) > 0 then begin
  ParseSearchResults(HTML);
  LogMessage('Parsepage');
 
  Result := prList; 
  LogMessage('Result=' + IntToStr(Result));
 
 Exit;
 //riga400
 end else
 LogMessage('Pos=' +IntToStr(Pos('Regia:',HTML)) );

 if Pos('Regia:',HTML) > 0 then begin
  if intParsed = 0 then begin
          LogMessage('Inizio ParseReview');
 	  ParseReview(HTML);
          LogMessage('Inizio ParseMovie');
	  ParseMovie(URL, HTML);
          LogMessage('Inizio ParseCast');
          ParseCast(HTML);
          LogMessage('Finito ParseCast');
	  intParsed := 1;
  end;
end;
 
 Mode := NextMode(Mode);
 
 if Mode <> smFinished then
  Result := prDownload
 else
  Result := prFinished;  
 LogMessage('Result=' + IntToStr(Result));

end;

begin
 Mode := smSearch;
 LogMessage('smsearch=' + IntToStr(smSearch));

 for ELI := Low(ExtraLinks) to High(ExtraLinks) do
  ExtraLinks[ELI] := '';
end.
