Get movie info from terminal with IMDbPY and Raspberry PI
Last Updated on 2nd September 2023 by peppe8o
In this tutorial, I’m going to show you how to use a simple script with IMDbPY on Raspberry PI to get movies info.
Many modern media centres can get automatically film and movie info from internet. If you need to get them in your terminal console (for your programs or just for funny), the IMDbPY package make this task simple and fast, even from Raspberry PI.
What is IMDbPY
IMDbPY is a very simple and fast Python library able to get movies info from internet. Written in Python, it can get data both from IMDb’s web servers or from local copies. IMDbPY is not linked in any way with IMDb (Internet Movie Database Inc), being simply an independent and open-source project to query their data.
IMDbPY is useful to get a huge number of info: titles, directors, ratings, casting, stunts and so on. In this tutorial, I will use IMDbPY package to create a simple script able to query for film titles and retrieve some of this info directly in your terminal console.
What We Need
As usual, I suggest adding from now to your favourite e-commerce shopping cart all the needed hardware, so that at the end you will be able to evaluate overall costs and decide if to continue with the project or remove them from the shopping cart. So, hardware will be only:
- Raspberry PI Computer Board (including proper power supply or using a smartphone micro usb charger with at least 3A)
- high speed micro SD card (at least 16 GB, at least class 10)
Step-by-Step Procedure
Prepare Operating System
Start installing your OS. You can install Raspberry PI OS Lite (for a light, high-performing environment) or install Raspberry PI OS Desktop (in this case, working from its internal terminal console).
Make your OS up to date. From the terminal, use the following command:
sudo apt update -y && sudo apt upgrade -y
Install the required packages. IMDbPY can be installed with pip, but also requires libxslt package:
From terminal:
sudo apt install python3-pip libxslt-dev
python3 -m pip install imdbpy
Get Python Script
We’ll use a ready script available from my download area. You can get it directly in your Raspberry PI with the following terminal command:
wget https://peppe8o.com/download/python/queryFilms.py
This script will be used passing a film title as argument (inside double quotes) as below:
python3 queryFilms.py "my favourite film title"
The following paragraphs will describe this script’s tasks. If you want to try it, you can go directly to the end of this page.
The script starts by importing the required packages. IMDb package is the core in this script, while sys is used to allow passing terminal arguments to the script:
import imdb
import sys
A function is defined to grab data including referrals to people (like directors, casting, stunts and so on). In these cases, I had to manage lists including “person” objects instead of simple name strings. namesInList function requires as input a list of IMDbPY person objects, usually linked from movies objects.
This function starts defining a “names” string (initialized to empty), which will include all names get with a for loop. Before starting the grabbing loop, an if statement will check if the input list is empty, in order to avoid errors when this list isn’t containing anything. The main for loop will then scan the input list appending results to the names string:
def namesInList(nameList):
names=''
if nameList is None: return ''
for i in nameList: names=names+'; '+str(i.get('name'))
return names[2:]
As this iteration adds a semicolumn to separate names before each name, the very first loop will start with a semicolon as in the following example:
; name1; name2; mane3
To cut the first 2 chars and return a clean list, we use the built-in string shift function, asking to cut the first 2 chars ([2:]) and return the resulting value to the script call.
The next lines will initialize the imdb function and run a search for film titles containing a string passed as argument from the terminal (sys.argv[1]). Search results will be stored in a variable named “movie” (list type):
ia = imdb.IMDb()
movies = ia.search_movie(sys.argv[1])
As IMDbPY performs “like” queries, in the sense that it will search for every film matching your search, even partially, we need to manage cases when more titles can come out.
If more results are available (“len(movies)” > 1), then a numbered list of all film titles will be shown to the user and the program will ask to input the number corresponding to the user’s choice. Otherwise, if only 1 result is available, this if statement will go automatically ahead with no need for inputs:
if len(movies) > 1:
print('More films matching query:\n')
print('Number | Film title')
print('--------------------')
id=0
for i in movies:
print(str(id)+' | '+i['title'])
id +=1
userInput=input("Please input film number: ")
film=movies[int(userInput)]
print()
else:
film=movies[0]
The following line will get movieID for the selected film. This ID matches the IMDb ID and is used to grab film data in the following “get_movie” line:
filmID=film.movieID
movie = ia.get_movie(filmID)
The final part simply prints some film info. Some get methods give back simple numbers (converted in a string with str function) or strings, some others will answer with a list of people’s names and are printed using namesInList() function already defined:
print('Title: '+movie.get('title'))
print('IMDb ID: '+str(filmID))
print()
print('Cover URL: '+str(movie.get('cover url')))
print()
print('Original title: '+movie.get('original title')+' | '+str(movie.get('genres')))
print()
print('Rating: '+str(movie.get('rating'))+' (based on '+str(movie.get('votes'))+' votes)')
print()
print('Directors: '+namesInList(movie.get('directors')))
print('Composers: '+namesInList(movie.get('composers')))
print()
print('Cast: '+namesInList(movie.get('cast')))
print()
print('Sound Department: '+namesInList(movie.get('sound department')))
print()
print('Special effects: '+namesInList(movie.get('special effects')))
print()
print('Stunts: '+namesInList(movie.get('stunts')))
You can include more info in your report, as the full set is available with the “movie.infoset2keys” command and is listed below:
- title
- year
- genres
- rating
- votes
- cast
- directors
- sound department
- special effects
- stunts
- runtimes
- countries
- country codes
- language codes
- color info
- aspect ratio
- sound mix
- box office
- certificates
- original air date
- cover url
- imdbID
- plot outline
- languages
- kind
- writers
- producers
- composers
- cinematographers
- editors
- editorial department
- production designers
- costume designers
- make up department
- production managers
- assistant directors
- art department
- camera department
- music department
- script department
- miscellaneous
- thanks
- akas
- writer
- director
- top 250 rank
- production companies
- distributors
- other companies
Run queryFilms Script
Finally, test queryFilms script. For terminal:
python3 queryFilms.py "the good"
As said, this will search for all films including “the good” in the title. As more than one matches are available, a numbered list will be shown:
More films matching query:
Number | Film title
--------------------
0 | The Good
1 | The Good Doctor
2 | Honest Thief
3 | The Good Place
4 | The Good Fight
5 | The Good Wife
6 | Good Girls
7 | The Goonies
8 | The Good, the Bad and the Ugly
9 | The Good Liar
10 | Four Good Days
11 | The Good Son
12 | The Good Cop
13 | Good Trouble
14 | Good Will Hunting
15 | The Good Witch
16 | The Good Lie
17 | The Good Lord Bird
18 | The Good Girl
19 | Good Time
and the user is required to write the number near the title you want to show info about:
Please input film number:
I will write 8 and press return because I want to see info about “8 | The Good, the Bad and the Ugly”.
The following will be returned from the script (also note the Cover URL, which reports a link to find the film cover from your browser):
Title: The Good, the Bad and the Ugly
IMDb ID: 0060196
Cover URL: https://m.media-amazon.com/images/M/MV5BOTQ5NDI3MTI4MF5BMl5BanBnXkFtZTgwNDQ4ODE5MDE@._V1_SX101_CR0,0,101,150_.jpg
Original title: Il buono, il brutto, il cattivo (1966) | ['Western']
Rating: 8.8 (based on 694564 votes)
Directors: Sergio Leone
Composers: Ennio Morricone
Cast: Eli Wallach; Clint Eastwood; Lee Van Cleef; Aldo Giuffrè; Luigi Pistilli; Rada Rassimov; Enzo Petito; Claudio Scarchilli; John Bartha; Livio Lorenzon; Antonio Casale; Sandro Scarchilli; Benito Stefanelli; Angelo Novi; Antonio Casas; Aldo Sambrell; Al Mulock; Sergio Mendizábal; Antonio Molino Rojo; Lorenzo Robledo; Mario Brega; Richard Alagich; Chelo Alonso; Fortunato Arena; Román Ariznavarreta; Silvana Bacci; Joseph Bradley; Frank Braña; Amerigo Castrighella; Saturno Cerra; Luigi Ciavarro; William Conroy; Antonio Contreras; Domingo Contreras; Axel Darna; Tony Di Mitri; Alberigo Donadeo; Attilio Dottesio; Luis Fernández de Eribe; Veriano Ginesi; Jesús Guzmán; Víctor Israel; Nazzareno Natale; Ricardo Palacios; Antonio Palombi; Julio Martínez Piernavieja; Jesús Porras; Romano Puppo; Antoñito Ruiz; Aysanoa Runachagua; Enrique Santiago; José Terrón; Franco Tocci; Alfonso Veady
Sound Department: James Alfaro; Aldo Ciorba; Vittorio De Sisti; Bob Lacivita; Preston Martin; Elio Pacella; Alan Porzio; Nathan Scruggs; Burton Sharp; Fausto Ancillai
Special effects: Eros Bacciucchi; Antonio Baquero; Giovanni Corridori
Stunts: Román Ariznavarreta; Luigi Ciavarro; John Landis; Valentino Pelizzi; Romano Puppo; Benito Stefanelli; Fabio Testi
Next Steps
Interested in more projects with your RPI? Try to look at my Raspberry PI computer tutorial pages.
Enjoy IMDbPY!