For entertainment purposes, a lot of movies, seasons, music videos, and games are released worldwide. We can extract the information of all these movies and TV shows in the Raspberry Pi terminal easily using Python. To extract the movie information, we use the IMDbPY library of Python and with the help of a script can collect the information about the movies from the IMDb database.

In this write-up, we will install the Python library and learn about the Python script by which we can collect the information about the movies.

How to install IMDbPY on Raspberry Pi OS

The IMDbPY is the Python library, for its installation, we have to make sure the Python3 and its dependencies are installed, if they are not installed, then we can install them using the command:

$ sudo apt install python3-pip libxslt1-dev -y

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image1-21.png" data-lazy- height="457" src="data:image/svg xml,” width=”1211″>

After the installation of the python3 package and its dependency, we will install the IMDbPY using the pip:

$ python3 -m pip install imdbpy

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image3-18.png" data-lazy- height="454" src="data:image/svg xml,” width=”1212″>

A notification will appears when the installation of the IMDbPY is finished:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image2-19.png" data-lazy- height="271" src="data:image/svg xml,” width=”1207″>

How to use the IMDbPY to get movie information in the Raspberry Pi terminal

We will create a file with the extension of “.py” using the nano text editor:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image5-17.png" data-lazy- height="44" src="data:image/svg xml,” width=”786″>

In the newly opened file, we will type the following Python code to extract the movies information from the IMDb:

#import required libraries

import imdb

import sys

# define a function to print names from a list

def List_of_names(nameList):


    names=


    # for each person object, extracts name tag and append to our names string


    if nameList is None: return


    for i in nameList: names=names ‘; ‘ str(i.get(‘name’))


    # returns final string shifted by 2 chars to manage initial ” ;”


    return names[2:]

# initializes IMDb funtion and searches for our name


x= imdb.IMDb()


movies = x.search_movie(sys.argv[1])

# if more film titles are matching search, ask user to refine search title

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


    # Ask user to choos film mumber


    userInput=input(“Please input film number: “)


    film=movies[int(userInput)]


    print()

else:


    # if only 1 film matches search, it is automatically selected


    film=movies[0]

filmID=film.movieID

# get film data


movie = x.get_movie(filmID)

# print main film data

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: ‘ List_of_names(movie.get(‘directors’)))

print(‘Composers: ‘ List_of_names(movie.get(‘composers’)))

print()

print(‘Cast: ‘ List_of_names(movie.get(‘cast’)))

print()

print(‘Sound Department: ‘ List_of_names(movie.get(‘sound department’)))

print()

print(‘Special effects: ‘ List_of_names(movie.get(‘special effects’)))

print()

print(‘Stunts: ‘ List_of_names(movie.get(‘stunts’)))

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image4-17.png" data-lazy- height="493" src="data:image/svg xml,” width=”1215″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image7-13.png" data-lazy- height="454" src="data:image/svg xml,” width=”1217″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image6-16.png" data-lazy- height="460" src="data:image/svg xml,” width=”1199″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image9-9.png" data-lazy- height="452" src="data:image/svg xml,” width=”1215″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image8-11.png" data-lazy- height="102" src="data:image/svg xml,” width=”1181″>

Explanation of the code: We will explain the above Python code in different steps in detail.

Import Libraries: We have imported two libraries, one is IMDbPY which is used to extract information from the IMDb database and the other is sys library which is used to change the values of different variables while the execution of the Python code.

List_of_names(): We have defined a function with the “List_of_names” and in this function, we are just evaluating whether the parameters that are passed to the function are one or many in numbers. If the names are more than 1, then it will display the names of the pass parameter else display the one name.

len(movies)>1: When the user runs the script with the title of the movie, the script will search the movies including the input title. If the movies are greater than 1, matching the title, it will display all those movies with the number and titles. And the user inputs the number of the movie, whose information he/she wants to extract, and will save the information in the variable “film”.

In the remaining code, it will print the information of the movie which is available on the IMDB server. For better understanding, we will execute the command to extract the information of the “Mr Bean”, for this will run the command:

$ python3 movies.py “Mr Bean”

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image12-6.png" data-lazy- height="65" src="data:image/svg xml,” width=”1014″>

The movies which include the keywords of “Mr Bean” in their title are displayed:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image10-8.png" data-lazy- height="415" src="data:image/svg xml,” width=”1204″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image11-7.png" data-lazy- height="536" src="data:image/svg xml,” width=”941″>


In the above output, we entered “10” because we want to extract the information related to “Mr. Bean’s Holiday: Beans in Cannes”. The Python script will display the detailed information of the input movie name with rating:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image13-5.png" data-lazy- height="424" src="data:image/svg xml,” width=”1209″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image14-4.png" data-lazy- height="498" src="data:image/svg xml,” width=”1209″>


Now again, we will use the python script to find the information of the movie “Kings man” using the command:

$ python3 movies.py “Kings man

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image15-2.png" data-lazy- height="527" src="data:image/svg xml,” width=”1045″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image16-1.png" data-lazy- height="384" src="data:image/svg xml,” width=”1194″>


In the above list, we will find information of the movie displayed on position 0:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image17.png" data-lazy- height="440" src="data:image/svg xml,” width=”1216″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image18.png" data-lazy- height="563" src="data:image/svg xml,” width=”1204″>

It will list every information about the movie including the star cast.

Conclusion

To get the information about the movies like the cast, producer’s name, director’s name as well as the IMDb rating of the movie, we can use the terminal of the Raspberry Pi OS. We can find out the information by using the Python script in which we include the IMDbPY library. In this write-up, we have discussed the method of extracting information about the movies using a Python script from the IMDb database.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/hammad–150×150.jpg6253a60009b8c.jpg" height="112" src="data:image/svg xml,” width=”112″>

Hammad Zahid

I’m an Engineering graduate and my passion for IT has brought me to Linux. Now here I’m learning and sharing my knowledge with the world.