const
	script_name='35mm';
	language='Italian';
	version=3;
	author='Vlad';
	MinProgramBuild=5500;
	PCount = 13;
	//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, pics_code: string;
	person_height: integer;
	DownloadPersonBiography, DownloadPersonAdditionalInfo: boolean;
	MaxPhotosCount: integer;

function GetSearchURLbyName(PersonName: string): string;
var
	url: string;
begin
	url:='http://personaggi-film.35mm.it/search/?string=' + PersonName + '&year=0&month=0&day=0&sections%5B%5D=star&page_star=1&open_tab=0';
	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:='';
	i:=pos('<!-- Start result star -->', all);
	if i>0 then begin
		j:=MyPos('<!-- End result star -->', all, i+1);
		s:=copy(all, i, j-i);
	end;
	if (s='') or (pos('La ricerca non ha individuato alcuna star', all)>0) then
		//Person not found
	else begin
		i := pos('<li>',s);
		while i>0 do begin
			j := MyPos('</a>', s, i+1);
			s2 := copy(s, i, j-i);
			person_code:=ParseString(s2, 'href="http://personaggi-film.35mm.it/(.*?).html" title="Scheda', 1);
			name:=ParseString(s2, 'title="Scheda (.*?)">', 1);
			name:=DecodeHTML(name);
			name:=ClearExtraSpaces(name);
			if name <> '' then begin
				persons_count:=persons_count+1;
				SetSearchResult(name, person_code);
			end;
			delete(s, 1, j+4);
			i:=pos('<li>', s);
		end;
	end;
	result:=persons_count;
end;

function GetPersonURL(code: string; images_only: boolean): string;
begin
		result:=format('http://personaggi-film.35mm.it/%s.html', [code])
end;


procedure GetPhotos;
var
	s, s2, s3, img_code, pic_url: string;
	img_count, i, j: integer;
begin
	img_count := 1;
	s := 'http://personaggi-film.35mm.it/'+person_code+'/foto.html';
	i := Pos(s, all);
	if i>0 then begin
		all := DownloadURL(s);
		i := Pos('<div id="gallery_description">Le foto', all);
		if i>0 then begin
			j := MyPos('</ul>', all, i+1);
			s2 := copy(all, i, j-i);
			i := Pos('<li ', s2);
			while (i>0) and (img_count<=MaxPhotosCount) do begin
				j := MyPos('</li>', s2, i+1);
				s3 := copy(s2, i, j-i);
				img_code := ParseString(s3, '<img src="(.*?)" style', 1);
				if img_code > '' then begin
					img_count := img_count + 1;
					img_code := ReplaceStr(img_code, ' ', '%20', -1);
					pic_url := 'http://personaggi-film.35mm.it' + img_code;
					SetImageName('', pic_url, '', 3);
				end;
				delete(s2, 1, j);
				i := Pos('<li ', s2);
			end;
		end;
	end;
					
end;

//returnes images links
procedure GetImages;
begin
	if photo_url <> '' then
		SetImageName('', photo_url, '', 6);
	if MaxPhotosCount > 0 then
		GetPhotos;
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 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 ClearExtraBlankCharacters(text: string): string;
begin
	// Clear all extra blank charackters
	while (length(text) > 0) and (ord(text[1]) in [1..20, 32]) do
		delete(text, 1, 1);
	while (length(text) > 0) and (ord(text[length(text)]) in [1..20, 32]) do
		delete(text, length(text), 1);
	Result := text;
end;

function ExtractMovies(text: string; start, mode: integer): string;
var
	i, j, k, l: Integer;
	s, s2, title, code, character, year: string;
begin
	i := MyPos('<li class="list_type">', text, start + 1);
	if i=0 then
		i:=Length(text);
	if i>0 then begin
		s := copy(text, 1, i-1);
		delete(text, 1, i-1);
		i := Pos('<li class="list_filmografy">', s);
		while (i>0) do begin
			j := MyPos('</li>', s, i+1);
			s2 := copy(s, i, j-i);
			code:=ParseString(s2, '<a href="http://film.35mm.it/(.*?).html" title="', 1);
			title:=ParseString(s2, 'titolo">(.*?)</a>', 1);
			title:=DecodeHTML(title);
			
			//character
			character := '';
			k := Pos('( ruolo:', s2);
			if k>0 then begin
				l := MyPos(')', s2, k+1);
				character := copy(s2, k+8, l-k-8);
				character := ClearExtraBlankCharacters(character);
				character := DecodeHTML(character);
			end;
			
			//year
			year := copy(code, Length(code)-3, 4);
			if title>'' then
				SetMovieName(title, character, mode, code, year);
				
			delete(s, 1, j);
			i := Pos('<li class="list_filmografy">', s);
		end;
	end;
	result := text;
			
