//Some useful constants
const
 //Script types
 stMovies = 0;
 stPeople = 1; 
 stPoster = 2;
  
 //Script modes
 smSearch   = 0; 
 smNormal   = 1;
 //Custom modes
 smCredits  = 2;
 smStudio   = 3;
 smPoster   = 4;
 smFinished = 5;
  
//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    = stMovies;
 
 BASE_URL    = 'http://www.world-art.ru';
 RATING_NAME = 'World-Art.ru';
 
 SEARCH_STR  = 'http://world-art.ru/search.php?name=%s&global_sector=animation';
 
 CODE_PAGE   = 1251; //Use 0 for Autodetect 
 
 //OPTIONS
 DOWNLOAD_POSTER = True;
 DOWNLOAD_EPISODES = True;
 DOWNLOAD_STUDIO = True;
 DOWNLOAD_CONNECTIONS = True;
 DOWNLOAD_AUTHOR_INSTEAD_OF_PRODUCER = True;

//Global variables 
var
 ELI : Integer;
 Mode : Byte;
 ExtraLinks : array [smCredits..smPoster] of WideString;
date_ext: Extended;
date: WideString;
 
//Functions
function GetScriptVersion : WideString;
begin
 Result := SCRIPT_VERSION;
end; 
 
function GetScriptName : WideString;
begin
 Result :=  SCRIPT_NAME;
end;

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

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

function GetBaseURL : String;
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 : String;
begin
 if (Mode = smSearch) then
  Result := SEARCH_STR
 else
  Result := ExtraLinks[Mode]; 
end;

procedure ParseStudios(HTML : WideString);
var
 P : Integer;
begin
 P := 1;
 //Studio
 AddFieldValue(mfStudio, HTMLValues2(HTML, 
                             '<b>Производство:</b>', '</table>',
                             '<a href=', '</a>',
                             ', ', P)); 
end;

procedure ParseCredits(HTML : WideString);
var
 curPos, EndPos : Integer;
 TransName, Name, Role, URL : WideString;
