const
	script_name='interSinema';
	language='Turkish';
	version=6;
	author='Vlad';
	MinProgramBuild=4007;
	PCount = 12;
	//MaxPicturesCount = 3;
var
	title, MovieName, s, s2, s3, s4, year, director, name, character, BasicURL, country, description, salary: string;
	episodes, movie_code, movie_site_url, title_code, imdb_code: string;
	titles: array of string;
	titles_count: integer;
	i, j: integer;
	director_code, code: string;
	//directors, writers, actors, characters, genres, imdb_codes: array of string;
	duration: integer;
	rating, imdb_rating: double;
	front_cover_url: string;
	posters_exists, ask_amazon: boolean;
	original_title, mpaa, mpaa_rating: string;
	prefixes: array [1..PCount] of string;
	resolution, barcode, aspect_ratio, features, media_format, video_codec: string;

function MovieSearch: integer;
var
	i, j, k: integer;
	title: string;
begin
	s:='';
	titles_count:=0;
	i:=pos('LMLER</b></font>', all);
	if i>0 then begin
		i:=MyPos('<p style=', all, i+1);
		j:=MyPos('</p>', all, i+1);
		if j>0 then
			s:=copy(all, i, j-i);
	end;
	if (s='') or (pos('bir film bulunamad', all)>0) then
		//Movie not found
	else begin
		i:=Pos('<a href=', s);
		while i>0 do begin
			titles_count:=titles_count+1;
			SetLength(titles, titles_count);
			j:=MyPos('</a>', s, i+1);
			s2:=copy(s, i, j-i);

			title_code:=ParseString(s2, '<a href="/(.*?)-filmi/"', 1);
			title:=RemoveTags(s2);
			title:=DecodeHTML(title);
			title:=ClearExtraSpaces(title);
			title:=ClearExtraBlankCharacters(title);

			while (length(title)>0) and (title[1]=' ') do
				delete(title, 1, 1);
			titles[titles_count-1]:=title_code;
			SetSearchResult(title, title_code);
			delete(s, 1, j);
			i:=Pos('<a href=', s);
		end;
	end;
	result:=titles_count;
end;

//get  movie name by barcode(UPC-12)
function GetMovieNameByUPC(barcode: string): string;
var
	url, all, s2, s3, res: string;
	i, j: integer;
begin
	res:='';
	url:=format('http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&SubscriptionId=0J0N83PT9M72BYH98YR2&Operation=ItemSearch&Keywords=%s&SearchIndex=Video&ResponseGroup=Request,Images,Tracks,ItemAttributes', [barcode]);	
	url:=ReplaceStr(url, ' ', '+', -1);
	all:=DownloadURL(url);
	//search image by title
	i:=pos('<Item>', all);
	while (i>0) and (res='') do begin
		j:=MyPos('</Item>', all, i+1);
		if j>0 then begin
			s2:=copy(all, i, j-i);
			s3:=ParseString(s2, '<Title>(.*?) \(.*\)</Title>', 1);
			if s3='' then
				s3:=ParseString(s2, '<Title>(.*?)</Title>', 1);
			if s3<>'' then
				res:=s3;
		end;
		i:=MyPos('<Item>', all, i+1);
	end;
	i:=pos('[', res);
	if i>0 then begin
		j:=MyPos(']', res, i+1);
		if j>0 then
			delete(res, i, j-i+1);
	end;
	result:=res;
end;

//Get search URL by movie name
function GetSearchURLbyName(MovieName, AYear, ABarcode: string): string;
var
	url: string;
begin
	url:=format('http://www.intersinema.com/ara.asp?ara=%s&I1.x=0&I1.y=0', [MovieName]);
	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:=MovieSearch;
end;

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

//returned link to front cover
function GetAmazonImages: string;
var
	res, all, url, s, s2, s3, amazon_url: string;
	i, j: integer;
