
// SCRIPT INFO:

// ---------------------------------------------

// SCRIPT:		BoxOfficepro Script 
// AUTHOR:			Ivek23
// VERSION:		0.1.0.0
// DATE:			22/11/2015

// ---------------------------------------------

//SCRIPT BEGINS HERE...

//Some useful constants

const

//Script types
	stMovies		= 0;
	stPeople		= 1;
	stPoster		= 2;
	
//Script modes
	smSearch		= 0;
	smNormal		= 1;
	smCast			= 2;
	smReview		= 3;
	smCredits		= 4;
	smMiscellaneous	= 5;
	smPoster		= 6;
	smFinished		= 7;
	
//Parse results
	prError			= 0;
	prFinished		= 1;
	prList			= 2;
	prListImage		= 3;
	prDownload		= 4;
	
//Prefix modes
	pmNone			= 0;
	pmEnd			= 1;
	pmBegin			= 2;
	pmRemove		= 3;
	
//Download methods
	dmGET			= 0;
	dmPOST			= 1;
	
//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.0.0';
	SCRIPT_NAME		= 'Box Officepro';
	SCRIPT_DESC		= '[EN] Get Movie Information from Box Officepro.com';
	SCRIPT_LANG		= $09; //English
	SCRIPT_TYPE		= stMovies;
	
	BASE_URL		= 'http://pro.boxoffice.com';
	RATING_NAME		= 'BoxOfficepro Script';
	SEARCH_STR		= 'http://pro.boxoffice.com/statistics/search?only=movies&q=%s';
//	SEARCH_STR		= 'http://pro.boxoffice.com/statistics/search?q=%s';
	CODE_PAGE		= 65001;	//Use 0 for Autodetect
	
//User Options
	GET_THEMES	= True;	//Set to False to ensure ~mfCategory~ not added even if 'Overwrite fields' setting allows
    GET_POSTER  = False; //Set to False or True

//Global variables
var
ELI : Integer;
Mode : Byte;
ExtraLinks : array [smCast..smPoster] of 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 GetScriptLang: Cardinal;
begin
	Result := SCRIPT_LANG;
end;

function GetCodePage : Cardinal;
begin
	Result := CODE_PAGE;
end;

function GetBaseURL : AnsiString;
begin
	Result := BASE_URL;
end;

function GetDownloadURL : AnsiString;
begin
	if (Mode = smSearch) then
		Result := SEARCH_STR
	else
		Result := ExtraLinks[Mode];
end;

function GetDownloadMethod : Byte;
begin
	Result := dmGET;
end;

function GetPrefixMode : Byte;
begin
	Result := pmBegin;
end;

function GetScriptType : Byte;
begin
	Result := SCRIPT_TYPE;
end;

function GetCurrentMode : Byte;
begin
	Result := Mode;
end;

procedure ParseMovie(MovieURL : String; HTML : String);
var
	curPos, endPos : Integer;
	TmpStr, TmpStr1, TmpStr2 : String;
begin

//Get ~mfURL~ or ~URL~
AddFieldValue(mfURL, MovieURL);
//AddCustomFieldValueByName('Release Date', '<link url="' + MovieURL + '">' + MovieURL + '</link>');


