const
	script_name='Mymovies.it';
	language='Italian';
	version=13;
	author='Vlad';
	MinProgramBuild=6100;
	PCount = 12;
	//MAX_PHOTOS_COUNT = 3;
var
	DownloadPersonBiography, DownloadPersonAdditionalInfo: boolean;
	MaxPhotosCount: integer;
	
	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;
	bio_all: string;
	
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 GetSearchURLbyName(PersonName: string): string;
var
	url: string;
begin
	PersonName := EncodeURL(PersonName, 'iso-8859-1');
	url:=format('http://www.mymovies.it/database/ricerca/?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:='';
	j:=0;
	i:=Pos('seguenti risultati:', all);
	if i>0 then begin
		i:=MyPos('Persone</div>', all, i+1);
		if i>0 then
			j:=MyPos('<div style="height:20px; clear:both;"></div>', all, i+1);
		s:=copy(all, i, j-i);
	end;
	
	//not found
	if (s='') or (pos('<h1>Ricerca libera</h1>', all)>0) then
	
	else begin
		i:=pos('<h3>', s);
		while i>0 do begin
			j:=MyPos('</h3>', s, i+1);
			s2:=copy(s, i, j-i);
			persons_count:=persons_count+1;
			SetLength(persons, persons_count);
			person_code:=ParseString(s2, '<a href="http://www.mymovies.it/biografia/\?(.*?)"', 1);
			name:=RemoveTags(s2);
			name:=DecodeHTML(name);
			if name<>'' then
				SetSearchResult(name, person_code);
			k:=MyPos('<div style="clear: both; height: 10px;"></div>', s, i+1);
			if k>j then
				j:=k;
			delete(s, 1, j+5);
			i:=pos('<h3>', s);
		end;
	end;
	result:=persons_count;
end;

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


//returnes images links
procedure GetImages;
var
	i, j, photo_cnt: Integer;
	s: String;
begin
	if photo_url<>'' then
		SetImageName('', photo_url, '', 6);
	if MaxPhotosCount>1 then begin
		photo_cnt := 1;
		i := Pos('http://pad.mymovies.it/cinemanews/', bio_all);
		while (i>0) and (photo_cnt < MaxPhotosCount) do begin
			j := MyPos('_imm.jpg', bio_all, i+1);
			s := copy(bio_all, i, j-i);
			s := s + '.jpg';
			SetImageName('', s, '', 6);
			photo_cnt := photo_cnt + 1;
			delete(bio_all, 1, j);
			i := Pos('http://pad.mymovies.it/cinemanews/', bio_all);
		end;
	end;
	//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 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 MyRemoveTags(s: string): string;
var
	i, j: integer;
begin
	i:=pos('<', s);
	while i>0 do begin
		if i=MyPos('<br />', s, i) then begin
			delete(s, i+6, 1);
			i:=MyPos('<', s, i+1);
		end
		else begin
			j:=MyPos('>', s, i+1);
			delete(s, i, j-i+1);
			i:=MyPos('<', s, i);
		end;
	end;
	result:=s;

end;

function ClearExtraBlankCharacters(text: string): string;
begin
	// Clear all extra blank charackters
	while (length(text) > 0) and (ord(text[1]) in [1..32]) do
		delete(text, 1, 1);
	while (length(text) > 0) and (ord(text[length(text)]) in [1..32]) do
		delete(text, length(text), 1);
	Result := text;
end;


function GetInfo(url: string; images_only: boolean): string;
var
	s, s2, s3, s4, day, month, year, next_page, mov_title, mov_code: string;
	i, j, k, l, mode, page: integer;
begin
	result:='';
	BasicURL:=url;
	person_code:=ParseString(url, 'http://www.mymovies.it/biografia/\?(.*?)$', 1);
	person_site_url:=GetPersonSiteURL(person_code);
	all:=DownloadURL(url);
	if all='-1' then
		result:='-1'
	else
		result:=all;
	//name
	person := ParseString(all, '<title>(.*?) \| MYmovies</title>', 1);
	person := DecodeHTML(person);

	//original name
	original_name:= ParseString(all, '<em>Nome</em>: (.*?)<br', 1);
	original_name:= DecodeHTML(original_name);
	original_name:= ClearExtraBlankCharacters(original_name);
	
	//biography
	i:=pos('<p ', all);
	if i>0 then begin
		j:=MyPos('<script>', all, i+1);
		if j=0 then
			j:=MyPos('</p>', all, i+1);
		s2:=copy(all, i, j-i);
		biography:=MyRemoveTags(s2);
		biography:=ClearExtraBlankCharacters(biography);
		biography:=DecodeHTML(biography);
		biography:=ClearExtraSpaces(biography);
	end;
	
	//Date of birth
	i:=Pos('anni</a>', all);
	if i=0 then
		i:=Pos('Data nascita: <', all);
	if i>0 then begin
		j:=MyPos('</div>', all, i+1);
		s:=copy(all, i, j-i);
		s2:=ParseString(s, 'persone/data/\?data=(.*?)"', 1);
		day:=ParseString(s2, '^(\d+)_', 1);
		month:=ParseString(s2, '_(\d+)$', 1);
		year:=ParseString(s, 'persone/data/\?anno=(\d+)"', 1);
		if (day<>'') and (month<>'') and (year<>'') then
			birth_date:=month+'/'+day+'/'+year;
		//Place of birth
		birthplace:=ParseString(s, '<a href="http://www.mymovies.it/persone/luogonascita/\?luogo=.*?">(.*?)</span>', 1);
		birthplace:=RemoveTags(birthplace);
	end;

	//Date of death
	death_date := '';
	s := GetStr(all, 'Data morte: ', ' (');
	s := ClearExtraBlankCharacters(s);
	if s <> '' then begin
		day := ParseString(s, '^(\d+)', 1);
		year := ParseString(s, '(\d+)$', 1);
		month := GetStr(s, ' ', ' ');
		i := 12;
		while (i > 0) and (monthes[i] <> month) do
			i := i - 1;
		if (day <> '') and (i > 0) and (year <> '') then
			death_date := IntToStr(i) + '/' + day + '/' + year
		else if year <> '' then
			death_date := year;
	end;
	
	//photo
	s:=ParseString(all, 'http://www.mymovies.it//filmclub/(.*?)"', 1);
	if s='' then begin
		s:=ParseString(all, 'http://www.mymovies.it/filmclub/attori/(.*?)"', 1);
		if s='' then
			s:=ParseString(all, 'http://pad.mymovies.it/filmclub/attori/(.*?)"', 1);
		if s<>'' then
			s:='attori/' + s;
	end;
	if s<>'' then
		photo_url:='http://www.mymovies.it/filmclub/'+s;
		
	//backup the current page to extract photos later
	bio_all := all;
	//load filmography
	s:='http://www.mymovies.it/filmografia/?'+person_code;
	all:=DownloadURL(s);
	repeat begin
		if all<>'-1' then begin
			i:=Pos('<span class="spagina">', all);
			if i>0 then begin
				j:=MyPos('<span class="spagina">', all, i+1);
				s:=copy(all, i, j-i);
				
				//detecting the next page (if present), creating URL for it
				next_page:='';
				page:=Pos('">p. successiva', s);
				if page>0 then begin
					page:=page-32;
					page:=MyPos('<a href="?page=', s, page);
					if page>0 then begin
						l:=MyPos('">', s, page+1);
						next_page:=copy(s, page+15, l-page-15);
						next_page:='http://www.mymovies.it/filmografia/?page='+next_page;
					end;
				end;
				
				//extracting movies on the current page
				i:=Pos('<h2>', s);
				while i>0 do begin
					j:=MyPos('<div style="clear:both; height:20px;"></div>', s, i+1);
					s2:=copy(s, i, j-i);
					mov_code:=ParseString(s2, '<a href="http://www.mymovies.it/dizionario/recensione.asp\?id=(\d+)"', 1);
					s3:=ParseString(s2, '<h2>(.*?)</h2>', 1);
					if mov_code = '' then
						mov_code := ParseString(s3, '<a href="http://www.mymovies.it/film/(.*?)/" title', 1);
					mov_title:=ParseString(s2, '<h2>.*?>(.*?)</a>', 1);
					year:=ParseString(s2, 'href="http://www.mymovies.it/film/\?anno=(\d+)">', 1);
					if (mov_title<>'') and (mov_code<>'') then begin
						k:=Pos('[', mov_title);
						if k>0 then
							delete(mov_title, k, length(mov_title)-k+1);
						k:=Pos('Un film di', s2);
						if k>0 then begin
							l:=MyPos(' Con ', s2, k+1);
							if l=0 then
								l:=length(s2);
							s3:=copy(s2, k, l-k);
							if pos(person, s3)>0 then
								SetMovieName(mov_title, '', 1, mov_code, year)
							else
								SetMovieName(mov_title, '', 3, mov_code, year);
						end;
					end;
					delete(s, 1, j);
					i:=Pos('<h2>', s);
				end;
				
				//downloading the previously detected next page
				if next_page<>'' then
					all:=DownloadURL(next_page);
			end;
		end;
	end
	until next_page='';
end;

	
//get movie site url
function GetMovieSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else if (Pos('/', code)>0) then
		result := 'http://www.mymovies.it/film/' + code + '/'
	else
		result:='http://www.mymovies.it/dizionario/recensione.asp?id='+code;
end;

//get person site url
function GetPersonSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else	
		result:=format('http://www.mymovies.it/biografia/?%s', [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';

	monthes[1]:='Gennaio';
	monthes[2]:='Febbraio';
	monthes[3]:='Marzo';
	monthes[4]:='Aprile';
	monthes[5]:='Maggio';
	monthes[6]:='Giugno';
	monthes[7]:='Luglio';
	monthes[8]:='Agosto';
	monthes[9]:='Settembre';
	monthes[10]:='Ottobre';
	monthes[11]:='Novembre';
	monthes[12]:='Dicembre';

end.