const
	script_name='iafd';
	language='English';
	version=7;
	author='Alex/JeF';
	MinProgramBuild=6000;
	Dbg = false;
	PCount = 12;
var
	title, MovieName, year, director, name, character, BasicURL, country, description, salary: string;
	movie_code, movie_site_url, title_code, imdb_code: string;
	titles: array of string;
	titles_count: integer;
	director_code, code: string;
	directors, writers, actors, characters, genres, imdb_codes: array of string;
	directors_codes, writers_codes, actors_codes: array of integer;
	directors_count, writers_count, actors_count, genres_count: integer;
	duration: integer;
	imdb_rating: single;
	front_cover_url: string;
	posters_exists, boxes_exists, screenshots_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 ClearExtraBlankCharacters(text: string): string;
begin
	// Clear all extra blank charackters
	while (length(text) > 0) and (ord(text[1]) in [1..32]) do
		delete(text, 1, 1);
	while (length(text) > 0) and (ord(text[length(text)]) in [1..32]) do
		delete(text, length(text), 1);
	Result := text;
end;

function MovieSearch: integer;
var
	i, j, k: integer;
	s, s2, s3, description, another_title, year: string;
begin
	s:='';
	titles_count:=0;
	
	//forwarded to the movie page, one result
	if (Pos('- porn movie -  iafd.com - internet adult film database</title>', all)>0) then begin
		i := Pos('http://www.iafd.com/title.rme/title=', all);
		j := MyPos('.htm', all, i+1);
		title_code := copy(all, i+36, j-i-36);
		if title_code<>'' then
			SetSearchResult('title', title_code);
		result := 1;
		exit;
	end;
	
	i:=pos('iafd</a> : not found :', all);
	if (all='') or (i>0) then
		// Movie not found!
	else begin
		i:=pos('<ol class="tresult">',all);
		if i=0 then
			i:=pos('<ol class=tresult>',all);
		if i>0 then begin
			j:=MyPos('</ol>', all, i+1);
			s:=copy(all, i, j-i);
			i:=Pos('<a href="title.rme/', s);
			while i>0 do begin
				j:=MyPos('<li>', s, i+1);
				if j=0 then j:=length(s);
				s2:=copy(s, i, j-i);
				title_code:=ParseString(s2, '<a href="title.rme/title=(.*?).htm">', 1);
				title:=ClearExtraBlankCharacters(DecodeHTML(ParseString(s2, 'htm">(.*?)</a></b>', 1)));
				description:=ClearExtraBlankCharacters(DecodeHTML(ParseString(s2, '<b>Release Info:</b> (.*?)<br>', 1)));
				year := ParseString(description, ', (\d+)$', 1);
				k := Pos(', ', description);
				if k>0 then
					delete(description, k, length(description)-k+1);
				another_title:=ParseString(s2, '<b>Also Known As:</b> (.*?)<br>', 1);
				another_title := ClearExtraBlankCharacters(DecodeHTML(another_title));
				if (title<>'') and (title_code <> '') then begin
					titles_count := titles_count + 1;
					SetSearchResult2(title, title_code, year, another_title, description, '');
				end;	
				delete(s, 1, j);
				i:=Pos('<a href="title.rme/', s);
			end;
		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 := ClearExtraBlankCharacters(MovieName);
	MovieName := EncodeURL(MovieName, 'iso-8859-1');
	url:=format('http://www.iafd.com/results.asp?searchtype=title&searchstring=%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 images_only then
		result:=format('http://www.iafd.com/title.rme/title=%s.htm', [code])
	else
		result:=format('http://www.iafd.com/title.rme/title=%s.htm', [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 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 GetInfo(url: string; images_only: boolean): string;
var
	s, s2, s3, keyword, note, genres, genre: string;   //20101007-JeF
	i, j, k: integer;
begin
	result:='';
	BasicURL:=url;
	all:=DownloadURL(url);
	if all='-1' then
		result:='-1'
	else
		result:=all;

	imdb_code:='';

	title:=ParseString(all, '<h2>(.*?) \(\d+\)</h2>', 1);
	title := MovePrefixLeft(title);
	title := DecodeHTML(title);
	if title='' then
		exit;

	movie_code:=ParseString(BasicURL, 'http://www.iafd.com/title.rme/title=(.*?).htm', 1);
	movie_site_url:=GetMovieSiteURL(movie_code);

	//movie year
	year:=ParseString(all, '<h2>.*? \((\d+)\)</h2>', 1);


	//movie duration (minutes)
	duration:=0;
	i:=Pos('<dt>Minutes</dt>', all);
	if i>0 then begin
		j:=MyPos('</dd>', all, i+1);
		s:=copy(all, i, j-i);
		s2:=ParseString(s, '<dd>(\d+)$', 1);
		if s2<>'' then
			try
				duration:=StrToInt(s2);
			except
			end;
	end;
	
   //studio
   i:=Pos('<dt>Studio (Distributor)</dt>', all);
   if i=0 then
      i:=Pos('<dt>Studio</dt>', all);
   if i=0 then
      i:=Pos('<dt>Distributor</dt>', all);
   if i>0 then begin
      j:=MyPos('</dd>', all, i+1);
      s:=copy(all, i, j-i);
      s2:=ParseString(s, '.htm">(.*?)</a> ', 1);
      if s2<>'' then
         SetStudio(s2);
      //this add Distributor as studio
      s2:=ParseString(s, '.htm">(.*?)</a>', 1);
      if s2<>'' then
         SetStudio(s2);
   end;

	
	//director
	i:=Pos('<dt>Director</dt>', all);
	if i>0 then begin
		j:=MyPos('</dd>', all, i+1);
		s:=copy(all, i, j-i);
		name:=ParseString(s, '.htm">(.*?)</a>', 1);
		k:=Pos('(', name);
		if k>0 then
			delete(name, k-1, length(name)-k+2);
		code:=ParseString(s, 'href="/person.rme/perfid=(.*?).htm">', 1);
		code:=LowerCase(code);
		if (name<>'') and (name<>'No Director Credited') then
			SetPersonName(name, '', 1, code);
	end;
	
	//actors
	i:=Pos('<div id="castinfo">', all);
	if i=0 then
		i:=Pos('<div id=castinfo>', all);
	if i>0 then begin
		j:=MyPos('</div></div>', all, i+1);
		s:=copy(all, i, j-i);
		i:=Pos('href="/person.rme/perfid=', s);
		while i>0 do begin
			j:=MyPos('</li>', s, i+1);
			s2:=copy(s, i, j-i);
			character:='';
			code:=ParseString(s2, 'href="/person.rme/perfid=(.*?).htm"', 1);
			code:=LowerCase(code);
			name:=ParseString(s2, '.htm">(.*?)</a>', 1);
			k:=Pos('(', name);
			if k>0 then begin
				character:=ParseString(name, '\(as (.*?)\)', 1);
				delete(name, k-1, length(name)-k+2);
			end;
			note:=ParseString(s2, '\[(.*?)\]', 1);   //20101007-JeF
			if (note>'') then note:='['+note+']';    //20101007-JeF
			if (character>'') and (note>'')          //20101007-JeF
				then character:=character+' '; 		 //20101007-JeF
			character:=character+note;   			 //20101007-JeF
			//this add characters of actors as genres
			genres:=ParseString(s2, '\[(.*?)\]', 1);
			genre:=ParseString(genres, '(^\S+)', 1);
			if genre<> '' then
				SetGenre(genre);
			genre:=ParseString(genres, '(\S+)', 1);
			if genre<> '' then
				SetGenre(genre);

			
			if name<>'' then
				SetPersonName(name, character, 3, code);
			delete(s, 1, j);
			i:=Pos('href="/person.rme/perfid=', s);
		end;
	end;
	
	//aka
	s:=ParseString(all, '<dl><dt>Also Known As</dt><dd>(.*?)</dd></dl>', 1);
	if s<>'' then
		description:='Also known as: '+s+#13#10;
	
	//scenes
	s:=ParseString(all, '<h3>Scene Breakdowns</h3><ul>(.*?)</ul>', 1);
	description:=description+RemoveTags(s);

end;

//returned images links
procedure GetImages;
begin

end;



//get movie site url
function GetMovieSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else
		result:='http://www.iafd.com/title.rme/title='+LowerCase(code)+'.htm';
end;

//get person site url
function GetPersonSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else
		result:='http://www.iafd.com/person.rme/perfid='+LowerCase(code)+'.htm';
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.