Level of Difficulty: Beginner – Senior.
There are many different workflow and automation suites/platforms available out there (some of which include IFTTT, Power Automate and Tonkean) that allow users to interact with their YouTube connector. Most of these workflows classify the functions within the connectors as either a Trigger or an Action. A trigger would be seen as an event that “kicks off” the workflow/process, whereas an action would be an event (or a set of events) that should be executed once the workflow/process has been triggered.
Many of these workflows make use of APIs to get their triggers and actions functioning. There is one small problem though… They don’t always have the predefined triggers or actions that we might be looking to use. Platforms like IFTTT and Power Automate do not yet have a “When an item is added to a Playlist” trigger. Not a train smash though… In this post, we work through how to monitor a YouTube playlist for the addition of new items using Python and the YouTube Data API.
What are the steps?
The steps that we will be following are:
- Get a Developer Key
- Create Project
- Create Credentials
- Get API Key
- Create the Python Script
- Import Python Libraries
- Obtain Playlist ID
- Query Playlist Items
- Process New Items
Let’s dive deeper into the steps listed above.
Please note: This will require a YouTube Playlist to be created if it doesn’t already exist.
Get a Developer Key
You’ll first need to create a project by either clicking on “Create Project” if you have the option, or by clicking on “Select a Project” proceeded by “New Project”:
Once you’ve selected an option to create a new project, you’ll be prompted to enter a name. Thereafter, you may click “Create”:
After the redirect, you should be focused on “Credentials” where you can add a new API key by selecting the “Create Credentials” option:
Next, copy the API key as we will need it to get the Python script working properly:
Create Python Script
Now, let’s switch to Python and install the correct libraries before we can import them:
!pip install google-api-python-client !pip install google_auth_oauthlib
Import Libraries and Instantiate Variables
The following libraries should be imported:
from googleapiclient.discovery import build from googleapiclient.errors import HttpError from oauth2client.tools import argparser import pandas as pd import numpy as np import requests import json
Next, let’s instantiate our first three variables needed to work with the YouTube Data API:
DEVELOPER_KEY = '<insert key>' YOUTUBE_API_SERVICE_NAME = 'youtube' YOUTUBE_API_VERSION = 'v3'
Obtain Playlist ID
To get familiar with how the Google documentation works, let’s explore how to get a list of playlists, here, using the “Try it” function:
The documentation explains what parameters are required and which are optional. For the sake of getting a list of my own playlists, I added the following values before selecting “Execute”:
You should receive a 200 response with information regarding your playlists:
By selecting “Show Code” shown above, you should be able to select “Python” to see the Python Code if you wanted to add it to the automation script:
Once you have the ID of the playlist that you’d like to monitor, assign it to a variable:
playlist_id = '<insert id>'
Query Playlist Items
There is a max result limit of 50 results per call to the API which means that the results will need to be paged if there are more than 50 items in a playlist (multiple calls will need to be made to get all the results, 50 at a time). The response will contain a page token if there is a next page.
Now, let’s create a method that allows for paging through results:
# Get all items in specified playlist def get_playlist_items(page_token): # Auth with YouTube service youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY) # Call the playlistItems.list method to retrieve results matching the specified query term. request = youtube.playlistItems().list( part="snippet,contentDetails", pageToken=page_token, maxResults=50, playlistId=playlist_id ) response = request.execute() return response
Process New Items
In the true spirit of automation workflows/processes, if the trigger is “new items found in a playlist”, then we need actions to execute once that is found to be true. We can encapsulate these actions into a “Process New” method:
# process any items that were not found in the previous set of results def process_new(df_old, df_new): df_diff = df_new.set_index('title').drop(df_old['title'], errors='ignore').reset_index(drop=False) print(len(df_diff)) for index, element in df_diff.iterrows(): print("New Item Added: " + str(element['title']).encode('utf-8'))
Let’s tie the top two methods together through a “main” method code snippet. Make sure you have an “items.xlsx” file that records all of the items that are in the playlist:
isEnd = False page_token = None df = pd.DataFrame() # instantiate blank dataframe df_history = pd.read_excel('items.xlsx', headers=False) # read history before querying new results so that the new records may be identified while not isEnd: playlist_items = get_playlist_items(page_token) current_count = playlist_items['pageInfo']['totalResults'] # if there is a page token, use it for the next call or assign it back to None if 'nextPageToken' in playlist_items.keys(): page_token = playlist_items['nextPageToken'] else: isEnd = True page_token = None # write playlist item information to the dataframe for item in playlist_items['items']: temp_df = pd.DataFrame.from_dict(item) temp_df = temp_df[['snippet']].transpose() df = df.append(temp_df) df.to_excel('items.xlsx') # write the dataframe to excel process_new(df_history, df) # process the new items
Did this work for you? Feel free to drop a comment below or reach out to me through email, email@example.com.
The full Python script is available on Github here.