const
	script_name='ofdb';
	language='German';
	version=11;
	author='Alex';
	MinProgramBuild=5500;
	PCount = 24;
var
	persons_count: integer;
	persons: array of string;
	imdb_code, person_code: string;
	person, biography, trivia, description, character: string;
	birth_date, death_date, birthplace, birth_name, original_name, person_site_url: string;
	photo_url: string;
	monthes: array [1..12] of string;
	prefixes: array [1..PCount] of string;
	person_height: integer;

function GetSearchURLbyName(PersonName: string): string;
var
	url: string;
begin
	PersonName := ClearExtraBlankCharacters(PersonName);
	PersonName := ReplaceStr(PersonName, ' ', '+', -1);
	PersonName := EncodeURL(PersonName, 'utf-8');
	url := format('http://www.ofdb.de/view.php?page=suchergebnis&neu=1&Kat=Person&SText=%s', [PersonName]);
	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 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 PersonSearch: integer;
var
	i, j, k: integer;
	person, code, s, s2, small_photo, birth_date, original_name: string;
	no_code, present: boolean;
	persons_with_codes: array of string;
begin
	persons_count := 0;
	
	i := pos('</b> <i>Keine Ergebnisse</i>', all);
	j := MyPos('</b> <i>Keine Ergebnisse</i>', all, i+1);
	k := MyPos('</b> <i>Keine Ergebnisse</i>', all, j+1);

	if (all='')	or ((i>0)and(j>0)and(k>0)) then
		// Person not found!
	else 
	begin
		s:='';
		i := pos('<b>Person:</b>', all);
		if i>0 then begin
			j := MyPos('</font></p><br>', all, i+1);
			if j>0 then
				s:=copy(all, i, j-i);
		end;
		
		if s<>'' then 
		begin
			i := pos('<a href="view.php?page=person&id=',s);
			if i<=0 then begin
				i := pos('<a href="view.php?page=liste&Name=',s);
				no_code := true;
			end else 
				no_code := false;
			if i>0 then
				delete(s, 1, i-1);
			
			while (i>0) do begin
				s2 := '';
				j := pos('</a><br>', s);
				if j > 0 then begin
					s2 := copy(s, 1, j-1);
					delete(s, 1, j);
				end;
				small_photo := GetStr(s2, 'images/person/', '.jpg');
				if small_photo <> '' then
					small_photo := 'http://www.ofdb.de/images/person/' + small_photo + '.jpg';
				
				code := '';
				if not no_code then
					code := GetTextBetween(s2, '&id=', '"')
				else
					code := GetTextBetween(s2, '&Name=', '"');
				
				i := pos('onmouseover="', s2);
				j := MyPos('"', s2, i+length('onmouseover="'));
				if (i>0)and(j>0) then
					delete(s2, i, j-i+1);
					
				original_name := DecodeHTML(GetStr(s2, '<i>alias</i> ', '<'));
				i := Pos('<i>alias</i>', s2);
				if i>0 then
					delete(s2, i, length(s2)-i+1);
				
				person := RemoveTags(s2);
				birth_date := '';
				i := Pos('(', person);
				if i>0 then begin
					birth_date := ParseString(person, '\((.*?)\)', 1);
					delete(person, i, length(person)-i+1);
				end;
				person := DecodeHTML(person);
				person := ClearExtraBlankCharacters(person);
				
				if (person<>'') and (code<>'') then 
				begin
					present := false;
					if no_code then 
					begin
						for i := 0 to Length(persons_with_codes)-1 do 
						begin
							if person = ClearBrackets(persons_with_codes[i]) then
							begin
								present := true;
								break;
							end;
						end;
					end;
					if not present then
					begin
						SetLength(persons_with_codes, Length(persons_with_codes)+1);
						persons_with_codes[Length(persons_with_codes)-1] := person;
					end;
				end;
				
				if (person<>'') and (code<>'') and (not present) then 
				begin
					persons_count:=persons_count+1;
					SetLength(persons, persons_count);
					SetSearchResult2(person, code, birth_date, original_name, '', small_photo);
				end;
				
				i := pos('<a href="view.php?page=person&id=',s);
				if i<=0 then begin
					i := pos('<a href="view.php?page=liste&Name=',s);
					no_code := true;
				end else 
					no_code := false;
				if i>0 then
					delete(s, 1, i-1);
			end;
		end;
	end;
	result:=persons_count;
end;

function GetPersonURL(code: string; images_only: boolean): string;
var
	num: integer;
