const
	script_name='35mm';
	language='Italian';
	version=6;
	author='Dwiti';
	MinProgramBuild=5500;
	PCount = 12;
	//MaxPicturesCount = 3;
var

//20090205-Dwiti_Begin
	DownloadMovieBigCover, DownloadMovieShortCast, DownloadMovieFullCast, 
	DownloadMovieBigDescription, DownloadMovieComments, 
	DownloadMovieStudios,	DownloadMovieAwards, DownloadMovieEarnings, 
	DownloadMovieAdditionalInfo, DownloadMovieSeasonsList: boolean;
	MaxShotsCount: integer;
//20090205-Dwiti_End

	title, MovieName, s, s2, s3, s4, year, director, name, character, BasicURL, country, description, : string;
	episodes, movie_code, movie_site_url, search_url, cast_crew_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, l: integer;
	title, url: string;
begin
	url:=search_url;
	s:='';
	titles_count:=0;

	all:=RemoveBadChars(Chr(13), all);
	all:=RemoveBadChars(Chr(10), all);
	all:=RemoveBadChars(Chr(9), all);
	all:=RemoveBadChars('  ', all);

	i:=pos('<!-- Start result film -->', all);
	j:=pos('<!-- End result film -->', all);
	s:=copy(all, i, j-i);
	
	s2:=parsestring(s, '<div class="details">(.*?)</div>', 1);
	
	if s2='La ricerca non ha individuato alcun film' then exit;
	
	if s2='' then exit;
	
	//check if there are more search tabs
	s2:=ParseString(s, '<div class="pagination">(.*?)</div>', 1);
	s2:=removetags(s2);		
	i:=pos('|', s2);
	while i>0 do begin
		delete(s2, 1, i);
		i:=pos('|', s2);
	end;
	s2:=clearextraspaces(s2);
	try
		l:=strtoint(s2)
	except
	end;
		
	//search successful - check results
	k:=i;
	while k<=l do begin
		i:=pos('<a class="title"', s);

		while i>0 do begin
			j:=mypos('</a>', s, i+1)+4;
			s2:=copy(s, i, j-i);
			
			titles_count:=titles_count+1;
			SetLength(titles, titles_count);
			title_code:=ParseString(s2, '<a class="title" href="http://film.35mm.it/(.*?)[.]html', 1);
			
			//improve readability for the user
			title:=ParseString(s2, '<a.*?>(.*?)</a>', 1);
			//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:=mypos('<a class="title"', s, i+1);
		end;
		
		k:=k+1;		
		if k<l then begin
			url[length(url)]:=inttostr(k);
			
			all:=downloadurl(url);
			i:=pos('<!-- Start result film -->', all);
			j:=pos('<!-- End result film -->', all);
			s:=copy(all, i, j-i);
 		end;
	end;
	result:=titles_count;
end;

//Get search URL by movie name
function GetSearchURLbyName(MovieName, AYear, ABarcode: string): string;
var
	url: string;
begin
	//url:=format('http://www.35mm.it/search/?string=%s&where=1', [MovieName]);
	url:=format('http://personaggi-film.35mm.it/search/?string=%s&sections[]=film&page_film=1', [MovieName]);
	url:=ReplaceStr(url, ' ', '+', -1);
	search_url:=url;
	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://film.35mm.it/%s.html', [code]);
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 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);
		name:=replacestr(name, ' ', '%20', -1);			//20090205-Dwiti
	Result := name;
end;

function getcorrectname(name: string): string;
begin
	// Remove unwanted numbers at the end of names (e.g. James Cameron (I), Francis Lawrence (II))
	if (name[length(name)]=')') then begin
		i:=pos('(', name);
		name := copy(name,1,i-1);
	end;	
	Result := clearextraspaces(name);
end;

function RemoveBadChars(bad_char, text: string): string;
var
	i: integer;
begin
	i:=Pos(bad_char, text);
	while i>0 do begin
		delete(text, i, 1);
		i:=Pos(bad_char, text);
	end;
	result:=text;
end;

function GetInfo(url: string; images_only: boolean): string;
var
	keyword, s, s2, s3, plot_outline, shot, shot_prefix, shot_ext: string;
	i, j, k, l, m, frames_count: integer;
	actors, awards_exist: boolean;
