const
	script_name='Film.tv.it';
	language='Italian';
	version=4;
	author='Vlad';
	MinProgramBuild=4100;
	PCount = 12;
	//MAX_PHOTOS_COUNT = 3;
var
	persons_count: integer;
	persons: array of string;
	imdb_codes: array of string;
	imdb_code, person_code: string;
	name, code, year, description: string;
	person, BasicURL, biography, trivia, character, birth_date, birthplace, birth_name, salary, height, original_name, death_date, person_site_url: string;
	photo_url: string;
	monthes: array [1..12] of string;
	prefixes: array [1..PCount] of string;
	movie_html: string;
	person_height: integer;

function GetSearchURLbyName(PersonName: string): string;
var
	url: string;
begin
	url:=format('http://www.film.tv.it/cerca.php?q=%s', [PersonName]);
	url:=ReplaceStr(url, ' ', '+', -1);
	result:=url;
end;

//Download search results from URL
function DownloadSearchURL(url: string): integer;
begin
	all:=DownloadURL(url);
	if all='-1' then
		result:=-1
	else
		result:=PersonSearch;
end;

function PersonSearch: integer;
var
	url, s, s2: string;
	i, j, k: integer;
begin
	persons_count:=0;
	s:='';
	//we check if we're forwarded to the filmography page
	s2:=ParseString(all, '<title>(.*?)</title>', 1);
	i:=pos('Ricerca ', s2);
	if i<=0 then begin
		name:=ParseString(s2, '^(.*?) | Film.tv.it', 1);
		person_code:=ParseString(all, '/persona/(\d+)/.*?', 1);
		SetSearchResult(name, person_code);
		persons_count:=1;
		SetLength(persons, persons_count);
	end;
	
	s:=all;

	//if it's a usual search results page
	if i>0 then begin
		i:=pos('<div class="name"><a href="/persona/', s);
		while i>0 do begin
			persons_count:=persons_count+1;
			SetLength(persons, persons_count);
			j:=MyPos('</a></div>', s, i+1);
			s2:=copy(s, i, j-i);
			person_code:=ParseString(s2, '<a href="/persona/(\d+)/.*?', 1);
			name:=RemoveTags(s2);
			name:=DecodeHTML(name);
			SetSearchResult2(name, person_code, '', '', '', '');
			delete(s, 1, j+10);
			i:=pos('<div class="name"><a href="/persona/', s);
		end;
	end;
	result:=persons_count;
end;

function GetPersonURL(code: string; images_only: boolean): string;
begin
		result:=format('http://www.film.tv.it/persona/%s/', [code])
end;


//returnes images links
procedure GetImages;
begin
	if photo_url<>'' then
		SetImageName('', photo_url, '', 6);
	//GetPhotos;
	//GetGoogleImage;
end;

function GetGoogleImage: boolean;
var
	all, url: string;
	i, j: integer;
begin
	url:=format('http://images.google.com/images?svnum=20&hl=en&lr=&safe=on&c2coff=1&q=%%22%s%%22', [person]);
	url:=ReplaceStr(url, ' ', '+', -1);
	all:=DownloadURL(url);
	i:=pos('dyn.Img(', all);
	if i>0 then begin
		i:=MyPos('","', all, i+1);
		i:=MyPos('","', all, i+1);
		i:=MyPos('","', all, i+1) + 3;
		j:=MyPos('","', all, i+1);
		if j>0 then begin
			url:=copy(all, i, j-i);
			SetImageName('', url, '', 6);
		end;
	end;
end;

function SavePhoto(url, FileName: string): boolean;
var
	fs: TFileStream;
	s: string;
begin
	s:=DownloadURL(url);
	if (length(s)>0) and (s<>'-1') then begin
		try
			fs:=TFileStream.Create(FileName, fmCreate);
			fs.Write(s, Length(s));
			fs.Free;
			result:=true;
		except
			result:=false;
		end;
	end
	else
		result:=false;
end;

function MovePrefixLeft(name: string): string;
var
	i, j: integer;
