Python script to search content using YouTube Data API | Daily Python #8
This article is a tutorial on how to utilize YouTube Data 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.
As seen in Daily Python #7, to access Google API we need to create a project on console.developers.google.com
After enabling the YouTube Data API for your project, let’s create an API key to access the API from our Python script.
After the API Key is generated, copy the key and store it in a variable to access it for authorization.
api_key = "YOUR_API_KEY"
Install the required libraries
pip install google-api-python-client
Now, let’s import the required libraries and create a resource to access YouTube Data API
from apiclient.discovery import build
youtube = build('youtube','v3',developerKey = api_key)
print(type(youtube))
Let’s visit the YouTube Data API’s documentation to read more about searching content using the API
The API provides a list() function to list the content as per our search query.
Let’s write a code to fetch videos for given keywords, just like how we usually search on the YouTube platform.
request = youtube.search().list(q='Countless Storeys',part='snippet',type='video')
print(type(request))res = request.execute()
from pprint import PrettyPrinter
pp = PrettyPrinter()
pp.pprint(res)
The output shows a JSON response of the search result for the query ‘Countless Storeys’. The JSON has multiple fields, the main content is in the ‘items’ filed which is a list of the search result documents.
The type parameter in the list() function is used to modify the search result.
The ‘part’ parameter set to snippet is a required parameter by the list function.
The default number of items returned by the API is 5, which can be changed up to a maximum of 50.
#Print the total number of results
request = youtube.search().list(q='Countless Storeys',part='snippet',type='video',maxResults=50)
res = request.execute()
print('Total items : ',len(res['items']))
Let’s print the title of the fetched results
#Print the title
request = youtube.search().list(q='Countless Storeys',part='snippet',type='video')
res = request.execute()
for item in res['items']:
print(item['snippet']['title'])
Let’s search for a channel by changing the type parameter
#Search for channel
request = youtube.search().list(q='Countless Storeys',part='snippet',type='channel')
res = request.execute()
for item in res['items']:
pp.pprint(item['snippet'])
Let’s search for content published by a particular channel
From the previous query, we were able to fetch the ‘channelId’, which can be used for searching the content published by the channel
# Search content by channel
channelId = 'UCoEAf0U-QUWl9TeMZSC8D3w'
request = youtube.search().list(q='Countless Storeys',part='snippet',type='video',channelId=channelId)
res = request.execute()
for item in res['items']:
pp.pprint(item['snippet'])
I hope this article was helpful and that it will help you use the YouTube Data API for your own purposes. This article can be combined with Daily Python #6 to download the content of a particular channel or to collect data of all the content produced by the channel.
Leave some claps if you liked this article. Stay tuned for more and follow the Daily Python challenge here: