Case study #2: Automate YouTube subscriptions

Context

Sharing individual YouTube videos with students is easy as they can just be added to a playlist, accessed via a single link. Sharing a list of recommended subject-specific channels to subscribe to is harder. I use a Google Sheet to share a curated list and attach a script that automates subscribing to all at once in a single click.

1. Downloading the subscriptions from my YouTube account

Google Takeout allows you to download any of your Google account data as a .csv file, including your YouTube subscriptions. There is a warning that this can take “a long time”, but it won’t if this is the only data you are downloading (mine took an hour).

2. Sharing them via a simple Google Sheet

The downloaded list is copied into a simple 1-column sheet (named “Channels”) that is shared (“View only”) with students. The Takeout download does actually include columns of the channel name and direct URL that could be included as well so that students could work through the list manually, one at a time.

A (“container-bound”) script is then added by clicking on Extensions > Apps Script in the Sheets menus.

3. Adding the script

Here’s the script. The first function adds a custom menu so that students can more easily access the main function. As the function writes back to the sheet to confirm subscription to each channel, the student must “make a copy” of the sheet first.

function onOpen(e) {
  SpreadsheetApp.getUi()
    .createMenu("YouTube")
    .addItem("Subscribe to listed channels", "subscribeToChannels")
    .addToUi();
}

function subscribeToChannels() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Channels");
  let channelIds = sheet.getDataRange().getValues().map(row => row[0]).flat();
  let output = [];
  for (channelId of channelIds) {
    const resource = {
      snippet: {
        resourceId: {
          kind: 'youtube#channel',
          channelId: channelId
        }
      }
    };
    try {
      YouTube.Subscriptions.insert(resource, 'snippet');
      output.push([channelId, 'Subscribed']);
    } catch (err) {
      if (err.message.includes('already exists')) {
        output.push([channelId, 'Already subscribed']);
      } else if (err.message.includes('cannot be found')) {
        output.push([channelId, 'Channel does not exist']);
      } else {
        output.push([channelId, 'Subscription failed: ' + err.message]);
      }
    }
  }
  sheet.getDataRange.clearContents();
  sheet.getRange(1, 1, output.length, 2).setValues(output);
}

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x