const
	script_name='Film.tv.it';
	language='Italian';
	version=26;
	author='Vlad';
	MinProgramBuild=6000;
	PCount = 12;
	//MaxPicturesCount = 3;
var
	DownloadMovieBigCover, DownloadMovieShortCast, DownloadMovieFullCast, 
	DownloadMovieBigDescription, DownloadMovieComments, 
	DownloadMovieStudios,	DownloadMovieAwards, DownloadMovieEarnings, 
	DownloadMovieAdditionalInfo, DownloadMovieSeasonsList: boolean;
	MaxShotsCount, MaxPostersCount: integer;

	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;
	
	foto_referer: String;
	
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 MovieSearch: integer;
var
	i, j, k, l: integer;
	title, year, description, small_cover, original_title: string;
begin
	s:='';
	titles_count:=0;
	//if the search is forwarded to the movie's page (only one result)
	if (pos('<title>Ricerca ', all)<=0) then begin
		title_code:=ParseString(all, '<li id="sel"><a href="/scheda.php/film/(\d+)/', 1);
		if title_code<>'' then begin
			SetSearchResult('movie', title_code);
			titles_count:=1;
		end;
	end
	//otherwise check seach results
	else begin		
		i:=pos('<div id="ricerca">', all);
		if i>0 then begin
			j:=MyPos('<!-- fine ricerca -->', all, i+1);
			if j>0 then
				s:=copy(all, i+17, j-i-17);
		end;
		if (s='') or (pos('<h3>Non ho trovato nulla!</h3>', all)>0) then
			//Movie not found
		else begin
		i := pos('<div class="box">',s);
		while i>0 do begin
			j := MyPos('<br class="clear" />', s, i+1);

			s2 := copy(s, i, j-i);
			title_code:=ParseString(s2, '<a href="/film/(\d+)/', 1);
						
			title:=GetStr(s2, '<h3>', '</h3>');
			title:=RemoveTags(title);
			title:=DecodeHTML(title);
			title:=ClearExtraSpaces(title);

			small_cover := GetStr(s2, '<img src="', '" alt');
			if Pos('DUMMY', small_cover) > 0 then
				small_cover := '';
			
			year := GetStr(s2, '</h3> <span>(', ')</span></dt>');
			year := ParseString(year, '^.*?, (\d+)$', 1);
			
			original_title := GetStr(s2, '<dd>Titolo originale: <em>', '</em></dd>');
			
			description := GetStr(s2, '<dd>di ', '<br />');
			
			while (length(title)>0) and (title[1]=' ') do
				delete(title, 1, 1);
			if title <> '' then begin
				SetSearchResult2(title, title_code, year, original_title, description, small_cover);
				titles_count:=titles_count+1;
			end;
			delete(s, 1, j+20);
			i:=pos('<div class="box">', s);
			end;
		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
	MovieName := EncodeURL(MovieName, 'iso-8859-1');
	url:=format('http://www.film.tv.it/cerca.php?q=%s&x=0&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.film.tv.it/film/%s/', [code]);
end;

//returned link to front cover
function GetAmazonImages: string;
var
	res, all, url, s, s2, s3, amazon_url: string;
	i, j: integer;
begin
	//discard info in brackets
	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 title=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, referer: string;
	i: integer;
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 ClearExtraBlankCharacters(text: string): string;
begin
	// Clear all extra blank characters
	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 GetInfo(url: string; images_only: boolean): string;
var 
	keyword, s, s2, s3, plot_outline, shot, shot_prefix, shot_ext, temp: string;
	i, j, k, frames_count: integer;
	actors: boolean;
	film_dati: String;
