//Some useful constants
const
 //Script types
 stMovies = 0;
 stPeople = 1; 
 stPoster = 2;
  
 //Script modes
 smSearch = 0; 
 smNormal = 1;
  
//Parse results
 prError     = 0; 
 prFinished  = 1;
 prList      = 2;
 prListImage = 3;
 prDownload  = 4;
  
 //Movie fields
 mfURL         = 0;
 mfTitle       = 1;
 mfOrigTitle   = 2;
 mfAka         = 3;
 mfYear        = 4;
 mfGenre       = 5;
 mfCategory    = 6;
 mfCountry     = 7;
 mfStudio      = 8;
 mfMPAA        = 9;
 mfRating      = 10;
 mfTags        = 11;
 mfTagline     = 12;
 mfDescription = 13;
 mfDuration    = 14;
 mfFeatures    = 15;
  
 //People fields
 pfURL        = 0;
 pfName       = 1;
 pfTransName  = 2;
 pfAltNames   = 3;
 pfBirthday   = 4;
 pfBirthplace = 5;
 pfGenre      = 6;
 pfBio        = 7;
 pfDeathDate  = 8;
  
 //Credits types
 ctActors    = 0;
 ctDirectors = 1;
 ctWriters   = 2;
 ctComposers = 3;
 ctProducers = 4;
  
 //Script data 
 SCRIPT_VERSION = '0.1.5.0';  
 SCRIPT_NAME    = 'World-Art.ru персоны';
 SCRIPT_DESC    = '[RU] Импорт информации о персонах с World-Art.ru';
 SCRIPT_LANG    = $19; //Russian
 SCRIPT_TYPE    = stPeople;
 
 BASE_URL    = 'http://www.world-art.ru';
 RATING_NAME = 'World-Art.ru';
 
 SEARCH_STR  = 'http://world-art.ru/search.php?name=%s&global_sector=people';
 
 CODE_PAGE   = 1251; //Use 0 for Autodetect 
 

//Global variables 
var
 Mode : Byte;
 PhotoLink : String;
(*date_ext: Extended;
date: String;*)
  
//Functions
function GetScriptVersion : String;
begin
 Result := SCRIPT_VERSION;
end; 
 
function GetScriptName : String;
begin
 Result :=  SCRIPT_NAME;
end;

function GetScriptDesc : String;
begin
 Result := SCRIPT_DESC;
end;

function GetRatingName : String;
begin
 Result := RATING_NAME;
end;

function GetBaseURL : AnsiString;
begin
 Result := BASE_URL;
end;

function GetScriptLang: Cardinal;
begin
 Result := SCRIPT_LANG;
end;

function GetCodePage : Cardinal;
begin
 Result := CODE_PAGE;
end;

function GetScriptType : Byte; 
begin
 Result := SCRIPT_TYPE;
end;

function GetCurrentMode : Byte; 
begin
 Result := Mode;
end;

function GetDownloadURL : AnsiString;
begin
 if PhotoLink = '' then
  Result := SEARCH_STR
 else
  Result := PhotoLink; 
end;

procedure ParsePerson(URL, HTML : String);
var
 curPos, EndPos : Integer;
 Tmp : String;
 Title, Role, Year, pURL : String;
