const
	script_name='Screenrush';
	language='English';
	version=3;
	author='Vlad';
	MinProgramBuild=4007;
	PCount = 12;
	//MaxPicturesCount = 3;
var
	title, MovieName, s, s2, s3, s4, year, director, name, character, BasicURL, country, description, salary: string;
	movie_code, movie_site_url, title_code, small_image, imdb_code: string;
	titles: array of string;
	titles_count, image_mode: integer;
	i, j: integer;
	director_code, code: string;
	directors, writers, actors, characters, genres, imdb_codes: array of string;
	duration: integer;
	imdb_rating: double;
	front_cover_url: string;
	posters_exists: 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: integer;
	title: string;
begin
	s:='';
	titles_count:=0;
	i:=pos('>Films <h4>', all);
	j:=pos('<tr></tr></table></td></tr></table>',all);
	if (i>0) and (j>i) then
		s:=copy(all, i, j-i);
	if (s='') or (pos('we have no matches', all)>0) then
	//movie not found
	else begin
		i := pos('<h4><a href="/film/fichefilm_gen_cfilm=',s);
		while i>0 do begin
			delete(s, 1, i+3);
			titles_count:=titles_count+1;
			SetLength(titles, titles_count);
			i:= pos('<hr />', s);
			s2:=copy(s, 1, i+5);
			title_code:=ParseString(s2, '<a href="/film/fichefilm_gen_cfilm=(\d+).html"', 1);
			j:=pos('<div', s2);
			while j>0 do begin
				delete(s2, j, 4);
				insert(' - <', s2, j);
				j:=pos('<div', s2);
			end;
			title:=RemoveTags(s2);
			title:=DecodeHTML(title);
			title:=ClearExtraSpaces(title);
			
			while (length(title)>0) and (title[1]=' ') do
				delete(title, 1, 1);
			titles[titles_count-1]:=title_code;
			SetSearchResult(title, title_code);
			i := pos('<h4><a href="/film/fichefilm_gen_cfilm=',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.screenrush.co.uk/recherche/?motcle=%s&rub=1&x=1&y=1', [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.screenrush.co.uk/film/fichefilm_gen_cfilm=%s.html', [code])
end;

function GetAmazonImages: string;
var
	res, all, url, s, s2, s3, amazon_url: string;
	i, j: integer;
