A Python Guide to the NY Times Movie Review Search

A while ago I wrote an article on How to Download Archived News Headlines using the NY Times API. In this post, we’re going to go over how to use the Movie Reviews API from the NY Times to pull movie review articles. The only non-native Python library you’ll need for this is the requests module. You will also need to go to the NY Times Developer Portal and sign up for an API key. Save your API key somewhere safe. For me, I saved it in the parent folder of where I’m writing this code. Use the line below in your command line to install the requests module:

pip install requests

Setting Up the Search Request

The first thing we need to do is set up the search request. As always, we’ll start with our imports. We will import requests to send the HTTP GET request and json to parse the response. I also need to import sys to get access to the config file I saved in the parent directory. Using sys.path.append(‘../..’) grants us access to modules in the parent directory. After the imports, all we have to do next is define the URL.

import requests
import json
import sys
sys.path.append('../..')
from nyt.config import public_key
 
search_url = "https://api.nytimes.com/svc/movies/v2/reviews/search.json"

Searching Movie Reviews

Now that everything is set up and ready for us to use the API, let’s define our search function. Our search function will take one parameter, the search term, in the form of a string. Python doesn’t actually require us to tell it what the type of the parameter is, but I put the type as str here anyway. This lets the program know that we’re expecting a string for this parameter. Since we’re using a GET request and not a POST request as we so often do for web APIs, we can only pass parameters and not a json formatted body. 

The first thing we’ll do in our function is define our parameters. For the NY Times Movie Review API, these are pretty simple. All we do is set the query to the term we passed in and the api-key to the public API key we got earlier. Then we send a GET request and parse our response. We can optionally print the response or skip printing it and just directly save it in a JSON file. Then we call the search function on whatever term we want to get NY Times Movie Reviews for that movie. In this case, I used “The Godfather”.

def search(term: str):
    params = {
        "query": term,
        "api-key": public_key
    }
    res = requests.get(search_url, params=params)
    json_res = json.loads(res.text)
    print(json_res)
    with open(f"{term}.json", "w") as f:
        json.dump(json_res, f)
 
search("the godfather")

Let’s take a look at our results. There’s actually a surprisingly small number of results (4) so I’ll just display them here. Personally, I expected the NY Times Movie Reviews to have more results, but ¯\_(ツ)_/¯

{
    "status": "OK",
    "copyright": "Copyright (c) 2021 The New York Times Company. All Rights Reserved.",
    "has_more": false,
    "num_results": 4,
    "results": [
        {
            "display_title": "Square Grouper: The Godfathers of Ganja",
            "mpaa_rating": "R",
            "critics_pick": 0,
            "byline": "RACHEL SALTZ",
            "headline": "Adventures in the Drug Trades",
            "summary_short": "\u201cSquare Grouper\u201d is a documentary about pot smuggling in South Florida in the 1970s and \u201980s.",
            "publication_date": "2011-04-14",
            "opening_date": "2011-04-15",
            "date_updated": "2017-11-02 04:18:13",
            "link": {
                "type": "article",
                "url": "https://www.nytimes.com/2011/04/15/movies/square-grouper-movie-review.html",
                "suggested_link_text": "Read the New York Times Review of Square Grouper: The Godfathers of Ganja"
            },
            "multimedia": null
        },
        {
            "display_title": "The Godfather, Part III",
            "mpaa_rating": "R",
            "critics_pick": 1,
            "byline": "Janet Maslin",
            "headline": "GODFATHER PART III (MOVIE)",
            "summary_short": "Mafia heir trapped by legacy of past. Coppola's director's cut. Deeply moving.",
            "publication_date": "1990-12-25",
            "opening_date": "1990-12-25",
            "date_updated": "2017-11-02 04:17:42",
            "link": {
                "type": "article",
                "url": "https://www.nytimes.com/1990/12/25/movies/review-film-the-corleones-try-to-go-straight-in-the-godfather-part-iii.html",
                "suggested_link_text": "Read the New York Times Review of The Godfather, Part III"
            },
            "multimedia": null
        },
        {
            "display_title": "The Godfather, Part II",
            "mpaa_rating": "R",
            "critics_pick": 1,
            "byline": "VINCENT CANBY",
            "headline": "Godfather: Part II, The (Movie)",
            "summary_short": "",
            "publication_date": "1974-12-13",
            "opening_date": "1974-12-20",
            "date_updated": "2017-11-02 04:17:27",
            "link": {
                "type": "article",
                "url": "https://www.nytimes.com/1974/12/13/archives/godfather-part-ii-is-hard-to-define-the-cast.html",
                "suggested_link_text": "Read the New York Times Review of The Godfather, Part II"
            },
            "multimedia": null
        },
        {
            "display_title": "The Godfather",
            "mpaa_rating": "R",
            "critics_pick": 1,
            "byline": "Vincent Canby",
            "headline": "THE GODFATHER (MOVIE)",
            "summary_short": "Puzo's Mafioso novel. Scalding and memorable.",
            "publication_date": "1972-03-16",
            "opening_date": "1972-03-24",
            "date_updated": "2017-11-02 04:17:26",
            "link": {
                "type": "article",
                "url": "https://www.nytimes.com/1972/03/16/archives/moving-and-brutal-godfather-bows.html",
                "suggested_link_text": "Read the New York Times Review of The Godfather"
            },
            "multimedia": null
        }
    ]
}

Learn More

To learn more, feel free to reach out to me @yujian_tang on Twitter, connect with me on LinkedIn, and join our Discord. Remember to follow the blog to stay updated with cool Python projects and ways to level up your Software and Python skills! If you liked this article, please Tweet it, share it on LinkedIn, or tell your friends!

I run this site to help you and others like you find cool projects and practice software skills. If this is helpful for you and you enjoy your ad free site, please help fund this site by donating below! If you can’t donate right now, please think of us next time.

Yujian Tang
Yujian Tang

I started my professional software career interning for IBM in high school after winning ACSL two years in a row. I got into AI/ML in college where I published a first author paper to IEEE Big Data. After college I worked on the AutoML infrastructure at Amazon before leaving to work in startups. I believe I create the highest quality software content so that’s what I’m doing now. Drop a comment to let me know!

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

$5.00
$15.00
$100.00
$5.00
$15.00
$100.00
$5.00
$15.00
$100.00

Or enter a custom amount

$

Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly

Leave a Reply

%d bloggers like this: