You are not logged in.
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:
Applescript:
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:
1. 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.
2. 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,
Applescript:
--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
Last edited by jprokos (2021-04-09 10:02:32 pm)
Offline
You want to skip or select records based on the contents of some of their properties.
Nigel Garvey posted a script for that here, with example usage.
You need to write a handler in the script for each record property you want to test.
Post back if you need help with that.
Caution: some of your properties have piped labels (i.e., their name is enclosed in pipes: |).
A script fiddling with them should keep those pipes.
There 's a zillion examples for writing to text files on this forum.
The relevant commands are in the Standard Additions osax.
Offline
You may want to look into using AsOBJC.
You’ll want to convert the JSON into a NSArray.
(Look into NSJSONSerialization)
https://macscripter.net/viewtopic.php?id=42971
Have a look down towards Shane Stanley’s reply.
He shows how to use it.....
The you can use NSPredicate to filter the NSArray into a new NSArray.
(Look into NSPredicate)
https://macscripter.net/viewtopic.php?id=47841
Again Shane Stanley’s answer
Once you get the hang of creating a NSPredicate in AppleScript these sites are great references for constructing various Predicates:
https://academy.realm.io/posts/nspredicate-cheatsheet
This is a great reference page:
https://nshipster.com/nspredicate/
Offline
I don't know the term for what I want to accomplish so it's hard to search for it.
Applescript:
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
It is not clear what this repeatable code means, and what your code does. So, remove repeatable code. Trim handler and repeat loop ratings no need. So, you code can be shortened to this:
Applescript:
set {imdbRatings, rottenRatings, metacriticRatings} to {{}, {}, {}}
set {productions, goodTTs, badTTs} to {{}, {}, ""}
-- build OMDB Query
set OMDB_URL to "https://www.omdbapi.com/"
set OMDB_API_key to OMDB_URL & "?apikey=xxxx"
set OMDB_Tomatoes to OMDB_API_key & "&tomatoes=true" --get rotten tomatoes info
set OMDB_Query to OMDB_Tomatoes & "&i=" --do a search by valid IMDb ID
repeat with tt in |IMDbID_list|
if contents of tt is not "" then --skip blank lines
tell application "JSON Helper"
set _JSON to fetch JSON from (OMDB_Query & tt)
if |Response| of _JSON is "True" then -- if the title exists on OMDB proceed...
set end of productions 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
tell Value of item 1 of ratings to set imdbRating to text 1 thru -6 & text -4 as integer
tell Value of item 2 of ratings to set rottenRating to text 1 thru -2 as integer
tell Value of item 3 of ratings to set metacriticRating to text 1 thru -5 as integer
set end of imdbRatings to imdbRating
set end of rottenRatings to rottenRating
set end of metacriticRatings to metacriticRating
set end of goodTTs to contents of tt
else
set badTTs to badTTs & (contents of tt) & linefeed
end if
end tell
tell application "Safari" to make new tab at end of tabs of window id winID with properties {URL:"https://www.imdb.com/title/" & tt & "/"}
delay 0.0
end if
end repeat
-- write bad TTs to UTF8 encoded text file
set the_file to ((path to desktop folder) as text) & "skipped.txt"
set file_ID to open for access the_file with write permission
-- Clear any existing content if the file already exists:
set eof file_ID to 0
-- Add the UTF-8 encoded text:
write badTTs to file_ID as «class utf8»
close access file_ID
The JSON response gives you title information:
Applescript:
set theTitle to Title of _JSON
You say about filtering titles of films, but your code doesn't work with titles at all.
Clearing and appending ratings lists it is unclear for me as well. What you want to do with them? What is this lists and how you want to use them? I added writing bad TTs to text file.
Last edited by KniazidisR (2021-04-10 10:27:21 pm)
Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB
Offline