const
	script_name='Allocine';
	language='French';
	version=30;
	author='Vlad/JeF';
	MinProgramBuild=5500;
	PCount = 12;
	//MaxPicturesCount = 3;
var
	DownloadMovieBigCover, DownloadMovieShortCast, DownloadMovieFullCast,
	DownloadMovieBigDescription, DownloadMovieComments, 
	DownloadMovieStudios,	DownloadMovieAwards, DownloadMovieEarnings, 
	DownloadMovieAdditionalInfo, DownloadMovieSeasonsList: boolean;
	MaxShotsCount: integer;

	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: 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, ask_amazon, shots_exist: boolean;
	original_title, mpaa, mpaa_rating: string;
	prefixes: array [1..PCount] of string;
	resolution, barcode, aspect_ratio, features, media_format, video_codec: string;

	OvrMainDlOptions, DlOriginalTitle, DlYear, DlGenres, DlCountries, DlDuration, DlMovieDesc, DlStudios : boolean;
	DlDirectors, DlActors, DlWriters, DlComposers : boolean;

function MovieSearch: integer;
var
	i, j: integer;
	title: string;
begin
	s:='';
	titles_count:=0;
	i:=pos('>Films <h4>', all);
	//28.01.09 Medved
	j:=pos('<!-- ADSENSE -->',all);
	if (i>0) and (j>i) then
		s:=copy(all, i, j-i);
	//28.01.09 Medved
	if (s='') or (pos('on n''a pas trouv', all)>0) then
	//movie not found
		titles_count:=NewMovieSearch  //20091114-JeF
	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;

function NewMovieSearch: integer;
var
	i, j: integer;
	title, year, small_cover, description: string;
