// 20091007-JeF : Bug Fix on IMDB infos retrieval
// 20110922-JeF : Bug Fix on IMDB rating info retrieval

const
	script_name='ofdb';
	language='German';
	version=28;
	author='Alex/JeF';
	MinProgramBuild=5500;
	PCount = 25;
	//MaxPicturesCount = 3;
var

	DownloadMovieBigCover, DownloadMovieShortCast, DownloadMovieFullCast, 
	DownloadMovieBigDescription, DownloadMovieComments, 
	DownloadMovieStudios,	DownloadMovieAwards, DownloadMovieEarnings, 
	DownloadMovieAdditionalInfo, DownloadMovieSeasonsList, DownloadMovieTechnicalInfo: boolean;
	MaxShotsCount: integer;

	title, year, country, description: string;
	movie_code, movie_site_url, imdb_code, salary: string;
	duration: integer;
	imdb_rating, rating: double;
	front_cover_url, amazon_url: string;
	original_title, mpaa, mpaa_rating: string;
	prefixes: array [1..PCount] of string;
	resolution, barcode, aspect_ratio, features, media_format, video_codec: string;
	
	f: TForm;
	b: TButton;
	edUserName, edPassword, edURL: TEdit;
	labCaption, labCaption2, labUserName, labPassword: TLabel;
	
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 MovieSearch: integer;
var
	i, j, k: integer;
	title, code, s, s2, orig_title, small_cover: string;
	titles_count: integer;
begin
	titles_count := 0;
	i := pos('</b> <i>Keine Ergebnisse</i>', all);
	if (all = '') or (i > 0) then
		//Movie not found!
	else
	begin
		s:='';
		i := pos('<b>Titel:</b>', all);
		if i = 0 then
			i := pos('<b>EAN/UPC:</b>', all);
		if i>0 then begin
			j := MyPos('</font></p><br>', all, i+1);
			if j>0 then
				s:=copy(all, i, j-i);
		end;
		
		if s<>'' then 
		begin
		
			i := pos('<a href="view.php?page=film&fid=',s);
			if i>0 then
				delete(s, 1, i-1)
			else begin
				i:= pos('<a href="film/', s);
				delete(s, 1, i-1);
			end;
			while (i>0) do begin
				
				s2 := '';
				j := pos('</a><br>', s);
				if j > 0 then begin
					s2 := copy(s, 1, j-1);
					delete(s, 1, j);
				end;
				small_cover := GetStr(s2, 'images/film/', '.jpg');
				if (small_cover <> '') and (Pos('na.gif', small_cover) = 0) then
					small_cover := 'http://www.ofdb.de/images/film/' + small_cover + '.jpg'
				else
					small_cover := '';
				
				code := '';
				i := pos('&fid=', s2);
				if i<=0 then begin
					i:= pos('"film', s2);
					//we adjust the position to match length of '&fid='
					i:=i+1;
				end;
				j := MyPos('"', s2, i+1);
				//discard the name of the movie in the URL
				k := MyPos(',', s2, i+1);
				if (k>0) and (k<j) then
					j:=k;
				if (i>0)and(j>0) then begin
					i := i + length('&fid=');
					code := copy(s2, i, j-i);
				end;
				
				orig_title := '';
				i := pos('<font', s2);
				j := MyPos('font>', s2, i+5);
				if (i>0)and(j>0) then begin
					// original title
					j := j + 5;
					orig_title := copy(s2, i, j-i);
					delete(s2, i, j-i);
					i := pos(' / ', orig_title);
					if i > 0 then
						delete(orig_title, i, 3);
					orig_title := RemoveTags(orig_title);
					orig_title := ClearExtraBlankCharacters(orig_title);
					orig_title := MovePrefixLeft(orig_title);
				end;
				
				i := pos('onmouseover="', s2);
				j := MyPos('"', s2, i+length('onmouseover="'));
				if (i>0)and(j>0) then
					delete(s2, i, j-i+1);
				
				year := '';
				i := pos(' (', s2);
				j := MyPos(')', s2, i+1);
				if (i>0)and(j>0) then begin
					// year
					i := i + 2;
					year := copy(s2, i, j-i);
					delete(s2, i-2, j-i+3);
					year := ClearExtraBlankCharacters(year);
				end;
				
				
				title := RemoveTags(s2);
				title := DecodeHTML(title);
				title := ClearExtraBlankCharacters(title);
				title := ClearBrackets(title);
				title := MovePrefixLeft(title);
				
				if (title<>'') and (code<>'') then begin
					titles_count := titles_count+1;
					SetSearchResult2(title, code, year, orig_title, '', small_cover);
				end;
				
				i := pos('<a href="view.php?page=film&fid=',s);
				if i>0 then
					delete(s, 1, i-1)
				else begin
					i:=pos('<a href="film/', s);
					delete(s, 1, i-1);
				end;
			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;
	result:=res;
