const
	script_name='Screenrush';
	language='English';
	version=1;
	author='Vlad';
	MinProgramBuild=2120;
	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.screenrush.co.uk/recherche/?motcle=%s&rub=2', [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:='';	
	i:=pos('>Celebs <h4>', all);
	if i>0 then begin
		j:=MyPos('</tr></table></td></tr></table>', all, i+1);
		if j>0 then
			s:=copy(all, i, j-i);
	end;
	
	if (s='') or (pos('we have no matches for your search', all)>0) then
		//Person not found
	else begin
	  i:=pos('<a href="/personne/', s);
	  while i>0 do begin
			persons_count:=persons_count+1;
			SetLength(persons, persons_count);
			j:=pos('<hr />', s);
			s2:=copy(s, i, j-i);
			
			person_code:=ParseString(s2, '<a href="/personne/fichepersonne_gen_cpersonne=(\d+).html">', 1);
			person_site_url:=GetPersonSiteURL(person_code);

			//improve readability
			k:=pos('<div', s2);
			while k>0 do begin
				delete(s2, k, 4);
				insert(' - <', s2, k);
				k:=pos('<div', s2);
			end;
			
			name:=RemoveTags(s2);
			name:=DecodeHTML(name);
			name:=ClearExtraSpaces(name);
			while (length(name)>0) and (name[1]=' ') do
				delete(name, 1, 1);
			SetSearchResult(name, person_code);
			delete(s, 1, j+6);
			i:=pos('<a href="/personne/', s);
		end;
	end;
	result:=persons_count;
end;

function GetPersonURL(code: string; images_only: boolean): string;
begin
	result:=format('http://www.screenrush.co.uk/personne/fichepersonne_gen_cpersonne=%s.html', [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 MyRemoveTags(s: string): string;
var
	i, j: integer;
begin
	i:=pos('<', s);
	while i>0 do begin
		if i=MyPos('<br />', s, i) then
			i:=MyPos('<', s, i+1)
		else begin
			j:=MyPos('>', s, i+1);
			delete(s, i, j-i+1);
			i:=MyPos('<', s, i);
		end;
	end;
	result:=s;

end;

function ExtractDate(s: string): string;
var
	i, j: integer;
	day, month, year, formatted_date: string;
begin
	formatted_date:='';
	day:=ParseString(s, ' (\d+),', 1);
	if day='' then
		//no date
	else begin
		month:=ParseString(s, '^ (.*?) \d+', 1);
		if month<>'' then
			year:=ParseString(s, ', (\d+)$', 1);
		i:=1;
		while (i<=12) and (monthes[i]<>month) do begin	
			i:=i+1;
		end;
		if (i<=12) and (month<>'') then
			formatted_date:=IntToStr(i)+format('/%s/%s', [day, year]);
	end;
	result:=formatted_date;
end;

function GetInfo(url: string; images_only: boolean): string;
var
	s, s2, s3, s4: string;
	i, j, k, ke, mode: integer;
	pictures_exist: boolean;
begin
	result:='';
	BasicURL:=url;
	person_code:=ParseString(url, 'http://www.screenrush.co.uk/personne/fichepersonne_gen_cpersonne=(\d+).html', 1);

	all:=DownloadURL(url);
	if all='-1' then
		result:='-1'
	else
		result:=all;


	//name
	person := ParseString(all, '<title>(.*?)</title>', 1);
	person := DecodeHTML(person);

	original_name:='';
	//biography
	i:=pos('>Biography</h2>', all);
	if i>0 then begin
		i:=MyPos('<h4>', all, i+1);
		j:=MyPos('</h4>', all, i+1);
		s2:=copy(all, i+4, j-i-4);
		biography:=MyRemoveTags(s2);
		biography:=DecodeHTML(biography);
		biography:=ClearExtraSpaces(biography);
	end;
	//photo
	photo_url:=ParseString(all, '<table width="100%" cellpadding="0" cellspacing="0" border="0" style="padding:10 0 10 0"><tr><td valign="top" style="padding:0 10 10 0" width="120"><img src="(.*?)" width=', 1);
	
	//Galerie
	pictures_exist:=false;
	i:= pos('<div  align="center"><a href="/personne/galerievignette_gen_cpersonne', all);
	if i>0 then
		pictures_exist:=true;
	
	//birthplace
	s2:=ParseString(all, '<h4><div style="padding:10 0 0 0">(.*?)<h4>', 1);
	i:=Pos('<br><br>', s2);
	if i=0 then
		i:=Pos('<br />', s2);
	if i>0 then
		delete(s2, i, length(s2)-i+1);
	i:=pos(' in ', s2);
	if i>0 then begin
		birthplace:=copy(s2, i+4, length(s2)-4);
		birthplace:=DecodeHTML(birthplace);
		//date of birth
		s3:=copy(s2, 5, i-5);
		birth_date:=ExtractDate(s3);
//		s3:=copy(s2, j, length(s2)-j+1);
//		death_date:=ExtractDate(s3);
	end;

	//loading filmography
	url:='http://www.screenrush.co.uk/personne/filmographie_gen_cpersonne='+person_code+'.html';
	all:=DownloadURL(url);
	
	//filmography - director
	i:=pos('Director</b></h3>', all);
	if i>0 then begin
		j:=MyPos('</table>', all, i+1);
		s2:=copy(all, i, j-i);
		i:=pos('<h4><a href="/film/fichefilm_gen_cfilm=', s2);
		while i>0 do begin
			j:=MyPos('<h4>', s2, i+4);
			if j=0 then
				j:=length(s2);
			s3:=copy(s2, i, j-i);
			code:=ParseString(s3, '<a href="/film/fichefilm_gen_cfilm=(\d+).html', 1);
			name:=ParseString(s3, '<b>(.*?)</b>', 1);
			name:=DecodeHTML(name);
			year:=ParseString(s3, '\((\d+)\)', 1);
			if name<>'' then
				SetMovieName(name, '', 1, code, year);
			delete(s2, 1, j-1);
			i:=pos('<h4><a href="/film/fichefilm_gen_cfilm=', s2);
		end;
	end;
	//filmography - writer
	i:=pos('Writer</b></h3>', all);
	if i>0 then begin
		j:=MyPos('</table>', all, i+1);
		s2:=copy(all, i, j-i);
		i:=pos('<h4><a href="/film/fichefilm_gen_cfilm=', s2);
		while i>0 do begin
			j:=MyPos('<h4>', s2, i+4);
			if j=0 then
				j:=length(s2);
			s3:=copy(s2, i, j-i);
			code:=ParseString(s3, '<a href="/film/fichefilm_gen_cfilm=(\d+).html', 1);
			name:=ParseString(s3, '<b>(.*?)</b>', 1);
			name:=DecodeHTML(name);
			year:=ParseString(s3, '\((\d+)\)', 1);
			if name<>'' then
				SetMovieName(name, '', 2, code, year);
			delete(s2, 1, j-1);
			i:=pos('<h4><a href="/film/fichefilm_gen_cfilm=', s2);
		end;
	end;

	//filmography - actor
	i:=pos('<b>Actor</b></h3>', all);
	if i=0 then
		i:=pos('<b>Actress</b></h3>', all);
	if i>0 then begin
		j:=MyPos('</table>', all, i+1);
		s2:=copy(all, i, j-i);
		i:=pos('<h4><a href="/film/fichefilm_gen_cfilm=', s2);
		while i>0 do begin
			j:=MyPos('<h4>', s2, i+4);
			if j=0 then
				j:=length(s2);
			s3:=copy(s2, i, j-i);
			code:=ParseString(s3, '<a href="/film/fichefilm_gen_cfilm=(\d+).html', 1);
			name:=ParseString(s3, '<b>(.*?)</b>', 1);
			name:=DecodeHTML(name);
			year:=ParseString(s3, '\((\d+)\),', 1);
			character:=ParseString(s3, '<h5>(.*?)</h5>', 1);
			character:=DecodeHTML(character);
			if name<>'' then
				SetMovieName(name, character, 3, code, year);
			delete(s2, 1, j-1);
			i:=pos('<h4><a href="/film/fichefilm_gen_cfilm=', s2);
		end;
	end;
	
	//filmography - composer
	i:=pos('<b>Composer</b></h3>', all);
	if i>0 then begin
		j:=MyPos('</table>', all, i+1);
		s2:=copy(all, i, j-i);
		i:=pos('<h4><a href="/film/fichefilm_gen_cfilm=', s2);
		while i>0 do begin
			j:=MyPos('<h4>', s2, i+4);
			if j=0 then
				j:=length(s2);
			s3:=copy(s2, i, j-i);
			code:=ParseString(s3, '<a href="/film/fichefilm_gen_cfilm=(\d+).html', 1);
			name:=ParseString(s3, '<b>(.*?)</b>', 1);
			name:=DecodeHTML(name);
			year:=ParseString(s3, '\((\d+)\)', 1);
			if name<>'' then
				SetMovieName(name, '', 4, code, year);
			delete(s2, 1, j-1);
			i:=pos('<h4><a href="/film/fichefilm_gen_cfilm=', s2);
		end;
	end;

	//if photo is not present
	if (photo_url='') and (pictures_exist) then begin
		url:='http://www.screenrush.co.uk/personne/galerievignette_gen_cpersonne='+person_code+'.html';
		all:=DownloadURL(url);
		s2:=ParseString(all, '<img id=''imgNormal'' class=''photo'' src=''(.*?)'' title=', 1);
		if s2<>'' then
			photo_url:=s2;
	end;
	
	
end;

//get movie site url
function GetPersonSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else
		result:='http://www.screenrush.co.uk/personne/fichepersonne_gen_cpersonne='+code+'.html';
end;

//get movie site url
function GetMovieSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else
		result:='http://www.screenrush.co.uk/film/fichefilm_gen_cfilm='+code+'.html';
end;

begin
monthes[1]:='January';
monthes[2]:='February';
monthes[3]:='March';
monthes[4]:='April';
monthes[5]:='May';
monthes[6]:='June';
monthes[7]:='July';
monthes[8]:='August';
monthes[9]:='September';
monthes[10]:='October';
monthes[11]:='November';
monthes[12]:='December';

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.