You may want to check whether a Youtube channel has published a new video. This tutorial will teach you how to accomplished it. We will write a simple Python program that make a request for channel activities.
Requirements:
- Youtube Account
- Obtain user authentication (Please check my previous post)
Open a new Python file and save it as list_activities.py on the same folder where your authentication files are saved. These are client_secret.json and client-oauth2.json. This was discussed on my previous post.
Let enter the libraries first
import os, sys, json, httplib2
import google_auth_oauthlib.flow
import googleapiclient.discovery
#import googleapiclient.errors
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.tools import run_flow
from oauth2client.client import flow_from_clientsecrets
Next is the global variables:
CLIENT_SECRETS_FILE = os.path.join(sys.path[0], "client_secret.json")
CLIENT_OAUTH2_FILE = os.path.join(sys.path[0], "client-oauth2.json")
YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
MISSING_CLIENT_SECRETS_MESSAGE = ""
Now type the authentication service.
def get_authenticated_service():
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
scope=YOUTUBE_READ_WRITE_SCOPE,
message=MISSING_CLIENT_SECRETS_MESSAGE)
storage = Storage(CLIENT_OAUTH2_FILE)
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run_flow(flow, storage)
return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
http=credentials.authorize(httplib2.Http()))
Then the query function
def remove_empty_kwargs(**kwargs):
good_kwargs = {}
if kwargs is not None:
for key, value in kwargs.items():
if value:
good_kwargs[key] = value
return good_kwargs
def check_my_subscription_activities(client, **kwargs):
kwargs = remove_empty_kwargs(**kwargs)
response = client.activities().list(
**kwargs
).execute()
return response
And next is the function where we can change according to what we want to show:
def show_my_subscription_activities(client):
#https://www.youtube.com/channel/UC16niRr50-MSBwiO3YDb3RA
sChannelID = 'UC16niRr50-MSBwiO3YDb3RA'
results = check_my_subscription_activities(client,
part='snippet,contentDetails',
channelId = sChannelID)
return results
To make it simple I just used a fix channelid. Channel id can be found on the address of your browser.
Now to see the output, I made a function to save the result as JSON file.
def write_json(results):
filename = os.path.join(sys.path[0], "activities.json")
with open(filename, 'w') as outfile:
json.dump(results, outfile)
To see the output on your terminal then add this function:
def print_video_title(results):
for sActivies in results['items']:
title = sActivies['snippet']['title']
print (title)
Finally, the main function:
if __name__ == "__main__":
client = get_authenticated_service()
results = show_my_subscription_activities(client)
write_json(results)
print_video_title(results)
Save the python file and run it:
python list_activities.py
It should show you the list of video titles on your terminal.
Also you should see a new file created by this program. Check your python folder and you will see a file named: activities.json
To view the data of this json, please use firefox and install an addon called JSON Lite.
On your file explorer, on your folder, right click the json file activities.json and open with Firefox. you should see something like this:
Having this data, it opens up more possibilities. You can do downloads, likes and many more.