const
	script_name='filmweb';
	language='Polish';
	version=2;
	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 GetSearchURLbyName(PersonName: string): string;
var
	url: string;
begin
	PersonName := EncodeURL(PersonName, 'utf-8');
	url:=format('http://www.filmweb.pl/search?q=%s', [PersonName]);
	url:=ReplaceStr(url, ' ', '+', -1);
	result:=url;
end;

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;

//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 GetCodeFromImageURL(image: String): String;
var
	i, j : Integer;
	code : String;
begin
	i := Pos('.pl/', image);
	code := copy(image, i + 4, length(image) - i - 3);
	for i := 1 to 3 do begin
		j := Pos('/', code);
		if j > 0 then
			delete(code, 1, j);
	end;
	code := ParseString(code, '^(\d+)', 1);
	Result := code;
end;

function PersonSearch: integer;
var
	url, s, s2, name, code, true_name, small_photo, description, tempCode, item_type: string;
	i, j, k: integer;
begin
	persons_count:=0;
	s:='';
	i := Pos('resultsList">', all);
	if i>0 then begin
		delete(all, 1, i);
		i := Pos('<div class=hitDesc>', all);
		while i>0 do begin
			j := MyPos('<button', all, i + 1);
			tempCode := copy(all, i, j-i);
			item_type := LowerCase(GetStr(tempCode, ' class="badge', '</'));
			if (Pos('osoba', item_type) > 0) then begin
				name := GetStr(tempCode, '<h3>', '</h3>');
				code := ParseString(name, ' href="/person/-(\d+)"', 1);
				small_photo := GetStr(tempCode, ' src="', '"');
				if Pos('NoImg', small_photo) > 0 then
					small_photo := '';
				if code = '' then
					code := GetCodeFromImageURL(small_photo);
				name := ClearExtraBlankCharacters(DecodeHTML(RemoveTags(name)));
				true_name := GetStr(tempCode, 'prawdziwe nazwisko:', '</div>');
				true_name := ClearExtraBlankCharacters(DecodeHTML(RemoveTags(true_name)));
				description := GetStr(tempCode, '<div class=searchResultDescription>', '</div>');
				description := ClearExtraBlankCharacters(DecodeHTML(RemoveTags(description)));
				if (name <> '') and (code <> '') then begin
					persons_count := persons_count + 1;
					SetSearchResult2(name, code, '', true_name, description, small_photo);
				end;
			end;
			delete(all, 1, i);
			i := Pos('<div class=hitDesc>', all);
		end;
	end;
	result:=persons_count;
end;

function GetPersonURL(code: string; images_only: boolean): string;
begin
	if code='' then
		result:=''
	else
		result:='http://www.filmweb.pl/person/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;

procedure ExtractMovies(text: string; start, mode: Integer);
var
	i: Integer;
	temp, s, code, title, character, year, new_year: String;
