Fetch TMDB movie data using Python
Hey! Everyone I’m glad to see you here, hope you all are doing well. This post is going to be an interesting one for you and I hope it’ll fulfill your expectation after reading it. For fetching TMDB movie data we have two ways to do it first using RESTful APIs or second using tmdbv3api.
In this post, I’ll explain to you how you can
- Fetch TMDB(The Movie Database) movie data from using RESTful APIs in Python. (Using only requests module)
- Fetch TMDB(The Movie Database) movie data using tmdbv3api library.
So let’s start.
TMDB(The Movie Database):
So before going into the coding part let me first explain TMDB. TMDB(The Movie Database) is a community movie and TV database where you can find all the data regarding any movie or TV show. It came in 2008 and since then TMDB has become a greater source of metadata. You can find information about people, movies, and TV shows here, and a thousand fine-grained movie posters. So now maybe you’re thinking about what is the purpose of this TMDB here so let me explain to you using TMDB you can extract all the information about movies and TV shows using an API(Application Programming Interface) key which is provided to you by TMDB free of cost and I hope you know already about API. If you don’t know browse it and you can find more about it. So I hope you get a good idea about TMDB.
Now let’s suppose as a developer you want to access details about some popular movies using coding or you want to develop an application related to movies for your organization and how you will do that, so let’s get started. It’s an easy one.
Fetching TMDB movie data using RESTful API in Python:
Let us understand what is a RESTful API. It is an interface that two autonomous computer systems use to exchange information amongst themselves. I visited the TMDB website to get an API key for myself. You also need to Sign up yourself to get your own curated API key.
Follow these links:-
Register an account: https://www.themoviedb.org/account/signup
Check how you can get your unique API: https://developers.themoviedb.org/3/getting-started/introduction
So I hope now you have set up your system for the coding part and have access to your unique API key.
Code:
import requests from pprint import pprint url = "https://api.themoviedb.org/3/discover/movie/" api_key = "YOUR_TMDB_API_KEY" response = requests.get(url, params = {'api_key' : api_key}) json_data = response.json() pprint(json_data)
I have used the request module to enable myself to request data from the TMDB server. Check out our blog on Send GET and POST requests in Python to know more about the requests module. As I want to retrieve data on all popular movies I’ve used the URL to that page, https://api.themoviedb.org/3/discover/movie/
. I have stored my API key in a temporary variable, api_key
. I have used the get method to get the movie data. The arguments for the method are my URL and the params field which takes my unique API key.
Output:-
The response variable returns a dictionary of the JSON data. I’ve pretty printed the dictionary which contains details on the popular movies using pprint module.
Fetch the release date from
Now, let’s retrieve the release date of every movie. You can refer to our How to parse JSON in python to learn more on this topic.
Code:-
Release_data = json_data['results'] for i in range(0, len(Release_data)): print(Release_data[i]['release_date'])
Now that I know that my JSON object contains two values: pages and results. As I’m interested in getting the release dates of the movies, I made a list of the results of the JSON object with the name, Release_data. I’ve used a for loop with a starting value of 0 and an end value as the length of the list variable Release_data
, to iterate over each movie which is stored in the form of a dictionary. To get the release date I’ve subscripted the list as Release_data[i]['release_date']
this value returns me the release date value of the ith movie in the list Release_data
.
Output:-
Thus you can now fetch any information from the TMDB movie data.
Search for a particular movie:-
Now that you know how to fetch all and particular information from the TMDB API. Let us look at how you can search for a particular movie using the REST API.
Code:-
import requests from pprint import pprint base_url = "https://api.themoviedb.org/3" api_key = "YOUR_TMDB_API_KEY" input_Str = input("Movie's name : ") url = f"{base_url}/search/movie?api_key={api_key}&query={input_Str}" response = requests.get(url) json_data = response.json() pprint(json_data)
In the above code, I have taken the name of the movie you want to search for as an input and stored it in a temporary variable, input_Str. I have also curated the URL here by formatting a string, where I’ve given my api_key’s value in the api_key parameter and the query parameter’s value as input_Str, the name of the movie you want to search for. I have used the get method to get the JSON object from the URL. Now I’ve used the json
function to convert the JSON object to a dictionary and print the same.
Output:-
I have given the input_Str value as Avengers and got the details on all movies that have the word ‘Avengers’ in them.
Fetching TMDB movie data using tmdb3api
Here I’ll explain to you using the tmdbv3api Python library. Since maybe you don’t have some experience with REST before it it’ll become typical for you.
tmdbv3api:-
tmdbv3api is a Python library using which you can work with TMDB and access any information from TMDB while creating your application in Python.
You can install the tmdbv3api library using pip install tmdbv3api and in order to work with it, you need to install it, so before going ahead install it using this above command on your development environment.
And before going with the coding part you also need to Register your account to get your API key from TMDB.
Follow these links:-
Register an account: https://www.themoviedb.org/account/signup
Check how you can get your unique API: https://developers.themoviedb.org/3/getting-started/introduction
So I hope now you have set up your system for the coding part and have access to your unique API key
from TMDB copy it somewhere you’ll need it.
So, now let’s start with the coding part. I used Jupyter Lab as my id:-
#Python program for fetching TMDB movie data #after installing our library we'll import or initialize TMDB and Movie object from tmdbv3api import TMDb from tmdbv3api import Movie tmdb=TMDb() #set you unique API key that you get from TMDB after registartion in my case this is my API key TMDb.api_key='6f15568d9aa3d15d0261a5454578c28b' #you can also export your api key as environment variable using this below export command # export TMDb_API_KEY='6f15568d9aa3d15d0261a5454578c28b' #it's optional it's for setting language of Data that we'll fetch from TMDB tmdb.language = 'en' #for debugging we have set it true tmdb.debug = True movie=Movie() #here I have fetched movie data using movie_id similar = movie.similar(343611) #iterate through the data since it have multiple information for s in similar: #print required data like in my case title of movie and it's overview, poster path print("Title of the movie: ", s.title) print("Overview of the movie:", s.overview) print("Poster of the Movie:", s.poster_path) #it will give us all movie data which have similar id=343611 #you can also fetch movies using other methods also like popular(), recommendation() etc.
Output:-
Below is the output of the above code:
Thank You. I hope you have enjoyed this post and learned a lot from it.
You can also check: Python JSON Encoder and Decoder
If you like this post comment on it.
Leave a Reply