begin
	try
		num := StrToInt(code);
		// "code" is an integer number
		result := format('http://www.ofdb.de/view.php?page=person&id=%s', [code]);
	except
		// "code" is a 'name+surname'
		result := format('http://www.ofdb.de/view.php?page=liste&Name=%s', [code]);
	end;
end;

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

function SavePhoto(url, FileName: string): boolean;
var
	fs: TFileStream;
	s, referer: string;
	i: integer;
begin
	i := length(url);
	repeat dec(i) until (i=0)or(url[i] = '/');
	if i=0 then 
		exit;
	referer := copy(url, 1, i);
	s := DownloadURLWithReferer(url, referer);
	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])] in ['''','-'] 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);
	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 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 GetTextFromAllBetween(str1, str2: string): string;
var
	i, j: integer;
begin
	Result := '';
	i := pos(str1, all);
	j := MyPos(str2, all, i+length(str1));
	if (i>0)and(j>0) then begin
		i := i + length(str1);
		Result := copy(all, i, j-i);
	end;
end;

function GetTextBetween(fromStr, str1, str2: string): string;
var
	i, j: integer;
begin
	Result := '';
	if str1 = '' then
		i := 1
	else
		i := pos(str1, fromStr);
	if str2 = '' then
		j := Length(fromStr) + 1
	else
		j := MyPos(str2, fromStr, i+length(str1));
	if (i>0)and(j>0) then begin
		i := i + length(str1);
		Result := copy(fromStr, i, j-i);
	end;
end;

function GetInfo(url: string; images_only: boolean): string;
var
	day, month, year, s, s2, s3: string;
	i, j: integer;
	name, code: string;
	no_code: boolean;
begin
	result:='';
	
	if pos('&Name=', url) > 0 then begin
		person_code := GetTextBetween(url, '&Name=', '');
		no_code := true;
	end
	else begin
		person_code := GetTextBetween(url, '&id=', '');
		no_code := false;
	end;

	person_site_url:=GetPersonSiteURL(person_code);

	all:=DownloadURL(url);
	result:=all;

	imdb_code := GetTextFromAllBetween('href="http://german.imdb.com/name/nm', '"');
	
	if not no_code then begin
		person := GetTextFromAllBetween('<td width="99%">', '</a>');
		person := DecodeHTML(RemoveTags(person));
	end
	else begin
		person := GetTextFromAllBetween('Filme mit Beteiligung von ', '<');
		person := RemoveTags(person);
		person := DecodeHTML(person);
	end;

	original_name := ParseString(all, 'B&uuml;rgerlicher Name: (.*?)<br>', 1);
	original_name:=DecodeHTML(original_name);
	if original_name='' then
		original_name:=person;
	birth_name := '';

	//person photo
	s2 := copy(person_code, 1, length(person_code) - 3);
	if s2 = '' 
		then s2 := '0';
	photo_url := 'images/person/'+ s2 +'/'+ person_code +'.jpg';
	i := pos(photo_url, all);
	if i > 0 then
		photo_url := 'http://www.ofdb.de/' + photo_url
	else
		photo_url := '';
	
	//dates of birth and death
	i := pos('<i>Geburtsdatum:</i>', all);
	if i=0 then
		i:=pos('* ', all);
	if i>0 then begin
		j := MyPos('<br><br>', all, i+1);
		if MyPos('&#8224; ', all, j+1)>0 then
			j:=MyPos('<br>', all, j+8);
		s2 := copy(all, i, j-i);
		
		//date of death
		s3 := ParseString(s2, '† (.*?)$', 1);
		if s3<>'' then
		begin
			day := ParseString(s3, '^(\d+).', 1);
			month := GetTextBetween(s3, '. ', ' ');
			delete(s3, 1, pos('. ', s3)+1);
			year := ParseString(s3, ' (\d+) ', 1);
			if (day<>'')and(month<>'')and(year<>'') then
			begin
				i:=1;
				while (i<=12) and (monthes[i]<>month) do begin	
					i:=i+1;
				end;
				if i<=12 then
					death_date := IntToStr(i) + format('/%s/%s', [day, year]);
			end;
		end;
		
		//date of birth
		s3 := GetTextBetween(s2, '* ', '<i>');
		s3 := RemoveTags(s3);
		day := GetTextBetween(s3, '', '.');
		month := GetTextBetween(s3, '. ', ' ');
		delete(s3, 1, pos('. ', s3)+1);
		year := GetTextBetween(s3, ' ', '');
		if (day<>'')and(month<>'')and(year<>'') then
		begin
			i:=1;
			while (i<=12) and (monthes[i]<>month) do begin	
				i:=i+1;
			end;
			if i<=12 then
				birth_date := IntToStr(i) + format('/%s/%s', [day, year]);
		end;
		
		//birthplace
		s := GetTextBetween(s2, '<i>', '</i>');
		s := RemoveTags(s);
		s := DecodeHTML(s);
		birthplace := ClearExtraBlankCharacters(s);
		if pos('-', birthplace) = 1 then
			birthplace := '';
	end;
	
	//Actor - filmography
	if not no_code then begin
		s := GetTextFromAllBetween('<b>Darsteller(in):</b>', '</table>');
		s := GetTextBetween(s, '<tr valign="top">', '');
		s2 := GetTextBetween(s, '', '<td colspan="2">');
		if s2 <> '' then
			s := s2;
	end
	else begin
		s := GetTextFromAllBetween('<b>Darsteller(in):</b><br><br>', '<br><br><br>') + '<br>';
	end;

	while length(s) > 0 do
	begin
		if not no_code then begin
			s2 := GetTextBetween(s, '</td><td>', '</td>');
			delete(s, 1, pos('</tr>', s)+4);
		end
		else begin
			s2 := GetTextBetween(s, '. ', '<br>');
			delete(s, 1, pos('<br>', s)+4);
		end;

		i := pos('onmouseover="', s2);
		j := MyPos('"', s2, i+length('onmouseover="'));
		if (i>0)and(j>0) then
			delete(s2, i, j-i+1);
		
		code := GetTextBetween(s2, 'view.php?page=film&fid=', '"');
		if code='' then
			code:=GetTextBetween(s2, '<a href="film/', ',');
		
		name := GetTextBetween(s2, '', '</a>');
		name := RemoveTags(name);
		name := DecodeHTML(name);
		name := ClearExtraBlankCharacters(name);
		if pos(')', name) = Length(name) then
		begin
			year := copy(name, length(name)-4, 4);
			try i := StrToInt(year); except year := ''; end;
			if year <> '' then
				name := copy(name,  1, length(name)-7);
		end;
		name := ClearBrackets(name);
		name := MovePrefixLeft(name);
		
		character := GetTextBetween(s2, '> ... ', '<');
		character := ClearBrackets(character);
		if length(character) > 0 then
			if (character[1]='(') and (character[length(character)]=')') then
				character := '';
		
		if (name <> '')and(code <> '') then begin
			SetMovieName(name, character, 3, code, year);
		end
		else break;
	end;

	//Director - filmography 
	if not no_code then begin
		s := GetTextFromAllBetween('<b>Regisseur(in):</b>', '</table>');
		s := GetTextBetween(s, '<tr valign="top">', '');
		s2 := GetTextBetween(s, '', '<td colspan="2">');
		if s2 <> '' then
			s := s2;
	end
	else begin
		s := GetTextFromAllBetween('<b>Regisseur(in):</b><br><br>', '<br><br><br>') + '<br>';
	end;
	while length(s) > 0 do
	begin
		if not no_code then begin
			s2 := GetTextBetween(s, '</td><td>', '</td>');
			delete(s, 1, pos('</tr>', s)+4);
		end
		else begin
			s2 := GetTextBetween(s, '. ', '<br>');
			delete(s, 1, pos('<br>', s)+4);
		end;
		
		i := pos('onmouseover="', s2);
		j := MyPos('"', s2, i+length('onmouseover="'));
		if (i>0)and(j>0) then
			delete(s2, i, j-i+1);
		
		code := GetTextBetween(s2, 'view.php?page=film&fid=', '"');
		if code='' then
			code:=GetTextBetween(s2, '<a href="film/', ',');
		name := GetTextBetween(s2, '', '</a>');
		name := RemoveTags(name);
		name := DecodeHTML(name);
		name := ClearExtraBlankCharacters(name);
		if pos(')', name) = Length(name) then
		begin
			year := copy(name, length(name)-4, 4);
			try i := StrToInt(year); except year := ''; end;
			if year <> '' then
				name := copy(name,  1, length(name)-7);
		end;
		name := ClearBrackets(name);
		name := MovePrefixLeft(name);
		if (name <> '')and(code <> '') then begin
			SetMovieName(name, '', 1, code, year);
		end
		else break;
	end;
	
	//Writer - filmography 
	if not no_code then begin
		s := GetTextFromAllBetween('<b>Drehbuchautor(in):</b>', '</table>');
		s := GetTextBetween(s, '<tr valign="top">', '');
		s2 := GetTextBetween(s, '', '<td colspan="2">');
		if s2 <> '' then
			s := s2;
	end
	else begin
		s := GetTextFromAllBetween('<b>Drehbuchautor(in):</b><br><br>', '<br><br><br>') + '<br>';
	end;
	while length(s) > 0 do
	begin
		if not no_code then begin
			s2 := GetTextBetween(s, '</td><td>', '</td>');
			delete(s, 1, pos('</tr>', s)+4);
		end
		else begin
			s2 := GetTextBetween(s, '. ', '<br>');
			delete(s, 1, pos('<br>', s)+4);
		end;

		i := pos('onmouseover="', s2);
		j := MyPos('"', s2, i+length('onmouseover="'));
		if (i>0)and(j>0) then
			delete(s2, i, j-i+1);
		
		code := GetTextBetween(s2, 'view.php?page=film&fid=', '"');
		if code='' then
			code:=GetTextBetween(s2, '<a href="film/', ',');		
		name := GetTextBetween(s2, '', '</a>');
		name := RemoveTags(name);
		name := DecodeHTML(name);
		name := ClearExtraBlankCharacters(name);
		if pos(')', name) = Length(name) then
		begin
			year := copy(name, length(name)-4, 4);
			try i := StrToInt(year); except year := ''; end;
			if year <> '' then
				name := copy(name,  1, length(name)-7);
		end;
		name := ClearBrackets(name);
		name := MovePrefixLeft(name);
		if (name <> '')and(code <> '') then begin
			SetMovieName(name, '', 2, code, year);
		end
		else break;
	end;
	
	//person height
	//no person height on this site
	person_height := 0;
	
	biography := '';
	i := pos('href="view.php?page=person_text&id=4&pid=' + person_code, all);
	if i>0 then
	begin
		i := i + 6;
		j := MyPos('"', all, i);
		url := 'http://www.ofdb.de/' + copy(all, i, j-i);
		all := DownloadURL(url);
		s := GetTextFromAllBetween('<p class="Blocksatz">', '</p>');
		s := GetTextBetween(s, '<br><br>', '');
		s := DecodeHTML(s);
		s := ReplaceStr(s, '</p>', #10, -1);
		s := ReplaceStr(s, '</P>', #10, -1);
		s := ReplaceStr(s, '</div>', #10, -1);
		s := ReplaceStr(s, '</DIV>', #10, -1);
		s := ReplaceStr(s, '<BR>', #10, -1);
		s := ReplaceStr(s, '<br>', #10, -1);
		s := ReplaceStr(s, '<br/>', #10, -1);
		s := ReplaceStr(s, '<br />', #10, -1);
		s := ReplaceStr(s, '</li>', #10, -1);
		s := RemoveTags(s);
		s := ClearExtraBlankCharacters(s);
		s := ReplaceStr(s, #10, '<br>', -1);
		biography := s;
	end;
end;

//get movie site url
function GetMovieSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else
		result:='http://www.ofdb.de/view.php?page=film&fid='+code;
end;

//get person site url
function GetPersonSiteURL(code: string): string;
var
	num: integer;
begin
	if code='' then
		result:=''
	else
	try
		num := StrToInt(code);
		// "code" is an integer number
		result := format('http://www.ofdb.de/view.php?page=person&id=%s', [code]);
	except
		// "code" is a 'name+surname'
		result := format('http://www.ofdb.de/view.php?page=liste&Name=%s', [code]);
	end;
end;

begin
monthes[1]:='Januar';
monthes[2]:='Februar';
monthes[3]:='M&auml;rz';
monthes[4]:='April';
monthes[5]:='Mai';
monthes[6]:='Juni';
monthes[7]:='Juli';
monthes[8]:='August';
monthes[9]:='September';
monthes[10]:='Oktober';
monthes[11]:='November';
monthes[12]:='Dezember';

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';
prefixes[13]:='Die';
prefixes[14]:='Ein';
prefixes[15]:='Der';
prefixes[16]:='Das';
prefixes[17]:='Eine';
prefixes[18]:='Ha-';
prefixes[19]:='I';
prefixes[20]:='Den';
prefixes[21]:='En';
prefixes[22]:='O';
prefixes[23]:='Una';
prefixes[24]:='De';

end.