const
	script_name='Mymovies.it';
	language='Italian';
	version=22;
	author='Vlad';
	MinProgramBuild=6100;
	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, awards: 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;
	poster_glob_url, foto_content: String;

function MovieSearch: integer;
var
	i, j, k, l: integer;
	title, small_cover, year, original_title, description: string;
begin
	s:='';
	titles_count:=0;
	i:=Pos('seguenti risultati:', all);
	if i>0 then begin
		j:=MyPos('Altri risultati tra i film con la parola', all, i+1);
		if j=0 then
			j:=MyPos('<strong>Calendario</strong>', all, i+1);
		s:=copy(all, i, j-i);
	end;
	//No results
	if (s='') or (pos('<h1>Ricerca libera</h1>', all)>0) then
	//otherwise check seach results
	else begin		
		i:=pos('<a href="http://www.mymovies.it/dizionario/recensione.asp', s);
		while i>0 do begin
			j:=MyPos('</div>', s, i+1);
			s2:=copy(s, i, j-i);
			title_code:=ParseString(s2, 'recensione\.asp\?id=(\d+)"', 1);
			title:=ParseString(s2, '<a href="http://www.mymovies.it/dizionario/recensione.asp.*?">(.*?)</a>', 1);
			title:=RemoveTags(title);
			title:=DecodeHTML(title);
			title:=ClearExtraBlankCharacters(title);
			small_cover := ParseString(s2, 'src="http://www.mymovies.it/filmclub/(.*?)" ', 1);
			if small_cover<>'' then
				small_cover := 'http://www.mymovies.it/filmclub/' + small_cover;
			year := ParseString(s2, 'http://www.mymovies.it/film/\?anno=(\d+)">', 1);
			original_title := '';
			description := ParseString(s2, '<h2>(.*?)</h2>', 1);
			if description = '' then
				description := ParseString(s2, '<div class="linkblu2".+">(.*?)</div>', 1);
			description := RemoveTags(description);
			description := DecodeHTML(description);
			description := ClearExtraBlankCharacters(description);
			if title<>'' then begin
				titles_count:=titles_count+1;
				SetSearchResult2(title, title_code, year, original_title, description, small_cover);
			end;
			delete(s, 1, j);
			i:=pos('<a href="http://www.mymovies.it/dizionario/recensione.asp', 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
	MovieName := EncodeURL(MovieName, 'iso-8859-1');
	url:=format('http://www.mymovies.it/database/ricerca/?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
	if (Pos('/', code)>0) then
		result := 'http://www.mymovies.it/film/' + code + '/'
	else
		result:=format('http://www.mymovies.it/dizionario/recensione.asp?id=%s', [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 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 GetInfo(url: string; images_only: boolean): string;
var 
	keyword, s, s2, s3, name, code, character, mode: string;
	i, j, k, l, frames_count, person_mode: integer;
	FilmmakersSection: Boolean;
	subdomain, cast_url, small_photo: String;
begin
	result:='';
	all:=DownloadURL(url);
	if all='-1' then
		result:='-1'
	else
		result:=all;

	cast_url := '';
	movie_code:=ParseString(url, 'recensione\.asp\?id=(\d+)$',1);
	if movie_code = '' then
		movie_code := ParseString(url, '/film/(.*?)/$', 1);
	movie_site_url:=GetMovieSiteURL(movie_code);
	
	if DownloadMovieFullCast then begin
		if (Pos('/', movie_code)>0) then
			cast_url := 'http://www.mymovies.it/film/' + movie_code + '/cast/'
		else
			cast_url:='http://www.mymovies.it/cast/?id='+movie_code;
		AddRequestForHTMLPageContent(cast_url, False);
	end;
	
	if MaxPostersCount > 0 then begin
		s := ParseString(all, '<a title="Poster(.*?)Poster</a>', 1);
		s := GetStr(s, 'href="http://www.mymovies.it/film/', '/poster/');
		if s <> '' then begin
			poster_glob_url := 'http://www.mymovies.it/film/'+s+'/poster/';
			AddRequestForHTMLPageContent(poster_glob_url, False);
		end;
	end;

	//movie title
	s := GetStr(all, '<meta property="og:title" content="', '" />');
	title:=ParseString(s, '^(.*?) \(\d+\)', 1);
	if title = '' then
		title := ParseString(s, '^(.*?) - MYmovies', 1);
	if title = '' then begin
		i := Pos(' |', s);
		delete(s, i, length(s) - i + 1);
		title := s;
	end;
	title:=DecodeHTML(title);
	title:=ClearExtraSpaces(title);

	if title='' then
		exit;
		
	//year
	year:=ParseString(s, '^.*? \((\d+)\)', 1);
	if year = '' then begin
		i := Pos('|', s);
		while i>0 do begin
			delete(s, 1, i+1);
			i := Pos('|', s);
		end;
		year := ParseString(s, '^(\d+) - ', 1);
	end;
		
	//original title
	original_title:=ParseString(all, 'Titolo originale <em>(.*?)</em>.', 1);
	
	//genre
	i:=Pos('<h1', all);
	i:=MyPos('Un film di ', all, i+1);
	if i>0 then begin
		i:=MyPos('<a title="Film', all, i+1);
		if i>0 then begin
			j:=MyPos('</a>', all, i+1);
			s:=copy(all, i, j-i);
			s:=RemoveTags(s);
			if s<>'' then
				SetGenre(s);
		end;
	end;
	
	//delete not needed part
	i:=Pos('</head>', all);
	delete(all, 1, i+7);
	
	j:=Pos('durata', all);
	//country
	i:=MyPos('<strong>', all, j+1);
	s:=copy(all, j, i-j);
	s2:=ParseString(s, '- (.*?) $', 1);
	if s2='' then
		s2:=ParseString(all, '- Produzione (.*?) <strong> <a title=', 1);
	if s2='' then begin
		i:=Pos('<h1', all);
		i:=MyPos('</strong>', all, i+1);
		j:=MyPos('</div>', all, i+1);
		s3:=copy(all, i, j-i);
		s2:=ParseString(s3, '- (.*?)  <strong>', 1);
	end;
		
	i:=Pos(',', s2);
	while i>0 do begin
		s3:=copy(s2, 1, i-1);
		if s3<>'' then
			SetCountry(s3);
		delete(s2, 1, i+1);
		i:=Pos(',', s2);
	end;
	if s2<>'' then
		SetCountry(s2);
	
	//duration
	s:=ParseString(all, 'durata (\d+) min.', 1);
	try
		duration:=StrToInt(s);
	except
	end;
	
	//description
	description:=ParseString(all, '<div id="recensione">(.*?)<script>', 1);
	if description='' then
		description:=ParseString(all, '<div id="recensione">(.*?)</div>', 1);
	if description='' then
		description:=ParseString(all, '<p style="text-align:justify;">(.*?)</p>', 1);
	description:=RemoveTags(description);
	description:=DecodeHTML(description);
	i := Pos('-->', description);
	while i>0 do begin
		delete(description, i, 3);
		i := Pos('-->', description);
	end;
	description := ClearExtraBlankCharacters(description);
	
	//shots content
	foto_content := GetStr(all, '/foto/">Foto ', 'clear:both;');
	
	//image
	i := Pos('<a title="Locandina', all);
	j := MyPos('</a>', all, i+1);
	s2 := copy(all, i, j-i);
	i:=Pos('http://www.mymovies.it/poster/?id=', all);
	subdomain := 'www';
	s:=ParseString(s2, 'src="http://www.mymovies.it/filmclub/(.*?)/imm.jpg', 1);
	if s='' then begin
		s := ParseString(s2, 'src="http://pad.mymovies.it/filmclub/(.*?)/imm.jpg', 1);
		subdomain := 'pad';
	end;
	if s='' then
		s:=ParseString(s2, 'src="http://www.mymovies.it/extra/filmclub/(.*?)/imm.jpg', 1);
	if s='' then
		s:=ParseString(all, 'slideshow.asp.*?cd=(.*?)\&version', 1);
	if (i>0) and (s<>'') then
			SetImageName('', 'http://'+subdomain+'.mymovies.it/filmclub/'+s+'/locandina.jpg', '', 1);
	if s='' then
		s:=ParseString(all, 'src="http://www.mymovies.it/filmclub/(.*?)/imm.jpg"', 1);
	if s='' then
		s:=ParseString(all, 'src="http://pad.mymovies.it/filmclub/(.*?)/imm.jpg"', 1);
	if s<>'' then
		SetImageName('', 'http://www.mymovies.it/filmclub/'+s+'/imm.jpg', '', 1);

		
	//download cast page
	if DownloadMovieFullCast then begin
		FilmmakersSection := false;
		all:=GetHTMLPageContentFromRequest(cast_url);
		delete(all, 1, (Pos('<body>', all)-1));
		if all<>'-1' then begin
			//directors
			i:=Pos('<div class="linkblu" style="float:left; margin-right:5px;">', all);
			while i > 0 do begin
				j := MyPos('</div>', all, i + 1);
				s := copy(all, i, j - i);
				code := ParseString(s, '<a href="http://www.mymovies.it/biografia/\?(.*?)"', 1);
				name := ParseString(s, '<a href="http://www.mymovies.it/biografia/\?.*?" title="(.*?)">', 1);
				character := ParseString(s, '<div style="margin-top:3px; font-weight:bold;">(.*?)$', 1);
				character := RemoveTags(character);
				small_photo := GetStr(s, ' src="http://pad.mymovies.it/filmclub/', '.jpg');
				if small_photo <> '' then
					small_photo := 'http://pad.mymovies.it/filmclub/' + small_photo + '.jpg';
				if (character = 'Regista') then
					SetPersonName(name, '', 1, code, small_photo)
				else if not FilmmakersSection then
					SetPersonName(name, character, 3, code, small_photo)
				else begin
					if (character = 'Soggetto') or (character = 'Sceneggiatura') then
						SetPersonName(name, '', 2, code, small_photo);
					if (character = 'Musica') then
						SetPersonName(name, '', 4, code, small_photo);
				end;
				delete(all, 1, j);
				i := Pos('<div class="linkblu" style="float:left; margin-right:5px;">', all);
				if (Pos('Filmmakers</div>', all) < i) and (Pos('Filmmakers</div>', all) > 0) then
					FilmmakersSection := True;
			end;
		end;
	end
	else begin
		i := Pos('Un film di <a', all);
		if i>0 then begin
			k := MyPos('Con <a', all, i+1);
			s := copy(all, i+11, k-i-11);
			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="http://www.mymovies.it/biografia/\?(.*?)"', 1);
				name := RemoveTags(s2);
				name := DecodeHTML(name);
				if name<>'' then
					SetPersonName(name, '', 1, code);
				delete(s, 1, j);
				i := Pos('<a href', s);
			end;
			//actors. Attention: k is used from the previous!
			j := MyPos('<strong>', all, k+1);
			if j>0 then begin
				s := copy(all, k+4, j-k-4);
				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="http://www.mymovies.it/biografia/\?(.*?)"', 1);
					name := RemoveTags(s2);
					name := DecodeHTML(name);
					if name<>'' then
						SetPersonName(name, '', 3, code);
					delete(s, 1, j);
					i := Pos('<a href', s);
				end;
			end;
		end;
	end;
end;

procedure GetPosters;
var
	s, temp, posters, poster_url: String;
	i, j, poster_cnt: Integer;
begin
	poster_cnt := 0;
	temp := GetHTMLPageContentFromRequest(poster_glob_url);
	posters := GetStr(temp, '<div id="poster">', '</div>');
	i := Pos('src="http://pad.mymovies.it/filmclub/', posters);
	while (i>0) and (poster_cnt < MaxPostersCount) do begin
		j := MyPos('</a>', posters, i+1);
		if j=0 then
			j := Length(posters);
		s := copy(posters, i, j-i);
		poster_url := GetStr(s, 'src="', '" ');
		poster_url := ReplaceStr(poster_url, '/imm', '/locandina', -1);
		//skip front cover
		if (poster_url <> '') and (Pos('locandina.jpg', poster_url) = 0) then begin
			SetImageName('', poster_url, '', 4);
			poster_cnt := poster_cnt + 1;
		end;
		delete(posters, 1, j);
		i := Pos('src="http://pad.mymovies.it/filmclub/', posters);
	end;
end;

procedure GetShots;
var
	s, img_url: String;
	i, img_cnt: Integer;
begin
	img_cnt := 0;
	i := Pos('<a href="http://www.mymovies.it/film/', foto_content);
	while (i>0) and (img_cnt < MaxShotsCount) do begin
		s := GetStr(foto_content, '<a href="http://www.mymovies.it/film/', '</a>');
		img_url := GetStr(s, ' src="', '"');
		img_url := ReplaceStr(img_url, '_imm', '', -1);
		if img_url <> '' then begin
			Inc(img_cnt);
			SetImageName('', img_url, '', 3);
		end;
		delete(foto_content, 1, i);
		i := Pos('<a href="http://www.mymovies.it/film/', foto_content);
	end;
end;

//returned images links
procedure GetImages;
begin
	if front_cover_url<>'' then
		SetImageName('', front_cover_url, '', 1);
	if MaxPostersCount > 0 then
		GetPosters;
	if MaxShotsCount > 0 then
		GetShots;
end;

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;

//get movie site url
function GetMovieSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else if (Pos('/', code)>0) then
		result := 'http://www.mymovies.it/film/' + code + '/'
	else
		result:='http://www.mymovies.it/dizionario/recensione.asp?id='+code;
end;

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