begin
 EndPos := 1; 
 
 //Original Author
 if DOWNLOAD_AUTHOR_INSTEAD_OF_PRODUCER then begin   
  curPos := PosFrom('<b>Автор оригинала:</b>', HTML, EndPos);
  if curPos > 0 then begin 
   EndPos := curPos;
   curPos := PosFrom('../people.php?id=', HTML, curPos);
   while (curPos > 0) AND (curPos < PosFrom('</table>', HTML, EndPos)) do begin
    curPos := curPos + Length('../people.php?id=');
    EndPos := PosFrom('"', HTML, curPos);
   
    URL := BASE_URL + '/people.php?id=' + Copy(HTML, curPos, EndPos - curPos);
    
    curPos := PosFrom('>', HTML, curPos) + 1;
    EndPos := PosFrom('</a>', HTML, curPos);
   
    TransName := Copy(HTML, curPos, EndPos - curPos); 
  
    curPos := PosFrom('/ <font size=2>', HTML, EndPos) + Length('/ <font size=2>');
    EndPos := PosFrom('</font>', HTML, curPos);
   
    Name := Trim(Copy(HTML, curPos, EndPos - curPos));
   
    AddMoviePerson(Name, TransName, '', URL, ctProducers);

    curPos := PosFrom('../people.php?id=', HTML, curPos);
   end;
  end;  
 end;
 
 //Directors
 curPos := PosFrom('<b>Режиссер:</b>', HTML, EndPos); 
 if curPos > 0 then begin 
  EndPos := curPos;
  curPos := PosFrom('../people.php?id=', HTML, curPos);
  while (curPos > 0) AND (curPos < PosFrom('</table>', HTML, EndPos)) do begin
   curPos := curPos + Length('../people.php?id=');
   EndPos := PosFrom('"', HTML, curPos);
   
   URL := BASE_URL + '/people.php?id=' + Copy(HTML, curPos, EndPos - curPos);
    
   curPos := PosFrom('>', HTML, curPos) + 1;
   EndPos := PosFrom('</a>', HTML, curPos);
   
   TransName := Copy(HTML, curPos, EndPos - curPos); 
  
   curPos := PosFrom('/ <font size=2>', HTML, EndPos) + Length('/ <font size=2>');
   EndPos := PosFrom('</font>', HTML, curPos);
   
   Name := Trim(Copy(HTML, curPos, EndPos - curPos));
   
   AddMoviePerson(Name, TransName, '', URL, ctDirectors);

   curPos := PosFrom('../people.php?id=', HTML, curPos);
  end;
 end; 
  
 //Writers
 curPos := PosFrom('<b>Сценарий:</b>', HTML, EndPos);
 if curPos > 0 then begin 
  EndPos := curPos;
  curPos := PosFrom('../people.php?id=', HTML, curPos);
  while (curPos > 0) AND (curPos < PosFrom('</table>', HTML, EndPos)) do begin
   curPos := curPos + Length('../people.php?id=');
   EndPos := PosFrom('"', HTML, curPos);
   
   URL := BASE_URL + '/people.php?id=' + Copy(HTML, curPos, EndPos - curPos);
    
   curPos := PosFrom('>', HTML, curPos) + 1;
   EndPos := PosFrom('</a>', HTML, curPos);
   
   TransName := Copy(HTML, curPos, EndPos - curPos); 
  
   curPos := PosFrom('/ <font size=2>', HTML, EndPos) + Length('/ <font size=2>');
   EndPos := PosFrom('</font>', HTML, curPos);
   
   Name := Trim(Copy(HTML, curPos, EndPos - curPos));
   
   AddMoviePerson(Name, TransName, '', URL, ctWriters);

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

 //Actors
 curPos := PosFrom('<b>Роли озвучивали:</b>', HTML, EndPos);
 if curPos > 0 then begin 
  EndPos := curPos;
  curPos := PosFrom('../people.php?id=', HTML, curPos);
  while (curPos > 0) AND (curPos < PosFrom('</table>', HTML, EndPos)) do begin
   curPos := curPos + Length('../people.php?id=');
   EndPos := PosFrom('"', HTML, curPos);
   
   URL := BASE_URL + '/people.php?id=' + Copy(HTML, curPos, EndPos - curPos);
    
   curPos := PosFrom('>', HTML, curPos) + 1;
   EndPos := PosFrom('</a>', HTML, curPos);
   
   TransName := Copy(HTML, curPos, EndPos - curPos); 
  
   curPos := PosFrom('/ <font size=2>', HTML, EndPos) + Length('/ <font size=2>');
   EndPos := PosFrom('</font>', HTML, curPos);
   
   Name := Trim(Copy(HTML, curPos, EndPos - curPos));
   
   curPos := PosFrom('&#151;&nbsp;', HTML, EndPos);
   if curPos < PosFrom('</tr>', HTML, EndPos) then begin
    curPos := curPos + Length('&#151;&nbsp;'); 
    EndPos := PosFrom('</td>', HTML, curPos);
	
	Role := Trim(Copy(HTML, curPos, EndPos - curPos));
   end else
    Role := '';
   
   AddMoviePerson(Name, TransName, Role, URL, ctActors);

   curPos := PosFrom('../people.php?id=', HTML, curPos);
  end;
 end; 
 
 //Composers
 curPos := PosFrom('<b>Композитор:</b>', HTML, EndPos);
 if curPos > 0 then begin 
  EndPos := curPos;
  curPos := PosFrom('../people.php?id=', HTML, curPos);
  while (curPos > 0) AND (curPos < PosFrom('</table>', HTML, EndPos)) do begin
   curPos := curPos + Length('../people.php?id=');
   EndPos := PosFrom('"', HTML, curPos);
   
   URL := BASE_URL + '/people.php?id=' + Copy(HTML, curPos, EndPos - curPos);
    
   curPos := PosFrom('>', HTML, curPos) + 1;
   EndPos := PosFrom('</a>', HTML, curPos);
   
   TransName := Copy(HTML, curPos, EndPos - curPos); 
  
   curPos := PosFrom(' / ', HTML, EndPos);
   if (curPos > 0) AND (curPos - EndPos < 10) then begin
    curPos := PosFrom('>', HTML, curPos) + 1;
    EndPos := PosFrom('</a>', HTML, curPos);
   
    Name := Trim(RemoveTags(Copy(HTML, curPos, EndPos - curPos), False));
   end else
    curPos := EndPos;   
   
   AddMoviePerson(Name, TransName, '', URL, ctComposers);

   curPos := PosFrom('../people.php?id=', HTML, curPos);
  end;
 end; 
 
 //Producers
 if not DOWNLOAD_AUTHOR_INSTEAD_OF_PRODUCER then begin
  curPos := PosFrom('<b>Продюсер:</b>', HTML, EndPos);
  if curPos > 0 then begin 
   EndPos := curPos;
   curPos := PosFrom('../people.php?id=', HTML, curPos);
   while (curPos > 0) AND (curPos < PosFrom('</table>', HTML, EndPos)) do begin
    curPos := curPos + Length('../people.php?id=');
    EndPos := PosFrom('"', HTML, curPos);
   
    URL := BASE_URL + '/people.php?id=' + Copy(HTML, curPos, EndPos - curPos);
     
    curPos := PosFrom('>', HTML, curPos) + 1;
    EndPos := PosFrom('</a>', HTML, curPos);
   
    TransName := Copy(HTML, curPos, EndPos - curPos); 
  
    curPos := PosFrom('/ <font size=2>', HTML, EndPos) + Length('/ <font size=2>');
    EndPos := PosFrom('</font>', HTML, curPos);
   
    Name := Trim(Copy(HTML, curPos, EndPos - curPos));
   
    AddMoviePerson(Name, TransName, '', URL, ctProducers);

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