begin
	s:=original_title;
	amazon_url:='';
	url:=format('http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&SubscriptionId=0J0N83PT9M72BYH98YR2&Operation=ItemSearch&Keywords=%s&SearchIndex=Video&ResponseGroup=Request,Images,Tracks,ItemAttributes', [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);
			if s3<>'' then 
			begin
				//if title=s3 then begin //!!!
				if (pos(UpperCase(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
		SetImageName('', amazon_url, '', 1);
	res:=amazon_url;

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, plot_outline: string;
	i, j, k: integer;
	pictures_exist: boolean;
begin
	result:='';
	BasicURL:=url;
	all:=DownloadURL(url);
	if all='-1' then
		result:='-1'
	else
		result:=all;
	
	movie_code:=ParseString(url, 'http://www.screenrush.co.uk/film/fichefilm_gen_cfilm=(\d+).html', 1);
	movie_site_url:=GetMovieSiteURL(movie_code);
	
	//Movie title
	title:=ParseString(all, '<title>(.*?) - Screenrush.co.uk</title>', 1);
	title:=DecodeHTML(title);
	
	//Original title
	i:=pos('Titre original : ', all);
	if i>0 then begin
		j:=MyPos('</h3>', all, i+1);
		//21 is the number of letters in the above expression
		s2:=copy(all, i+17, j-i-17);
		original_title:=RemoveTags(s2);
		original_title:=DecodeHTML(original_title);
		original_title:=ClearExtraSpaces(original_title);
	end;
	if original_title='' then
		original_title:=title;
	
	//Year
	i:=pos('Production year: ', all);
	if i>0 then
		year:=copy(all, i+17, 4);
	
	//Genre
	i:=pos('>Genre: <a', all);
	if i>0 then begin
		j:=MyPos('</h3>', all, i+1);
		s2:=copy(all, i, j-i);
		i:=pos('class="link1">', s2);
		while i>0 do begin
			j:=pos('</a>', s2);
			s3:=copy(s2, i, j-i);
			name:=ParseString(s3, 'class="link1">(.*?)$', 1);
			if name<>'' then begin
				name:=DecodeHTML(name);
				setGenre(name);
			end;
			delete(s2, 1, j+5);
			i:=pos('class="link1">', s2);
		end;
	end;
	//countries
	s2:=ParseString(all, 'Nationality: (.*?).&nbsp;</h3>', 1);
	if s2<>'' then begin
		i:=pos(',', s2);
		while i>0 do begin
			name:=copy(s2, 1, i-1);
			if name<>'' then begin
				name:=DecodeHTML(name);
				setCountry(name);
			end;
			delete(s2, 1, i+1);
			i:=pos(',', s2);
		end;
		if s2<>'' then begin
			name:=DecodeHTML(s2);
			setCountry(name);	
		end;
	end;

	//duration
	duration:=0;
	j:=0;         
	s2:=ParseString(all, 'Running time: (.*?).&nbsp;</h3>', 1);
	s3:=ParseString(s2, '^(\d+)h', 1);
	if s3<>'' then
		try
		//extract hours to j
		j:=StrToInt(s3);
		except
		end;
	//hours to minutes
	j:=j*60;
	s3:=ParseString(s2, ' (\d+)min', 1);
	if s3='' then
		//if the duration is less than an hour
		s3:=ParseString(s2, '^(\d+)min', 1);
	if s3<>'' then
		try
		duration:=StrToInt(s3);
		except
		end;
	duration:=duration+j;
	
	//Synopsis / description
	i:=pos('Synopsis</h2>', all);
	if i>0 then begin
		i:=MyPos('<h4>', all, i+1);
		j:=MyPos('</h4>', all, i+1);
		s2:=copy(all, i, j-i);
		if s2<>'' then begin
			description:=RemoveTags(s2);
			description:=DecodeHTML(description);
			description:=ClearExtraSpaces(description);
		end;
	end;
	
	//small image
	i:=pos('<td valign="top" width="120">', all);
	if i>0 then begin
		j:=MyPos('class="affichette" />', all, i+1);
		s2:=copy(all, i, j-i);
		if s2<>'' then
			small_image:=ParseString(s2, '<img src="(.*?)" border="0"', 1);
	end;
	
	//check if the 'galerie' exists
	pictures_exist:=false;
	i:=pos('<div  align="center"><a href="/film/galerievignette_gen_cfilm=', all);
	if i>0 then
		pictures_exist:=true;
	
	//download casting page
	url:='http://www.screenrush.co.uk/film/casting_gen_cfilm='+movie_code+'.html';
	all:=DownloadURL(url);
	
	//Directors
	i:=pos('Director</h5></td>', all);
	if i>0 then begin
		j:=MyPos('</table>', all, i+1);
		s2:=copy(all, i+19, j-i-19);
		i:=pos('<a href=', s2);
		while i>0 do begin
			j:=MyPos('</a>', s2, i+1);
			s3:=copy(s2, i, j-i+5);
			name:=ParseString(s3, 'class="link1">(.*?)</a>', 1);
			name:=DecodeHTML(name);
			code:=ParseString(s3, '<a href="/personne/fichepersonne_gen_cpersonne=(\d+).html', 1);
			if name<>'' then
				setPersonName(name, '', 1, code);
			delete(s2, 1, j);
			i:=pos('<a href=', s2);
		end;
	end;
	
	//Actors
	i:=pos('Actor(s)</h2><hr />', all);
	if i>0 then begin
		j:=MyPos('</table>', all, i+1);
		s2:=copy(all, i+14, j-i-14);
		i:=pos('<tr ', s2);
		while i>0 do begin
			j:=MyPos('</tr>', s2, i+1);
			s3:=copy(s2, i, j-i);
			i:=pos('<h5>', s3);
			k:=MyPos('</h5>', s3, i+1);
			character:=copy(s3, i+4, k-i-4);
			character:=DecodeHTML(character);
			code:=ParseString(s3, '<a href="/personne/fichepersonne_gen_cpersonne=(\d+).html', 1);
			name:=ParseString(s3, 'class="link1">(.*?)</a>', 1);
			name:=DecodeHTML(name);
			if name<>'' then
				setPersonName(name, character, 3, code);
			delete(s2, 1, j+5);
			i:=pos('<tr ', s2);
		end;
	end;
	
	//Screenplay writers
	i:=pos('Writer</h5></td>', all);
	if i>0 then begin
		j:=MyPos('</table>', all, i+1);
		s2:=copy(all, i+17, j-i-17);
		i:=pos('<a href=', s2);
		while i>0 do begin
			j:=MyPos('</a>', s2, i+1);
			s3:=copy(s2, i, j-i+5);
			name:=ParseString(s3, 'class="link1">(.*?)</a>', 1);
			name:=DecodeHTML(name);
			code:=ParseString(s3, '<a href="/personne/fichepersonne_gen_cpersonne=(\d+).html', 1);
			if name<>'' then
				setPersonName(name, '', 2, code);
			delete(s2, 1, j);
			i:=pos('<a href=', s2);
		end;
	end;
	
	//Composers
	i:=pos('<h5>Composer</h5></td>', all);
	j:=MyPos('</tr>', all, i+1);
	s2:=copy(all, i, j-i);
	i:=pos('<a href="/personne/fichepersonne_gen_cpersonne=', s2);
	while i>0 do begin
		j:=MyPos('</a>', s2, i+1);
		s3:=copy(s2, i, j-i+5);
		code:=ParseString(s3, '<a href="/personne/fichepersonne_gen_cpersonne=(\d+).html', 1);
		name:=ParseString(s3, 'class="link1">(.*?)</a>', 1);
		name:=DecodeHTML(name);
		if name<>'' then
			setPersonName(name, '', 4, code);
		delete(s2, 1, j-1);
		i:=pos('<a href="/personne/fichepersonne_gen_cpersonne=', s2);
	end;
	
	//Studios
	i:=pos('<h5>Production</h5></td>', all);
	j:=MyPos('</table>', all, i+1);
	s2:=copy(all, i+24, j-i-24);
	i:=pos('<td width="50%" valign="top" style="padding:5 2 0 0"><h5>', s2);
	if i>0 then
		delete(s2, i, length(s2)-i);
	i:=pos('<h4><a href="/societe/fichesociete_gen_csociete=', s2);
	while i>0 do begin
		j:=MyPos('</a>', s2, i+1);
		s3:=copy(s2, i, j-i+5);
		name:=ParseString(s3, 'class="link1"><h4>(.*?)</h4></a>', 1);
		name:=DecodeHTML(name);
		if name<>'' then
			SetStudio(name);
		delete(s2, 1, j+4);
		i:=pos('<h4><a href="/societe/fichesociete_gen_csociete=', s2);
	end;
	
	//download page with images
	if pictures_exist then begin
		url:='http://www.screenrush.co.uk/film/galerievignette_gen_cfilm='+movie_code+'.html';
		all:=DownloadURL(url);
		front_cover_url:=ParseString(all, '<img id=''imgNormal'' class=''photo'' src=''(.*?)''', 1);
		s2:=ParseString(all, '<img id=''imgNormal'' class=''photo'' src=''.*?'' title="(.*?)" alt', 1);
		if (s2=title) then
			image_mode:=1
		else
			image_mode:=3;
	end;

end;

//returned images links
procedure GetImages;
begin
	if front_cover_url<>'' then
		SetImageName('', front_cover_url, '', image_mode);
	GetAmazonImages;
	if small_image<>'' then
		SetImageName('', small_image, '', 1);
end;

//get person site url
function GetPersonSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else
		result:='http://www.screenrush.co.uk/personne/fichepersonne_gen_cpersonne='+code+'.html';
end;

//get movie site url
function GetMovieSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else
		result:='http://www.screenrush.co.uk/film/fichefilm_gen_cfilm='+code+'.html';
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.