begin
	s:=original_title;
	url:=format('http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&SubscriptionId=0J0N83PT9M72BYH98YR2&Operation=ItemSearch&Keywords=%s&SearchIndex=Video&ResponseGroup=Request,Images,Tracks,ItemAttributes', [original_title]);
	url:=ReplaceStr(url, ' ', '+', -1);
	all:=DownloadURL(url);
	//search image by title
	amazon_url:='';
	i:=pos('<Item>', all);
	while (i>0) and (amazon_url='') do begin
		j:=MyPos('</Item>', all, i+1);
		if j>0 then begin
			s2:=copy(all, i, j-i);
			s3:=ParseString(s2, '<Title>(.*?) \(.*\)</Title>', 1);
			if s3='' then
				s3:=ParseString(s2, '<Title>(.*?)</Title>', 1);
			s3 := DecodeHTML(s3);
			if s3<>'' then begin
				if pos(UpperCase(original_title), UpperCase(s3))=1 then begin
					amazon_url:=ParseString(s2, '<LargeImage><URL>(.*?)</URL>', 1);
				end;
			end;
		end;
		i:=MyPos('<Item>', all, i+1);
	end;
	if amazon_url<>'' then begin
		SetImageName('', amazon_url, '', 1);
		res:=amazon_url;
	end;
	result:=res;
end;