procedure ParseMovie(MovieURL : WideString; HTML : WideString);
var
 curPos, EndPos, P : Integer;
 Tmp : WideString;
 
 OrigTitleFound : Boolean;
begin
  
 EndPos := Pos('</title>', HTML) - 3;
 curPos := PrevPos('(', HTML, EndPos);
 P := PrevPos('(', HTML, curPos);
 if (P > Pos('<title>', HTML)) AND (PosFrom(')', HTML, P) > curPos) then
  curPos := P;
 if (EndPos > 0) AND (curPos > 0) AND (curPos < EndPos) then begin
  AddFieldValue(mfOrigTitle, Copy(HTML, curPos + 1, EndPos - curPos));
  OrigTitleFound := True;
 end else
  OrigTitleFound := False; 
 
//дАТА ОБНОВЛЕНИЯ
date_ext := CurrentDateTime; 
  date := DateToStr (date_ext); 
    tmp := date; 
 AddFieldValueXML('dateadded', tmp); 
 
 //Get Cast URL
 EndPos := Pos('>авторский состав', HTML);
 if EndPos > 0 then begin
  curPos := PrevPos('animation_full_cast.php?id=', HTML, EndPos);
  EndPos := PosFrom('"', HTML, curPos);
  
  ExtraLinks[smCredits] := BASE_URL + '/animation/' + Copy(HTML, curPos, EndPos - curPos);
 end else
  EndPos := 1; 
  
 //Get Studio URL
 if DOWNLOAD_STUDIO then begin
  EndPos := PosFrom('>компании</a>', HTML, EndPos);
  if EndPos > 0 then begin
   curPos := PrevPos('animation_full_production.php?id=', HTML, EndPos);
   EndPos := PosFrom('"', HTML, curPos);
  
   ExtraLinks[smStudio] := BASE_URL + '/animation/' + Copy(HTML, curPos, EndPos - curPos);
  end else
   EndPos := 1;  
 end;  
 
AddFieldValue(mfURL, MovieURL); 

//Aditional URLs
 AddFieldValue(mfURL, HTMLValues(HTML, 
                             'На других сайтах</b>:</td></tr></table>', '</td></tr></table>',
			                 '<a href = "', '"', 
			                 ' ', EndPos));


//Wikipedia URLs
 AddFieldValue(mfURL, HTMLValues(HTML, 
                             '<b>Википедия</b>:</td></tr></table>', '</td></tr></table>',
			                 '<a href = "', '"', 
			                 ' ', EndPos));
 


 //Издатель в России
 curPos := Pos('<b>Издатель в России</b>', HTML);
 if curPos > 0 then begin

