const
	script_name='HKMDB';
	language='English';
	version=6;
	author='Vlad';
	MinProgramBuild=2120;
	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, alt_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://hkmdb.com/db/search/simple_search_results.mhtml?display_set=eng&search_for=people&search_string=%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, name, code: string;
	i, j: integer;
begin
	persons_count:=0;

	s:='';	
	
	//forwarded to the person page
	i := Pos('<TITLE>', all);
	if i>0 then begin
		code := ParseString(all, '<A HREF="/db/people/view.mhtml\?id=(\d+)\&display_set=eng">', 1);
		SetSearchResult('name', code);
		result := 1;
		exit;
	end;
	
	i:=pos('<H3>People</H3>', all);
	if i>0 then begin
		j:=MyPos('</TABLE></CENTER>', all, i+1);
		if j>0 then
			s:=copy(all, i+3, j-i-4);
	end;
	
	if (s='') or (pos('Nothing matched your search', all)>0) then
		result:=-2
		//person not found
	else begin
	  i := pos('<TABLE ',s);
	  s := copy(s,i+3,length(s));
	  
	  i:=Pos('<TR>', s);
	  while i>0 do begin			
			j:=MyPos('</TR>', s, i+1);
			s2 := copy(s, i, j-i);
			
			code:=ParseString(s2, '<a href="/db/people/view.mhtml\?id=(\d+)\&display_set=eng">', 1);

			
			name:=ParseString(s2, 'display_set=eng"> (.*?) \/ <\/a>', 1);
			name:=DecodeHTML(name);
			name:=ClearExtraSpaces(name);

			if name<>'' then begin
				persons_count:=persons_count+1;
				SetSearchResult(name, code);
			end;
			delete(s, 1, j+5);
			i:=pos('<TR>', s);
		end;
		result := persons_count;
	end;
end;

function GetPersonURL(code: string; images_only: boolean): string;
begin
		result:=format('http://hkmdb.com/db/people/view.mhtml?id=%s&display_set=eng', [code]);
end;

//returnes images links
procedure GetImages;
var
	i, j, photo_cnt: Integer;
	s, url: String;
begin
	photo_cnt := 0;
	if MaxPhotosCount = 0 then exit;
	
	url := format('http://hkmdb.com/db/people/images.mhtml?id=%s&display_set=eng', [person_code]);
	all := DownloadURL(url);
	
	i := Pos('<IMG SRC="/db/images/', all);
	while (photo_cnt < MaxPhotosCount) and (i>0) do begin
		j := MyPos('"', all, i+10);
		s := copy(all, i+10, j-i-10);
		if s<>'' then begin
			s := ReplaceStr(s, 't.jpg', 'b.jpg', -1);
			SetImageName('', 'http://hkmdb.com' + s, '', 6);
			photo_cnt := photo_cnt + 1;
		end;
		delete(all, i, j-i);
		i := Pos('<IMG SRC="/db/images/', all);
	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 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;
	s, title, code, character, year: String;
begin
	i := Pos('<TR>', text);
	while i>0 do begin
		j := MyPos('</TR>', text, i+1);
		s := copy(text, i, j-i);
		code := ParseString(s, '<a href="/db/movies/view.mhtml\?id=(\d+)\&display_set=eng">', 1);
		title := ParseString(s, 'display_set=eng">(.*?) \(\d+\)<\/a>', 1);
		year := ParseString(s, 'display_set=eng">.*? \((\d+)\)<\/a>', 1);
		k := Pos('...</TD>', s);
		character := '';
		if k>0 then begin
			delete(s, 1, k+7);
			s := RemoveTags(s);
			s := DecodeHTML(s);
			k := Pos('[', s);
			if k>0 then
				delete(s, k, length(s) - k + 1);
			character := ClearExtraBlankCharacters(s);
		end;
		if title<>'' then
			SetMovieName(title, character, mode, code, year);
		delete(text, 1, i);
		i := Pos('<TR>', text);
	end;
end;

function GetInfo(url: string; images_only: boolean): string;
var
	day, month, year, s, s2, s3, s4: string;
	i, j, k, ke, person_mode: integer;
begin
	result:='';

	BasicURL:=url;

	person_code:=ParseString(url, 'http://hkmdb.com/db/people/view.mhtml\?id=(\d+)\&display_set=eng', 1);
	person_site_url:=GetPersonSiteURL(person_code);

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

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

	original_name:='';
	
	//Filmography - director
	i := Pos('">Director</TD>', all);
	if i=0 then
		i := Pos('">Director <i>', all);
	if i>0 then begin
		j := MyPos('<TD COLSPAN=', all, i+1);
		s := copy(all, i, j-i);
		ExtractMovies(s, 1);
	end;
	
	//Filmography - writer
	i := Pos('">Writer</TD>', all);
	if i=0 then
		i := Pos('">Writer <i>', all);
	if i>0 then begin
		j := MyPos('<TD COLSPAN=', all, i+1);
		s := copy(all, i, j-i);
		ExtractMovies(s, 2);
	end;
	
	//Filmography - actor
	i := Pos('">Actor</TD>', all);
	if i=0 then
		i := Pos('">Actor <i>', all);
	if i>0 then begin
		j := MyPos('<TD COLSPAN=', all, i+1);
		s := copy(all, i, j-i);
		ExtractMovies(s, 3);
	end;
	
	//Filmography - composer
	i := Pos('">Composer</TD>', all);
	if i=0 then
		i := Pos('">Composer <i>', all);
	if i>0 then begin
		j := MyPos('<TD COLSPAN=', all, i+1);
		s := copy(all, i, j-i);
		ExtractMovies(s, 4);
	end;
	
	//bio
	i := Pos('<b>Biography</b></TD>', all);
	if i>0 then begin
		j := MyPos('</TABLE>', all, i+1);
		s := copy(all, i+21, j-i-21);
		s := RemoveTags(s);
		s := DecodeHTML(s);
		biography := ClearExtraBlankCharacters(s);
	end;

	//date of birth
	i:=pos('<i>Born:', all);
	if i>0 then begin
		j:=MyPos('</i>', all, i+1);
		s2:=copy(all, i+8, j-i-8);
		month := ParseString(s2, '^ (.*?) .*?,', 1);
		day:=ParseString(s2, '^ .*? (\d+)..,', 1);
		year:=ParseString(s2, ', (\d+) $', 1);
		if year<>'' then begin
			i:=1;
			while (i<=12) and (monthes[i]<>month) do
				i:=i+1;
			if (i<=12) and (month<>'') then
				birth_date:=IntToStr(i)+format('/%s/%s', [day, year]);
		end;
	end;
		
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;

	
//get movie site url
function GetMovieSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else
		result:='http://hkmdb.com/db/movies/view.mhtml?id='+code+'&display_set=eng';
end;

//get person url
function GetPersonSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else
		result:=format('http://hkmdb.com/db/people/view.mhtml?id=%s&display_set=eng', [code]);
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.