begin
	result:='';
	all:=DownloadURL(url);
	if all='-1' then
		result:='-1'
	else
		result:=all;

	movie_code:=ParseString(url, 'http://www.film.tv.it/film/(\d+)/',1);
	movie_site_url:=GetMovieSiteURL(movie_code);
	foto_referer := movie_site_url + 'foto/';
	//movie title
	i:=pos('<h1>', all);
	j:=MyPos('</h1>', all, i+1);
	title:=copy(all, i+4, j-i-4);
	title:=RemoveTags(title);
	title:=DecodeHTML(title);
	title:=ClearExtraSpaces(title);
	i:=pos('>', title);
	if i>0 then
		delete(title, 1, i);
	i := Pos(' (', title);
	if i>0 then
		delete(title, i, length(title) - i + 1);
	title := ClearExtraQuotes(title);
	title := MovePrefixLeft(title);

	if title='' then
		exit;

	duration:=0;
	//original title
	i:=pos('[<i>', all);
	if i>0 then begin
		j:=MyPos('</i>', all, i+1);
		s2:=copy(all, i+4, j-i-4);
		original_title:=DecodeHTML(RemoveTags(s2));
		original_title:=ClearExtraSpaces(original_title);
	end;
	
	film_dati := ParseString(all, '<div id="filmDati">(.*?)]', 1);
		//Countries
	i:=pos('</i>,', film_dati);
	if i>0 then begin
		j:=MyPos('</a>', film_dati, i+1);
		s:=copy(film_dati, i+5, j-i-5);
		s:=ClearExtraBlankCharacters(RemoveTags(s));
		k:=pos('/', s);
		while k>0 do begin
			name:=copy(s, 1, k-1);
			if name<>'' then SetCountry(name);
			if (name='USA') or (name='Gran Bretagna') or (name='Australia') then
				ask_amazon:=true;
			delete(s, 1, k);
			k:=pos('/',s);
		end;
		if s<>'' then SetCountry(s);
		if (s='USA') or (s='Gran Bretagna') or (s='Australia') then
			ask_amazon:=true;
	end;
	
	//Year
	i:=Pos('<a href="/film/anno-', film_dati);
	if i>0 then
		year:=copy(film_dati, i+20, 4);
		
	//remove everything before genres
	delete(film_dati, 1, i);
	i := Pos('<a ', film_dati);
	delete(film_dati, 1, i-1);
	
	//Genres
	i:=Pos('<a href="/film/', film_dati);
	if i>0 then begin
		i:=MyPos('">', film_dati, i+1);
		j:=MyPos('</a>', film_dati, i+1);
		name:=RemoveTags(copy(film_dati, i+2, j-i-2));
		if name<>'' then SetGenre(name);
	end;
	
	//Duration
	i:=Pos('durata ', all);
	if i>0 then begin
		j:=MyPos('''', all, i+1);
		s:=copy(all, i+7, j-i-7);
		try
			duration:=StrToInt(s);
		except
		end;
	end;
		
	
	//directors and actors
	//directors
	actors:=true;
	i:=pos('Regia di', all);
	if i>0 then begin
		j:=MyPos('Con ', all, i+1);
		k:=MyPos('</div>', all, i+1);
		if (j>k) or (j=0) then begin
			j:=k;
			actors:=false;
		end;
		s:=copy(all, i, j-i);
		i:=pos('<a href=', s);
		while i>0 do begin
			k:=MyPos('</a>', s, i+1);
			s2:=copy(s, i, k-i);
			code:=ParseString(s2, '<a href="/persona/(\d+)/', 1);
			name:=RemoveTags(s2);
			name:=DecodeHTML(name);
			name:=ClearExtraSpaces(name);
			if name<>'' then
				SetPersonName(name, '', 1, code);
			delete(s, 1, k+4);
			i:=pos('<a href=', s);
		end;
		//actors
		if actors then begin
			i:=j+4; //j is taken from the previous
			j:=MyPos('</div>', all, i+1);
			s:=copy(all, i, j-i);
			i:=pos('<a href=', s);
			while i>0 do begin
				j:=MyPos('</a>', s, i+1);
				s2:=copy(s, i, j-i);
				code:=ParseString(s2, '<a href="/persona/(\d+)/', 1);
				name:=RemoveTags(s2);
				name:=DecodeHTML(name);
				name:=ClearExtraSpaces(name);
				if name<>'' then
					SetPersonName(name, '', 3, code);
				delete(s, 1, j+4);
				i:=pos('<a href=', s);
			end;
		end;
	end;

	//image
	i:=pos('<img src="http://ftv01.stbm.it/imgbank/', all);
	if i>0 then begin
		j:=MyPos('"', all, i+11);
		s2:=copy(all, i, j-i+1);
		s3:=ParseString(s2, '<img src="(.*?)"', 1);
		if s3='/imgbank/DUMMY/locandina.gif' then 
			s3:=''
		else
			front_cover_url:=s3;
	end;
	if front_cover_url<>'' then
		SetImageName('', front_cover_url, '', 1);
	
	
	//description
	i:=pos('<p class="sommario">', all);
	if i>0 then begin
		j:=MyPos('</p>', all, i+1);
		s2:=copy(all, i, j-i);
		description:=ClearExtraBlankCharacters(RemoveTags(s2));
		description:=description+'<br><br>';
	end;
	i:=pos('<p class="trama"', all);
	if i>0 then begin
		j:=MyPos('</p>', all, i+1);
		s2:=copy(all, i, j-i);
		s2:=ClearExtraBlankCharacters(RemoveTags('<' + s2));
		description:=description+s2;
	end;
	i:=pos('<p class="commento">', all);
	if i>0 then begin
		j:=MyPos('</p>', all, i+1);
		s2:=copy(all, i, j-i);
		s2:=ClearExtraBlankCharacters(RemoveTags(s2));
		description:=description+'<br><br>';
		description:=description+s2;
	end;
	description:=DecodeHTML(description);
	
	
	//snapshots
	frames_count:=0;
	i:=pos('<div class="thumbs"', all);
	if i>0 then begin
		j:=MyPos('</div>', all, i+1);
		s2:=copy(all, i, j-i);
		i:=pos(' href="', s2);
		while (i>0) and (frames_count<MaxShotsCount) do begin
			j:=MyPos('</a>', s2, i+1);
			s3:=copy(s2, i, j-i);
			temp := GetStr(s3, ' href="', '"');
			if temp <> '' then begin
				temp := 'http://www.filmtv.it' + temp;
			end;
			
			if (pos('JPG', s3)>0) then
				shot_ext:='JPG'
			else
				shot_ext:='jpg';
			shot:=ParseString(s3, 'http://ftv01.stbm.it/imgbank/THUMB/.*?/(\d+).jpg', 1);
			if shot='' then
				shot:=ParseString(s3, 'http://ftv01.stbm.it/imgbank/THUMB/.*?/(\d+).JPG', 1);
			shot_prefix:=ParseString(s3, 'http://ftv01.stbm.it/imgbank/THUMB/(.*?)/\d+.*?', 1);
			if shot<>'' then begin
				shot:='http://ftv01.stbm.it/imgbank/GALLERYXL/'+shot_prefix+'/'+shot+'.'+shot_ext;
				DownloadURLWithReferer(shot, temp);
				SetImageName('', shot, '', 3);
				if (shot<>front_cover_url) then
					frames_count:=frames_count+1;
			end;(**)
			delete(s2, 1, j+10);
			i:=pos(' href="', s2);
		end;
	end;(**)

end;

//returned images links
procedure GetImages;
begin
	//if ask_amazon then
	//	GetAmazonImages;
end;

//get movie site url
function GetMovieSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else
		result:='http://www.film.tv.it/film/'+code+'/';
end;

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