begin
	year := '';
	i := MyPos('</tbody>', text, start);
	temp := copy(text, start, i - start);
	i := Pos('<tr', temp);
	while i>0 do begin
		s := GetStr(temp, '<tr', '</tr>');
		code := GetStr(s, ' href="/film/', '"');
		code := ParseString(code, '(\d+)$', 1);
		new_year := GetStr(s, 'class=filmographyYear>', '</');
		if new_year <> '' then
			year := new_year;
		if code = '' then begin
			code := GetStr(s, '<div class=userFilmographyfalse', '/div>');
			code := GetStr(code, '>', '<');
		end;
		title := GetStr(s, ' class=filmTitleCol>', '</a>');
		title := ClearExtraBlankCharacters(DecodeHTML(RemoveTags(title)));
		character := '';
		if (mode = 3) then begin
			character := GetStr(s, ' class=roleName>', '<');
			character := ClearExtraBlankCharacters(DecodeHTML(RemoveTags(character)));
		end;
		if (title <> '') then
			SetMovieName(title, character, mode, code, year);
		delete(temp, 1, i);
		i := Pos('<tr', temp);
	end;
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, '(\d+)$', 1);
	person_site_url:=GetPersonSiteURL(person_code);
	all:=DownloadURL(url);
	if all='-1' then
		result:='-1'
	else
		result:=all;
	
	//name
	person:=ParseString(all, '<title>(.*?) - Filmweb', 1);
	person := DecodeHTML(person);
	i := Pos('(', person);
	if i>0 then
		delete(person, i, length(person) - i + 1);
		
	person := ClearExtraBlankCharacters(person);

	//original name
	original_name := GetStr(all, '<h2 class=personSubName>', '</h2>');
	original_name := ClearExtraBlankCharacters(DecodeHTML(RemoveTags(original_name)));
	
	//birth place
	birthplace := GetStr(all, 'miejsce urodzenia:', '</td>');
	birthplace := ClearExtraBlankCharacters(DecodeHTML(RemoveTags(birthplace)));
	
	//birth date
	s := GetStr(all, 'data urodzenia:', '</td');
	if s <> '' then begin
		i := Pos('<script', s);
		if i>0 then
			delete(s, i, length(s)-i+1);
		s := ClearExtraBlankCharacters(DecodeHTML(RemoveTags(s)));
		year := ParseString(s, '^(\d+)', 1);
		month := ParseString(s, '-(\d+)-', 1);
		day := ParseString(s, '(\d+)$', 1);
		birth_date:=month+'/'+day+'/'+year;
	end;
	
	//height
	person_height := 0;
	s := GetStr(all, 'Wzrost:', ' cm');
	if s<>'' then begin
		s := RemoveTags(s);
		try
			person_height := StrToInt(ClearExtraBlankCharacters(s));
		except
		end;
	end;
	
	//death date
	s := GetStr(all, 'data śmierci:', '</td');
	if s <> '' then begin
		i := Pos('<script', s);
		if i>0 then
			delete(s, i, length(s)-i+1);
		s := ClearExtraBlankCharacters(DecodeHTML(RemoveTags(s)));
		year := ParseString(s, '^(\d+)', 1);
		month := ParseString(s, '-(\d+)-', 1);
		day := ParseString(s, '(\d+)$', 1);
		death_date:=month+'/'+day+'/'+year;
	end;
	
	//photo
	photo_url := GetStr(all, '<meta property="og:image" content="', '"');
	if (Pos('NoImg', photo_url) > 0) then
		photo_url := '';
		
	//biography
	temp := GetStr(all, 'biografie </a>', '</p>');
	if temp = '' then
		temp := GetStr(all, 'biografia </a>', '</p>');
	biography := GetStr(temp, '<p class="text', '</');
	if biography <> '' then
		biography := '<' + biography;
	//biography := GetStr(temp, '<span class=personDescrBg>', '<a href="#" class=see-more>');
	biography := ClearExtraBlankCharacters(DecodeHTML(RemoveTags(biography)));
	
	//filmography
	temp := GetStr(all, 'filmografia</h2>', '<div class="sideBox');
	i := Pos('id=films-director>', temp);
	if i>0 then
		ExtractMovies(temp, i, 1);
	i := Pos('id=series-director>', temp);
	if i>0 then
		ExtractMovies(temp, i, 1);
	i := Pos('id=films-screenwriter>', temp);
	if i>0 then
		ExtractMovies(temp, i, 2);
	i := Pos('id=series-screenwriter>', temp);
	if i>0 then
		ExtractMovies(temp, i, 2);
	i := Pos('id=films-actors>', temp);
	if i>0 then
		ExtractMovies(temp, i, 3);
	i := Pos('id=series-actors>', temp);
	if i>0 then
		ExtractMovies(temp, i, 3);
	i := Pos('id=films-music>', temp);
	if i>0 then
		ExtractMovies(temp, i, 4);
	i := Pos('id=series-music>', temp);
	if i>0 then
		ExtractMovies(temp, i, 4);		
	
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;
end;

	
//get movie site url
function GetMovieSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else
		result:='http://www.filmweb.pl/film/s-1999-'+code;
end;

//get person site url
function GetPersonSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else
		result:='http://www.filmweb.pl/person/s-'+code;
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.