const
	script_name='interSinema';
	language='Turkish';
	version=5;
	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: 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://www.intersinema.com/ara.asp?ara=%s&I1.x=0&I1.y=0', [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, s3: string;
	i, j, k: integer;
begin
	persons_count:=0;

	s:='';
	s2:=ParseString(all, '<title>(.*?)</title>', 1);
	//check if we are forwarded to the filmography page
	if Pos('Filmleri,', s2)>0 then begin
		s3:=ParseString(s2, '^(.*?) Filmleri', 1);
		person_code:=ReplaceStr(s3, ' ', '-', -1);
		if person_code<>'' then begin
			SetSearchResult('name', person_code);
			persons_count:=1;
		end;
	end
	
	else begin
		i:=pos('KİŞİLER', all);
		if i>0 then begin
			j:=MyPos('</p>', all, i+1);
			if j>0 then
				s:=copy(all, i, j-i);
		end;
		
		if (s='') or (pos('için arama sonuçları</title>', all)=0) then
			//Person not found
		else begin
		  i:=pos('<a href=', s);
		  while i>0 do begin
				persons_count:=persons_count+1;
				j:=MyPos('</a>', s, i+1);
				s2:=copy(s, i, j-i);
				
				person_code:=ParseString(s2, '<a href="/(.*?)-filmleri/', 1);
				name:=ParseString(s2, '>(.*?)$', 1);
				name:=RemoveTags(name);
				name:=DecodeHTML(name);
				name:=ClearExtraSpaces(name);
				SetSearchResult(name, person_code);
				delete(s, 1, j+4);
				i:=pos('<a href=', s);
			end;
		end;
	end;
	result:=persons_count;
end;

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


//returnes images links
procedure GetImages;
begin
	if photo_url<>'' then
		SetImageName('', photo_url, '', 6);
	//GetPhotos;
	//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, mode: integer;
begin
	result:='';
	BasicURL:=url;
	person_code:=ParseString(url, 'http://www.intersinema.com/(.*?)-filmleri/', 1);
	person_site_url:=GetPersonSiteURL(person_code);
	
	all:=DownloadURL(url);
	if all='-1' then
		result:='-1'
	else
		result:=all;

	i:=Pos('<a class="hb">', all);
	if i>0 then begin
		j:=MyPos('</ul>', all, i+1);
		s:=copy(all, i, j-i+5);
	end;

	//photo
	s2 := ParseString(s, '<img border="0" src="/kisi/resimler/(.*?)" alt', 1);
	if s2<>'' then
		photo_url:='http://www.intersinema.com/kisi/resimler/'+s2;
	//name
	person := ParseString(s, '<a class="hb">(.*?)</a>', 1);
	person := ClearBrackets(person);
	person := DecodeHTML(person);

	original_name:='';
	
	//date and place of birth
	s2:= ParseString(all, 'Doğum: (.*?)</b>', 1);
	if s2<>'' then begin
		i:=pos(' - ', s2);
		birthplace:=copy(s2, 1, i-1);
		//date of birth
		s3:=copy(s2, i+3, length(s2) - 3);
		i:=pos('.', s3);
		if i>0 then begin
			day:=copy(s3, 1, i-1);
			j:=MyPos('.', s3, i+1);
			if j>0 then begin
				month:=copy(s3, i+1, j-i-1);
				year:=copy(s3, j+1, length(s3)-j);
				birth_date:=month+'/'+day+'/'+year;
			end;
		end;
	end;

	//filmography
	mode:=0;
	i:=pos('<ul', s);
	j:=pos('</ul>', s);
	s2:=copy(s, i, j-i);
	i:=pos('<li>', s2);
	while i>0 do begin
		j:=MyPos('</li>', s2, i+1);
		s3:=copy(s2, i+4, j-i+1);
		code:=ParseString(s3, '<a href="/(.*?)-filmi/', 1);
		name:=ParseString(s3, 'class="ss">(.*?)</a>', 1);
		year:=ParseString(s3, '</i>, (\d+),', 1);
		if (pos('Oyuncu', s3)>0) or (pos('Seslendirme', s3)>0) then 
			mode:=3;
		if (pos('netmen', s3)>0) then
			mode:=1;
		if (pos('Senaryo', s3)>0) then
			mode:=2;
		if (name<>'') and (mode>0) then
			SetMovieName(name, '', mode, code, year);
		delete(s2, 1, j+5);
		i:=pos('<li', s2);
	end;
end;

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

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