begin
 AddFieldValue(pfURL, URL); 
 EndPos := 1;
 
 curPos := PosFrom('<img src=''img/people/', HTML, EndPos);
 if curPos > 0 then begin
  curPos := curPos + Length('<img src=''img/people/');
  EndPos := PosFrom('''', HTML, curPos);
  PhotoLink := 'http://www.world-art.ru/img/people/' + Copy(HTML, curPos, EndPos - curPos);
 end;

 //TransName
 curPos := Pos('</title><meta name=''description'' content=''', HTML) + Length('</title><meta name=''description'' content=''');
 EndPos := PosFrom('[', HTML, curPos);
 Tmp := Copy(HTML, curPos, EndPos - curPos);
 AddFieldValue(pfTransName, Tmp); 

 //Name
 curPos := Pos('[', HTML) + Length('['); 
 EndPos := PosFrom(']</title>', HTML, curPos);
 Tmp := Copy(HTML, curPos, EndPos - curPos);
 AddFieldValue(pfName, Tmp); 

 //AltNames
 curPos := Pos('decoration:none><font size=3>', HTML);
 if curPos > 0 then begin
  curPos := curPos + Length('decoration:none><font size=3>');
  EndPos := PosFrom('</font></a>', HTML, curPos);
  Tmp := Copy(HTML, curPos, EndPos - curPos);
  AddFieldValue(pfAltNames, Tmp);
 end; 

 //Birthday
 AddFieldValue(pfBirthday, Trim(HTMLToText(TextBetween(HTML, '<b>Дата рождения:</b>', 'г.', False, EndPos)))); 

 //DeathDate
 AddFieldValue(pfDeathDate, Trim(HTMLToText(TextBetween(HTML, '<b>Дата смерти:</b>', 'г.', False, EndPos))));
 
 //Birthplace
 curPos := PosFrom('<b>Место рождения:</b>', HTML, EndPos);
 if curPos > 0 then begin
  curPos := curPos + Length('<b>Место рождения:</b>');
  EndPos := PosFrom('</tr>', HTML, curPos); 

  AddFieldValue(pfBirthplace, Trim(RemoveTags(Copy(HTML, curPos, EndPos - curPos), False))); 
 end; 
 
 //Biography
 AddFieldValue(pfBio, TextBetween(HTML, '<p class=''review'' align=''justify''>', '</p>', True, EndPos));

 (*//дАТА ОБНОВЛЕНИЯ
date_ext := CurrentDateTime; 
  date := DateToStr (date_ext); 
    tmp := date; 
 AddFieldValueXML('dateadded', tmp);*) 
 
 //Filmography
 //Actor
 curPos := Pos('<b>Озвучка в анимации</b>', HTML); 
 if curPos > 0 then begin 
  EndPos := curPos;
  curPos := PosFrom('animation/animation.php?id=', HTML, EndPos);
  while (curPos > 0) AND (curPos < PosFrom('</table>', HTML, EndPos)) do begin
   curPos := curPos + Length('animation/animation.php?id=');
   EndPos := PosFrom('"', HTML, curPos);
   
   pURL := BASE_URL + '/animation/animation.php?id=' + Copy(HTML, curPos, EndPos - curPos);
    
   curPos := PosFrom('>', HTML, curPos) + 1;
   EndPos := PosFrom('</a>', HTML, curPos);
   
   Title := Copy(HTML, curPos, EndPos - curPos); 
   Title := StringReplace(Title, #13#10, ' ', True, True, False);	
   Title := StringReplace(Title, '  ', ' ', True, True, False);	
  
   curPos := PosFrom('</td>', HTML, EndPos);
   if (curPos > 0) AND (curPos < PosFrom('</tr>', HTML, EndPos)) then begin
    EndPos := EndPos + Length('</a>') + 2;
	
	Role := Trim(Copy(HTML, EndPos, curPos - EndPos));
	
	//Remove ] to make results more readable
	curPos := Pos(']', Role);
    if curPos > 0 then
     Delete(Role, curPos, 1);
   end;	
   
   //Year
   curPos := PrevPos('<tr><td>', HTML, EndPos) + Length('<tr><td>');
   Year := Copy(HTML, curPos, 4);
   
   AddPersonMovie(Title, '', Role, Year, pURL, ctActors);

   curPos := PosFrom('animation/animation.php?id=', HTML, EndPos);
  end; 
 end;

 //Composers
curPos := Pos('<b>Композитор в анимации</b>', HTML); 
 if curPos > 0 then begin 
  EndPos := curPos;
  curPos := PosFrom('animation/animation.php?id=', HTML, EndPos);
  while (curPos > 0) AND (curPos < PosFrom('</table>', HTML, EndPos)) do begin
   curPos := curPos + Length('animation/animation.php?id=');
   EndPos := PosFrom('"', HTML, curPos);
   
   pURL := BASE_URL + '/animation/animation.php?id=' + Copy(HTML, curPos, EndPos - curPos);
    
   curPos := PosFrom('>', HTML, curPos) + 1;
   EndPos := PosFrom('</a>', HTML, curPos);
   
   Title := Copy(HTML, curPos, EndPos - curPos); 
   Title := StringReplace(Title, #13#10, ' ', True, True, False);	
   Title := StringReplace(Title, '  ', ' ', True, True, False);	
    
   curPos := PosFrom('</td>', HTML, EndPos);
   if (curPos > 0) AND (curPos < PosFrom('</tr>', HTML, EndPos)) then begin
    EndPos := EndPos + Length('</a>') + 2;
	
	Role := Trim(Copy(HTML, EndPos, curPos - EndPos));
	
	//Remove ] to make results more readable
	curPos := Pos(']', Role);
    if curPos > 0 then
     Delete(Role, curPos, 1);
   end;	
   
   //Year
   curPos := PrevPos('<tr><td>', HTML, EndPos) + Length('<tr><td>');
   Year := Copy(HTML, curPos, 4);
   
   AddPersonMovie(Title, '', Role, Year, pURL, ctComposers);

   curPos := PosFrom('animation/animation.php?id=', HTML, EndPos);
  end; 
 end;


 //Original Author
 curPos := Pos('<b>Автор оригинала для анимационного фильма</b>', HTML); 
 if curPos > 0 then begin 
  EndPos := curPos;
  curPos := PosFrom('animation/animation.php?id=', HTML, EndPos);
  while (curPos > 0) AND (curPos < PosFrom('</table>', HTML, EndPos)) do begin
   curPos := curPos + Length('animation/animation.php?id=');
   EndPos := PosFrom('"', HTML, curPos);
   
   pURL := BASE_URL + '/animation/animation.php?id=' + Copy(HTML, curPos, EndPos - curPos);
    
   curPos := PosFrom('>', HTML, curPos) + 1;
   EndPos := PosFrom('</a>', HTML, curPos);
   
   Title := Copy(HTML, curPos, EndPos - curPos); 
   Title := StringReplace(Title, #13#10, ' ', True, True, False);	
   Title := StringReplace(Title, '  ', ' ', True, True, False);	
    
   curPos := PosFrom('</td>', HTML, EndPos);
   if (curPos > 0) AND (curPos < PosFrom('</tr>', HTML, EndPos)) then begin
    EndPos := EndPos + Length('</a>') + 2;
	
	Role := Trim(Copy(HTML, EndPos, curPos - EndPos));
	
	//Remove ] to make results more readable
	curPos := Pos(']', Role);
    if curPos > 0 then
     Delete(Role, curPos, 1);
   end;	
   
   //Year
   curPos := PrevPos('<tr><td>', HTML, EndPos) + Length('<tr><td>');
   Year := Copy(HTML, curPos, 4);
   AddPersonMovie(Title, '', Role, Year, pURL, ctProducers);

   curPos := PosFrom('animation/animation.php?id=', HTML, EndPos);
  end; 
 end;

 //Director
 curPos := Pos('<b>Режиссер в анимации</b>', HTML); 
 if curPos > 0 then begin 
  EndPos := curPos;
  curPos := PosFrom('animation/animation.php?id=', HTML, EndPos);
  while (curPos > 0) AND (curPos < PosFrom('</table>', HTML, EndPos)) do begin
   curPos := curPos + Length('animation/animation.php?id=');
   EndPos := PosFrom('"', HTML, curPos);
   
   pURL := BASE_URL + '/animation/animation.php?id=' + Copy(HTML, curPos, EndPos - curPos);
    
   curPos := PosFrom('>', HTML, curPos) + 1;
   EndPos := PosFrom('</a>', HTML, curPos);
   
   Title := Copy(HTML, curPos, EndPos - curPos); 
   Title := StringReplace(Title, #13#10, ' ', True, True, False);	
   Title := StringReplace(Title, '  ', ' ', True, True, False);	
  
   curPos := PosFrom('</td>', HTML, EndPos);
   if (curPos > 0) AND (curPos < PosFrom('</tr>', HTML, EndPos)) then begin
    EndPos := EndPos + Length('</a>') + 2;
	
	Role := Trim(Copy(HTML, EndPos, curPos - EndPos));	
	//Remove ] to make results more readable
	curPos := Pos(']', Role);
    if curPos > 0 then
     Delete(Role, curPos, 1);
   end;	
   
   //Year
   curPos := PrevPos('<tr><td>', HTML, EndPos) + Length('<tr><td>');
   Year := Copy(HTML, curPos, 4);
   
   AddPersonMovie(Title, '', Role, Year, pURL, ctDirectors);

   curPos := PosFrom('animation/animation.php?id=', HTML, EndPos);
  end; 
 end;

 //Writer
 curPos := Pos('<b>Сценарист в анимации</b>', HTML); 
 if curPos > 0 then begin 
  EndPos := curPos;
  curPos := PosFrom('animation/animation.php?id=', HTML, EndPos);
  while (curPos > 0) AND (curPos < PosFrom('</table>', HTML, EndPos)) do begin
   curPos := curPos + Length('animation/animation.php?id=');
   EndPos := PosFrom('"', HTML, curPos);
   
   pURL := BASE_URL + '/animation/animation.php?id=' + Copy(HTML, curPos, EndPos - curPos);
    
   curPos := PosFrom('>', HTML, curPos) + 1;
   EndPos := PosFrom('</a>', HTML, curPos);
   
   Title := Copy(HTML, curPos, EndPos - curPos);
   Title := StringReplace(Title, #13#10, ' ', True, True, False);	
   Title := StringReplace(Title, '  ', ' ', True, True, False);	 
     
   curPos := PosFrom('</td>', HTML, EndPos);
   if (curPos > 0) AND (curPos < PosFrom('</tr>', HTML, EndPos)) then begin
    EndPos := EndPos + Length('</a>') + 2;
	
	Role := Trim(Copy(HTML, EndPos, curPos - EndPos));
	
	//Remove ] to make results more readable
	curPos := Pos(']', Role);
    if curPos > 0 then
     Delete(Role, curPos, 1);
   end;	
   
   //Year
   curPos := PrevPos('<tr><td>', HTML, EndPos) + Length('<tr><td>');
   Year := Copy(HTML, curPos, 4);
   
   AddPersonMovie(Title, '', Role, Year, pURL, ctWriters);

   curPos := PosFrom('animation/animation.php?id=', HTML, EndPos);
  end; 
 end;
end; 

procedure ParseSearchResults(HTML : String);
var
 curPos, EndPos, P : Integer;
 Title, URL : String;
begin
 curPos := Pos('Раздел &laquo;персоны&raquo;</font>', HTML);
 if curPos < 1 then
  Exit;
  
 curPos := PosFrom('people.php?id=', HTML, curPos);  
 EndPos := curPos; 
 P := PosFrom('<font size=2 color=#99000><b>', HTML, EndPos);
 while (curPos > 0) AND ((P < 1) OR (curPos < P)) do begin
  EndPos := PosFrom('"', HTML, curPos);
  URL := BASE_URL + '/' + Copy(HTML, curPos, EndPos - curPos);
  
  curPos := PosFrom('>', HTML, EndPos) + 1;
  
  EndPos := PosFrom('</a>', HTML, curPos);
  Title := Copy(HTML, curPos, EndPos - curPos);
  
  AddSearchResult(Title, '', '', URL, '');
 
  curPos := PosFrom('people.php?id=', HTML, EndPos); 
  P := PosFrom('<font size=2 color=#99000><b>', HTML, EndPos);
 end;
end;

function ParsePage(HTML : String; URL : AnsiString) : Cardinal;
var
 P, P2 : Integer;
begin
 if Pos('<title>Поиск по сайту</title>', HTML) > 0 then begin
  ParseSearchResults(HTML);
  Result := prList; 
 end else
 if Pos('Ничего не найдено по запросу', HTML) > 0 then 
  Result := prError 
 else 
 if (Pos('<html><head><title>', HTML) > 0) then begin
  Mode := smNormal;
  ParsePerson(URL, HTML); 
  if PhotoLink = '' then
   Result := prFinished
  else
   Result := prDownload;  
 end else
 if Pos('<meta http-equiv=''Refresh''', HTML) > 0 then begin 
  P := Pos('url=', HTML) + 4; 
  P2 := PosFrom('''', HTML, P);
  AddSearchResult('', '', '', BASE_URL + '/' + Copy(HTML, P, P2 - P), '');
  
  Result := prList;
  Exit; 
 end else 
  Result := prError; 
end;

begin
 Mode := smSearch;
 PhotoLink := '';
end.
 
 





