Do it your self hobby code

How to list activities of a channel—Python Youtube API

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.


Open a new Python file and save it as 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 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")

Now type the authentication service.

def get_authenticated_service():
  flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
  storage = Storage(CLIENT_OAUTH2_FILE)
  credentials = storage.get()
  if credentials is None or credentials.invalid:
    credentials = run_flow(flow, storage)


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(
  return response

And next is the function where we can change according to what we want to show:

def show_my_subscription_activities(client):
  sChannelID = 'UC16niRr50-MSBwiO3YDb3RA'
  results = check_my_subscription_activities(client,
    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)

Save the python file and run it:


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.

Leave a Comment

Your email address will not be published.