const
	script_name='animator';
	language='Russian';
	version=9;
	author='BlackStream';
	MinProgramBuild=5500;
	Dbg = false;
	//PCount = 12;
var
	DownloadMovieBigCover, DownloadMovieShortCast, DownloadMovieFullCast, 
	DownloadMovieBigDescription, DownloadMovieComments, 
	DownloadMovieStudios,	DownloadMovieAwards, DownloadMovieEarnings, 
	DownloadMovieAdditionalInfo, DownloadMovieSeasonsList, DownloadMovieTechnicalInfo: boolean;
	MaxShotsCount: integer;
	TitlesCount: integer;
	
	title, MovieName, year, director, name, character, BasicURL, country, description, salary: string;
	movie_code, movie_site_url, title_code, imdb_code: string;
	t_code, my_code, code: string;
	duration: integer;
	imdb_rating: single;
	rating: string;
	back_cover_url, front_cover_url: string;
	posters_exists, boxes_exists, screenshots_exists: boolean;
	original_title, mpaa, mpaa_rating: string;
	awards: string;
	resolution, barcode, aspect_ratio, features, media_format, video_codec: string;
	duration_min: integer;
	SubtitlesCount, AudioTracksCount: integer;
	

//debug function
function DbgMsg(msg: string): boolean;
begin
	result := true;
	if Dbg then ShowMessage('='+msg+'=') 
	else result := false;
end;

function GetStr(str, s1, s2: string): string;
	var
		i, j: integer;
begin
	result := '';
	i := Pos(s1, str);
	if i>0 then
	begin
		j := MyPos(s2, str, i+1);
		if j>0 then
		begin
			result := copy(str, i+length(s1), j-i-length(s1));
		end;
	end;
end;

function Lower(str :string): string;
begin
	result := lowercase(str);
	result[1] := str[1];
end;
 
function GetSearchURLbyName(MovieName, AYear, ABarcode: string): string;
begin       
	MovieName := ReplaceStr(MovieName, ' ', '+', -1);
 // Result:=Format('http://animator.ru/db/?p=search&SearchMask=1&text=%s', [EncodeURL(MovieName)])
	Result:=Format('http://animator.ru/db/?p=search&SearchMask=1&text=%s', [MovieName])
end;

function DownloadSearchURL(url: string): integer;
begin
	all:=DownloadURL(URL);
	if (all<>'-1') and (GetResponseCode=200) then
		Result:=SelectMovie
	else   Result:=-1;
	//SaveFrontCover(URl, 'c:\1.html');
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 SelectMovie: Integer;
	var
		Movie,Code : string; 
		s, s2, temp: string;
		i, j: integer;