//~Total Domestic Gross:~
If Pos('<span style='+#39+'white-space:nowrap'+#39+'><strong>Total Domestic Gross:', HTML) > 0 then begin
EndPos := Pos('<span style='+#39+'white-space:nowrap'+#39+'><strong>Total Domestic Gross:', HTML);
curPos := PosFrom('</strong>', HTML, EndPos);
if curPos > 0 then begin
    curPos := curPos + Length('</strong>');
	EndPos := PosFrom('</span>', HTML, curPos);
	TmpStr1 := Trim(Copy(HTML, curPos, endPos - curPos));
	TmpStr1 := StringReplace(TmpStr1, '$', '', True, True, False);
	TmpStr1 := StringReplace(TmpStr1, ',', '.', True, True, False);
//	AddCustomFieldValueByName('Total Domestic Gross:', TmpStr1);
	AddCustomFieldValueByName('Worldwide Gross:', TmpStr1);
 end;
end;

//~Worldwide Gross:~
If Pos('<span style='+#39+'white-space:nowrap'+#39+'><strong>Worldwide Gross:', HTML) > 0 then begin
EndPos := Pos('<span style='+#39+'white-space:nowrap'+#39+'><strong>Worldwide Gross:', HTML);
curPos := PosFrom('</strong>', HTML, EndPos);
if curPos > 0 then begin
    curPos := curPos + Length('</strong>');
	EndPos := PosFrom('</span>', HTML, curPos);
	TmpStr2 := Trim(Copy(HTML, curPos, endPos - curPos));
	TmpStr2 := StringReplace(TmpStr2, '$', '', True, True, False);
	TmpStr2 := StringReplace(TmpStr2, ',', '.', True, True, False);
	AddCustomFieldValueByName('Worldwide Gross:', TmpStr2);
 end;
end;


//~Total Budget~
If Pos('<span style='+#39+'white-space:nowrap'+#39+'><strong> Total Budget*:', HTML) > 0 then begin
EndPos := Pos('<span style='+#39+'white-space:nowrap'+#39+'><strong> Total Budget*:', HTML);
curPos := PosFrom('</strong>', HTML, EndPos);
if curPos > 0 then begin
    curPos := curPos + Length('</strong>');
	EndPos := PosFrom('</span>', HTML, curPos);
	TmpStr := Trim(Copy(HTML, curPos, endPos - curPos));
	TmpStr := StringReplace(TmpStr, '$', '', True, True, False);
//	TmpStr := StringReplace(TmpStr, 'N/A', '', True, True, False);
	TmpStr := StringReplace(TmpStr, ',', '.', True, True, False);
	AddCustomFieldValueByName('Total Budget', TmpStr);
 end;
end;


end;

procedure ParseSearchResults(HTML : String);
var
	curPos, endPos : Integer;
	Title, Year, URL : String;
begin
	curPos := Pos('<h6>Show only results from:</h6>', HTML);
	if curPos < 1 then
		Exit;

LogMessage('Parsing search results...');

curPos := PosFrom('<a href='+#39+'/statistics/movies/', HTML, curPos);
while curPos > 0 do begin
//	endPos	:= PosFrom(#39+'>', HTML, curPos);
	endPos	:= PosFrom('?q=', HTML, curPos);
	URL		:= Trim(Copy(HTML, curPos+9, endPos - curPos-9));
	URL		:= BASE_URL + URL;
	
	endPos := PosFrom('<a href='+#39+'/statistics/movies/', HTML, curPos);
	curPos := PosFrom('<a href='+#39+'/statistics/movies/', HTML, EndPos);
	EndPos := PosFrom('?q=', HTML, curPos);
	curPos := EndPos - 4;
	Year := Copy(HTML, curPos, 4); 
	
	curPos	:= PosFrom(#39+'>', HTML, curPos)+2;
	endPos	:= PosFrom('</a>', HTML, curPos);
	Title	:= RemoveTags(Trim(Copy(HTML, curPos, endPos - curPos)), false); 
//	Title   := StringReplace(Title, ' - ', #13, true, false, true); 

	AddSearchResult(Title, '', Year, URL, '');
//	AddSearchResult(Title, '', '', URL, '');
	
	curPos := PosFrom('<a href='+#39+'/statistics/movies/', HTML, curPos);
 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 : String; URL : AnsiString) : Cardinal;
begin
HTML := HTMLToText(HTML);

//if Pos('<h1>Search results for: "<strong>', HTML) > 0 then begin
if Pos('<h1>Search results for: ', HTML) > 0 then begin
	ParseSearchResults(HTML);
	Result := prList;
	Exit;
	
end else
 if Pos('<title>BoxOffice'+#174+' '+#8212+'  ', HTML) > 0 then
// if Pos('<title>BoxOffice', HTML) > 0 then
    ParseMovie(URL, HTML);

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.