begin
	s:='';
	titles_count:=0;
	i:=pos('Films</h2>', all);
	if (i=0) then i:=pos('Films'+#13#10+'</h2>', all);  //20130714-JeF
	j:=MyPos('class="navbar"', all, i);
	j:=MyPos('class="navbar"', all, j+1);
	if (j=0) then j:=MyPos('class="searchenginezone"', all, i);  //20130714-JeF
	if (i>0) and (j>i) then
		s:=copy(all, i, j-i);
  if (s='') or (pos('on n''a pas trouv', all)>0) then
	//movie not found
	else begin
		i := pos('<tr><td ',s);
		if (i>0) then delete(s,1,i-1);
		while i>0 do begin
			titles_count:=titles_count+1;
			description := '';
			original_title := '';
			j:=MyPos('<tr><td ',s,2);
			if (j=0) and (length(s)>1) then j:=length(s);
			if (j=0) then s:='';
			s2:=copy(s, 1, j-1);
			delete(s, 1, j-1);
			title_code:=ParseString(s2, '<a href=''/film/fichefilm_gen_cfilm=(\d+).html', 1);
			small_cover := GetStr(s2, 'src=''', '''');
			if Pos('emptymedia', small_cover)>0 then
				small_cover := '';
			//if small_cover<>'' then
				//small_cover := 'http://images.allocine.fr/' + small_cover + '.jpg';
			year := ParseString(s2, '<span class="fs11">'#10'(\d+)<br />', 1);
			j := Pos('</td>', s2);
			j := MyPos('<a href=', s2, j+1);
			delete(s2, 1, j-1);
			title := ParseString(s2, '^(.*?)</a>', 1);
			title := RemoveTags(title);
			original_title := ParseString(s2, '</a>'#10'\((.*?)\)', 1);
			original_title := DecodeHTML(original_title);
			j := Pos('<span ', s2);
			j := MyPos('<br />', s2, j+1);
			delete(s2, 1, j+6);
			s2 := ReplaceStr(s2, '<br />', ' ', -1);
			description := RemoveTags(s2);
			SetSearchResult2(title, title_code, year, original_title, description, small_cover);
			i:=pos('<tr><td ', 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
	//28.01.09 Medved
	url:=format('http://www.allocine.fr/recherche/?motcle=%s&rub=1&x=1&y=1', [MovieName]);
	//28.01.09 Medved
	url:=format('http://www.allocine.fr/recherche/1/?q=%s', [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.allocine.fr/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', [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);
			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
		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;

// 20091114-JeF
function ClearExtraBlankCharacters(text: string): string;
begin
	// Clear all extra blank charackters
	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 GetStr(str: String; s1: String; s2: String): String;
var
  I: Integer;
  J: Integer;
begin
  // Вырезает кусок строки между s1 и s2
	Result := '';
	I := Pos(s1, str);
	if I > 0 then
	begin
		J := MyPos(s2, str, I + Length(s1));
		if J > 0 then
  		Result := Copy(str, I + Length(s1), J - I - Length(s1));
	end;
end;

function GetInfo(url: string; images_only: boolean): string;
var 
	keyword, s, s2, s3, s4, plot_outline: string;
	i, j, k, l: integer;
	pictures_exist: boolean;
sl:tstringlist;
	small_photo: string;
begin
	result:='';
	BasicURL:=url;
	all:=DownloadURL(url);
	if all='-1' then
		result:='-1'
	else
		result:=all;

	movie_code:=ParseString(url, 'http://www.allocine.fr/film/fichefilm_gen_cfilm=(\d+).html', 1);
	movie_site_url:=GetMovieSiteURL(movie_code);
	
	if DownloadMovieFullCast or DownloadMovieStudios or OvrMainDlOptions then
		AddRequestForHTMLPageContent('http://www.allocine.fr/film/casting_gen_cfilm='+movie_code+'.html', False);
		
	//small image
	small_image:='';
	i:=pos('<div class="poster">', all);
	if i>0 then begin
		i:=MyPos('<img src=', all, i+1);
		j:=MyPos('''', all, i+12);
		s2:=copy(all, i+10, j-i-10);
		if (s2<>'') and (Pos('emptymedia', s2)=0) then
			small_image:=s2;
	end;
	s2:=ParseString(small_image, '/medias/(.*?)$', 1);
	
	//check if the 'galerie' exists
	pictures_exist:=false;
	shots_exist := false;
	i:=pos('/film/fichefilm-'+movie_code+'/photos/">Photos</a></li>', all);
	if i>0 then begin
		if MaxShotsCount > 0 then
			AddRequestForHTMLPageContent('http://www.allocine.fr/film/fichefilm-'+movie_code+'/photos/', False);
		front_cover_url:='http://images.allocine.fr/medias/'+s2;
		pictures_exist:=true;
		shots_exist:= true;
	end;
	
	//Movie title
	title:=ParseString(all, '<title>(.*?) - film ', 1);
	title:=DecodeHTML(title);
	for j:=1 to 20 do title:=ReplaceStr(title,chr(j),'',-1);
	i:=pos(' (19',title);											// 20091117-JeF
	if (i=0) then i:=pos(' (20',title);								// 20091117-JeF
	if (i>0) and (MyPos(')',title,i)=i+6) then delete(title,i,7);	// 20091117-JeF
	
	//Original title
	if DlOriginalTitle then begin
		original_title:=GetStr(all, '<th>Titre original</th><td>', '</td>');
		original_title:=DecodeHTML(original_title);
		original_title:=ClearExtraBlankCharacters(original_title);
		if original_title='' then
			original_title:=title;
	end;
	//Year
	year:='';
	if DlYear then begin
		s2:=GetStr(all, 'Année de production', '</td>');
		s2 := RemoveTags(s2);
		year := ClearExtraBlankCharacters(s2);
	end;
	
	//Genre
	if DlGenres then begin
		s2:=GetStr(all, 'Genre</span>', '</div>');
		i:=pos('<span itemprop="genre">', s2);
		while i>0 do begin
			i:=MyPos('>',s2,i+1)+1;
			j:=pos('</span>', s2);
			s3:=copy(s2, i, j-i);
			name:=s3;
			if name<>'' then begin
				name:=DecodeHTML(name);
				setGenre(name);
			end;
			delete(s2, 1, j+4);
			i:=pos('<span itemprop="genre">', s2);
		end;
	end;

	//countries
	if DlCountries then begin
		s2 := GetStr(all, 'Nationalité</span>', '</div>');
		i:=pos('underline">', s2);
		while i>0 do begin
			name:=GetStr(s2, 'underline">', '</span>');
			if name<>'' then begin
				name:=DecodeHTML(name);
				setCountry(name);
			end;
			s := LowerCase(name);
			if (s='américain') or (s='britannique') or (s='australien') then
				ask_amazon:=true;
			delete(s2, 1, i);
			i:=Pos('underline">', s2);
		end;
	end;

	//duration
	duration:=0;
	if DlDuration then begin
		s2:=GetStr(all, '<span itemprop="duration"', '</span>');
		s2:= '<' + s2;
		s2:=RemoveTags(s2);
		s2:=ClearExtraBlankCharacters(s2);
		s3:=ParseString(s2, '^(\d+)h', 1);
		j:=0;  
		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
			s3:=ParseString(s2, '(\d+) min', 1);
		if s3='' then
			s3:=ParseString(s2, '^(\d+) min', 1);
		if s3<>'' then
			try
			duration:=StrToInt(s3);
			except
			end;
		duration:=duration+j;
	end;
	
	//Synopsis / description
	description := '';
	if DlMovieDesc then begin
		description := GetStr(all, '<p itemprop="description">', '</p>');
		description:=RemoveTags(description);
		description:=DecodeHTML(description);
		description:=ClearExtraBlankCharacters(description);
	end;
	
	//budget
	salary := ClearExtraBlankCharacters(GetStr(all, '<th>Budget</th><td>', '</td>'));
	if length(salary)>2 then
		salary := 'Budget: ' + salary
	else
		salary := '';
	
	if not DownloadMovieFullCast then begin
		//directors
		if DlDirectors then begin
			s := GetStr(all, 'Réalisé par', '</div>');
			i := Pos(' href="/personne/fichepersonne_gen_cpersonne=', s);
			while i>0 do begin
				j := MyPos('</a>', s, i+1);
				s2 := copy(s, i, j-i);
				s2 := '<' + s2;
				name := DecodeHTML(RemoveTags(s2));
				code := ParseString(s2, '/personne/fichepersonne_gen_cpersonne=(\d+).html', 1);
				if name <> '' then
					SetPersonName(name, '', 1, code);
				delete(s, 1, j);
				i := Pos(' href="/personne/fichepersonne_gen_cpersonne=', s);
			end;
		end;
		//actors
		if DlActors then begin
			s := GetStr(all, 'Avec </span>', '</div>');
			i := Pos(' href="/personne/fichepersonne_gen_cpersonne=', s);
			while i>0 do begin
				j := MyPos('</a>', s, i+1);
				s2 := copy(s, i, j-i);
				s2 := '<' + s2;
				name := DecodeHTML(RemoveTags(s2));
				code := ParseString(s2, '/personne/fichepersonne_gen_cpersonne=(\d+).html', 1);
				if name <> '' then
					SetPersonName(name, '', 3, code);
				delete(s, 1, j);
				i := Pos(' href="/personne/fichepersonne_gen_cpersonne=', s);
			end;
		end;
	end;
	
	if DownloadMovieFullCast or DownloadMovieStudios or OvrMainDlOptions then begin
		//download casting page
		url:='http://www.allocine.fr/film/casting_gen_cfilm='+movie_code+'.html';
		all:=GetHTMLPageContentFromRequest(url);
		
		if DownloadMovieFullCast or OvrMainDlOptions then begin
			//Directors
			if DlDirectors then begin
				s2:=GetStr(all, 'Réalisateurs'#10'</h2>', '</ul>');
				i:=pos('<li'#10, s2);
				while i>0 do begin
					j:=MyPos('</li>', s2, i+1);
					s3:=copy(s2, i, j-i+5);
					name:=GetStr(s3, 'html">', '</a>');
					if name = '' then
						name:=ParseString(s3, '<span class="acLnk.*?">(.*?)</span>', 1);
					name:=RemoveTags(name);
					name:=DecodeHTML(name);
					name:=ClearExtraBlankCharacters(name);
					small_photo := ParseString(s2, '<img src=''http://images.allocine.fr/.*?/medias/nmedia/(.*?).jpg', 1);			
					if small_photo <> '' then
						small_photo := 'http://images.allocine.fr/medias/nmedia/' + small_photo + '.jpg';
					code:=ParseString(s3, ' href="/personne/fichepersonne_gen_cpersonne=(\d+).html', 1);
					if (name>'') then setPersonName(name, '', 1, code, small_photo);
					delete(s2, 1, j);
					i:=pos('<li'#10, s2);
				end;
			end;
			
			//Actors
			if DlActors then begin
				s2:=GetStr(all, 'Acteurs et actrices'#10'</h2>', '<h2 ');
				i:=pos('<li'#10, s2);
				while i>0 do begin
					s3:=GetStr(s2, '<li'#10, '</li>');
					character:=GetStr(s3, 'Rôle : ', '</p>');
					character:=ClearExtraBlankCharacters(character);
					character:=DecodeHTML(character);
					code:=ParseString(s3, ' href="/personne/fichepersonne_gen_cpersonne=(\d+).html', 1);
					name:=ParseString(s3, 'html">(.*?)</a>', 1);
					if name = '' then
						name:=ParseString(s3, '<span class="acLnk.*?">(.*?)</span>', 1);
					name:=RemoveTags(name);
					name:=DecodeHTML(name);
					l := Pos('(as ', name);
					if l > 0 then
						delete(name, l, length(name) - l + 1);
					name:=ClearExtraBlankCharacters(name);
					small_photo := ParseString(s3, '<img src=''http://images.allocine.fr/.*?/medias/nmedia/(.*?).jpg', 1);			
					if small_photo <> '' then
						small_photo := 'http://images.allocine.fr/medias/nmedia/' + small_photo + '.jpg';
					if (name>'') then setPersonName(name, character, 3, code, small_photo);
					delete(s2, 1, i+5);
					i:=pos('<li'#10, s2);
				end;
				i:=Pos('<tr', s2);
				while i>0 do begin
					s3:=GetStr(s2, '<tr', '</tr>');
					k:=Pos('<td>', s3);
					l:=MyPos('</td>', s3, k+1);
					s4:=copy(s3, k+4, l-k-4);
					character:=ClearExtraBlankCharacters(s4);
					code:=ParseString(s3, ' href="/personne/fichepersonne_gen_cpersonne=(\d+).html', 1);
					name:=GetStr(s3, '<span itemprop="name">', '</span>');
					if name = '' then
						name:=ParseString(s3, '<span class="acLnk.*?">(.*?)</span>', 1);
					if name = '' then
						name:=ParseString(s3, 'html">(.*?)</a>', 1);
					name:=RemoveTags(name);
					name:=DecodeHTML(name);
					name:=ClearExtraBlankCharacters(name);
					if name>'' then
						SetPersonName(name, character, 3, code);
					delete(s2, 1, i+5);
					i:=Pos('<tr', s2);
				end;
			end;
			
			//Screenplay writers
			if DlWriters then begin
				i:=pos('nario'#10'</h2>', all);
				s2 := GetStr(all, 'nario'#10'</h2>', '<h2 ');
				i:=pos('<tr', s2);
				while i>0 do begin
					j:=MyPos('</tr>', s2, i+1);
					s3:=copy(s2, i, j-i+5);
					name:=ParseString(s3, 'html">(.*?)</a>', 1);
					if name = '' then
						name := ParseString(s3, '<span class="acLnk.*?">(.*?)</span>', 1);
					name:=RemoveTags(name);
					name:=DecodeHTML(name);
					name:=ClearExtraBlankCharacters(name);
					small_photo := ParseString(s3, '<img src=''http://images.allocine.fr/.*?/medias/nmedia/(.*?).jpg', 1);			
					if small_photo <> '' then
						small_photo := 'http://images.allocine.fr/medias/nmedia/' + small_photo + '.jpg';
					code:=ParseString(s3, ' href="/personne/fichepersonne_gen_cpersonne=(\d+).html', 1);
					if name<>'' then
						setPersonName(name, '', 2, code, small_photo);
					delete(s2, 1, j);
					i:=pos('<tr', s2);
				end;
			end;
			
			//Composers
			if DlComposers then begin
				i:=pos('Equipe technique'#10'</h2>', all);
				j:=MyPos('</table>', all, i+1);
				s2:=copy(all, i, j-i);
				i:=pos('Compositeur', s2);
				while i>0 do begin
					j:=MyPos('</tr>', s2, i+1);
					s3:=copy(s2, i, j-i+5);
					code:=ParseString(s3, ' href="/personne/fichepersonne_gen_cpersonne=(\d+).html', 1);
					name:=ParseString(s3, 'html">(.*?)</a>', 1);
					if name = '' then
						name := ParseString(s3, '<span class="acLnk.*?">(.*?)</span>', 1);
					name:=RemoveTags(name);
					name:=DecodeHTML(name);
					name:=ClearExtraBlankCharacters(name);
					small_photo := ParseString(s3, '<img src=''http://images.allocine.fr/.*?/medias/nmedia/(.*?).jpg', 1);			
					if small_photo <> '' then
						small_photo := 'http://images.allocine.fr/medias/nmedia/' + small_photo + '.jpg';
					if name<>'' then
						setPersonName(name, '', 4, code, small_photo);
					delete(s2, 1, j-1);
					i:=pos('Compositeur', s2);
				end;
			end;
		end;
		
		//Studios
		if DownloadMovieStudios or OvrMainDlOptions then begin
			if DlStudios then begin
				i:=pos('Sociétés'#10'</h2>', all);
				j:=MyPos('</table>', all, i+1);
				s2:=copy(all, i, j-i);
				i:=pos('<td>Production', s2);
				while i>0 do begin
					s3 := GetStr(s2, '<td>Production', '</tr>');
					name:=RemoveTags(s3);
					name:=DecodeHTML(name);
					name:=ClearExtraBlankCharacters(name);
					if name<>'' then
						SetStudio(name);
					delete(s2, 1, i+4);
					i:=pos('<td>Production', s2);
				end;
			end;
		end;
	end;

end;

//returned images links
procedure GetImages;
begin
	if front_cover_url<>'' then
		SetImageName('', front_cover_url, '', 1);
	if ask_amazon and DownloadMovieBigCover then
		GetAmazonImages;
	if (small_image<>'') and (front_cover_url = '') then
		SetImageName('', small_image, '', 1);
	if (shots_exist) and (MaxShotsCount>0) then
		GetShots;
end;

procedure GetShots;
var
	i, j, shots_cnt: Integer;
	url, s, s2, image_code, image_url: String;
begin
	shots_cnt := 0;
	url := 'http://www.allocine.fr/film/fichefilm-'+movie_code+'/photos/';
	all := GetHTMLPageContentFromRequest(url);
	i := Pos('Photos du film', all);
	if i>0 then
		delete(all, 1, i-1);
	s := '<a href="/film/fichefilm-'+movie_code+'/photos/detail';
	i := Pos(s, all);
	while (i>0) and (shots_cnt<=MaxShotsCount) do begin
		shots_cnt := shots_cnt + 1;
		j := MyPos('</a>', all, i+1);
		s2 := copy(all, i, j-i);
		//image_code := ParseString(s2, 'src=''http://images.allocine.fr/c_100_100/b_1_d6d6d6/medias/(.*?).jpg''', 1);
		image_code := GetStr(s2, ' src=''', '''');
		image_code := GetStr(image_code, '/medias/', '.jpg');
		if image_code <> '' then begin
			image_url := 'http://images.allocine.fr/medias/'+image_code+'.jpg';
			SetImageName('', image_url, '', 3);
		end;
		delete(all, 1, j);
		i := Pos(s, all);
	end;		
end;

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

function BoolToInt(b: boolean): integer;
begin
  if b then result:=1
  else result:=0;
end;

function IntToBool(intval: integer): boolean;
begin
  if intval < 1 then result := false
  else result := true;
end;

//show available settings for the script
function ShowScriptOptions: integer;
var
	s: string;
	f: TForm;
	b: TButton;
	cbOvrMainDlOptions, cbDlOriginalTitle, cbDlYear, cbDlGenres, cbDlCountries, cbDlDuration: TCheckBox;
	cbDlMovieDesc, cbDlDirectors, cbDlActors, cbDlWriters, cbDlComposers, cbDlStudios: TCheckBox;
	cbAdvSearch: TCheckBox;
	labTitleLang, labMPAADefault, labMPAAReplace, labLocalService, labPicSize, labPicMulti: TLabel;
	edTitleLang, edMPAADefault, edMPAAReplace, edPicSizeX, edPicSizeY: TEdit;
	gbDlOptions: TGroupBox;
	comboLocalService: TComboBox;
	posx, posy, my_value: integer;
	cbTMDBPosters, cbTMDBImages: TCheckBox;

procedure ButtonClick(Sender: TButton);
var
	zn: integer;
begin
	try
		RegWriteInteger('ALLOCINE_OvrMainDlOptions', BoolToInt(cbOvrMainDlOptions.Checked));
		RegWriteInteger('ALLOCINE_DlOriginalTitle' , BoolToInt(cbDlOriginalTitle.Checked));
		RegWriteInteger('ALLOCINE_DlYear'          , BoolToInt(cbDlYear.Checked));
		RegWriteInteger('ALLOCINE_DlGenres'        , BoolToInt(cbDlGenres.Checked));
		RegWriteInteger('ALLOCINE_DlCountries'     , BoolToInt(cbDlCountries.Checked));
		RegWriteInteger('ALLOCINE_DlDuration'      , BoolToInt(cbDlDuration.Checked));
		RegWriteInteger('ALLOCINE_DlMovieDesc'   , BoolToInt(cbDlMovieDesc.Checked));
		RegWriteInteger('ALLOCINE_DlDirectors'     , BoolToInt(cbDlDirectors.Checked));
		RegWriteInteger('ALLOCINE_DlActors'        , BoolToInt(cbDlActors.Checked));
		RegWriteInteger('ALLOCINE_DlWriters'       , BoolToInt(cbDlWriters.Checked));
		RegWriteInteger('ALLOCINE_DlComposers'     , BoolToInt(cbDlComposers.Checked));

	except
	end;

	f.ModalResult := mrOk;
end;

begin
	result:=0;
	f := TForm.Create(nil);
	f.Caption := GetLangStr(1924);
	f.BorderStyle := bsDialog;
	f.Position := poScreenCenter;
	f.Width:=450;
	f.Height:=300;
	f.AutoSize:=false;
	
	posx :=  10;
	posy := -10;
	
	posy := posy + 20;
	cbOvrMainDlOptions := TCheckBox.Create(f);
	cbOvrMainDlOptions.Name := 'cbOvrMainDlOptions';
	cbOvrMainDlOptions.Parent := f;
	cbOvrMainDlOptions.SetBounds(posx, posy, 350, 20);
	if GetLangStr(2744) = '' 
		then cbOvrMainDlOptions.Caption := 'Override Global Download Options (F10)'
	else cbOvrMainDlOptions.Caption := GetLangStr(2744);
	if GetLangStr(2745) = '' 
		then cbOvrMainDlOptions.Hint := 'Override values/flags set in main download options window (F10)'
	else cbOvrMainDlOptions.Hint := GetLangStr(2745);
	cbOvrMainDlOptions.Checked := IntToBool(RegReadInteger('ALLOCINE_OvrMainDlOptions', 0));

	posy := posy + 10;
	gbDlOptions:=TGroupBox.Create(f);
	gbDlOptions.Name:='gbDlOptions';
	gbDlOptions.Parent:=f;
	gbDlOptions.SetBounds(posx, posy + 20, 425, 30);
	gbDlOptions.Caption:=' ' + GetLangStr(2734) + ' ';
	gbDlOptions.Hint:=GetLangStr(2734);
	posy := 0;
	
	posy := posy + 20;
	gbDlOptions.Height := gbDlOptions.Height + 20;
	cbDlOriginalTitle := TCheckBox.Create(f);
	cbDlOriginalTitle.Name := 'cbDlOriginalTitle';
	cbDlOriginalTitle.Parent := gbDlOptions;
	cbDlOriginalTitle.SetBounds(posx, posy, 200, 20);
	cbDlOriginalTitle.Caption := GetLangStr(167);
	cbDlOriginalTitle.Checked := IntToBool(RegReadInteger('ALLOCINE_DlOriginalTitle', 1));
		
	posy := posy + 20;
	gbDlOptions.Height := gbDlOptions.Height + 20;
	cbDlDuration := TCheckBox.Create(f);
	cbDlDuration.Name := 'cbDlDuration';
	cbDlDuration.Parent := gbDlOptions;
	cbDlDuration.SetBounds(posx, posy, 200, 20);
	cbDlDuration.Caption := GetLangStr(147);
	cbDlDuration.Checked := IntToBool(RegReadInteger('ALLOCINE_DlDuration', 1));
		
	posy := posy + 20;
	gbDlOptions.Height := gbDlOptions.Height + 20;
	cbDlYear := TCheckBox.Create(f);
	cbDlYear.Name := 'cbDlYear';
	cbDlYear.Parent := gbDlOptions;
	cbDlYear.SetBounds(posx, posy, 200, 20);
	cbDlYear.Caption := GetLangStr(169);
	cbDlYear.Checked := IntToBool(RegReadInteger('ALLOCINE_DlYear', 1));
		
	posy := posy + 20;
	gbDlOptions.Height := gbDlOptions.Height + 20;
	cbDlGenres := TCheckBox.Create(f);
	cbDlGenres.Name := 'cbDlGenres';
	cbDlGenres.Parent := gbDlOptions;
	cbDlGenres.SetBounds(posx, posy, 200, 20);
	cbDlGenres.Caption := GetLangStr(179);
	cbDlGenres.Checked := IntToBool(RegReadInteger('ALLOCINE_DlGenres', 1));
		
	posy := posy + 20;
	gbDlOptions.Height := gbDlOptions.Height + 20;
	cbDlCountries := TCheckBox.Create(f);
	cbDlCountries.Name := 'cbDlCountries';
	cbDlCountries.Parent := gbDlOptions;
	cbDlCountries.SetBounds(posx, posy, 200, 20);
	cbDlCountries.Caption := GetLangStr(180);
	cbDlCountries.Checked := IntToBool(RegReadInteger('ALLOCINE_DlCountries', 1));
		
	posy := posy + 20;
	gbDlOptions.Height := gbDlOptions.Height + 20;
	cbDlStudios := TCheckBox.Create(f);
	cbDlStudios.Name := 'cbDlStudios';
	cbDlStudios.Parent := gbDlOptions;
	cbDlStudios.SetBounds(posx, posy, 200, 20);
	cbDlStudios.Caption := GetLangStr(493);
	cbDlStudios.Checked := IntToBool(RegReadInteger('ALLOCINE_DlStudios', 1));

	posx := 220;
	posy := 0;
	
	posy := posy + 20;
	cbDlMovieDesc := TCheckBox.Create(f);
	cbDlMovieDesc.Name := 'cbDlMovieDesc';
	cbDlMovieDesc.Parent := gbDlOptions;
	cbDlMovieDesc.SetBounds(posx, posy, 200, 20);
	cbDlMovieDesc.Caption := GetLangStr(162);
	cbDlMovieDesc.Checked := IntToBool(RegReadInteger('ALLOCINE_DlMovieDesc', 1));
		
	posy := posy + 20;
	cbDlDirectors := TCheckBox.Create(f);
	cbDlDirectors.Name := 'cbDlDirectors';
	cbDlDirectors.Parent := gbDlOptions;
	cbDlDirectors.SetBounds(posx, posy, 200, 20);
	cbDlDirectors.Caption := GetLangStr(181);
	cbDlDirectors.Checked := IntToBool(RegReadInteger('ALLOCINE_DlDirectors', 1));
		
	posy := posy + 20;
	cbDlActors := TCheckBox.Create(f);
	cbDlActors.Name := 'cbDlActors';
	cbDlActors.Parent := gbDlOptions;
	cbDlActors.SetBounds(posx, posy, 200, 20);
	cbDlActors.Caption := GetLangStr(164);
	cbDlActors.Checked := IntToBool(RegReadInteger('ALLOCINE_DlActors', 1));
		
	posy := posy + 20;
	cbDlWriters := TCheckBox.Create(f);
	cbDlWriters.Name := 'cbDlWriters';
	cbDlWriters.Parent := gbDlOptions;
	cbDlWriters.SetBounds(posx, posy, 200, 20);
	cbDlWriters.Caption := GetLangStr(182);
	cbDlWriters.Checked := IntToBool(RegReadInteger('ALLOCINE_DlWriters', 1));
		
	posy := posy + 20;
	cbDlComposers := TCheckBox.Create(f);
	cbDlComposers.Name := 'cbDlComposers';
	cbDlComposers.Parent := gbDlOptions;
	cbDlComposers.SetBounds(posx, posy, 200, 20);
	cbDlComposers.Caption := GetLangStr(491);
	cbDlComposers.Checked := IntToBool(RegReadInteger('ALLOCINE_DlComposers', 1));
		
	b := TButton.Create(f);
	b.Name := 'btnOK';
	b.Parent := f;
	b.SetBounds(trunc(f.width/2)-40, f.Height-60, 80, 25);
	b.Caption := '&OK';
	b.OnClick := @ButtonClick;

	f.ShowModal;
	f.Free;
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';

	OvrMainDlOptions := IntToBool(RegReadInteger('ALLOCINE_OvrMainDlOptions', 0));
	DlOriginalTitle  := IntToBool(RegReadInteger('ALLOCINE_DlOriginalTitle', 1));
	DlYear           := IntToBool(RegReadInteger('ALLOCINE_DlYear', 1));
	DlGenres         := IntToBool(RegReadInteger('ALLOCINE_DlGenres', 1));
	DlCountries      := IntToBool(RegReadInteger('ALLOCINE_DlCountries', 1));
	DlDuration       := IntToBool(RegReadInteger('ALLOCINE_DlDuration', 1));
	DlMovieDesc      := IntToBool(RegReadInteger('ALLOCINE_DlMovieDesc', 1));
	DlDirectors      := IntToBool(RegReadInteger('ALLOCINE_DlDirectors', 1));
	DlActors         := IntToBool(RegReadInteger('ALLOCINE_DlActors', 1));
	DlWriters        := IntToBool(RegReadInteger('ALLOCINE_DlWriters', 1));
	DlComposers      := IntToBool(RegReadInteger('ALLOCINE_DlComposers', 1));
	DlStudios        := IntToBool(RegReadInteger('ALLOCINE_DlStudios', 1));
	
end.