begin
	Result:=0;
	s2 :=  '<br>'+#10+'Ничего не найдено';
	if Pos(s2, all)>0 then
	exit;
	s2 := '<br><br>'+#10+'Найдено';
	temp := GetStr(all, s2, '<br><br>'+#10+'</DIV>');
	if temp<>'' then 
	begin
		i := Pos('<br><br>', temp);
		Delete(temp, 1, i+7);
		temp:=ReplaceStr(temp, '<br><br>', #13#10, -1);
		//Showmessage(temp);
		repeat
			j := Pos(#13#10, temp);
			if j>0 then
			begin
				s := copy(temp, 1, j-1);
				Delete(temp, 1, j+2);
				i:=Pos('Цитаты из фильма', s); //temp
				if i=0 then
				begin
					Code := ParseString(s, 'fid=(\d+)', 1);
					Movie := GetStr(s, '">Фильмы | ', #10+'</a>');
					if (Movie<>'') and (Code<>'') then
					begin
						Movie := DecodeHTML(Movie);
						Movie := ReplaceStr(Movie, '«', '', -1);
						Movie := ReplaceStr(Movie, '»', '', -1);
						Movie := Lower(Movie);
						inc(Result);
                        SetSearchResult(Movie, Code)
					end;
				end;	
			end;
		until (j=0);
	end;
end;

function GetMovieURL(code: string; images_only: boolean): string;
begin
	if Code<>''
	then   Result:='http://www.animator.ru/db/?p=show_film&fid='+Code
	else   Result:=''
end;

function GetPersonSiteURL(Code: string): string;
begin
	if Code<>''
	then   Result:='http://www.animator.ru/db/?p=show_person&pid='+Code
	else   Result:=''
end;

function GetInfo(URL: string; Images_Only: boolean): string;
	var
	i, j: integer;
	s, s2, temp: string;
	function GetPeople(Str, role:string; mode:byte): boolean;
		var
			s, nm:string;
			code2, character, code: string;
			i, j : integer;
	begin
		result := false;
		s := GetStr(Str, '<td valign="top" class="PProf">'+role+'</td>', '</td></tr>');
		//code := ParseString(S, '"showPerson\((\d+)\)">', 1);
		//code2 := GetStr(s, '"showPerson(', ')">');
		//s := RemoveTags(s)+#13;
		character := '';
		code := '';
		nm := '';
		s := ReplaceStr(s, ', ', #13, -1)+#13;
		repeat
			j := Pos(#13, s);
			if j>0 then
			begin
				nm := copy(s, 1, j-1);
				code := ParseString(nm, '"showPerson\((\d+)\)">', 1);
				nm := RemoveTags(nm);
				Delete(s, 1, j);
				if nm<> '' then
				begin
					result := true;
					i := Pos(' (', nm);
					if i>0 then
					begin
							character := GetStr(nm, ' (', ')');
							Delete(nm, i, length(nm)-i);
					end;
					SetPersonName(nm, character, mode, code);
					nm := '';
				end;
			end;	
		until (j=0)	;
	end;
begin
	result := '';
	all := DownloadURL(url);
	if all='-1' then
		result := '-1'
	else
		result := all;
	temp := GetStr(all, '<td id=MainTD', '<br><br>'+#10+'</DIV>');
	temp := '<td id=MainTD'+temp;
	original_title := '';
	imdb_code := '';
	movie_code := ParseString(url, 'fid=(\d+)', 1);
	movie_site_url := GetMovieURL(movie_code, true);
	code := '';
	if temp<>'' then
	begin
		title := GetStr(temp, '<td align="left" class="FilmName"><B>«', '»</B></td>');
		title := Lower(title);
		description := GetStr(temp, '<td align="left" class="FilmComments">', '</td></tr>');
		description := RemoveTags(description);
		description := DecodeHTML(description);
		GetPeople(temp, 'режиссер', 1);
		GetPeople(temp, 'режиссеры', 1);
		GetPeople(temp, 'режиссер-постановщик', 1);
		GetPeople(temp, 'режиссеры-постановщики', 1);
		GetPeople(temp, 'композиторы', 4);
		GetPeople(temp, 'композитор', 4);
		GetPeople(temp, 'сценаристы', 2);
		GetPeople(temp, 'сценарист', 2);
		GetPeople(temp, 'роли озвучивали', 3);
		s2 := 'location.href=''/db/?p=films&year=';
		year := GetStr(temp, s2,'''">');
		s2 := 'onclick="showStudia';
		s := GetStr(temp, s2, '</span>');
		if s<>'' then
		begin
			s2 := ParseString(s, '">(.+)', 1);
			SetStudio(s2);
		end;
		s := GetStr(all,'Скачать фильм (', ' мин');
		if s='' then
		begin
			s := GetStr(all, '<td align="left" class="FilmType"><i>', '</i></td></tr>');
			s := ParseString(s, '(\d+) мин', 1);
		end;	
		try
			duration := StrToInt(s);
		except
		end;
		s := GetStr(all, '<td align="left" class="FilmType"><i>', '</i></td></tr>');
		s := ReplaceStr(s, #10, '', -1)+', ';
		s := ReplaceStr(s, ', ', #13, -1);
		repeat
			j := Pos(#13, s);
			if j>0 then
			begin
				s2:= copy(s, 1, j-1);
				if not (Pos('мин', s2)>0) then
					if not (Pos('сек', s2)>0) then
						SetGenre(s2);
				Delete(s, 1, j);
			end;	
		until (j=0)	;
	end;
end;

procedure GetImages;
	var
		s, s2, url, temp: string;
		i, j: integer;
		flErr: boolean;
begin   
	s2 := 'еще кадры (';
	flErr := false;
	if Pos(s2, all)>0 then
	begin
		s := GetStr(all, 'еще кадры (', ')');
		try
			i := StrToInt(s);
			i := i-1;
			j := 0;
		except
			flErr := true;
		end;
		s2 := GetStr(all, '/film_img/', '/film_');
		if not flErr then
		while (j<>i) and (j<>MaxShotsCount) do
		begin
			inc(j);
			s := 'http://animator.ru/film_img/'+s2+'/film_'+movie_code+'_0'+IntToStr(j)+'.jpg';
			SetImageName('', s, '', 3);
		end;	
	end
	else
	if Pos('<img src="../film_img', all)>0 then
	begin
		if MaxShotsCount<>0 then
		begin
			s := GetStr(all, '<img src="../film_img', '" ');
			if s<>'' then
			begin
				s := 'http://www.animator.ru/film_img'+s;
				SetImageName('', s, '', 3);
			end;	
		end;	
	end;
end;

begin
end.