begin
	result:='';
	
	all:=DownloadURL(url);
	if all='-1' then
		result:='-1'
	else
		result:=all;

	//purge whitespace (line feed, carriage return, etc.)
	all:=RemoveBadChars(Chr(13), all);
	all:=RemoveBadChars(Chr(10), all);
	all:=RemoveBadChars(Chr(9), all);
	all:=RemoveBadChars('  ', all);

	//if pos('/awards', all)>0 then awards_exist:=true;

	movie_code:=ParseString(url, 'http://film.35mm.it/(.*?).html',1);
	
	movie_site_url:=GetMovieSiteURL(movie_code);
	//movie title
	title:=ParseString(all, '<li><span>Titolo:</span>(.*?)</li>', 1);
	title:=ClearExtraSpaces(title);
	
	original_title:=ParseString(all, '<li><span>Titolo originale:</span>(.*?)</li>', 1);
	//original title
	original_title:=ClearExtraSpaces(original_title);
	
	if title='' then
		exit;

	//duration
	s2:=ParseString(all, '<li><span>Durata:</span>(.*?)min</li>', 1);
	s2:=clearextraspaces(s2);
	if s2<>'' then
	try
		duration:=strtoint(s2);
	except
	end;

	//year
	year:=ParseString(all, '<li><span>Anno di produzione:</span>(.*?)</li>', 1);
	year:=removetags(year);
	year:=clearextraspaces(year);
	
 	//Countries
	s2:=ParseString(all, '<li><span>Nazione:</span>(.*?)</li>', 1);
	i:=pos(',', s2);
	while i>0 do begin
		name:=copy(s2, 1, i-1);
		if name<>'' then
			SetCountry(name);
		delete(s2, 1, i);
		i:=Pos(',', s2);
	end;
	if s2<>'' then begin
		SetCountry(s2);
	end;

	//genres
	s2:=ParseString(all, '<li><span>Genere:</span>(.*?)</li>', 1);
	s2:=clearextraspaces(removetags(s2));
	i:=pos(',', s2);
	while i>0 do begin
		name:=copy(s2, 1, i-1);
		if name<>'' then
			Setgenre(name);
		delete(s2, 1, i);
		i:=Pos(',', s2);
	end;
	if s2<>'' then begin
		Setgenre(s2);
	end;

	//studios
	if downloadmoviestudios then begin //20090205-Dwiti
		s2:=ParseString(all, '<li><span>Distribuzione:</span>(.*?)</li>', 1);
		s2:=clearextraspaces(removetags(s2));
		i:=pos(',', s2);
		while i>0 do begin
			name:=copy(s2, 1, i-1);
			if name<>'' then
				Setstudio(name);
			delete(s2, 1, i);
			i:=Pos(',', s2);
		end;
		if s2<>'' then begin
			Setstudio(s2);
		end;
	end;	//20090205-Dwiti
	
	//description
	if downloadmoviebigdescription then begin //20090205-Dwiti
		i:=pos('<div class="description">', all);
		j:=mypos('</div>', all, i+1)+6;
		s2:=copy(all, i, j-i);
		s2:=parsestring(s2, '<div class="description">(.*?)</div>', 1);
		s2:=clearextraspaces(removetags(s2));
		s2:=decodehtml((s2));
		description:=s2;
	end; //20090205-Dwiti
	
	//front cover
	i:=pos('<div id="cover_left">', all);
	j:=mypos('</div>', all, i+1)+6;
	s2:=copy(all, i, j-i);
	s2:=parsestring(s2, '<img src="(.*?)".*?/>', 1);
	i:=pos('/images/default', s2);
	if i>0 then s2:='';
	if s2<>'' then front_cover_url := ('http://film.35mm.it'+s2);
	if front_cover_url<>'' then
		SetImageName('', front_cover_url, '', 1);

	//shots from the movie
	s:=ReplaceStr(url, '.html', '/foto.html', -1);
 	if pos(s, all)>0 then	//20090205-Dwiti Ensure that a link to the Gallery page exists
		s:=downloadurl(s)
	//20090205-Dwiti_Begin
	else
		s:='';
	//20090205-Dwiti_End
 	
	if s<>'' then begin
		if downloadmoviebigcover then begin	//20090205-Dwiti
			s2:=parsestring(s, '<a href="/files/(.*?)_poster.jpg".*?>', 1);
			//showmessage(s2);
			if s2<>'' then
				SetImageName('', 'http://film.35mm.it/files/'+s2+'_poster.jpg', '', 4);
		end;								//20090205-Dwiti
		s2:=parsestring(s, 'var listOfImages = \[(.*?)\];', 1);
		i:=pos(',', s2);
		while (i>0) and (maxshotscount>0) do begin
			name:=copy(s2, 1, i-1);
			name:=clearextraquotes(name);	
			if name<>'' then 
			begin									//20090205-Dwiti
				maxshotscount:=maxshotscount-1;		//20090205-Dwiti
				SetImageName('', 'http://film.35mm.it'+name, '', 3);
			end;							//20090205-Dwiti
			delete(s2, 1, i);
			i:=Pos(',', s2);
		end;
		if (s2<>'') and (maxshotscount>0) then begin
			name:=clearextraquotes(s2);			
			SetImageName('', 'http://film.35mm.it'+name, '', 3);
		end;
	end;
	
	//directors
	s2:=parsestring(all, '<li><span>Regia:</span>(.*?)</li>', 1);
	//s:=clearextraspaces(removetags(s2));
	
	i:=pos(',', s2);
	while i>0 do begin
		name:=copy(s2, 1, i-1);
		code:=parsestring(name, '<a href="http://personaggi-film.35mm.it/(.*?).html".*?</a>', 1);
		name:=parsestring(name, '<a href=.*?>(.*?)</a>', 1);
		if name<>'' then
			setpersonname(getcorrectname(name), '', 1, code);				
		delete(s2, 1, i);
		i:=Pos(',', s2);
	end;
	if s2<>'' then begin
		code:=parsestring(s2, '<a href="http://personaggi-film.35mm.it/(.*?).html".*?</a>', 1);
		name:=parsestring(s2, '<a href=.*?>(.*?)</a>', 1);
		setpersonname(getcorrectname(name), '', 1, code);
	end;
		
	//cast
	if not downloadmoviefullcast then begin //20090205-Dwiti
		i:=pos('<div class="mod_castcrew_tab" id="mod_castcrew_tab_cast"', all);
		j:=mypos('<div class="mod_castcrew_tab" id="mod_castcrew_tab_crew"', all, i+1);
		s:=copy(all, i, j-i);
		
		i:=pos('<h4>', s);
		while i>0 do begin
			j:=mypos('</h4>', s, i+1)+5;
			s2:=copy(s, i, j-i);
			
			name:=parsestring(s2, '<a href=.*?>(.*?)</a>', 1);
			k := Pos('  ', name);
			while k>0 do begin
				delete(name, k, 1);
				k := Pos('  ', name);
			end;
			code:=parsestring(s2, '<a href="http://personaggi-film.35mm.it/(.*?).html".*?</a>', 1);
			
			i:=mypos('<small>', s, j);
			j:=mypos('</small>', s, i+1)+8;
			s2:=copy(s, i, j-i);
			
			character:=parsestring(s2, '<small>(.*?)</small>', 1);
			k := Pos('(', character);
			if k>0 then
				delete(character, k, length(character) - k + 1);
			if name<>'' then
				setpersonname(getcorrectname(name), character, 3, code);
			i:=mypos('<h4>', s, j);
		end;
	end										//20090205-Dwiti
	else begin								//20090205-Dwiti
		//20100205-Dwiti_Begin 
		//detailed cast&crew
		cast_crew_url:=replacestr(movie_site_url, '.html', '/cast-crew/pagina-1.html', -1);
		all:=downloadurl(cast_crew_url);
		//check if the details span over more than one page
		s2:=ParseString(all, '<div class="pagination">(.*?)</div>', 1);
		s2:=removetags(s2);		
		i:=pos('|', s2);
		while i>0 do begin
			delete(s2, 1, i);
			i:=pos('|', s2);
		end;
		s2:=clearextraspaces(s2);
		try
			l:=strtoint(s2)
		except
		end;
		
		s:=ParseString(all, '<div class="pagination">(.*?)<div class="pagination">', 1);
		
		k:=i;
		while k<=l do begin
		
			i:=pos('<h4>', s);
			while i>0 do begin
				j:=mypos('</h4>', s, i+1)+5;
				s2:=copy(s, i, j-i);
				
				name:=parsestring(s2, '<a href=.*?>(.*?)</a>', 1);
				m := Pos('  ', name);
				while m>0 do begin
					delete(name, m, 1);
					m := Pos('  ', name);
				end;
				code:=parsestring(s2, '<a href="http://personaggi-film.35mm.it/(.*?).html".*?</a>', 1);
				
				i:=mypos('<small>', s, j);
				j:=mypos('</small>', s, i+1)+8;
				s2:=copy(s, i, j-i);
				
				character:=parsestring(s2, '<small>(.*?)</small>', 1);
				m := Pos('(', character);
				if m>0 then
					delete(character, m, length(character) - m + 1);
				if name<>'' then
					setpersonname(getcorrectname(name), character, 3, code);
				i:=mypos('<h4>', s, j);
			end;
			k:=k+1;		
			if k<=l then begin
				cast_crew_url[length(cast_crew_url)-5]:=inttostr(k);
				
				all:=downloadurl(cast_crew_url);
				s:=ParseString(all, '<div class="pagination">(.*?)<div class="pagination">', 1);
			end;
		end;
	end;
	//20100205-Dwiti_End
	
end;

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

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

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