Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 2

Accessing Movie Data with Public APIs

Recommended resources

Hello! Welcome to your second lesson.

In our last session, we evaluated several data sources and selected JustWatch as the best fit for our project, primarily because it offers an API with timely, country-specific movie popularity data.

Today, we will tackle the next logical step: learning how to programmatically extract that data. This lesson is dedicated to the learning outcome: Extract movie viewing data from sources with public APIs. We'll cover the fundamental concepts of APIs and walk through a practical workflow using Python to request data, handle the response, and prepare it for analysis. This is the crucial step where we move from planning to actually acquiring the raw material for our project.


1. Understanding APIs: The Basics

Before we write any code, let's solidify our understanding of what an Application Programming Interface (API) is and the role it plays in data exchange. Since you have a background in software engineering, you're likely familiar with the term, but a focused refresher is always helpful.

APIs act as intermediaries, allowing different software applications to communicate with each other according to a predefined set of rules. When we request data from an API, our program (the "client") sends a request to a remote server, which then sends back a response, typically containing the data we asked for.

To get a clear and intuitive explanation of this process, please watch the first few minutes of the following video.

How to use a Public API | Using a Public API with Python

The video 'How to use a Public API' by Alex The Analyst provides a great conceptual overview of APIs, using a helpful metaphor to explain the client-server relationship.

Watch from the beginning to the 1:34 mark. Pay attention to the explanation of the client-server model and the 'butcher shop' analogy.

This client-server model is the foundation of how we'll interact with data sources like JustWatch. Our Python script will be the client, making a request to the JustWatch API server to get the latest movie charts.



2. The API Interaction Workflow with Python

Now, let's get practical. The most common way to interact with web-based APIs in Python is by using the requests library. It provides a simple and elegant way to send HTTP requests.

The general workflow involves several key steps:

  1. Read the API Documentation: Understand the available "endpoints" (the specific URLs you send requests to), required parameters, and authentication methods.
  2. Make the Request: Use the requests library to send a request (e.g., a GET request) to the correct endpoint, including any necessary authentication like an API key.
  3. Check the Response: Verify that the request was successful by checking the HTTP status code.
  4. Process the Data: Parse the response, which is most commonly in JSON format, into a usable Python data structure like a dictionary or list.

The following video walks through this entire process, from finding the API documentation to writing the Python code and even handling the resulting data.

How to use a Public API | Using a Public API with Python

This video continues the 'How to use a Public API' guide, demonstrating the complete, practical workflow of using a public API with Python.

Watch from 1:34 to 6:26. This section covers finding API documentation, getting an API key, and using a Python script with the requests library to fetch data. The example uses a cryptocurrency API, but the principles are universal.

Let's break down the key takeaways from the video.

Reading Documentation and Using API Keys

As the video emphasizes, the API documentation is your most important tool. It's the instruction manual that tells you exactly how to ask for data. For most public APIs, you'll need to sign up for a free account to get an API key. This is a unique string that identifies your application when you make a request, and it's used to track usage and enforce rate limits.

You typically include the API key in your request, either as part of the URL (a query parameter) or, more commonly, in the request headers.

Making the Request and Handling the JSON Response

The core of the interaction happens with the requests library. For a detailed reference on this process, the following article provides clear code examples.

Python Code for Pulling API Data Easily

This article, 'Python Code for Pulling API Data Easily', provides concise Python snippets for making API calls and handling the data.

Read the sections 'Getting Started: Python API Request Example', 'Python Code for Pulling API Data', and 'Handling JSON Responses in Python'. This will reinforce what you saw in the video with clear, text-based code examples.

As shown in the resources, a basic request looks like this:

import requests

# Define the API endpoint URL and your parameters/headers
api_url = "https://api.example.com/v1/data"
headers = {
    "X-Api-Key": "YOUR_API_KEY_HERE" 
}

# Make the request
response = requests.get(api_url, headers=headers)

# Check for a successful response (status code 200 means OK)
if response.status_code == 200:
    # Parse the JSON response into a Python dictionary
    data = response.json()
    print(data)
else:
    print(f"Request failed with status code: {response.status_code}")

The response.json() method is incredibly convenient. It takes the raw JSON text from the API response and automatically converts it into a Python dictionary or list of dictionaries, which you can then easily work with.

