Raspberry Pi

How to Get Movie Information on Raspberry Pi Terminal

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


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

$ python3 -m pip install imdbpy


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

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:

$ nano movies.py


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')))






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”


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


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:



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



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

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

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.