It's a find-a-movie challenge

10

This is a simple(ish) web scraping challenge.

Input

Your code should take an MPAA film rating and a number from 0 to 100 as input. It can do this in any way you find convenient.

Output

Your code should return the name of any movie which has a) that film rating and b) the numerical score on the Tomatometer from rottentomatoes.

If there is no such movie it can output anything you like.

The possible MPAA film ratings are G, PG, PG-13, R, NC-17, NR.

Your code may report one or more movies, that is up to you.

To clarify, your code is meant to access the web to get the answers to queries.

Example

Say the input is "PG, 98" then your code could output "Zootropolis".

Please show an example of your code working with the film rating PG and score 98 along with your answer.

user9206

Posted 2017-09-26T08:45:37.800

Reputation:

O_O NC-17 sounds bad – Christopher – 2017-09-26T10:54:59.513

1NC-17, unlike the others, is supposed to be inappropriate. :P – totallyhuman – 2017-09-26T11:07:11.790

Should my answer return more than one movie? – BlackCap – 2017-09-26T11:29:52.920

1@BlackCap Edited the question. That's up to you. – None – 2017-09-26T11:32:37.343

Zootropolis or Zootopia? – BlackCap – 2017-09-26T11:44:59.497

@BlackCap They are the same movie I believe just renamed for different countries. – None – 2017-09-26T13:04:54.107

This challenge should be edited so the answer isn't a simple web request to the rottentomatoes API. – Tom – 2017-09-26T13:12:42.357

@Tom I think the current answerers wouldn't like that. But it's not quite a simple web request currently, there is json to parse (or grep). – None – 2017-09-26T13:13:36.543

Would it break a rule if someone created a goo.gl link to decrease the link address size? – Robert Benson – 2017-09-26T17:09:45.693

@RobertBenson Yes.

– totallyhuman – 2017-09-26T17:31:08.247

PG-13 doesn't seem to appear in the API, only PG13 does. Consider changing your question to account for that. – Okx – 2017-09-26T19:01:47.723

I'll be saving some of the answers to help when I can't pick a movie...fun puzzle! – BruceWayne – 2017-09-27T00:05:08.893

I'm not sure where people have found this API address, but as best as I can tell, this challenge seems to go against the policies of Fandango (the owner of rottentomatoes) as can be viewed here and from their ToS linked from there.

– Tahg – 2017-09-27T04:28:17.343

In looking into the API's JSON, I have discovered that it will, by default, only return 32 results at at a time. There are however 123 films (as of this writing) with a score of 98 (for example) meaning that there is a possibility that, although films might exist with the given score and rating, they may not be returned in those 32. More results can be requested using the limit parameter but the maximum is 876 and there are currently 1315 films with a score of 100 (for example) - the other 439 can be retrieved by adding the page parameter. – Shaggy – 2017-09-27T13:29:28.723

Does this information invalidate all answers, including my own? – Shaggy – 2017-09-27T13:29:55.240

@Shaggy Only if you can find a case where your code doesn't give the right answer in practice. – None – 2017-09-27T13:30:38.180

Answers

2

Stratos, 133 bytes

{
f"www.rottentomatoes.com/api/private/v2.0/browse?minTomato=%&maxTomato=%&type=dvd-streaming-all"r"results")s"mpaaRating"=⁰
s"title"

Try it!

Okx

Posted 2017-09-26T08:45:37.800

Reputation: 15 025

Could you show an example of your code being used please. – None – 2017-09-26T18:54:39.070

@Lembik Well, run it, and type in the number, press enter, type in the rating, press enter, and it will give you the output. – Okx – 2017-09-26T18:55:36.887

Can you drop the www.? – Shaggy – 2017-09-26T20:22:43.960

@Shaggy Unfortunately not. – Okx – 2017-09-27T15:06:11.270

Maybe you could just post a screenshot of it running? – None – 2017-09-28T18:31:47.780

@Lembik Added a link. – Okx – 2017-10-21T21:08:36.060

12

Bash, 182 bytes

curl "https://www.rottentomatoes.com/api/private/v2.0/browse?minTomato=$2&maxTomato=$2&type=dvd-streaming-all" 2>/dev/null|grep -o "{[^}]*aRating\":\"$1\""|grep -Po 'title":"\K[^"]*'

Usage:

$ bash script PG 98
The Island President
Inside Out
Zootopia
Paddington
Love & Friendship
Long Way North (Tout en haut du monde)

BlackCap

Posted 2017-09-26T08:45:37.800

Reputation: 3 576

I really like this answer. – None – 2017-09-28T18:15:44.947

6

JavaScript (ES6), 167 162 159 bytes

Needs to be run from the root of rottentomatoes.com. Returns a Promise object containing the title.

