Python script to consume the OMDb API | Daily Python #15
This article is a tutorial on how to consume the Open Movie Database API using Python.
This article is a part of Daily Python challenge that I have taken up for myself. I will be writing short python articles daily.
Create an account on the OMDb website: http://www.omdbapi.com/. After this step, you will receive an API key via email. This API Key will be used for further communication with the OMDb API.
Requirements:
- Python 3.0
- Pip
Install the following packages:
- requests — Library to making HTTP requests.
- pprint — Library for printing structures of data in a formatted way.
pip install requests pprint
Let’s import these libraries
import requests
from pprint import PrettyPrinter
pp = PrettyPrinter()
The ‘pp’ instance of PrettyPrinter will be used to beautify the JSON output.
Let’s store the API Key in a variable to be used in the program
apiKey = 'YOUR_OMDb_API_KEY'
Now, we simply request data from the OMDb data API
One easy method to pass these params to the URL is to store them in a dictionary and pass it to the get() method provided by requests module.
#Fetch Movie Data
data_URL = 'http://www.omdbapi.com/?apikey='+apiKey
year = ''
movieTitle = 'Fast & Furious'
params = {
's':movieTitle,
'type':'movie',
'y':year
}
Let’s search for a series by changing the ‘type’ parameter
#Fetch Series Data
data_URL = 'http://www.omdbapi.com/?apikey='+apiKey
year = ''
series = 'Friends'
params = {
's':series,
'type':'series',
'y':year
}
Let’s search for a movie with a full plot
#Fetch Movie Data with Full Plot
data_URL = 'http://www.omdbapi.com/?apikey='+apiKey
year = ''
movie = 'Fast & Furious'
params = {
't':movie,
'type':'movie',
'y':year,
'plot':'full'
}response = requests.get(data_URL,params=params).json()
pp.pprint(response)
I hope this article was helpful, do leave some claps if you liked it.