This YouTube Video Has n Views – How the Video Title Updates Itself

If I somehow managed to pick a YouTube video with the most exact and generally modern title, this video by Tom Scott will presumably take the best position. The title of the video says “This video has n perspectives’ and this title refreshes naturally as the quantity of perspectives change over the long run.

Little miracle, the universe of YouTube is entranced with this “enchantment” title and the video has gathered more than 20 million perspectives up until now.

In the event that you are left considering how this is done, here’s the mystery ingredient – the YouTube API. We make a foundation cron work that runs, state, like clockwork and gets the current number of perspectives for the predetermined video. In the event that the quantity of perspectives has expanded since the last run, we update the video title with, you took care of business, the YouTube API.

Make your own “This Video has n Views” title

Like to construct something comparable for a video on your own YouTube channel? Indeed, there’s consistently Google Apps Script to the salvage.

Make a duplicate of this Google Script in your Google Drive.

Go to the Run menu inside the Apps Script manager, pick Run and select updateYouTubeVideo. Permit the content to deal with your YouTube account and that is it.

The content will run like clockwork and update the title of your YouTube video auto-mysteriously. Basic!

const updateYouTubeVideo = (e = null) => {
  const id = '<<VIDEO ID>>';
  const template = 'This video has VIEWCOUNT views and COMMENTCOUNT comments';

  // The cron job is created only when the script is run manually
  if (e === null) {
    const triggerName = 'updateYouTubeVideo';
    const triggers = ScriptApp.getProjectTriggers().filter((trigger) => {
      return trigger.getHandlerFunction() === triggerName;
    });

    // If time based trigger doesn't exist, create one that runs every 5 minutes
    if (triggers.length === 0) {
      ScriptApp.newTrigger(triggerName).timeBased().everyMinutes(5).create();
    }
  }

  // Get the watch statistics of the video
  const { items: [video = {}] = [] } = YouTube.Videos.list(
    'snippet,statistics',
    { id }
  );

  // Parse the YouTube API response to get views and comment count
  const {
    snippet: { title: oldTitle, categoryId } = {},
    statistics: { viewCount, commentCount } = {},
  } = video;

  if (viewCount && commentCount) {
    const newTitle = template
      .replace('VIEWCOUNT', viewCount)
      .replace('COMMENTCOUNT', commentCount);

    // If the video title has not changed, skip this step
    if (oldTitle !== newTitle) {
      YouTube.Videos.update(
        { id, snippet: { title: newTitle, categoryId } },
        'snippet'
      );
    }
  }
};

How to Stop the YouTube Google Script

Go to script.google.com and search for the YouTube script in the My Projects dashboard. Go to the triggers menu and remove the trigger. The script will stop updating the video title in the background.

Leave a Reply

Your email address will not be published. Required fields are marked *