s=>n=>fetch(`api/private/v2.0/browse?minTomato=${n}&maxTomato=${n}&type=dvd-streaming-all`).then(r=>r.json()).then(j=>j.results.find(x=>x.mpaaRating==s).title)

If we can require that it be run in a specific directory then it becomes 139 bytes:

s=>n=>fetch(`browse?minTomato=${n}&maxTomato=${n}&type=dvd-streaming-all`).then(r=>r.json()).then(j=>j.results.find(x=>x.mpaaRating==s).title)

Shaggy

Posted 2017-09-26T08:45:37.800

Reputation: 24 623

It would be great if there were a click and look way to test Javascript answers. – None – 2017-09-28T18:17:54.020

@Lembik: normally that can be done when working with APIs but, in this case, RT's CORS policy prevents it. Thanks for the accept, by the way. – Shaggy – 2017-09-28T18:22:42.487

Hmm..I just noticed there are shorter answers but I don't have the energy/expertise to test Stratos code right now. If I do, I will have to change the accept, sorry. – None – 2017-09-28T18:25:26.720

@Lembik: I fully expected Stratos to get it (so far). – Shaggy – 2017-09-28T18:26:50.740

Oh I added a bounty to the other find-a-movie question :) – None – 2017-09-28T18:27:18.433

3

Python 2 + requests, 209 204 bytes

-5 bytes thanks to Ian Gödel.

lambda r,t:[i['title']for i in get('http://rottentomatoes.com/api/private/v2.0/browse?minTomato=%d&maxTomato=%d&type=dvd-streaming-all'%(t,t)).json()['results']if i['mpaaRating']==r]
from requests import*

totallyhuman

Posted 2017-09-26T08:45:37.800

Reputation: 15 378

1Think you may need (t,t) where you have (r,r) – ElPedro – 2017-09-26T12:47:58.227

1

You can use this link: http://rottentomatoes.com/api/private/v2.0/browse?minTomato=%d&maxTomato=%d&type=dvd-streaming-all (https -> http and drop the www.) for a couple of bytes saved.

– None – 2017-09-26T14:17:28.940

1Could you show an example of your code being used please. – None – 2017-09-26T18:55:08.697

2

q/kdb+, 168 bytes

Solution:

{(r(&)(r:.j.k[.Q.hg`$"https://www.rottentomatoes.com/api/private/v2.0/browse?type=dvd-streaming-all&min",t,"&max",t:"Tomato=",y]`results)[;`mpaaRating]like x)[;`title]}

Example:

q){(r(&)(r:.j.k[.Q.hg`$"https://www.rottentomatoes.com/api/private/v2.0/browse?type=dvd-streaming-all&min",t,"&max",t:"Tomato=",y]`results)[;`mpaaRating]like x)[;`title]}["PG";"98"]
"The Island President"
"Inside Out"
"Zootopia"
"Paddington"
"Love & Friendship"
"Long Way North (Tout en haut du monde)"

Explanation:

.Q.hg           / fetch a URL
.j.k            / parse json string into Q dictionaries
`results        / index into dictionary with key `results
[;`mpaaRating]  / index into these sub dictionaries extracting mpaaRating
like x          / binary list where they match, e.g. "PG"
(&)             / where, list indices where true
[;`title]       / index into dictionary with key `title

Notes:

  • If you want to try this out yourself, take a read through the Cookbook/SSL page to ensure your environment is setup correctly.
  • Fails if we remove the s of https, or the www., gives 301 Permanently Moved response.

streetster

Posted 2017-09-26T08:45:37.800

Reputation: 3 635

Maybe add a link for "q/kdb+" in the title of your answer? A lot of people will never have heard of it and you can educate them (me). – None – 2017-09-26T13:51:15.440

Done :) I was hoping to write a solution in oK but getting a 'No 'Access-Control-Allow-Origin' header is present...' when using the online repl (http://johnearnest.github.io/ok/index.html), so have falled back to the closed-source Q.

– streetster – 2017-09-26T13:57:34.623

2

JavaScript (ES2017), 158 bytes

async(r,n)=>(await(await fetch(`api/private/v2.0/browse?minTomato=${n}&maxTomato=${n}&type=dvd-streaming-all`)).json()).results.find(m=>m.mpaaRating==r).title

Run from the Rotten Tomatoes home page:

f=async(r,n)=>(await fetch(`api/private/v2.0/browse?minTomato=${n}&maxTomato=${n}&type=dvd-streaming-all`).then(x=>x.json())).results.find(m=>m.mpaaRating==r).title
f('PG',98).then(console.log)

darrylyeo

Posted 2017-09-26T08:45:37.800

Reputation: 6 214