AddFieldValue(mfTags, TextBetween(HTML, '<b>Издатель в России</b>:</td></tr></table><table width=142 height=1 cellspacing=0 cellpadding=0 border=0><td class=line></td></tr></table>&nbsp;-&nbsp;', '</a>', True, EndPos));
 end; 
							 
 //Poster URL
 if DOWNLOAD_POSTER then begin
  curPos := PosFrom('animation_poster.php?id=', HTML, EndPos);
  if curPos > 0 then begin
   curPos := PosFrom('<img src=''', HTML, curPos) + Length('<img src=''');
   EndPos := PosFrom('''', HTML, curPos);
   ExtraLinks[smPoster] := 'http://www.world-art.ru/animation/' + Copy(HTML, curPos, EndPos - curPos);
  end;
 end; 
 
 //Title 
 curPos := Pos('td valign=top><font size=3><b>', HTML) + Length('td valign=top><font size=3><b>'); 
 EndPos := PosFrom('[</font>', HTML, curPos);
 Tmp := Copy(HTML, curPos, EndPos - curPos);
  AddFieldValue(mfTitle, Tmp); 
 
 //Year
 EndPos := curPos;
 curPos := PosFrom('<font size=3 color=#990000>', HTML, EndPos); 
 if curPos > 0 then begin
  curPos := curPos + Length('<font size=3 color=#990000>');
  EndPos := PosFrom('</font>', HTML, curPos);
 
  AddFieldValue(mfYear, Copy(HTML, curPos, EndPos - curPos)); 
  
  EndPos := EndPos + 1;
 end; 
 
 //Other titles
 curPos := PosFrom('</font><br>', HTML, EndPos);
 if curPos - EndPos < 50 then begin
  EndPos := curPos;
  Tmp := HTMLValues(HTML, '</font>', '<br><br>',
			       '<br>', '<br>', 
			       #13#10, EndPos);
				   
  //Write first to Original title field
  if not OrigTitleFound then begin
   P := Pos(#13#10, Tmp);
   if P < 1 then
    P := Length(Tmp) + 1;
   AddFieldValue(mfOrigTitle, Copy(Tmp, 1, P - 1));  
  
   Delete(Tmp, 1, P + 1);
  end;    
  
  //Write all other titles to Aka field
  AddFieldValue(mfAka, Tmp);  
 end; 
 
 //Country
 AddFieldValue(mfCountry, HTMLValues2(HTML, 
                             '<b>Производство</b>', '<br>',
			                 'country.php?id=', '</a>', 
			                 ', ', EndPos));
							 
 //Genre
 AddFieldValue(mfGenre, HTMLValues2(HTML, 
                             '<b>Жанр</b>', '<br>',
			                 'list.php?genre=', '</a>', 
			                 ', ', EndPos));							 
							 
 //Category
 curPos := PosFrom('<b>Тип</b>:', HTML, EndPos);
 if curPos > 0 then begin
  curPos := curPos + Length('<b>Тип</b>:'); 
  EndPos := PosFrom('<br>', HTML, curPos);  
  AddFieldValue(mfCategory, Trim(Copy(HTML, curPos, EndPos - curPos)));    
 end; 		       						  
	
//Premiere
 curPos := PosFrom('<b>Премьера</b>:', HTML, EndPos);
 if curPos > 0 then begin
  curPos := curPos + Length('<b>Премьера</b>:');
  EndPos := PosFrom('<br>', HTML, curPos); 

  AddFieldValue(mfMPAA, Trim(RemoveTags(Copy(HTML, curPos, EndPos - curPos), False))); 
 end; 

 //Air Date
 curPos := PosFrom('<b>Трансляция</b>:', HTML, EndPos);
 if curPos > 0 then begin
  curPos := curPos + Length('<b>Трансляция</b>:');
  EndPos := PosFrom('<br>', HTML, curPos); 

  AddFieldValue(mfMPAA, Trim(RemoveTags(Copy(HTML, curPos, EndPos - curPos), False))); 
 end;
						 
							 
 //Rating
 curPos := PosFrom('<b>Средний балл</b>:', HTML, EndPos);
 if curPos > 0 then begin
  curPos := curPos + Length('<b>Средний балл</b>:');
  EndPos := PosFrom('из 10', HTML, curPos);
  
 AddFieldValueXML('rating', Trim(HTMLToText(Copy(HTML, curPos, EndPos - curPos))));
 end;	

 //Место в рейтинге
 curPos := PosFrom('<b>рейтинге</b></a></b>:', HTML, EndPos);
 if curPos > 0 then begin
  curPos := curPos + Length('<b>рейтинге</b></a></b>:');
  EndPos := PosFrom('<br>', HTML, curPos);

 AddFieldValueXML('code', Trim(HTMLToText(Copy(HTML, curPos, EndPos - curPos))));
 end;					 
  
 //Description 
 curPos := PosFrom('Краткое содержание:</font>', HTML, EndPos);
 if curPos > 0 then
  EndPos := curPos;
  
 AddFieldValue(mfDescription, TextBetween(HTML, 'Краткое содержание:</font>', '</p>', True, EndPos)); 

 //Reference
   curPos := PosFrom('>Справка:</font>', HTML, EndPos);
 if curPos > 0 then begin

  AddFieldValue(mfTagline, TextBetween(HTML, '>Справка:</font>', '</p>',True, EndPos)); 
 end; 
 
 //Connections
 if DOWNLOAD_CONNECTIONS then begin
  curPos := Pos('<font size=2 color=#990000>В каком порядке лучше смотреть эту серию:</font>', HTML);
  if curPos > 0 then begin
   curPos := PosFrom('estimation>', HTML, curPos);
   EndPos := PosFrom('</a>', HTML, curPos);  
   while (curPos > 0) AND (curPos < PosFrom('</table>', HTML, EndPos)) do begin
    curPos := curPos + Length('estimation>');  
   
    AddConnection(Copy(HTML, curPos, EndPos - curPos), '', '', '', '');

    curPos := PosFrom('estimation>', HTML, EndPos);
    EndPos := PosFrom('</a>', HTML, curPos);
   end;
  end; 
 end; 
 
 //Episodes
 if DOWNLOAD_EPISODES then begin
  curPos := Pos('<font size=2 color=#990000>Эпизоды:</font>', HTML);
  if curPos > 0 then begin 
   curPos := PosFrom('<tr><td>', HTML, curPos) + Length('<tr><td>');
   EndPos := PosFrom('<br>', HTML, curPos);
   while (EndPos > 0) AND (EndPos < PosFrom('</td>', HTML, curPos)) do begin
    Tmp := Trim(Copy(HTML, curPos, EndPos - curPos));
    P := Pos('<b>', Tmp);
    if (P < 1) AND (Pos('<i>', Tmp) < 1) then begin   
     P := Pos('.', Tmp);
   
     AddEpisode('', Copy(Tmp, P + 1, Length(Tmp) - P), '', '', '', '1', Copy(Tmp, 1, P - 1));  
    end;	
 
    curPos := EndPos + 4;
    EndPos := PosFrom('<br>', HTML, curPos);
   end;
  
   //Last episode
   EndPos := PosFrom('</td>', HTML, curPos);
   if EndPos < PosFrom('<b>', HTML, curPos) then begin
    Tmp := Trim(Copy(HTML, curPos, EndPos - curPos));
    P := Pos('<b>', Tmp);
    if (P < 1) AND (Pos('<i>', Tmp) < 1) then begin   
     P := Pos('.', Tmp);
   
     AddEpisode('', Copy(Tmp, P + 1, Length(Tmp) - P), '', '', '', '1', Copy(Tmp, 1, P - 1));  
    end; 	
   end; 
  end; 
 end; 
end;

procedure ParseSearchResults(HTML : WideString);
var
 curPos, EndPos, P : Integer;
 Title, URL : WideString;
begin
 curPos := Pos('Раздел &laquo;анимация&raquo;</font>', HTML);
 if curPos < 1 then
  Exit;
  
 curPos := PosFrom('animation/animation.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('animation/animation.php?id=', HTML, EndPos); 
  P := PosFrom('<font size=2 color=#99000><b>', HTML, EndPos);
 end;
end;

function NextMode(curMode : Integer) : Integer;
var
 I : Integer;
begin
 Result := smFinished;
 
 if curMode < Low(ExtraLinks) - 1 then
  curMode := Low(ExtraLinks) - 1;

 for I := curMode + 1 to High(ExtraLinks) do
  if ExtraLinks[I] <> '' then begin
   Result := I;
   Break;
  end;
end;

function ParsePage(HTML : WideString; URL : String) : Cardinal;
var
 P, P2 : Integer;
begin
 if Pos('<title>Поиск по сайту</title>', HTML) > 0 then begin  
  ParseSearchResults(HTML);
  Result := prList; 
  Exit;
 end else
 if Pos('Ничего не найдено по запросу', HTML) > 0 then begin
  Result := prError;
  Exit;
 end else 
 if (Pos('<title>Аниме', HTML) > 0) OR (Pos('<title>Мультфильм', HTML) > 0) then
  ParseMovie(URL, HTML)
 else
 if Pos('съемочная группа</title>', HTML) > 0 then
  ParseCredits(HTML)
 else
 if Pos('компании производители</title>', HTML) > 0 then
  ParseStudios(HTML)
 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; 
 
 Mode := NextMode(Mode);
 if Mode <> smFinished then
  Result := prDownload
 else
  Result := prFinished; 
end;

begin
 Mode := smSearch;

 for ELI := Low(ExtraLinks) to High(ExtraLinks) do
  ExtraLinks[ELI] := '';
end.