const
	script_name='AnimeNfo';
	language='English';
	version=4;
	author='Vlad';
	MinProgramBuild=2120;
	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, 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://animenfo.com/search.php?query=%s&queryin=person&action=Go&option=keywords', [PersonName]);
	url:=ReplaceStr(url, ' ', '+', -1);
	alt_url:=format('http://animenfo.com/search.php?query=%s&queryin=people&action=Go&option=keywords', [PersonName]);
	alt_url:=ReplaceStr(alt_url, ' ', '+', -1);
	result:=url;
end;

//Download search results from URL
function DownloadSearchURL(url: string): integer;
var
	alt_search: integer;
begin
	all:=DownloadURL(url);
	if all='-1' then
		result:=-1
	else begin
		alt_search:=PersonSearch;
		if alt_search=-2 then begin
			all:=DownloadURL(alt_url);
			if all='-1' then
				result:=-1
			else result:=PersonSearch;
		end
		else result:=alt_search;
	end;
end;

function PersonSearch: integer;
var
	url, s, s2: string;
	i, j: integer;
begin
	persons_count:=0;

	s:='';	
	i:=pos('<h2>Search Result</h2>', all);
	if i>0 then begin
		j:=MyPos('Search found', all, i+1);
		if j>0 then
			s:=copy(all, i+3, j-i-4);
	end;
	
	if (s='') or (pos('Search cannot find', all)>0) then
		result:=-2
		//person not found
	else begin
		i := pos('<a href',s);
		j := MyPos('</a>', s, i+1);
		while j>0 do begin
			persons_count:=persons_count+1;
			SetLength(persons, persons_count);
			s2:=copy(s, i, j-i);
			person_code:=ParseString(s2, '<a href=''(.*?).html', 1);
			s2:=ParseString(s2, '<a href=.*?>(.*?) \(', 1);
			s2:=RemoveTags(s2);
			s2:=DecodeHTML(s2);
			s2:=ClearExtraSpaces(s2);
			SetSearchResult(s2, person_code);
			i:=j+4;
			j:=MyPos('</a>', s, i+1);
		
		end;
		result:=persons_count;
	end;
end;

function GetPersonURL(code: string; images_only: boolean): string;
begin
		result:=format('http://animenfo.com/%s.html', [code]);
end;

//returnes images links
procedure GetImages;
begin
	if photo_url<>'' then
		SetImageName('', photo_url, '', 6);
	//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;


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://animenfo.com/(.*?).html', 1);
	person_site_url:=GetPersonSiteURL(person_code);

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

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

	original_name:='';

	//person photo
	photo_url:= ParseString(all, '<img class="float" src="(.*?)" alt', 1);
	if photo_url='general/imagenotavailable.jpg' then
		photo_url:='';

	//date of birth
	i:=pos('<b>Date of Birth</b>', all);
	if i>0 then begin
		j:=MyPos('</tr>', all, i+1);
		s2:=copy(all, i+26, j-i-26);
		i:=pos('anime_info">', s2);
		j:=pos('</td>', s2);
		s3:=copy(s2, i+12, j-i-12);
		day:=ParseString(s3, '^(\d+) .*?', 1);
		if day<>'' then begin
			year:=ParseString(s3, ' .*? (\d+)', 1);
			if year<>'' then begin
				month:=ParseString(s3, ' (.*?) ', 1);
				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;

	//place of birth	
	i:=pos('<b>Place of Birth</b>', all);
	if i>0 then begin
		j:=MyPos('</tr>', all, i+1);
		s2:=copy(all, i+25, j-i-25);
		i:=pos('anime_info">', s2);
		j:=pos('</td>', s2);
		birthplace:=copy(s2, i+12, j-i-12);
		if (length(birthplace)<2) then
			birthplace:='';
	end;
	

	//Info about the person
	i:=pos('Personal Notes', all);
	if i>0 then begin
		j:=MyPos('<table ', all, i+1);
		s2:=copy(all, i, j-i);
		description:=ParseString(s2, '<td class="description">.*?\(.*?\)(.*?)</td>', 1);
		description:=RemoveTags(description);
		description:=DecodeHTML(description);
		description:=ClearExtraSpaces(description);
	end;

	//Filmography
	i:=pos('Careers', all);
	person_mode:=0;
	character:='';
	if i>0 then begin
		i:=MyPos('<td class=''anime_info''>', all, i+1);
		if i>0 then begin
			j:=MyPos('</table>', all, i+1);
			s:=copy(all, i, j-i);
			i:=1;
			j:=pos('</tr>', s);
			while j>0 do begin
				s2:=copy(s, i, j-i);
				k:=pos('</a>', s2);
				s3:=copy(s2, 1, k+4);
				name:=ParseString(s3, '.html">(.*?)</a>', 1);
				name:=RemoveTags(name);
				name:=DecodeHTML(name);
				name:=ClearExtraSpaces(name);
				code:=ParseString(s3, '<a href="(.*?).html', 1);
				
		 		if (pos('Director', s2)>0) then
					person_mode:=1;
		 		if (pos('Script', s2)>0) then
					person_mode:=2;
				i:=MyPos('</a>', s2, k+4);
				if i>0 then begin
					s3:=copy(s2, k+4, i-k);
					character:=ParseString(s3, '.html">(.*?)</a>', 1);
					person_mode:=3;
				
				end;
		                SetMovieName(name, character, person_mode, code, '');
				i:=j+4;
				j:=MyPos('</tr>', s, i+1);
			end;
			
		end;
	
	end;

	
end;

	
//get movie site url
function GetMovieSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else
		result:='http://animenfo.com/'+code+'.html';
end;

//get person url
function GetPersonSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else
		result:=format('http://animenfo.com/%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.