Get the Latest and Popular Movies with Python.

Published by Prince Jan on March 18, 2021

In this tutorial, you will learn to retrieve and track the latest, popular and trending movies via an API at www.themoviedb.org (TMDb) using python.

This is a great resource for developers if you are building a movie web app or mobile app, the API gives you access to millions of movies and tvshows with their details, actors, release dates, trailers, movie poster images, backdrops, ratings, etc all for free.

To use their API, you have to get access to an API key which you can generate for free when you sign up on their website(www.themoviedb.org). You can generate the API in your account settings (Getting Started).

After signing up and generating your API keys, you can view their API documentation at https://developers.themoviedb.org/3/getting-started .

 

Finding popular movies and tvshows with python

Before we start accessing their API, we to set the base_url - that is the API's base url which is "https://api.themoviedb.org/3". From the api docs, to get the popular movies, we will have to use the base_url/movie/popular:

import requests

api_key = "your_api_key"
base_url = "https://api.themoviedb.org/3"

r = requests.get(f"{base_url}/movie/popular", params={'api_key':api_key})

data = r.json()

for movie in data["results"]:
    title = movie['title']
    overview = movie['overview']
    print("Movie Title: " + title)
    print("Overview: " + overview[:50] + "...")
    print()

output

Movie Title: Tenet
Overview: Armed with only one word - Tenet - and fighting fo...

Movie Title: Fatman
Overview: A rowdy, unorthodox Santa Claus is fighting to sav...

Movie Title: Upside-Down Magic
Overview: Nory and her best friend Reina enter the Sage Acad...

Movie Title: Joker
Overview: During the 1980s, a failed stand-up comedian is dr...

Movie Title: Rogue Warfare: Death of a Nation
Overview: After rescuing Daniel from the terrorist Black Mas...

Movie Title: Chick Fight
Overview: When Anna Wyncomb is introduced to an an undergrou...

Movie Title: Hard Kill
Overview: The work of billionaire tech CEO Donovan Chalmers ...

Movie Title: Mulan
Overview: When the Emperor of China issues a decree that one...

Movie Title: The Christmas Chronicles: Part Two
Overview: Kate Pierce is reluctantly spending Christmas with...

Movie Title: Peninsula
Overview: A soldier and his team battle hordes of post-apoca...

Movie Title: Roald Dahl's The Witches
Overview: In late 1967, a young orphaned boy goes to live wi...

Movie Title: Rogue City
Overview: Caught in the crosshairs of police corruption and ...

Movie Title: The SpongeBob Movie: Sponge on the Run
Overview: When his best friend Gary is suddenly snatched awa...

Movie Title: Welcome to Sudden Death
Overview: Jesse Freeman is a former special forces officer a...

Movie Title: Demon Slayer the Movie: Mugen Train
Overview: TanjirĊ Kamado, joined with Inosuke Hashibira, a b...

(output truncated)

You always need to append your api_key when making a request to their API. This request automatically returns page 1 of popular movies which contains 20 movies. To request another page of popular movies, you need to specify the page number in the url parameters of the request.

r = requests.get(f"{base_url}/movie/popular", params={'api_key':api_key, 'page':2})

You can request up 500 pages of popular movies.

 

To retrieve popular tvshows, we will have to make a request to base_url/tv/popular

import requests

api_key = "your_api_key"
base_url = "https://api.themoviedb.org/3"

r = requests.get(f"{base_url}/tv/popular", params={'api_key':api_key, 'page':2})

data = r.json()

for show in data["results"]:
    name = show['name']
    overview = show['overview']
    print("TvShow Name: " + name)
    print("Overview: " + overview[:50] + "...")
    print()

output

TvShow Name: Vikings
Overview: The adventures of Ragnar Lothbrok, the greatest he...

TvShow Name: The Walking Dead
Overview: Sheriff's deputy Rick Grimes awakens from a coma t...

TvShow Name: The Flash
Overview: After a particle accelerator causes a freak storm,...

(truncated)

You can also request different pages for popular tv shows.

 

Finding latest movies and tvshows now playing in theatres

For movies now playing in theatres, that is available on base_url/movie/now_playing . You can optionally specify a region prameter which will narrow the search to only look for theatrical release dates within the specified country.

import requests

api_key = "your_api_key"
base_url = "https://api.themoviedb.org/3"

r = requests.get(f"{base_url}/movie/now_playing", params={'api_key':api_key})

data = r.json()

for movie in data["results"]:
    title = movie['title']
    overview = movie['overview']
    print("Movie title: " + title)
    print("Overview: " + overview[:50] + "...")
    print()

 

For tv shows, we can retrive that from base_url/tv/on_the_air. A request to this url gives us a list of tv shows that are currently on the air.

This query looks for any TV show that has an episode with an air date in the next 7 days.

import requests

api_key = "b5fead990c45bf6da5a6e002df29743d"
base_url = "https://api.themoviedb.org/3"

r = requests.get(f"{base_url}/tv/on_the_air", params={'api_key':api_key})

data = r.json()

for show in data["results"]:
    name = show['name']
    overview = show['overview']
    print("TvShow Name: " + name)
    print("Overview: " + overview[:50] + "...")
    print()

 

More URLS

  • Top_Rated movies - base_url/movie/top_rated

  • Upcoming movies - base_url/movie/upcoming

  • Popular tvshows - base_url/tv/popular

  • Top rated TvShows - base_url/tv/top_rated

The base_url is the base API url which is "https://api.themoviedb.org/3".

 

This is just a scratch on what the TMDb API can offer. Visit https://developers.themoviedb.org/3  to explore and learn more.


Subscribe to receive updates!

0 Comments

Leave a comment

* Email would not published