Test your understanding!

Suppose you're using an API that returns data about a movie in the following JSON format:

{
  "id": 123,
  "title": "Christmas Movie Magic",
  "popularity_stats": {
    "country": "CA",
    "rank": 1,
    "viewers": 500000
  },
  "release_year": 2023
}

Assuming you have successfully fetched this data and stored it in a Python variable called movie_data (using movie_data = response.json()), how would you write a line of Python code to print the number of viewers in Canada?

Show answer

You would access the nested dictionary keys like this:

print(movie_data['popularity_stats']['viewers'])


3. From Raw JSON to a Structured DataFrame

API responses, especially those containing multiple items, can be nested and complex. As an ML engineer, you know that the ideal format for analysis is often a flat, tabular structure like a Pandas DataFrame.

Fortunately, there's a straightforward way to convert nested JSON into a DataFrame.

How to use a Public API | Using a Public API with Python

Let's return to the video from Alex The Analyst. This final segment shows a powerful technique for converting a complex JSON response directly into a clean Pandas DataFrame.

Watch the section from 9:26 to 12:13. Focus on the use of pandas.json_normalize to handle the nested JSON data.

As you saw, pandas.json_normalize() is a powerful function. If your API returns a list of dictionaries (even nested ones), you can often turn it into a DataFrame in one line:

import pandas as pd

# Assuming 'data' contains the list of results from the API
# e.g., data = response.json()['results']

df = pd.json_normalize(data)
print(df.head())

This is a massive time-saver and a key step in transforming raw API data into an analysis-ready format, which will be essential for the later stages of our project.



4. Building Robust API Clients: Best Practices

In a real-world setting, simply writing a script that works once isn't enough. Your code needs to be robust and handle potential issues gracefully. Given your engineering background, these principles of building resilient software will be familiar.

  1. Error Handling: Network connections can fail, servers can be down, and your requests might be invalid. Wrap your API calls in try...except blocks to catch potential requests exceptions (e.g., requests.exceptions.Timeout, requests.exceptions.ConnectionError) and handle non-200 status codes.
  2. Respect Rate Limits: Most APIs limit the number of requests you can make in a given time period to prevent abuse. If you exceed this limit, you'll typically get a 429 Too Many Requests status code. Your code should anticipate this and wait for a short period before retrying (a strategy known as "exponential backoff" is a common, robust solution).
  3. Secure Your API Keys: Never hardcode API keys directly in your script, especially if you plan to share your code or commit it to a version control system like Git. Use environment variables or a configuration file to store sensitive credentials.

The following resource offers a great summary of these practices and provides code examples for implementing them.

Fetching Data Through APIs with Python

This article, 'Fetching Data Through APIs with Python', concludes with a valuable list of best practices for building robust and secure API clients.

Please read the section 'Best Practices for Fetching Data from APIs'. It provides a checklist of essential considerations for writing professional-grade API interaction code.


5. Applying This to Our Project: The JustWatch API

The principles we've covered today are directly applicable to the JustWatch API we identified in the last lesson. However, it's important to note that the JustWatch API is a commercial product aimed at businesses, meaning it requires a paid subscription for access.

For the purposes of this course, we will not require you to purchase a subscription. We will proceed by using mock data that simulates the structure of what the JustWatch API would provide. The key takeaway is that the workflow you've just learned—using requests to contact an endpoint, handling authentication, parsing JSON, and normalizing it into a DataFrame—is precisely the process you would use with the real JustWatch API.



Conclusion

In this lesson, we moved from theory to practice, establishing the technical foundation for data acquisition. You now understand how to use Python to interact with public APIs to fetch the data you need.

Key Takeaways:

  • APIs work on a client-server model, where your script (the client) requests data from a server.
  • The Python requests library is the standard tool for making HTTP requests to API endpoints.
  • The common workflow is to make a request, check the status, and parse the JSON response using response.json().
  • pandas.json_normalize is an efficient tool for converting nested JSON into a clean DataFrame for analysis.
  • Writing robust code involves handling errors, respecting rate limits, and keeping API keys secure.

We've now covered how to get data from a source that provides a well-documented, public API. But what happens when the data you want is on a website that doesn't offer one?

In the next lesson, we will address that exact scenario by exploring another powerful data extraction technique: web scraping.