//save front cover to file
function SaveFrontCover(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 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 
	keyword, s, s2, s3, s4, cast_page, cast_url, plot_outline: string;
	i, j, k: integer;
begin
	result:='';
	all:=DownloadURL(url);
	if all='-1' then
		result:='-1'
	else
		result:=all;

	movie_code:=ParseString(url, 'http://www.intersinema.com/(.*?)-filmi$',1);
	//ShowMessage(movie_code);
	movie_site_url:=GetMovieSiteURL(movie_code);

	cast_url:='http://www.intersinema.com/'+movie_code+'-filmi-ekibi/';
	cast_page:=DownloadURL(cast_url);
	
	//movie title
	title:=ParseString(all, '<title>(.*?) Filmi </title>', 1);
	title:=RemoveTags(title);
	title:=DecodeHTML(title);
	title:=ClearExtraSpaces(title);
	title := ClearExtraQuotes(title);
	if title='' then
		exit;

	//original title
	i:=pos('Orijinal Adı :', all);
	if i>0 then begin
		j:=MyPos('</tr>', all, i+1);
		s2:=copy(all, i+15, j-i-15);
		original_title:=RemoveTags(s2);
		original_title:=ClearExtraBlankCharacters(original_title);
		original_title:=DecodeHTML(original_title);
		original_title:=ClearExtraSpaces(original_title);
		original_title:=MovePrefixLeft(original_title);
	end;

	//directors
	i:=pos('netmen : </b></td>', all);
	if i>0 then begin
		j:=MyPos('</tr>', all, i+1);
		s2:=copy(all, i+6, j-i-6);
		i:=pos('<a href', s2);
		while i>0 do begin
			j:=MyPos('</a>', s2, i+1);
			s3:=copy(s2, i, j+4);
			code:=ParseString(s3, '<a href="/(.*?)-filmleri/', 1);
			name:=ParseString(s3, '">(.*?)</a>', 1);
			name:=RemoveTags(name);
			name:=DecodeHTML(name);
			name:=ClearExtraSpaces(name);

			if name<>'' then
				setPersonName(name, '', 1, code);
			delete(s2, 1, j+4);
			i:=pos('<a href', s2);
		end;
	end;
	
	//actors
	i:=pos('Oyuncu&nbsp;', cast_page);
	if i>0 then begin
		j:=MyPos('</tr>', cast_page, i+1);
		s2:=copy(cast_page, i+13, j-i-13);
		i:=pos('<a href', s2);
		while i>0 do begin
			j:=MyPos('<br>', s2, i+1);
			if j=0 then
				j:=MyPos(' </td>', s2, i+1);
			s3:=copy(s2, i, j-i);
			code:=ParseString(s3, '<a href="/(.*?)-filmleri/', 1);
			name:=ParseString(s3, '">(.*?)</a>', 1);
			character:=ParseString(s3, '<i>\((.*?)\)</i>', 1);
			name:=RemoveTags(name);
			name:=DecodeHTML(name);
			name:=ClearExtraSpaces(name);

			if name<>'' then
				setPersonName(name, character, 3, code);
			delete(s2, 1, j+1);
			i:=pos('<a href', s2);
		end;
	end;
	
	//writers
	i:=pos('Senaryo&nbsp;', cast_page);
	if i>0 then begin
		j:=MyPos('</tr>', cast_page, i+1);
		s2:=copy(cast_page, i+13, j-i-13);
		i:=pos('<a href', s2);
		while i>0 do begin
			j:=MyPos('</a>', s2, i+1);
			s3:=copy(s2, i, j-i+4);
			code:=ParseString(s3, '<a href="/(.*?)-filmleri/', 1);
			name:=ParseString(s3, '">(.*?)</a>', 1);
			name:=RemoveTags(name);
			name:=DecodeHTML(name);
			name:=ClearExtraSpaces(name);

			if name<>'' then
				setPersonName(name, '', 2, code);
			delete(s2, 1, j+4);
			i:=pos('<a href', s2);
		end;
	end;

	//genres
	i:=pos('Filmin Türü :', all);
	if i>0 then begin
		j:=MyPos('</tr>', all, i+1);
		s2:=copy(all, i+14, j-i-14);
		s2:=RemoveTags(s2);
		s2:=ClearExtraBlankCharacters(s2);
		i:=pos(',', s2);
		while i>0 do begin
			s3:=copy(s2, 1, i-1);
			name:=RemoveTags(s3);
			name:=DecodeHTML(name);
			name:=ClearExtraSpaces(name);
			if name<>'' then
					SetGenre(name);
			delete(s2, 1, i+1);
			i:=pos(',', s2);
		end;
		name:=DecodeHTML(s2);
		name:=ClearExtraSpaces(name);
		if name<>'' then
			SetGenre(name);
	end;
	
	//description
	i:=pos('Konusu<br></font></b>', all);
	if i>0 then begin
		i:=MyPos('<tr>', all, i+1);
		j:=MyPos('</tr>', all, i+1);
		s2:=copy(all, i, j-i);
		description:=RemoveTags(s2);
		description:=DecodeHTML(description);
		description:=ClearExtraBlankCharacters(description);
	end;

	//image
	s2:=ParseString(all, 'src="/film/resimler(.*?)"></td>', 1);
	if s2<>'' then
		front_cover_url:=format('http://www.intersinema.com/film/resimler%s', [s2]);

	//studio
	i:=pos('Yapımcı Firma : </b></td>', all);
	if i>0 then begin
		j:=MyPos('</tr>', all, i+1);
		s2:=copy(all, i+17, j-i-17);
		s2:=RemoveTags(s2);
		s2:=ClearExtraBlankCharacters(s2);
		if s2<>'' then
			SetStudio(s2);
	end;
	
	//year
	i:=Pos('Yapım Yılı :', all);
	if i>0 then begin
		j:=MyPos('</tr>', all, i+1);
		s2:=copy(all, i+13, j-i-13);
		year:=ParseString(s2, 'align="left">(\d+)</td>', 1);
	end;

	//countries
	i:=Pos('Yapım Ülkesi :', all);
	if i>0 then begin
		j:=MyPos('</tr>', all, i+1);
		s2:=copy(all, i+15, j-i-15);
		s2:=RemoveTags(s2);
		s2:=ClearExtraBlankCharacters(s2);
		i:=Pos(',', s2);
		if i>0 then
			s4:=','
		else
			s4:='/';
		i:=Pos(s4, s2);
		while i>0 do begin
			s3:=copy(s2, 1, i-1);
			name:=RemoveTags(s3);
			name:=DecodeHTML(name);
			name:=ClearExtraSpaces(name);
			if name<>'' then
				SetCountry(name);
			delete(s2, 1, i);
			i:=pos(s4, s2);
		end;
		name:=RemoveTags(s2);
		name:=DecodeHTML(name);
		name:=ClearExtraSpaces(name);
		if name<>'' then
			SetCountry(name);

	end;
	
	//original language - s3
	i:=pos('Orijinal Dili :', all);
	if i>0 then begin
		j:=MyPos('</tr>', all, i+1);
		s2:=copy(all, i+16, j-i-16);
		if pos('İngilizce', s2)=1 then
			ask_amazon:=true;
	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;

//returned images links
procedure GetImages;
begin
	if ask_amazon then
		GetAmazonImages;	
	if front_cover_url<>'' then
		SetImageName('', front_cover_url, '', 1);
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

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.