end;

//Get search URL by movie name
function GetSearchURLbyName(MovieName, AYear, ABarcode: string): string;
var
	url, item, body: string;
	i: integer;
begin
	MovieName := ClearExtraBlankCharacters(MovieName);
	MovieName := RemovePrefix(MovieName);
	MovieName := ReplaceStr(MovieName, ' ', '+', -1);
	MovieName := EncodeURL(MovieName, 'utf-8');
	if Length(ABarcode)>9 then begin
		item := 'EAN';
		body := ABarcode;
	end		
	else begin
		item := 'Titel';
		body := MovieName;
	end;
	url := format('http://www.ofdb.de/view.php?page=suchergebnis&neu=1&Kat=%s&SText=%s', [item, body]);
	result := url;
end;

//Download search results from URL
function DownloadSearchURL(url: string): integer;
begin
	if RegReadInteger('OFDBLogin', 0) = 1 then
		SiteLogin;
	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.ofdb.de/view.php?page=film&fid=%s', [code]);
end;

function GetAmazonImages(original_title: string): string;
var
	res, all, url, s, s2, s3, local_amazon_url: string;
	i, j, frames_count, k: integer;
	item_url: string;
	image_data, image_height, image_width, image_url: string;
	frame_urls: array[1..MaxPicturesCount] of string;
	int_year: Word;
	year_prew, year_next: string;
	valid_year: boolean;
