Get movie info from terminal with IMDbPY and Raspberry PI
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
In this tutorial I’m going to show you how to use a simple script with IMDbPY on Raspberry PI to get movies info.
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 indipendent and open source project to query their data.
IMDbPY is useful to get a huge number of info: from titles, to directors, ratings, casting, stunts and so on. In this tutorial I will use IMDbPY package to ceate a simple script able to query for films title and retrieving some of these 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 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)
Check hardware prices with following links:
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 terminal, use following command:
sudo apt update -y && sudo apt upgrade -y
Install 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 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"
Following paragraphs will describe this script’s tasks. If you want to try it, you can go directly to the end of this page.
Script starts by importing required packages. imdb package is the core in this script, while sys is used to allow passing terminal arguments to 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 case, I had to manage lists including “person” objects instead of simple names strings. namesInList funtion 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 grabbing loop, an if statement will check if input list is empty, in order to avoid errors when this list isn’t containing anything. The main for loop will then scan input list appending results to 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 semicolum as in following example:
; name1; name2; mane3
To cut the first 2 chars and return a clean list, we use built-in string shift function, asking to cut first 2 chars ([2:]) and return resulting vaule to script call.
Next lines will initialize imdb function and run a search for film titles containing string passed as argument from 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 result are availble (“len(movies)” > 1), then a numbered list of all film titles will be showed to user and the program will ask to input the number corresponding to user choose. 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]
Following line will get movieID for selected film. This ID matches the IMDb ID and is used to grab film data in following “get_movie” line:
filmID=film.movieID
movie = ia.get_movie(filmID)
Final part simply prints some film info. Some get metods give back simple numbers (converted in stringd with str function) or strings, some other will answer with list of people 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 “movie.infoset2keys” command and are 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 title. As more than one matches are available, a numbered list will be showed:
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 user is required to write the number near 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”. Following will be returned from script (also note Cover URL, which reports link to find 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
Enjoy!