end;


function GetInfo(url: string; images_only: boolean): string;
var
	s, s2, s3, s4, day, month, year: string;
	i, j, k, l, mode: integer;
begin
	result:='';
	BasicURL:=url;
	person_code:=ParseString(url, 'http://personaggi-film.35mm.it/(.*?).html', 1);
	person_site_url:=GetPersonSiteURL(person_code);
	all:=DownloadURL(url);
	if all='-1' then
		result:='-1'
	else
		result:=all;
	
	//name
	person:=ParseString(all, '<h2>(.*?)</h2>', 1);
	person:=ReplaceStr(person, '  ', ' ', -1);
	person := DecodeHTML(person);

	//original name
	original_name:='';
	
	//birth place
	i:=Pos('Luogo di nascita:', all);
	if i>0 then begin
		j:=MyPos('</li>', all, i+1);
		s:=copy(all, i+17, j-i-17);
		s:=RemoveTags(s);
		s:=DecodeHTML(s);
		birthplace:=ClearExtraBlankCharacters(s);
	end;
	
	//birth place and date
	i:=Pos('Data di nascita:',  all);
	if i>0 then begin
		j:=MyPos('</li>', all, i+1);
		s:=copy(all, i+16, j-i-16);
		s:=RemoveTags(s);
		day:=ParseString(s, '^ (\d+)\/', 1);
		month:=ParseString(s, '\/(\d+)\/', 1);
		i:=Pos('/', s);
		while i>0 do begin
			delete(s, 1, i);
			i:=Pos('/', s);
		end;
		year := s;
		birth_date:=month+'/'+day+'/'+year;
	end;
	
	//death date
	i:=Pos('Data di morte:', all);
	if i>0 then begin
		j:=MyPos('</li>', all, i+1);
		s:=copy(all, i+14, j-i-14);
		s:=RemoveTags(s);
		day:=ParseString(s, '^ (\d+)\/', 1);
		month:=ParseString(s, '\/(\d+)\/', 1);
		i:=Pos('/', s);
		while i>0 do begin
			delete(s, 1, i);
			i:=Pos('/', s);
		end;
		year := s;
		death_date:=month+'/'+day+'/'+year;
	end;
	
	//biography
	if DownloadPersonBiography then begin
		i:=Pos('<div class="description">', all);
		if i>0 then begin
			j:=MyPos('</div>', all, i+1);
			s:=copy(all, i, j-i);
			i := Pos('<p ', s);
			if i>0 then 
				delete(s, 1, i-1);
			s:=RemoveTags(s);
			s:=DecodeHTML(s);
			biography:=ClearExtraBlankCharacters(s);
			if biography='biografia del personaggio non inserita' then
				biography:='';
		end;
	end;
	
	//photo image
	i:=Pos('<img src="/files/star/', all);
	if i=0 then
		i:=Pos('<img src="/immagini/personaggi/', all);
	if i>0 then begin
		j:=MyPos('"', all, i+20);
		s:=copy(all, i+10, j-i-10);
		photo_url:='http://personaggi-film.35mm.it'+s;
	end;
	
	//download filmography
	s := 'http://personaggi-film.35mm.it/'+person_code+'/filmografia-compatta.html';
	all := DownloadURL(s);
	i := Pos('<li class="list_type">', all);
	if i>0 then begin
		j:=MyPos('</ul>', all, i+1);
		s:=copy(all, i, j-i);
		i := Pos('<li class="list_type">Attore</li>', s);
		if i>0 then
			s := ExtractMovies(s, i, 3);
		i := Pos('<li class="list_type">Regia</li>', s);
		if i>0 then
			s := ExtractMovies(s, i, 1);
		i := Pos('<li class="list_type">Sceneggiatura</li>', s);
		if i>0 then
			s := ExtractMovies(s, i, 2);
		i := Pos('<li class="list_type">Musiche</li>', s);
		if i>0 then
			s := ExtractMovies(s, i, 4);
	end;	
end;

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

//get person site url
function GetPersonSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else	
		result:=format('http://personaggi-film.35mm.it/%s.html', [code]);
end;

begin

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.