const
	script_name='AnimeNfo';
	language='English';
	version=5;
	author='Vlad';
	MinProgramBuild=4007;
	PCount = 12;
	//MaxPicturesCount = 3;
var
	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: 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: integer;
	title: string;
begin
	s:='';
	titles_count:=0;
	i:=pos('<h2>Search Result</h2>', all);
	if i>0 then begin
		j:=MyPos('</table>', all, i+1);
		if j>0 then
			s:=copy(all, i+3, j-i-4);
	end;
	if (s='') or (pos('Search cannot find what you are looking for', all)>0) then
		//Movie not found
	else begin
	  i := pos('<table ',s);
	  s := copy(s,i+3,length(s));
	  i := pos('<tr>', s);
	  s := copy(s,i+1, length(s));
	  
	  i:=pos('</a></td>', s);

	  while i>0 do begin
			titles_count:=titles_count+1;
			SetLength(titles, titles_count);
			s2:=copy(s, 1, i-1);
			
			j:=Pos('</td>', s2);
			j:=MyPos('</td>', s2, j+1);
			s2:=copy(s, j+5, length(s2)-j-4);

			j:=Pos('<a href=', s2);
			k:=MyPos('.html''>', s2, j+1);
			title_code:=copy(s2, j+9, k-j-9);

			
			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);
			delete(s, 1, i+4);
			i:=pos('</a></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
	url:=format('http://animenfo.com/search.php?query=%s&queryin=anime_titles&action=Go&option=keywords', [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://animenfo.com/%s.html', [code]);
end;

//returned link to front cover
function GetFrontCoverURL: string;
var
	res, all, url, s, s2, s3, amazon_url: string;
	i, j: integer;
begin
	res:=front_cover_url;
	s:=title;
	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(title, 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);
	if front_cover_url<>'' then
		SetImageName('', front_cover_url, '', 1);
	if amazon_url<>'' then
		res:=amazon_url;
	result:=res;
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: integer;
begin
	result:='';
	all:=DownloadURL(url);
	if all='-1' then
		result:='-1'
	else
		result:=all;

	movie_code:=ParseString(url, 'http://animenfo.com/(.*?).html',1);
	movie_site_url:=GetMovieSiteURL(movie_code);
	
	//movie title
	title:=ParseString(all, '<h1 class="title">Anime : (.*)</h1>', 1);
	title:=RemoveTags(title);
	title:=DecodeHTML(title);
	title:=ClearExtraSpaces(title);
	title := ClearExtraQuotes(title);
	title := MovePrefixLeft(title);
	original_title := title;
	if title='' then
		exit;

	//episodes
	i:=pos('Total Episodes', all);
	if i>0 then begin
		j:=MyPos('</tr>', all, i+1);
		s:=copy(all, i, j-i);
		episodes:=ParseString(s, 'info">(\d+)', 1);
		j:=pos('<i>', s);
		if j>0 then begin
			s2:=ParseString(s, '<i>\((\d+)', 1);
			if s2<>'' then begin
				s3:=ParseString(s, ' x (\d+)', 1);
				try
					i:=StrToInt(s2);
					j:=StrToInt(s3);
				except
				end;
				duration:=i*j;
			end;
		end;
	end;

	//movie genres
	s2:=ParseString(all, 'var gn_array=new Array(.*?);', 1);
	if length(s2)>0 then begin
		i:=pos(',"', s2);
		j:=MyPos('"', s2, i+3);
		while i>0 do begin
			name:=copy(s2, i+2, j-i-2);
			if name<>'' then
				SetGenre(name);
			delete(s2, 1, i+3);
			i:=pos(',"', s2);
			j:=MyPos('"', s2, i+3);
		end;
	end;

	//year
	i:=Pos('animebyyear.php', all);
	if i>0 then begin
		year:=copy(all, i+21, 4);
	end;

	//description
	i:=pos('Description', all);
	j:=MyPos('</body>', all, i+1);
	s2:=copy(all, i+5, j-i-5);
	i:=pos('<td class="description">', s2);
	if i>0 then begin
		j:=MyPos('<!-- google_ad_section_end -->', s2, i+1);
		if j<=0 then begin
			j:=MyPos('</td>', s2, i+1);
		end;
		description:=copy(s2, i+24, j-i-24);
		description:=RemoveTags(description);
		description:=DecodeHTML(description);
		description:=ClearExtraSpaces(description);
	end;

	
	//movie image
	s2:=ParseString(all, '<img class="float" src="(.*?)" alt',1);
	if s2='general/imagenotavailable.jpg' then
		front_cover_url:=''
	else front_cover_url:=format('http://animenfo.com/%s',[s2]);

	//imdb rating
	i:=Pos('<b>User Rating</b>',all);
	s2:=copy(all, i+47, 3);
	i:=pos('.', s2);
	if i>0 then
		s2[i]:=DecimalSeparator;
		try
			rating:=StrToFloat(s2);
		except
		end;



	//movie countries - always Japan
	//setCountry('Japan');

	//directors
	i:=pos('Director', all);
	if i>0 then begin
		j:=MyPos('</td>', all, i+14);
		s:=copy(all, i, j-i);
		i:=1;
		j:=pos('</a>', s);
		while j>0 do begin
			s2:=copy(s, i, j-i);
			//get name without original name
			name:=ParseString(s2,'html">(.*?) \(', i+1);
			//if no original name present
			if name='' then
				name:=ParseString(s2,'html">(.*?)</a>', i+1);
			code:=ParseString(s2,'<a href="(.*?).html',i+1);
			if name<>'' then
				setPersonName(name, '', 1, code);
			i:=j;
			j:=MyPos('</a>', s, i+1);
		end;
	end;

	//writers
	i:=pos('Script</td>', all);
	if i>0 then begin
		j:=MyPos('</td>', all, i+11);
		s:=copy(all, i, j-i);
		i:=1;
		j:=pos('</a>', s);
		while j>0 do begin
			s2:=copy(s, i, j-i);
			name:=ParseString(s2, '<a href.*?>(.*?) \(', i+1);
			if name='' then
				name:=ParseString(s2, '<a href.*?.(.*?)</a>', i+1);
			code:=ParseString(s2, '<a href="(.*?).html', i+1);
			if name<>'' then
				setPersonName(name, '', 2, code);
			i:=j;
			j:=MyPos('</a>',s, i+1);
		end;
	end;

	//characters
	i:=pos('Character</td>', all);
	if i>0 then begin
		j:=MyPos('</table><br />', all, i+1);
		s:=copy(all, i, j-i);
		i:=1;
		j:=pos('</table>', s);
		while j>0 do begin
			s2:=copy(s, i, j-i);
			character:=ParseString(s2, '<a href.*?>(.*?)</a>', i+1);
			i:=MyPos('</table>', s, j+8);
			s2:=copy(s, j+8, i-j-8);
			name:=ParseString(s2, '<a href.*?>(.*?) \(', j+1);
			if name='' then
				name:=ParseString(s2, '<a href.*?>(.*?)</a>', j+1);
			code:=ParseString(s2, '<a href="(.*?).html', j+1);
			if name<>'' then
				SetPersonName(name, character, 3, code);
			j:=MyPos('</table>', s, i+8);
		end;
		
	end;
	


end;

//returned images links
procedure GetImages;
begin
	GetFrontCoverURL;
end;

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

//get person url
function GetPersonSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else
		result:=format('http://animenfo.com/%s.html', [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.