const
	script_name='cinefacts';
	language='German';
	version=8;
	author='Vlad';
	MinProgramBuild=6000;
	PCount = 13;
	//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, pics_code: string;
	person_height: integer;
	DownloadPersonBiography, DownloadPersonAdditionalInfo: boolean;
	MaxPhotosCount: integer;
	FotoURL: String;
	
	
function GetStr(str: String; s1: String; s2: String): String;
var
  I: Integer;
  J: Integer;
begin
  // Вырезает кусок строки между s1 и s2
	Result := '';
	I := Pos(s1, str);
	if I > 0 then
	begin
		J := MyPos(s2, str, I + Length(s1));
		if J > 0 then
  		Result := Copy(str, I + Length(s1), J - I - Length(s1));
	end;
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 GetSearchURLbyName(PersonName: string): string;
var
	url: string;
begin
	PersonName := EncodeURL(PersonName, 'utf-8');
	url:=format('http://www.cinefacts.de/search/site/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, name, code, small_photo, description: string;
	i, j, k: integer;
begin
	persons_count:=0;
	s:='';
	i := Pos('<h2>Suchergebnisse zu', all);
	if i > 0 then begin
		delete(all, 1, i);
		s := GetStr(all, '<h3>Stars', '</section>');
		i := Pos('<article>', s);
		while i > 0 do begin
			s2 := GetStr(s, '<article>', '</article>');
			small_photo := GetStr(s2, ' src="', '"');
			if (Pos('generic_person', small_photo) > 0) then
				small_photo := '';
			code := GetStr(s2, 'href="/Stars/', '"');
			code := ParseString(code, ',(\d+)', 1);
			name := ClearExtraBlankCharacters(DecodeHTML(RemoveTags(GetStr(s2, '<h4>', '</h4>'))));
			description := ClearExtraBlankCharacters(DecodeHTML(RemoveTags(GetStr(s2, '<p>', '</p>'))));
			if (name <> '') and (code <> '') then begin
				Inc(persons_count);
				SetSearchResult2(name, code, '', '', description, small_photo);
			end;
			delete(s, 1, i);
			i := Pos('<article>', s);
		end;
	end;

	result:=persons_count;
end;

function GetPersonURL(code: string; images_only: boolean): string;
begin
	if (Pos('/', code) > 0) then
		code := ParseString(code, '^(\d+)', 1);
	result := 'http://www.cinefacts.de/Stars/s,' + code;
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 GetInfo(url: string; images_only: boolean): string;
var
	s, s2, s3, s4, day, month, year, bio_url, filmo_url, temp, title, code, character: string;
	i, j, k, l, mode: integer;
begin
	result:='';
	BasicURL:=url;
	person_code := ParseString(url, '/kino/(\d+)', 1);
	if person_code = '' then
		person_code := ParseString(url, ',(\d+)', 1);
	person_site_url:=GetPersonSiteURL(person_code);
	all:=DownloadURL(url);
	if all='-1' then
		result:='-1'
	else
		result:=all;
	
	//download biography, if available
	bio_url := '';
	if DownloadPersonBiography and (Pos(person_code + '/Biografie', all) > 0) then begin
		bio_url := person_site_url + '/Biografie';
		AddRequestForHTMLPageContent(bio_url, False);
	end;
	
	//download filmography
	filmo_url := '';
	if (Pos(person_code + '/Filmografie', all) > 0) then begin
		filmo_url := person_site_url + '/Filmografie';
		AddRequestForHTMLPageContent(filmo_url, False);
	end;
	
	//download photos - never saw more than 3 photos on the site, so currently not used
	FotoURL := '';
	
	//name
	person:=ParseString(all, '<title>(.*?) \|', 1);
	i := Pos('(', person);
	if i>0 then
		delete(person, i, length(person)-i+1);
	person := ClearExtraBlankCharacters(DecodeHTML(person));

	//original name
	original_name := GetStr(all, 'Bürgerlicher Name: ', '<');
	original_name := ClearExtraBlankCharacters(DecodeHTML(original_name));
	
	//birth place
	birthplace := GetStr(all, 'Geburtsort: ', '<');
	birthplace := ClearExtraBlankCharacters(DecodeHTML(birthplace));
	
	//birth date
	s := GetStr(all, 'Geburtstag: ', '<');
	if s <> '' then begin
		day := ParseString(s, '^(\d+)', 1);
		month := ParseString(s, '\.(\d+)\.', 1);
		year := ParseString(s, '(\d+)$', 1);
		birth_date:=month+'/'+day+'/'+year;
	end;
	
	//death date
	s := GetStr(all, 'Todestag: ', '<');
	if s <> '' then begin
		day := ParseString(s, '^(\d+)', 1);
		month := ParseString(s, '\.(\d+)\.', 1);
		year := ParseString(s, '(\d+)$', 1);
		death_date:=month+'/'+day+'/'+year;
	end;
	
	//photo
	s := GetStr(all, '<div class="main_side">', '</figure>');
	photo_url := GetStr(s, ' src="', '"');
	i := Pos('/thumbs', photo_url);
	if i>0 then
		delete(photo_url, i, length('/thumbs'));
	if (Pos('generic_person', photo_url) > 0) then
		photo_url := '';
	
	//Loading filmography page
	if (filmo_url <> '') then begin
		temp := GetHTMLPageContentFromRequest(filmo_url);
		temp := GetStr(temp, 'Filmographie</h3>', 'id="sidebar">');
		i := Pos('<article>', temp);
		while i>0 do begin
			s2 := GetStr(temp, '<article>', '</article>');
			code := GetStr(s2, ' href="/Filme/', '"');
			code := ParseString(code, ',(\d+)', 1);
			if code <> '' then
				code := 'f,' + code;
			if code = '' then begin
				code := GetStr(s2, ' href="/Serien/', '"');
				code := ParseString(code, ',(\d+)', 1);
				if code <> '' then
					code := 's,' + code;
			end;
			title := GetStr(s2, '<h4>', '</h4>');
			title := ClearExtraBlankCharacters(DecodeHTML(RemoveTags(title)));
			s3 := GetStr(s2, '</h4>', '</header>');
			year := ParseString(s3, '\((\d+)\)', 1);
			s3 := GetStr(s2, '<div class="item_text">', '</p>');
			character := '';
			if Pos('Schauspieler', s3) > 0 then begin
				character := GetStr(s3, 'Rolle: ', ')');
				character := ClearExtraBlankCharacters(DecodeHTML(character));
				mode := 3;
			end;
			if Pos('Regisseur', s3) > 0 then
				mode := 1;
			if (Pos('Autor', s3) > 0) then
				mode := 2;
			if (title <> '') then
				SetMovieName(title, character, mode, code, year);
			delete(temp, 1, i);
			i := Pos('<article>', temp);
		end;
	end;		
	
end;

procedure GetImages;
var
	temp, s, img_url: String;
	i, j, cnt: Integer;
begin
	cnt := 0;
	if (photo_url<>'') and (MaxPhotosCount>0) then begin
		SetImageName('', photo_url, '', 6);
		Inc(cnt);
	end;
	temp := GetStr(all, 'Bilder zum Star</h3>', '</section>');
	i := Pos('<li>', temp);
	while (i > 0) and (cnt < MaxPhotosCount) do begin
		s := GetStr(temp, '<li>', '</li>');
		img_url := GetStr(s, ' src="', '"');
		j := Pos('/thumbs', img_url);
		if j > 0 then
			delete(img_url, j, length('/thumbs'));
		j := Pos('/square', img_url);
		if j > 0 then
			delete(img_url, j, length('/square'));
		if (img_url <> '') then begin
			Inc(cnt);
			SetImageName('', img_url, '', 6);
		end;
		delete(temp, 1, i);
		i := Pos('<li>', temp);
	end;
end;

	
function GetMovieSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else begin
		if (Pos('/', code) > 0) then begin
			code := ParseString(code, '^(\d+)', 1);
			result := 'http://www.cinefacts.de/Filme/f,' + code;
		end
		else if (Pos('s,', code) > 0) then
			result := 'http://www.cinefacts.de/Serien/' + code
		else
			result := 'http://www.cinefacts.de/Filme/' + code;
	end;
end;

//get person site url
function GetPersonSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else begin
		if (Pos('/', code) > 0) then
			code := ParseString(code, '^(\d+)', 1);
		result := 'http://www.cinefacts.de/Stars/s,' + code;
	end;
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';

end.