(* SCRIPT INFO: Following until 'SCRIPT BEGINS HERE...' is documentation. Please see 'USER OPTIONS' in the body of the script. --------------------------------------------- SCRIPT: Rottentomatoes (full) Script AUTHOR: Ivek23 VERSION: 0.1.2.0 DATE: 18/07/2014 --------------------------------------------- TYPES AND FUNCTIONS Additional types and functions that can be used in scripts: Types TWIDEARRAY : array of String Field functions procedure AddSearchResult(Title1, Title2, Year, URL, PreviewURL : String) procedure AddFieldValue(AField: Integer; AValue : String) procedure AddMoviePerson(Name, TransName, Role, URL : String; AType : Byte) procedure AddPersonMovie(Title, OrigTitle, Role, Year, URL : String; AType : Byte) procedure AddAward(Event, Award, Category, Recipient, Year: String; const Won : Boolean) procedure AddAwardEx(Event, Award, Category, RecipientVal1, RecipientVal2, Year : String; const Won : Boolean); procedure AddConnection(Title, OrigTitle, Category, URL, Year: String) procedure AddEpisode(Title, OrigTitle, Description, URL, Year, Season, Episode : String) String functions function Pos(Substr : String; Str: String): Integer function PosFrom(const SubStr, Str : String; FromIndex : Integer) : Integer function LastPos(const SubStr, Str : String) : Integer function PrevPos(const SubStr, Str : String; APos : Integer) : Integer function RemoveTags(AText : String; doLineBreaks : Boolean) : String function ExplodeString(AText : String; var Items : TWideArray; Delimiters : String) : Integer function Copy(S: String; Index, Count: Integer): String procedure Delete(var S: String; Index, Count: Integer) procedure Insert(Source: String; var Dest: String; Index: Integer) function Length(S: String): Integer function Trim(S: String): String function CompareText(S1, S2: String): Integer function CompareStr(S1, S2: String): Integer function UpperCase(S: String): String function LowerCase(S: String): String function AnsiString(S, OldPattern, NewPattern: String; All : Boolean; IgnoreCase : Boolean; WholeWord: Boolean): String function StrToInt(const S: String): Integer function IntToStr(const Value: Integer): String function StrToFloat(const S: String): Extended function FloatToStr(const Value: Extended): String function CurrentDateTime : Extended function DateToStr(Value : Extended) : String function TimeToStr(Value : Extended) : String function HTMLValues(const HTML : String; ABegin, AEnd, ItemBegin, ItemEnd : String; ValDelim : String; var Pos : Integer) : String function HTMLValues2(const HTML : String; ABegin, AEnd, ItemBegin, ItemEnd : String; ValDelim : String; var Pos : Integer) : String function TextBetween(const HTML : String; ABegin, AEnd : String; doLineBreaks : Boolean; var Pos : Integer) : String function HTMLToText(const HTML : String) : String procedure ShowMessage(const Msg, Head : String) *) //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; smDVDReleases = 5; smBiography = 6; smGenreindex = 7; smAwards = 8; smMiscellaneous = 9; smPoster = 10; smFinished = 11; //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; pfComment = 9; pfBookmark = 10; pfPid = 11; pfCareer = 12; //Credits types ctActors = 0; ctDirectors = 1; ctWriters = 2; ctComposers = 3; ctProducers = 4; //Script data SCRIPT_VERSION = '0.1.2.0'; SCRIPT_NAME = 'Rottentomatoes(full) Script'; SCRIPT_DESC = '[EN] Get Movie Information(full) from Rottentomatoes.com'; SCRIPT_LANG = $09; //English SCRIPT_TYPE = stMovies; BASE_URL = 'http://www.rottentomatoes.com'; RATING_NAME = 'Rottentomatoes'; //SEARCH_STR = 'http://www.rottentomatoes.com/search/?search=%s&sitesearch=rt'; SEARCH_STR = 'http://www.rottentomatoes.com/search/?search=%s'; CODE_PAGE = 28591; //Use 0 for Autodetect //CODE_PAGE = 65001; //Use 0 for Autodetect //CODE_PAGE = 0; //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 GET_RATING = True; //Set to False to ensure ~mfRating~ not set even if 'Overwrite setting' fields setting allows //Global variables var ELI : Integer; fullinfo, fullinfo1, fullinfo2 : String; 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; Date, Title, URL, Month, TmpStr, TmpStr0, TmpStr1, TmpStr2, TmpStr3, TmpStr4, TmpStr5, TmpStr6, TmpStr7, TmpStr8: String; DateParts : TWideArray; begin //Date ~Updated~ (choose simple or verbose version) Date := DateToStr(CurrentDateTime); if Date <> '' then begin ExplodeString(Date, DateParts, '-'); Date := DateParts[2] +'.'+ DateParts[1] +'.'+ DateParts[0]; AddCustomFieldValueByName('RTUpdated', Date + ' at ' + TimeToStr(CurrentDateTime) + ' • ' + RATING_NAME + ' ' + SCRIPT_VERSION); // Annoying end else LogMessage('date not found'); //Get ~mfURL~ or ~RT Url~ endPos := Pos('" rel="canonical" itemprop="url"/>', HTML); if endPos > 0 then begin curPos := PrevPos(' 0 then begin EndPos := curPos; TmpStr := TextBetween(HTML, 'Average Rating: ', '/10 ', True, curPos); // //TmpStr := StringReplace(TmpStr, ',', '', True, True, False); if TmpStr = '0' then TmpStr := ''; //if GET_RATING then // AddFieldValue(mfRating, TmpStr); AddCustomFieldValueByName('RT Rating', TmpStr); if TmpStr = '' then TmpStr := 'rating unknown'; end; //~Rating~ curPos := PosFrom('
Average Rating: ', '/10
', True, curPos); ////curPos := PosFrom('
', HTML, EndPos); ////if curPos > 0 then begin // //curPos := PosFrom('Average Rating: ', HTML, curPos); // //endPos := PosFrom('/5', HTML, curPos); // //TmpStr3 := StringReplace(FloatToStr((StrToFloat(Copy(HTML, curPos + 16, endPos - curPos - 16)) * 2)), '/5', '', True, True, False); if TmpStr3 = '0' then TmpStr3 := ''; AddCustomFieldValueByName('RT Rating1', TmpStr3); if TmpStr3 = '' then TmpStr3 := 'rating unknown'; // //end; EndPos := curPos; end; //end else //~Audience Rating~ //If Pos('audience', HTML) > 0 then begin //If Pos('', HTML) > 0 then begin //curPos := PosFrom('AUDIENCE SCORE', HTML, EndPos); //if curPos > 0 then begin // EndPos := curPos; //curPos := PosFrom('
', HTML, EndPos); curPos := Pos('
', HTML); if curPos > 0 then begin // EndPos := curPos; // curPos := PosFrom('
Average Rating:
', HTML, curPos); // endPos := PosFrom('/5', HTML, curPos); // TmpStr2 := StringReplace(FloatToStr((StrToFloat(Copy(HTML, curPos + 16, endPos - curPos - 16)) * 2)), '/5', '', True, True, False); TmpStr4 := FloatToStr(StrToFloat(TextBetween(HTML, '
Average Rating:', '/5
', True, curPos)) * 2); if TmpStr4 = '0' then TmpStr4 := ''; AddCustomFieldValueByName('RT Audience Rating', TmpStr4); if TmpStr4 = '' then TmpStr4 := 'rating unknown'; // end; EndPos := curPos; end; ////// ---- ~Movie~ ---- ////// //fullinfo1:= fullinfo1+' ~~ Rottentomatoes Movie Info ~~ '+#13+#13; //~Title~ curPos := Pos('