begin
	// replace "The", "A", "An"... to the left
	for j:=1 to PCount do
	begin
		i:=pos(', '+Prefixes[j], name);
		if i>0 then
			if (i = length(name)-length(Prefixes[j])-1) or
			(length(name) >= i+2+length(Prefixes[j])) and (name[i+2+length(Prefixes[j])]=':') then
			begin
				if Prefixes[j][length(Prefixes[j])] = '''' then
					name := Prefixes[j] + copy(name, 1, i-1) + copy(name, i+length(Prefixes[j])+2, length(name)-i-length(Prefixes[j])-2+1)
				else
					name := Prefixes[j]+' ' + copy(name, 1, i-1) + copy(name, i+length(Prefixes[j])+2, length(name)-i-length(Prefixes[j])-2+1);
				break;
			end;
	end;
	Result := name;
end;

function ClearBrackets(name: string): string;
var
	i: integer;
begin
	// clear word in brackets at the end of name
	i := pos(' (', name);
	if i>0 then
		name := copy(name, 1, i-1);
	Result := name;
end;

function ClearExtraQuotes(name: string): string;
begin
	// Clears extra quotes «"»
	if length(name)>0 then
		if (name[1]='"') and (name[length(name)]='"') then
			name := copy(name,2,length(name)-2);
	Result := name;
end;

function GetStr(str: String; s1: String; s2: String): String;
var
  I: Integer;
  J: Integer;
begin
  // Âûðåçàåò êóñîê ñòðîêè ìåæäó s1 è s2
	Result := '';
	I := Pos(LowerCase(s1), LowerCase(str));
	if I > 0 then
	begin
		J := MyPos(LowerCase(s2), LowerCase(str), I + Length(s1));
		if J > 0 then
  		Result := Copy(str, I + Length(s1), J - I - Length(s1));
	end;
end;


function GetInfo(url: string; images_only: boolean): string;
var
	s, s2, s3, s4: string;
	i, j, k, l, pages, mode, actor_start, next_page: integer;
	to_actor: boolean;
begin
	result:='';
	BasicURL:=url;
	person_code:=ParseString(url, 'http://www.film.tv.it/persona/(\d+)/', 1);
	person_site_url:=GetPersonSiteURL(person_code);
	url := url + person_code+'/filmografia';
	all:=DownloadURL(url);
	if all='-1' then
		result:='-1'
	else
		result:=all;
	//name
	i:=pos('<h1>', all);
	j:=pos('</h1>', all);
	person:=copy(all, i+4, j-i-4);
	i := Pos('Filmografia', person);
	if i>0 then
		delete(person, 1, 15);
	person := DecodeHTML(person);

	original_name:='';
	
	pages:=1;
	i:=pos('<div class="paginazione">', all);
	if i>0 then begin
		j:=MyPos('</div>', all, i+1);
		s2:=copy(all, i, j-i);
		i:=pos('<a href="/persona/', s2);
		while i>0 do begin
			pages:=pages+1;
			delete(s2, 1, i+1);
			i:=pos('<a href="/persona/', s2);
		end;
		//compensate for the 'avanti' hyperlink
		pages:=pages-1;
	end;
	if pages>1 then next_page:=2;
	
	//filmography - directors and actors

	i:=pos('<h4>Come regista</h4>', all);
	mode:=1;
	if i=0 then begin
		i:=pos('<h4>Come attore</h4>', all);
		mode:=3;
	end;
	if i>0 then begin
		while pages>0 do begin
			if (next_page>1) and (mode=1) then begin
				actor_start:=pos('<h4>Come attore</h4>', all)+1;
				if i<actor_start then
					to_actor:=true
				else begin
					if actor_start>1 then begin
						mode:=3;
						i:=actor_start;
					end;
				end;
			end;
			j:=MyPos('<!-- fine scheda -->', all, i+1);
			k:=pos('<h4>Come attore</h4>', all);
			if (k>0) and (k<j) and (to_actor)
					then j:=k;
			s2:=copy(all, i, j-i);
			k:=pos('<dt><a href="/film/', s2);
			while k>0 do begin
				l:=MyPos('</dt>', s2, k+1);
				s3:=copy(s2, k, l-k);
				code:=ParseString(s3, '<dt><a href="/film/(\d+)/.*?', 1);
				name:=ParseString(s3, '">(.*?)</a>', 1);
				name:=DecodeHTML(name);
				year:=ParseString(s3, ' (\d+)\)</span>', 1);
				if name<>'' then
					SetMovieName(name, '', mode, code, year);
				delete(s2, 1, l-1);
				k:=pos('<dt><a href="/film/', s2);
			end;
			if to_actor then begin
				i:=actor_start;
				mode:=3;
				to_actor:=false;
			end
			else begin
				pages:=pages-1;
				s4:='http://www.film.tv.it/persona/'+person_code+ '/' + person_code +'/filmografia/?p='+IntToStr(next_page);
				if pages>0 then
					all:=DownloadURL(s4);
				next_page:=next_page+1;
				i:=pos('<dt><a href="/film/', all);
			end;
		end;	
	end;
end;

	
//get movie site url
function GetMovieSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else
		result:='http://www.film.tv.it/film/'+code+'/';
end;

//get person site url
function GetPersonSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else	
		result:=format('http://www.film.tv.it/persona/%s/', [code]);
end;

begin
monthes[1]:='Janvier';
monthes[2]:='Février';
monthes[3]:='Mars';
monthes[4]:='Avril';
monthes[5]:='Mai';
monthes[6]:='Juin';
monthes[7]:='Juillet';
monthes[8]:='Août';
monthes[9]:='Septembre';
monthes[10]:='Octobre';
monthes[11]:='Novembre';
monthes[12]:='Décembre';

prefixes[1]:='The';
prefixes[2]:='A';
prefixes[3]:='An';
prefixes[4]:='La';
prefixes[5]:='Les';
prefixes[6]:='Des';
prefixes[7]:='L''';
prefixes[8]:='Un';
prefixes[9]:='El';
prefixes[10]:='Il';
prefixes[11]:='Le';
prefixes[12]:='Uno';

end.