const
	script_name='iafd';
	language='English';
	version=4;
	author='Vlad/JeF';
	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.iafd.com/results.asp?searchtype=name&searchstring=%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:='';
	i:=pos('None Found.', all);
	if (all='') or (i>0) then
		// Person not found!
	else begin
		i:=Pos('person.rme/perfid=', all);
		while i>0 do begin
			persons_count:=persons_count+1;
			SetLength(persons, persons_count);
			j:=MyPos('</dd>', all, i+1);
			s:=copy(all, i, j-i);
			person_code:=ParseString(s, 'person.rme/perfid=(.*?).htm">', 1);
			name:=ParseString(s, 'htm">(.*?)</a>', 1);
			s2:=ParseString(s, '<dd>(.*?)$', 1);
			if s2<>'' then
				name:=name+' - '+s2;
			SetSearchResult(name, person_code);
			delete(all, 1, j);
			i:=pos('person.rme/perfid=', all);
		end;
	end;
	result:=persons_count;
end;

function GetPersonURL(code: string; images_only: boolean): string;
begin
		result:=format('http://www.iafd.com/person.rme/perfid=%s.htm', [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=off&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;

procedure ExtractMovies(text: string; mode: integer);
var
	i, j, k: integer;   //20100112-JeF
	s,note: string;   //20100106-JeF
begin
	i:=Pos('href="/title.rme/title=', text);
	while i>0 do begin
		j:=MyPos('</td><td>', text, i+1);
		k:=MyPos('href="/title.rme/title=', text, i+10);   //20100113-JeF
		if (pos('</i>',copy(text,1,k))>0) then j:=MyPos('</i></td><td>', text, i+1)+9;   //20100113-JeF
		s:=copy(text, i, j-i);
		//20100112-JeF// name:=ParseString(s, '.htm">(.*?)</a>', 1);   
		name:=ParseString(s, '.htm">(.*?)</a></b>', 1);   //20100112-JeF
		code:=ParseString(s, 'href="/title.rme/title=(.*?).htm">', 1);
		year:=ParseString(s, '/year=(\d+)/', 1);
		note:=DecodeHTML(ParseString(s, '<td><i>(.*?)</i></td>', 1));   //20100106-JeF
		if name<>'' then
			//20100106-JeF// SetMovieName(name, '', mode, code, year);
			SetMovieName(name, note, mode, code, year);   //20100106-JeF
		delete(text, 1, j);
		i:=Pos('href="/title.rme/title=', text);
	end;

end;


function GetInfo(url: string; images_only: boolean): string;
var
	s, s2, day, month, year: string;
	i, j, k: integer;
begin
	result:='';
	BasicURL:=url;
	person_code:=ParseString(url, 'http://www.iafd.com/person.rme/perfid=(.*?).htm', 1);
	person_code:=LowerCase(person_code);
	person_site_url:=GetPersonSiteURL(person_code);
	all:=DownloadURL(url);
	if all='-1' then
		result:='-1'
	else
		result:=all;
	//name
	person:=ParseString(all, '<h1>(.*?)'#13, 1);
	person := DecodeHTML(person);

	original_name:='';
	
	//birth date
	i:=Pos('<b>Birthday</b>', all);
	if i>0 then begin
		j:=MyPos('</a>', all, i+1);
		s:=copy(all, i, j-i);
		day:=ParseString(s, 'calday=(\d+)"', 1);
		month:=ParseString(s, 'calmonth=(\d+)', 1);
		year:=ParseString(s, ', (\d+)$', 1);
		if (day<>'') and (month<>'') and (year<>'') then
			birth_date:=format('%s/%s/%s', [month, day, year]);
	end;
	
	//birthplace
	birthplace:=ParseString(all, '<b>Birthplace</b></td><td>(.*?)&nbsp;</td>', 1);
	if Pos('No data', birthplace)>0 then birthplace:='';
	
	//also known as
	i:=Pos(#9#9'AKA</b>', all);
	if i>0 then begin
		j:=MyPos('</tr>', all, i+1);
		s:=copy(all, i, j-i);
		s2:=ParseString(s, '<td>(.*?)</td>', 1);
		if (s2<>'') and (s2<>'No known aliases')  then
			description:='Also known as: '+s2+#13#10;
	end;
	
	//about
	i:=Pos('<b>Years Active</b>', all);
	if i=0 then
		i:=Pos('<b>Year Active</b>', all);
	if i>0 then begin
		j:=MyPos('<b>Website</b>', all, i+1);
		s:=copy(all, i, j-i);
		s:=ReplaceStr(s, '</td><td>', ': ', -1);
		s:=ReplaceStr(s, '</tr>', #13#10, -1);
		description:=description+RemoveTags(s);
	end;
	
	//photo
	s:=ParseString(all, 'src="/graphics/headshots/(.*?)">', 1);
	if (s<>'') and (s<>'no_photo.gif') then
		photo_url:='http://www.iafd.com/graphics/headshots/' + s;
		
	//filmography
	i:=Pos('<a name="perf">', all);
	if i=0 then
		i:=Pos('<a name=perf>', all);
	if i>0 then begin
		j:=MyPos('</table>', all, i+1);
		s:=copy(all, i, j-i);
		ExtractMovies(s, 3);
	end;
	
	i:=Pos('<a name="dir">', all);
	if i=0 then
		i:=Pos('<a name=dir>', all);
	if i>0 then begin
		j:=MyPos('</table>', all, i+1);
		s:=copy(all, i, j-i);
		ExtractMovies(s, 1);
	end;

end;

	
//get movie site url
function GetMovieSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else
		result:='http://www.iafd.com/title.rme/title='+LowerCase(code)+'.htm';
end;

//get person site url
function GetPersonSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else
		result:='http://www.iafd.com/person.rme/perfid='+LowerCase(code)+'.htm';
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.