Searching for specific content in netflix with Python
Luis Ch January 02, 2025 #api #python #projectsIntro
During these vacations I wanted to take advantage of my free time to complete at least one TV show. Avoiding unfinished ones, since I don't want to get hooked again to series that end up being canceled. On this opportunity we will be looking at the Netflix platform, which as we well know has a reputation of cancelling many of the series it produces.
Solution
Let's filter the content to find TV series with Ended
status. On the TMDB website we can see complete information about series and movies. It allows us to filter content by platform and even by region, but unfortunately it does not allow filtering by status. However, from its API, we can access to a lot of filters and among them, we can filter by status.
TMDB API
Discover TV Series
Log in to the TMDB developer portal, and go to the Discover series section. In this section we can see a list of available endpoints[1], their documentation and a small interface to test the API.
In this last section we will be able to use the API directly, and it also shows us the code to perform the query in different programming languages. Copy the API key that you can see in the “Credentials” field or in the example code.
In the endpoint documentation you will find a long list of filters, but we will only use some of them.
with_status
: The statusEnded
is represented by the value3
.with_watch_providers
: ID of the platform on which the content should be available, it is necessary to specify the region. The Netflix platform ID is8
.watch_region
: here we specify the region, which in my case will beVE
.with_networks
: to get original series from the specified company. The Netflix network ID is213
.
The values currently mentioned were extracted from the detailed content, from a Netflix original series, obtained from the TMDB API.
In the case of the status for TV series it is not specified in the documentation, but there is a clear reference in the TMDB forum.
Preparing the code
We start by creating the basic structure of the project. From Linux console we do it running the following commands.
# Create a directory for the project and get in
# Create a python virtual environment and activate it
# Update pip and install project requirements
# Create python files we will write later
Now we create a python module with the object models that we will get from the API, based on the results of the discover function.
models.py
file:
"""models.py"""
"""Media Content class"""
: =
:
"""TV show content"""
:
:
:
"""Response from TMDB API"""
:
:
:
:
Now we create the main module (discover.py
), where we will import the models.py module and write all the functionality. It is necessary to create a file called .env
inside our project, where we are going to store our API key as an environment variable.
.env
file:
TMDB_API_KEY= put here your api key
Import all the necessary modules, declare the constants and load the environment variable with our secret key in the main module.
discover.py
file:
"""Module to discover content in TMDB"""
=
=
# CONSTANTS
: =
: = 8
: = 213
: = 3
: =
...
Now, in the same module we are going to create the function to "get TV series".
...
"""This function returns the results from the TMDB API."""
=
=
=
=
=
return
...
And finally, we are going to model that data and present it in a table.
...
"""Main function"""
=
=
+=
=
=
Results
Here is the first results from TMDB, as of today, of complete series on Netflix available for Venezuela.
And here we filter for Netflix original series.
)
Conclusion
This way I have been able to find complete series on the Netflix platform. Also, by adjusting the filters we can get different results, for example, searching for content on multiple platforms or on some other specific platform like Apple TV.
I wrote this version of the code some time ago, I am currently creating a version with web interface (Web UI), to facilitate access to all types of users, since this version works from the command line (CLI).
In this case we are getting the first 20 results, pagination will be implemented later.
And of course, you can access the full code in this repository.