I have a list of IMDB tt IDs and I am feeding this into a loop to open those as IMDB URLs in Safari. This is working well, but now I want to filter some of those movies out based on certain criteria. I am getting the details of the movies from OMDB api using JSON Helper app. It looks like this:
tell application "JSON Helper"
fetch JSON from "https://www.omdbapi.com/?apikey=xxxx&tomatoes=true&i=tt5390430"
--> {Rated:"N/A", tomatoRating:"N/A", Country:"USA", tomatoURL:"https://www.rottentomatoes.com/m/accidental_courtesy_daryl_davis_race_and_america/", imdbRating:"7.6", Plot:"Daryl Davis is an accomplished musician who was played all over the world. He also has an unusual hobby, particularly for a middle aged black man. When not displaying his musical chops, ...", |language|:"English", Production:"First Run Features", tomatoImage:"N/A", tomatoConsensus:"N/A", Website:"N/A", tomatoReviews:"N/A", Response:"True", Metascore:"63", Runtime:"96 min", tomatoUserMeter:"N/A", Director:"Matthew Ornstein", tomatoMeter:"N/A", tomatoFresh:"N/A", Actors:"Daryl D. Davis, Kenneth Nwadike, Michael Wood Jr.", tomatoUserRating:"N/A", tomatoUserReviews:"N/A", Title:"Accidental Courtesy: Daryl Davis, Race & America", Writer:"N/A", tomatoRotten:"N/A", Ratings:{{Source:"Internet Movie Database", Value:"7.6/10"}, {Source:"Rotten Tomatoes", Value:"85%"}, {Source:"Metacritic", Value:"63/100"}}, Released:"23 Feb 2017", imdbID:"tt5390430", |year|:"2016", Genre:"Documentary, Biography, History", Poster:"https://m.media-amazon.com/images/M/MV5BYTczMTJmOTctMWM0NC00NTQwLWE4OGUtMTkxNDU0NDk3ZjhhXkEyXkFqcGdeQXVyNTI5NjIyMw@@._V1_SX300.jpg", imdbVotes:"946", Type:"movie", BoxOffice:"N/A", DVD:"11 Mar 2017", Awards:"2 wins & 3 nominations."}
end tell
I don’t know the term for what I want to accomplish so it’s hard to search for it.
I want to do a few things:
-
I want to create “filters” that will SKIP certain titles if they contain certain information, but not exit the “if then loop” just continue to the next IMDB tt ID in the list.
-
I want to export to a text file the list items that were skipped. Similar to how the bash shell can append to a file using output >> ~/skipped.txt
I have laid some of the groundwork for the filters. Production company and ratings, I plan to add some more, Genre, imdbVotes,
--prepare to pull data on titles from OMDB "repeat with x in list"
repeat with tt in |IMDbID_list|
--clear repeat variables
set imdbRating to {}
set rottenRating to {}
set metacriticRating to {}
if contents of tt is not "" then --skip blank lines
--create OMDB URL
set OMDB_URL to "https://www.omdbapi.com/"
set OMDB_API_key to "?apikey=xxxx"
set OMDB_Tomatoes to "&tomatoes=true" --get rotten tomatoes info
set OMDB_Parameter to "&i=" --do a search by valid IMDb ID
set OMDB_Query to the OMDB_URL & OMDB_API_key & OMDB_Tomatoes & OMDB_Parameter & tt
--use JSON Helper fetch to query OMDB with title and then set variable values
tell application "JSON Helper"
set _JSON to fetch JSON from OMDB_Query
set _response to |Response| of _JSON -- get response and proceed based on repsonse {true, false}
if _response is "True" then -- if the title exists on OMDB proceed...
set production to |Production| of _JSON --get production for filter
set ratings to |Ratings| of _JSON -- this is the list of ratings, there are three.
--get ratings from all three providers for filter
repeat with rating in ratings -- repeat through looking for the source we want
set thisSource to source of rating
if thisSource is "Internet Movie Database" then -- check the source
set imdbRating to trimText(value of rating, "/10", "end")
set imdbRating to imdbRating * 10 as string
set imdbRating to trimText(imdbRating, ".0", "end") as number
end if
if thisSource is "Rotten Tomatoes" then -- check the source
set rottenRating to trimText(value of rating, "%", "end")
end if
if thisSource is "Metacritic" then -- check the source
set metacriticRating to trimText(value of rating, "/100", "end")
end if
end repeat
--done with JSON Helper
tell application "Safari"
make new tab at end of tabs of window id winID with properties {URL:"https://www.imdb.com/title/" & tt & "/"}
delay 0.0
end tell
else
--we can't find title on OMDbAPI, open link without pulling any metadata.
tell application "Safari"
make new tab at end of tabs of window id winID with properties {URL:"https://www.imdb.com/title/" & tt & "/"}
delay 0.0
end tell
end if
end tell
end if
end repeat
--text trim handler
on trimText(theText, theCharactersToTrim, theTrimDirection)
set theTrimLength to length of theCharactersToTrim
if theTrimDirection is in {"beginning", "both"} then
repeat while theText begins with theCharactersToTrim
try
set theText to characters (theTrimLength + 1) thru -1 of theText as string
on error
-- text contains nothing but trim characters
return ""
end try
end repeat
end if
if theTrimDirection is in {"end", "both"} then
repeat while theText ends with theCharactersToTrim
try
set theText to characters 1 thru -(theTrimLength + 1) of theText as string
on error
-- text contains nothing but trim characters
return ""
end try
end repeat
end if
return theText
end trimText
Model: iMac
AppleScript: 2.11
Browser: Safari 605.1.15
Operating System: macOS 10.14