begin
	res:=front_cover_url;
	s:=original_title;
	all:=GetHTMLPageContentFromRequest(amazon_url);
	//search image by title
	local_amazon_url:='';
	frames_count := 0;
	i:=pos('<Item>', all);
	while (i>0) and (local_amazon_url='') do 
	begin
		j := MyPos('</Item>', all, i+1);
		if j>0 then 
		begin
			s2 := copy(all, i, j-i);
			s3 := GetTextBetween(s2, '<Title>', '</Title>');
			s3 := ClearBrackets(s3);
			s3 := DecodeHTML(s3);
			if s3<>'' then 
			begin
				if (UpperCase(original_title) = UpperCase(s3)) then
				begin
					valid_year := true;
					if pos('<TheatricalReleaseDate>', s2) > 0 then
					begin
						int_year := StrToInt(year);
						year_prew := IntToStr(int_year - 1);
						year_next := IntToStr(int_year + 1);
						valid_year := false;
						valid_year := pos('<TheatricalReleaseDate>'+year, s2) > 0;
						valid_year := valid_year or (pos('<TheatricalReleaseDate>'+year_prew, s2) > 0);
						valid_year := valid_year or (pos('<TheatricalReleaseDate>'+year_next, s2) > 0);						
					end;
					if valid_year then
					begin
						s := '';
						frames_count := 0;
						repeat
							image_data := GetTextBetween(s2, '<LargeImage>', '</LargeImage>');
							image_url := GetTextBetween(image_data, '<URL>', '</URL>');
							image_height := GetTextBetween(image_data, '<Height','</Height>');
							image_height := GetTextBetween(image_height, '>','');
							image_width := GetTextBetween(image_data, '<Width','</Width>');
							image_width := GetTextBetween(image_width, '>','');
							try
								if (image_height <> '0') and ((StrToInt(image_width) / StrToInt(image_height)) > 1.2) then 
								begin
									for k := 1 to frames_count do
										if frame_urls[k] = image_url then break;
									if (frames_count = 0) or (frame_urls[k] <> image_url) then begin
										frames_count := frames_count + 1;
										frame_urls[frames_count] := image_url;
									end;
								end
								else if local_amazon_url = '' then
									local_amazon_url := image_url;
							except
								//Wrong XML format!
							end;
							delete(s2, 1, 25 + length(image_data));
						until (image_data = '') or (frames_count = MaxPicturesCount);
					end;
				end; //if
			end; //if
		end; //if
		i := MyPos('<Item>', all, i+1);
	end; //while

	for i:=1 to frames_count do  // frames from amazon
		SetImageName('', frame_urls[i], '', 3);
	if local_amazon_url<>'' then   // front-cover from amazon
		SetImageName('', local_amazon_url, '', 1);
	if front_cover_url<>'' then // front-cover from movie site
		SetImageName('', front_cover_url, '', 1);
	Result := local_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])] in ['''','-'] 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 RemovePrefix(name: string): string;
var
	i: integer;
begin
	//remove prefix
	for i:=1 to PCount do
		if pos(prefixes[i]+' ', name) = 1 then
		begin
			delete(name, 1, length(prefixes[i])+1);
			break;
		end;
	Result := name;
end;

function ClearBrackets(name: string): string;
var
	i: integer;
begin
	// clear word in brackets at the end of name
	i := pos(' (', name);
	if i>0 then
		name := copy(name, 1, i-1);
	i := pos(' [', name);
	if i>0 then
		name := copy(name, 1, i-1);
	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 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 GetTextFromAllBetween(str1, str2: string): string;
var
	i, j: integer;
begin
	Result := '';
	i := pos(str1, all);
	j := MyPos(str2, all, i+length(str1));
	if (i>0)and(j>0) then begin
		i := i + length(str1);
		Result := copy(all, i, j-i);
	end;
end;

function GetTextBetween(fromStr, str1, str2: string): string;
var
	i, j: integer;
begin
	Result := '';
	if str1 = '' then
		i := 1
	else
		i := pos(str1, fromStr);
	if str2 = '' then
		j := Length(fromStr) + 1
	else
		j := MyPos(str2, fromStr, i+length(str1));
	if (i>0)and(j>0) then begin
		i := i + length(str1);
		Result := copy(fromStr, i, j-i);
	end;
end;

function GetInfo(url: string; images_only: boolean): string;
var
	s, s2, character, code, name, imdb_url, temp, plot_url, small_photo: string;
	i, j, person_cnt: integer;
begin
	result:='';
	if RegReadInteger('OFDBLogin', 0) = 1 then
		SiteLogin;	
	all:=DownloadURL(url);

	result:=all;
	if all='-1' then exit;
	movie_code := '';
	i := pos('&fid=', url);
	if i > 0 then begin
		i := i + 5;
		movie_code := copy(url, i, length(url) - i + 1);
	end;

	AddRequestForHTMLPageContent('http://www.ofdb.de/view.php?page=film_detail&fid=' + movie_code, False);
	
	plot_url := '';
	i := pos('view.php?page=inhalt&fid='+movie_code+'&sid=', all);
	if i<=0 then
		i:=pos('plot/', all);
	if i>0 then
	begin
		j := MyPos('"', all, i+1);
		url := copy(all, i, j-i);
		if length(url)>5 then
		begin
			plot_url := 'http://www.ofdb.de/' + url;
			AddRequestForHTMLPageContent(plot_url, False);
		end;
	end;
	
	movie_site_url := GetMovieSiteURL(movie_code);
	
	imdb_code := GetTextFromAllBetween('href="http://german.imdb.com/Title?', '"');
	if (imdb_code='') then imdb_code := GetTextFromAllBetween('href="http://www.imdb.com/Title?', '"');   // 20091007-JeF
	
	//imdb
	imdb_url := '';
	if imdb_code<>'' then begin
		imdb_url:='http://www.imdb.com/title/tt'+imdb_code;
		AddRequestForHTMLPageContent(imdb_url, False);
	end;
	
	//site rating
	rating := 0;
	i := Pos('>Note: ', all);
	if i>0 then begin
		j := MyPos('&', all, i+1);
		s := copy(all, i+7, j-i-7);
		s := ClearExtraBlankCharacters(RemoveTags(s));
		i:=pos('.', s);
		if i>0 then
			s[i]:=DecimalSeparator;
		try
			rating:=StrToFloat(s);
		except
		end;
	end;
	
	//movie year
	i := Pos('Erscheinungsjahr:',all);
	if i>0 then
	begin
		i := i + 17;
		j := MyPos('</a></b>',all,i+1);
		s := copy(all,i,j-i);
		year := RemoveTags(s);
		year := DecodeHTML(year);
		year := ClearExtraBlankCharacters(year);
	end;
	
	//movie title
	i := pos('<title>OFDb - ', all);
	j := MyPos('<', all, i+14);
	if (i>0)and(j>0) then begin
		i := i + 14;
		title := copy(all, i, j-i);
	end;
	title := RemoveTags(title);
	title := DecodeHTML(title);
	title := ClearExtraBlankCharacters(title);
	title := ClearBrackets(title);
	title := MovePrefixLeft(title);
	if title='' then
		exit;
	
	//original title
	i := Pos('Originaltitel:',all);
	if i>0 then
	begin
		i := i + 14;
		j := MyPos('</b>',all,i+1);
		s := copy(all,i,j-i);
		original_title := s;
		original_title := RemoveTags(original_title);
		original_title := DecodeHTML(original_title);
		original_title := ClearExtraBlankCharacters(original_title);
		original_title := ClearBrackets(original_title);
		original_title := ClearExtraQuotes(original_title);
		original_title := MovePrefixLeft(original_title);
	end;
	if length(original_title) <= 1 then
		original_title := title;
		
	amazon_url:=format('http://webservices.amazon.de/onca/xml?Service=AWSECommerceService&SubscriptionId=0J0N83PT9M72BYH98YR2&Operation=ItemSearch&Keywords=%s&SearchIndex=Video&ResponseGroup=Request,Images,Tracks,ItemAttributes', [original_title]);
	amazon_url:=ReplaceStr(amazon_url, ' ', '+', -1);
	AddRequestForHTMLPageContent(amazon_url, False);
	
	//movie image
	i:=Pos('<!-- Linke Spalte -->', all);
	i:=MyPos('<td width="1%">', all, i+1);
	j:=MyPos('alt=', all, i+1);
	s2:=copy(all, i, j-i);
	front_cover_url:=GetTextBetween(s2, '<img src="', '"');
	if pos('na.gif', front_cover_url) > 0
		then front_cover_url :='';
	
	//movie duration (minutes)
	//no duration on this site
	duration := 0;

	//movie genres
	s := GetTextFromAllBetween('>Genre(s):', '</tr>');
	while pos('<br>', s) > 0 do
	begin
		i := pos('<br>', s);
		name := copy(s, 1, i-1); 
		delete(s, 1, i+3);
		name := RemoveTags(name);
		name := DecodeHTML(name);
		name := ClearExtraBlankCharacters(name);
		if name <> '' then
			SetGenre(name);
	end;
	name := s;
	name := RemoveTags(name);
	name := DecodeHTML(name);
	name := ClearExtraBlankCharacters(name);
	if name <> '' then
		SetGenre(name);
	
	//movie countries
	s := GetTextFromAllBetween('Herstellungsland:', '</tr>');
	while pos('<br>', s) > 0 do
	begin
		i := pos('<br>', s);
		name := copy(s, 1, i-1); 
		delete(s, 1, i+3);
		name := RemoveTags(name);
		name := DecodeHTML(name);
		name := ClearExtraBlankCharacters(name);
		if name <> '' then
			SetCountry(name);
	end;
	name := s;
	name := RemoveTags(name);
	name := DecodeHTML(name);
	name := ClearExtraBlankCharacters(name);
	if name <> '' then
		SetCountry(name);

	//movie plot
	if plot_url <> '' then begin
			all := GetHTMLPageContentFromRequest(plot_url);
			s := GetTextFromAllBetween('<p class="Blocksatz">', '</font></p>');
			s2 := GetTextBetween(s, '</b><br><br>', '');
			if s2 <> '' then
				s := s2;
			s := RemoveTags(s);
			s := DecodeHTML(s);
			description := ClearExtraBlankCharacters(s);
	end;
	
	all := GetHTMLPageContentFromRequest('http://www.ofdb.de/view.php?page=film_detail&fid=' + movie_code);
	person_cnt := 0;

	//movie directors
	s := GetTextFromAllBetween('<i>Regie</i>', '</table>');
	s := DecodeHTML(s);
	while (length(s) > 0) and (person_cnt < MaxPersonsCount) do
	begin
		code := '';
		i := pos('</tr>', s);
		name := copy(s, 1, i-1); 
		delete(s, 1, i+4);
		small_photo := GetTextBetween(name, '<img src="thumbnail.php?', '&size=');
		if small_photo<>'' then
			small_photo := 'http://www.ofdb.de/thumbnail.php?' + small_photo;
		small_photo := ClearExtraBlankCharacters(small_photo);
		if (Pos('na.gif', small_photo)>0) or (Pos('na-m.gif', small_photo)>0) then
			small_photo := '';
		code := GetTextBetween(name, 'href="view.php?page=person&id=', '"');
		if code = '' then
			code := GetTextBetween(name, 'href="view.php?page=liste&Name=', '"');
		i := pos('<a href="view.php?page=', name);
		j := MyPos('>', name, i+1);
		if MyPos('<img', name, j) = j+1 then begin
			i := MyPos('<a href="view.php?page=', name, j+2);
			j := MyPos('>', name, i+1);
		end;
		if (i>0)and(j>0) then begin
			i := MyPos('</a>', name, j+1);
			if i>0 then
				name := copy(name, j+1, i-j-1);
		end;
		name := RemoveTags(name);
		name := DecodeHTML(name);
		name := ClearExtraBlankCharacters(name);
		if name <> '' then begin
			SetPersonName(name, '', 1, code, small_photo);
			person_cnt := person_cnt + 1;
		end;
	end;

	//movie writers
	s := GetTextFromAllBetween('<i>Drehbuchautor(in)</i>', '</table>');
	s := DecodeHTML(s);
	while (length(s) > 0) and (person_cnt < MaxPersonsCount) do
	begin
		i := pos('</tr>', s);
		name := copy(s, 1, i-1);
		delete(s, 1, i+4);
		small_photo := GetTextBetween(name, '<img src="thumbnail.php?', '&size=');
		if small_photo<>'' then
			small_photo := 'http://www.ofdb.de/thumbnail.php?' + small_photo;
		small_photo := ClearExtraBlankCharacters(small_photo);
		if (Pos('na.gif', small_photo)>0) or (Pos('na-m.gif', small_photo)>0) then
			small_photo := '';
		code := GetTextBetween(name, 'href="view.php?page=person&id=', '"');
		if code = '' then
			code := GetTextBetween(name, 'href="view.php?page=liste&Name=', '"');
		i := pos('<a href="view.php?page=', name);
		j := MyPos('>', name, i+1);
		if MyPos('<img', name, j) = j+1 then begin
			i := MyPos('<a href="view.php?page=', name, j+2);
			j := MyPos('>', name, i+1);
		end;
		if (i>0)and(j>0) then begin
			i := MyPos('</a>', name, j+1);
			if i>0 then
				name := copy(name, j+1, i-j-1);
		end;
		name := RemoveTags(name);
		name := DecodeHTML(name);
		name := ClearExtraBlankCharacters(name);
		if (name <> '') and (code <> '') then begin
			SetPersonName(name, '', 2, code, small_photo);
			person_cnt := person_cnt + 1;
		end;
	end;
	
	//movie cast
	s := GetTextFromAllBetween('<i>Darsteller</i>', '</table>');
	s := DecodeHTML(s);
	while (length(s) > 0) and (person_cnt < MaxPersonsCount) do
	begin
		i := pos('</tr>', s);
		name := copy(s, 1, i-1);
		delete(s, 1, i+4);
		small_photo := GetTextBetween(name, '<img src="thumbnail.php?', '&size=');
		if small_photo<>'' then
			small_photo := 'http://www.ofdb.de/thumbnail.php?' + small_photo;
		small_photo := ClearExtraBlankCharacters(small_photo);
		if (Pos('na.gif', small_photo)>0) or (Pos('na-m.gif', small_photo)>0) then
			small_photo := '';
		code := GetTextBetween(name, 'href="view.php?page=person&id=', '"');
		if code = '' then
			code := GetTextBetween(name, 'href="view.php?page=liste&Name=', '"');
		character := '';
		i := pos('>... ', name);
		j := MyPos('<', name, i+1);
		if (i>0)and(j>0) then begin
			i := i + 5;
			character := copy(name, i, j-i);
			//delete(name, i-5, j-i+5);
		end;
		i := pos('<a href="view.php?page=', name);
		j := MyPos('>', name, i+1);
		if MyPos('<img', name, j) = j+1 then begin
			i := MyPos('<a href="view.php?page=', name, j+2);
			j := MyPos('>', name, i+1);
		end;
		if (i>0)and(j>0) then begin
			i := MyPos('</a>', name, j+1);
			if i>0 then
				name := copy(name, j+1, i-j-1);
		end;
		name := RemoveTags(name);
		name := DecodeHTML(name);
		name := ClearExtraBlankCharacters(name);
		if (name <> '') and (code <> '') then begin
			SetPersonName(name, character, 3, code, small_photo);
			person_cnt := person_cnt + 1;
		end;
	end;
	
	if imdb_code<>'' then begin
		temp:=all;
		all:=GetHTMLPageContentFromRequest(imdb_url);
		if all<>'-1' then begin
    	//imdb rating
    	imdb_rating:=0;

    	i:=pos('<b>User Rating:</b>', all);
		if (i=0) then i:=pos('<h5>User Rating:</h5>', all);   // 20091007-JeF
		if i=0 then
			i:=Pos('user-rate"><b>', all);
		if (i=0) then begin  // 20110922-JeF
			i:=pos('<span itemprop="ratingValue">', all);
    		if i>0 then begin
				j:=MyPos('</span>', all, i+1);
				if (j>0) then begin
					s2 := copy(all, i, j-i);
					s2 := RemoveTags(s2);
    				i:=pos('.', s2);
    				if i>0 then
    					s2[i]:=DecimalSeparator;
    				try
						imdb_rating:=StrToFloat(s2);
					except
					end;
				end;
			end;
			i := 0;
		end;
    	if i>0 then begin
    		i:=MyPos('<b>', all, i+1);

    		if i>0 then begin
    			j:=MyPos('</b>', all, i+1);
    			if j>0 then begin	
    				s2:=copy(all, i, j-i);

    				if length(s2)>0 then begin
    					delete(s2, 1, 3);
    					i:=pos('/', s2);
    					if i>0 then
							s2:=copy(s2, 1, i-1);
    					i:=pos('.', s2);
    					if i>0 then
    						s2[i]:=DecimalSeparator;
    						try
    							imdb_rating:=StrToFloat(s2);
    						except
    						end;
    				end;
    			end;
    		end;
    	end;

    	//mpaa
    	mpaa:='';
    	mpaa_rating:='';
    	i:=pos('<h5><a href="/mpaa">MPAA</a>:</h5> ', all);
		if i=0 then
			i:=Pos('ng (<a href="/mpaa">MPAA</a>)</h4>', all);
    	if i>0 then begin
    		j:=MyPos('</div>', all, i+1);
    		if j>0 then begin
    			s2:=copy(all, i, j-1);
    			i:=pos('Rated ', s2);
    			if i>0 then begin
    				delete(s2, 1, i+5);
    				i:=pos(' ', s2);
    				if i>0 then begin
    					mpaa:=copy(s2, 1, i-1);
    					delete(s2, 1, i);
    					i:=pos('.', s2);
    					if i>0 then begin
    						mpaa_rating:=copy(s2, 1, i-1);
    					end;
						i:=pos('</span>', mpaa_rating);
						if i>0 then
							delete(mpaa_rating, i, length(mpaa_rating) - i + 1);
    				end;
    			end;
    		end;
    	end;


		end;
		all:=temp;
	end;
	
end;

//returned images links
procedure GetImages;
var
	url, title: string;
begin
	url := GetAmazonImages(original_title);
	if url = '' then 
	begin
		title := RemovePrefix(original_title);
		if title <> original_title then begin
			amazon_url:=format('http://webservices.amazon.de/onca/xml?Service=AWSECommerceService&SubscriptionId=0J0N83PT9M72BYH98YR2&Operation=ItemSearch&Keywords=%s&SearchIndex=Video&ResponseGroup=Request,Images,Tracks,ItemAttributes', [title]);
			amazon_url:=ReplaceStr(amazon_url, ' ', '+', -1);
			AddRequestForHTMLPageContent(amazon_url, False);
			url := GetAmazonImages(title);
		end;
	end;
end;

//get movie site url
function GetMovieSiteURL(code: string): string;
begin
	if code='' then
		result:=''
	else
		result:='http://www.ofdb.de/view.php?page=film&fid='+code;
end;

//get person site url
function GetPersonSiteURL(code: string): string;
var
	num: integer;
begin
	if code='' then
		result:=''
	else
	try
		num := StrToInt(code);
		// "code" is an integer number
		result := format('http://www.ofdb.de/view.php?page=person&id=%s', [code]);
	except
		// "code" is a 'name+surname'
		result := format('http://www.ofdb.de/view.php?page=liste&Name=%s', [code]);
	end;
end;

procedure SiteLogin;
var
	url, all: string;
begin
	if UserName='' then begin
		ShowLoginForm;
	end;
	url:=format('http://www.ofdb.de/usercenter/_login.php?Nick=%s&Password=%s&Submit=Login', [UserName, UserPassword]);
	url:=ReplaceStr(url, ' ', '+', -1);
	all:=DownloadURLWithReferer(url, 'post');
	if (GetResponseCode=403) then begin
		ShowLoginForm;
	end;
end;


procedure ButtonClick(Sender: TButton);
begin
	UserName:=edUserName.Text;
	UserPassword:=edPassword.Text;
	SaveLoginInfo(UserName, UserPassword, script_name);

	f.ModalResult := mrOk;
end;

procedure ShowLoginForm;
begin
	f := TForm.Create(nil);
	f.Caption := 'Bitte anmelden';
	f.BorderStyle := bsDialog;
	f.Position := poScreenCenter;
	f.Width:=400;


	labCaption := TLabel.Create(f);
	labCaption.Name := 'labCaption';
	labCaption.Parent := f;
	labCaption.SetBounds(10, 10, 380, 25);
	labCaption.Caption := 'Bitte melden Sie sich an.';

	labUserName := TLabel.Create(f);
	labUserName.Name := 'labUserName';
	labUserName.Parent := f;
	labUserName.SetBounds(10, 40, 100, 25);
	labUserName.Caption := 'Benutzername:';

	edUserName := TEdit.Create(f);
	edUserName.Name := 'Name';
	edUserName.Parent := f;
	edUserName.SetBounds(120, 40, 175, 25);
	edUserName.Text := UserName;

	labPassword := TLabel.Create(f);
	labPassword.Name := 'labPassword';
	labPassword.Parent := f;
	labPassword.SetBounds(10, 70, 380, 25);
	labPassword.Caption := 'Passwort:';

  edPassword := TEdit.Create(f);
	edPassword.Name := 'edPassword';
	edPassword.Parent := f;
	edPassword.SetBounds(120, 70, 175, 25);
	edPassword.Text := UserPassword;

	b := TButton.Create(f);
	b.Name := 'btnOK';
	b.Parent := f;
	b.SetBounds(300, 70, 75, 25);
	b.Caption := '&Einloggen';
	b.OnClick := @ButtonClick;


	labCaption2 := TLabel.Create(f);
	labCaption2.Name := 'labCaption2';
	labCaption2.Parent := f;
	labCaption2.SetBounds(10, 100, 275, 25);
	labCaption2.Caption := 'Wenn Sie keine Login und Passwort haben, Sie können sich hier registrieren:';

	edURL := TEdit.Create(f);
	edURL.Name := 'URL';
	edURL.Parent := f;
	edURL.SetBounds(10, 125, 370, 25);
	edURL.Text := 'http://www.ofdb.de/usercenter/view.php?page=signup';

	f.ShowModal;
	f.Free;
end;

//show available settings for the script
function ShowScriptOptions: integer;
var
	s: string;
	f: TForm;
	b: TButton;
	cbLogin: TCheckBox;
	my_value: integer;
	

procedure ButtonClick(Sender: TButton);
var
	zn: integer;
begin
	try
		if cbLogin.Checked then
			zn := 1
		else
			zn := 0;
		RegWriteInteger('OFDBLogin', zn);	
	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:=400;
	f.Height:=210;
	f.AutoSize:=false;

	
	cbLogin := TCheckBox.Create(f);
	cbLogin.Name := 'cbLogin';
	cbLogin.Parent := f;
	cbLogin.SetBounds(10, 10, 350, 20);
	cbLogin.Caption := 'Immer einloggen';
	my_value := RegReadInteger('OFDBLogin', 0);
	if my_value=1 then
		cbLogin.Checked := True;
		

	b := TButton.Create(f);
	b.Name := 'btnOK';
	b.Parent := f;
	b.SetBounds(160, 150, 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';
prefixes[13]:='Die';
prefixes[14]:='Ein';
prefixes[15]:='Der';
prefixes[16]:='Das';
prefixes[17]:='Eine';
prefixes[18]:='Ha-';
prefixes[19]:='I';
prefixes[20]:='Den';
prefixes[21]:='En';
prefixes[22]:='O';
prefixes[23]:='Una';
prefixes[24]:='De';
prefixes[25]:='Mga';

end.