const
	script_name='adultdvdempire';
	language='English';
	version=3;
	author='Vlad';
	MinProgramBuild=5200;
	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;
	
	DownloadPersonBiography, DownloadPersonAdditionalInfo: boolean;
	MaxPhotosCount: integer;
	
const
	SITE_MAIN_URL = 'http://www.adultdvdempire.com/';
	MOVIE_URL_END = '/1234-porn-movies.html';
	
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 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;

function GetSearchURLbyName(PersonName: string): string;
var
	url: string;
begin
	url:=SITE_MAIN_URL + format('performer/search?q=%s&pageSize=50', [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, small_photo, description: string;
	i, j, k: integer;
begin
	persons_count:=0;
	s:='';
	i:=pos('<strong>0</strong> Matches Found', all);
	if (all='') or (i>0) then
		// Person not found!
	else begin
		i:=Pos('<div class="performer">', all);
		while i>0 do begin
			j:=MyPos('</div>', all, i+1);
			s:=copy(all, i, j-i);
			person_code := GetStr(s, '<a href="/', '.html');
			name:=GetStr(s, 'class="name">', '</a>');
			name:=DecodeHTML(RemoveTags(name));
			name:=ClearExtraBlankCharacters(name);
			small_photo := GetStr(s, '<img src=''', '''');
			small_photo := ReplaceStr(small_photo, 's.jpg', '.jpg', -1);
			description := '';
			if (Pos('- Male', s)>0) then
				description := 'Male';
			if (name <> '') and (person_code <> '') then begin
				persons_count := persons_count + 1;
				SetSearchResult2(name, person_code, '', '', description, small_photo);
			end;
			delete(all, 1, j);
			i:=pos('<div class="performer">', all);
		end;
	end;
	result:=persons_count;
end;

function GetPersonURL(code: string; images_only: boolean): string;
begin
		result:=format('http://www.adultdvdempire.com/%s.html', [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, next_page_url, bio_url, day, month, year, alias: string;
	i, j, k, l, pages, mode, actor_start, next_page: integer;
	bio_exists: boolean;
begin
	result:='';
	BasicURL:=url;
	person_code:=ParseString(url, 'http://www.adultdvdempire.com/(.*?).html', 1);
	person_site_url:=GetPersonSiteURL(person_code);
	all:=DownloadURL(url+'?page=1&pp=100');
	if all='-1' then
		result:='-1'
	else
		result:=all;
	//name
	person:=GetStr(all, '<h1>', '<');
	person := ClearExtraBlankCharacters(DecodeHTML(RemoveTags(person)));

	original_name:='';
	
	//check if the biography exists
	s := 'biography/' + person_code + '.html';
	bio_url := SITE_MAIN_URL + s;
	if (Pos(s, all)>0) and (DownloadPersonBiography) then
		AddRequestForHTMLPageContent(bio_url, False);
	
	//description
	description:='';
	s:=GetStr(all, '<ul class="Stats">', '</ul>');
	s := ReplaceStr(s, '</li>', #13#10, -1);
	s := ReplaceStr(s, '</strong>', ':', -1);
	description := ClearExtraBlankCharacters(DecodeHTML(RemoveTags(s)));
	
	//photo
	s:=GetStr(all, '<div id="Headshot">', '</div>');
	photo_url:=GetStr(s, '<img src=''', '''');
	
	//filmography
	next_page:=1;
	repeat begin
		i:=Pos('<span class="rank">', all);
		while i>0 do begin
			j:=MyPos('<div class="clear">', all, i+1);
			s:=copy(all, i, j-i);
			name := GetStr(s, '<p class="title">', '</a>');
			code := ParseString(name, '<a href="\/(\d+)\/', 1);
			name:=ClearExtraBlankCharacters(DecodeHTML(RemoveTags(name)));
			year:=ParseString(s, 'Production Year: (\d+)</span>', 1);
			if year='' then
				year := ParseString(s, '- year: (\d+)', 1);
			if year='' then
				year:=ParseString(s, 'Release Date: \d+/\d+/(\d+)</span>', 1);
			if pos('-Directors', person_code)>0 then
				mode:=1
			else
				mode:=3;
			if name<>'' then
				SetMovieName(name, '', mode, code, year);
			delete(all, 1, j);
			i:=Pos('<span class="rank">', all);
		end;
		k:=Pos('class="Browse Next "', all);
		if k>0 then begin
			next_page:=next_page+1;
			s2:=IntToStr(next_page);
			next_page_url:=format('?page=%s&pp=100', [s2]);
			all:=DownloadURL(url+next_page_url);
		end;
	end
	until k=0;
	
	//biography
	if DownloadPersonBiography then begin
		all:=GetHTMLPageContentFromRequest(bio_url);
		s:=GetStr(all, '<h2>Biography</h2>', '</div>');
		//birth date
		s2:=GetStr(s, 'Born On:</strong> ', '<');
		month := ParseString(s2, '^(.*?) \d+,', 1);
		day := ParseString(s2, '.*? (\d+),', 1);
		year := ParseString(s2, ', (\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
				birth_date := IntToStr(i) + format('/%s/%s', [day, year]);
		end;
		//birthplace
		birthplace := GetStr(s, '<strong>Birthplace:</strong> ', '<');
		birthplace := ClearExtraBlankCharacters(DecodeHTML(RemoveTags(birthplace)));
		//alias
		alias := GetStr(s, '<strong>Alias:</strong> ', '<');
		if alias <> '' then begin
			if description <> '' then
				description := description + #13#10;
			description := description + 'Alias: ' + alias;
		end;
		
		biography := GetStr(s, '</p><p>', '<i>Biography Text');
		biography := ReplaceStr(biography, #10, '', -1);
		biography := ReplaceStr(biography, #13, '', -1);
		biography := ReplaceStr(biography, '<br/><br/>', #13#10#13#10, -1);
		biography := ClearExtraBlankCharacters(DecodeHTML(RemoveTags(biography)));
	end;

end;

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

	
//get movie site url
function GetMovieSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else
		result:=SITE_MAIN_URL+code+MOVIE_URL_END;
end;

//get person site url
function GetPersonSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else	
		result:=SITE_MAIN_URL + format('%s.html', [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.