', HTML) + Length('

'); EndPos := PosFrom('', HTML, curPos); TmpStr := RemoveTags(Trim(Copy(HTML, curPos, endPos - curPos)), false); TmpStr := StringReplace(TmpStr, ' ', ' ', True, True, False); if TmpStr <> '' then fullinfo1:= fullinfo1+''+TmpStr+' for Rottentomatoes
'; LogMessage(fullinfo1); ////---- ~Movie Info~ ----//// //~MPAA Rating~ curPos := PosFrom('Rating:', HTML, EndPos); curPos := PosFrom('', HTML, curPos); endPos := PosFrom(' (', HTML, curPos); if (EndPos < 1) OR (EndPos > PosFrom('', HTML, curPos)) then endPos := PosFrom('', HTML, curPos); TmpStr6 := RemoveTags(Trim(Copy(HTML, curPos, endPos - curPos)), false); // TmpStr6 := TextBetween(HTML, '
', ' (', True, curPos); // TmpStr6 := TextBetween(HTML, '
', '
', True, curPos); // TmpStr6 := StringReplace(TmpStr6, ', ', ' , ', True, True, False); // TmpStr6 := StringReplace(TmpStr6, ', ', ' , ', True, True, False); if TmpStr6 <> '' then fullinfo1:= fullinfo1+'MPAA Rating: '+TmpStr6+#13; LogMessage(fullinfo1); //~Genres~ curPos := PosFrom('Genre:', HTML, EndPos); if curPos > 0 then begin EndPos := curPos; //TmpStr5 := Copy(HTML, curPos, endPos - curPos); TmpStr5 := RemoveTags(TextBetween(HTML, '', '', True, curPos), false); TmpStr5 := StringReplace(TmpStr5, ' ', '', True, True, False); TmpStr5 := StringReplace(TmpStr5, ' ', '', True, True, False); if TmpStr5 <> '' then fullinfo1:= fullinfo1+'Genres: '+TmpStr5+#13; LogMessage(fullinfo1); end; //~Runtime~ curPos := PosFrom('Runtime:', HTML, EndPos); if curPos > 0 then begin curPos := PosFrom(' ', HTML, curPos); endPos := PosFrom(' ', HTML, curPos); TmpStr4 := TextBetween(HTML, ' ', ' ', True, curPos); if TmpStr4 <> '' then fullinfo1:= fullinfo1+'Runtime: '+TmpStr4+#13; LogMessage(fullinfo1); EndPos := curPos; end; //~Directed By~ If Pos('Directed By:', HTML) > 0 then begin EndPos := Pos('Directed By:', HTML); curPos := PosFrom('', HTML, EndPos); endPos := PosFrom('', HTML, curPos); TmpStr7 := RemoveTags(Trim(Copy(HTML, curPos, endPos - curPos)), false); TmpStr7 := StringReplace(TmpStr7, 'Directed By:', 'Directors: ', True, True, False); TmpStr7 := StringReplace(TmpStr7, ' ', '', True, True, False); TmpStr7 := StringReplace(TmpStr7, ' , ', ', ', True, True, False); // //TmpStr7 := StringReplace(TmpStr7, ' ', ' ', True, True, False); if TmpStr7 <> '' then fullinfo1:= fullinfo1+'Directors: '+TmpStr7+#13; LogMessage(fullinfo1); end; //~Written By~ If Pos('Written By:', HTML) > 0 then begin EndPos := Pos('Written By:', HTML); curPos := PosFrom(' ', HTML, EndPos); endPos := PosFrom('', HTML, curPos); TmpStr8 := RemoveTags(Trim(Copy(HTML, curPos, endPos - curPos)), false); TmpStr8 := StringReplace(TmpStr8, 'Written By', 'Writers', True, True, False); TmpStr8 := StringReplace(TmpStr8, ' ', '', True, True, False); TmpStr8 := StringReplace(TmpStr8, ' , ', ', ', True, True, False); // TmpStr8 := StringReplace(TmpStr8, ' ', ' ', True, True, False); if TmpStr8 <> '' then fullinfo1:= fullinfo1+'Writers:'+TmpStr8+#13; LogMessage(fullinfo1); end; //~Theaters~ curPos := PosFrom('In Theaters:', HTML, EndPos); curPos := PosFrom('In Theaters:', HTML, curPos); endPos := PosFrom('', HTML, curPos); TmpStr1 := TextBetween(HTML, 'In Theaters:', '', True, curPos); TmpStr1 := StringReplace(TmpStr1, ' ', '', True, True, False); if TmpStr1 <> '' then fullinfo1:= fullinfo1+'In Theaters: '+TmpStr1+#13; LogMessage(fullinfo1); //~On DVD~ (DD MMM YYYY text) curPos := PosFrom(' On DVD:', HTML, EndPos); curPos := PosFrom(' On DVD:', HTML, curPos); endPos := PosFrom('', HTML, curPos); TmpStr2 := TextBetween(HTML, ' On DVD:', '', True, curPos); if TmpStr2 <> '' then fullinfo1:= fullinfo1+'On DVD: '+TmpStr2+#13; LogMessage(fullinfo1); Month := ''; if Pos('Jan', TmpStr2) > 0 then Month := Month + '1.'; TmpStr2 := StringReplace(TmpStr2, 'Jan', '', true, true, true); if Pos('Feb', TmpStr2) > 0 then Month := Month + '2.'; TmpStr2 := StringReplace(TmpStr2,'Feb','',true,true,true); if Pos('Mar', TmpStr2) > 0 then Month := Month + '3.'; TmpStr2 := StringReplace(TmpStr2,'Mar','',true,true,true); if Pos('Apr', TmpStr2) > 0 then Month := Month + '4.'; TmpStr2:= StringReplace(TmpStr2,'Apr','',true,true,true); if Pos('May', TmpStr2) > 0 then Month := Month + '5.'; TmpStr2 := StringReplace(TmpStr2,'May','',true,true,true); if Pos('Jun', TmpStr2) > 0 then Month := Month + '6.'; TmpStr2 := StringReplace(TmpStr2,'Jun','',true,true,true); if Pos('Jul', TmpStr2) > 0 then Month := Month + '7.'; TmpStr2 := StringReplace(TmpStr2,'Jul','',true,true,true); if Pos('Aug', TmpStr2) > 0 then Month := Month + '8.'; TmpStr2 := StringReplace(TmpStr2,'Aug','',true,true,true); if Pos('Sep', TmpStr2) > 0 then Month := Month + '9.'; TmpStr2 := StringReplace(TmpStr2,'Sep','',true,true,true); if Pos('Oct', TmpStr2) > 0 then Month := Month + '10.'; TmpStr2 := StringReplace(TmpStr2, 'Oct', '', true, true, true); if Pos('Nov', TmpStr2) > 0 then Month := Month + '11.'; TmpStr2 := StringReplace(TmpStr2, 'Nov', '', true, true, true); if Pos('Dec', TmpStr2) > 0 then Month := Month + '12.'; TmpStr2 := StringReplace(TmpStr2,'Dec','',true,true,true); ExplodeString(TmpStr2, DateParts, ','); if Month <> '' then Month := DateParts[0] + '.' + Month + DateParts[1]; if Month = '0' then Month := ''; if Month <> '' then AddFieldValueXML('release', Month); if Month = '' then Month := 'release unknown'; // end; //~US Box Office~ curPos := PosFrom('US Box Office:', HTML, EndPos); curPos := PosFrom('US Box Office:', HTML, curPos); endPos := PosFrom('', HTML, curPos); TmpStr0 := TextBetween(HTML, 'US Box Office:', '', True, curPos); TmpStr0 := StringReplace(TmpStr0, ' ', '', True, True, False); TmpStr0 := StringReplace(TmpStr0, ' ', ' ', True, True, False); if TmpStr0 <> '' then AddCustomFieldValueByName('US Box Office', TmpStr0); if TmpStr0 <> '' then fullinfo1:= fullinfo1+'US Box Office: '+TmpStr0+#13; LogMessage(fullinfo1); //~ProductionCompany~ curPos := PosFrom('', HTML, EndPos); if curPos > 0 then begin EndPos := curPos; TmpStr3 := TextBetween(HTML, '', '', True, curPos); if TmpStr3 = '0' then TmpStr3 := ''; TmpStr3 := StringReplace(TmpStr3, 'Unknown', '', True, True, False); if TmpStr3 <> '' then fullinfo1:= fullinfo1+'Production Company: '+TmpStr3+' ' else if TmpStr3 = '' then TmpStr3 := 'productionCompany unknown'; //fullinfo1:= StringReplace(fullinfo1, 'Production Company: ', '', True, True, False); LogMessage(fullinfo1); end; fullinfo1 := fullinfo1; LogMessage(fullinfo1); If fullinfo1 <> '' then AddCustomFieldValueByName('Movie Info', fullinfo1); LogMessage(fullinfo1); end; procedure ParseSearchResults(HTML : String); var curPos, endPos : Integer; Title, Year, URL, Preview : String; begin // curPos := Pos('
    ', HTML); if curPos < 1 then Exit; LogMessage('Parsing search results...'); //EndPos := PosFrom('', HTML, EndPos); curPos := PosFrom('More Movies...', HTML, endPos)) do begin while curPos > 0 do begin endPos := PosFrom('">', HTML, curPos); URL := 'http://www.rottentomatoes.com'+Trim(Copy(HTML, curPos+71, endPos - curPos-71)); curPos := PosFrom('">', HTML, curPos); endPos := PosFrom('', HTML, curPos); Title := TextBetween(HTML, '">', '', True, curPos); curPos := PosFrom('', HTML, curPos); // curPos := PosFrom('', HTML, curPos); endPos := PosFrom('

', HTML, curPos); Year := TextBetween(HTML, '', '
', True, curPos); AddSearchResult(Title+' '+Year, '', '', URL, ''); curPos := PosFrom('