Displaying the most recent comment on the video's thumbnail automatically.

In this post, I'm going to teach you how to make your YouTube video's thumbnail update with its comments, that is, display a recent comment on the thumbnail and update the thumbnail frequently.


Before starting, let me make it clear that it's no magic, it's just basic programming. You can use any language you wish but I'll be using Python.







Create a Google Cloud Project


We're going to use YouTube Data API v3 for this script. So you need to create an account on Google Cloud Platform, create a project (call it whatever you like) and enable YouTube Data API v3 from the API & Services dashboard.


Now that you enabled the required API, you need to create an API Key and an OAuth Client ID.


..Why?


An API key is mandatory when you are making requests to the YouTube Data API, it allows Google to track who is making requests to their APIs.


OAuth is an advanced authorization protocol. It allows users to see/choose what permissions they are giving to a particular app. In our case, since our app needs to update the thumbnail of a video, we will need to authorize our app from a valid end-point (i.e., from the channel on which the video is uploaded or a manager account of this channel).

For example, consider TubeBuddy. When you are logging in to TubeBuddy you are presented with this screen which displays what permissions this app is requesting from you, this is nothing but OAuth authorization.







Creating an API key


In your Google Cloud Platform dashboard,

  • Go to API & Services -> Credentials

  • Click 'Create credentials' on the top and select 'API key' from the drop-down

An API key will be generated.



Creating OAuth Client ID

To create an OAuth Client ID for your app, you first need to configure an OAuth consent screen.

  • Go to API & Services -> OAuth Consent Screen

  • Choose External and click create


  • Submit the necessary details asked on the next page. It's basically your App name, developer contact email, etc.


  • In the Scopes menu, add the scope 'https://www.googleapis.com/auth/youtube.force-ssl'


  • In the Test Users menu, add your email ID (channel owner or manager)



That's it, you configured the OAuth Consent Screen for your app.


Now, in order to create an OAuth Client ID,

  • Go to APIs & Services -> Credentials

  • Click Create Credentials on the top, and select OAuth Client ID from the drop down



  • Select the Application type as 'Web application', and give your app a name.



  • You've now created your OAuth Client ID, download your ID and secret in JSON.


Alright, now you have an app that is waiting for your channel's authorization so that it will be able to update your thumbnails on your behalf.

To continue with the authorization process,

pip install -r requirements.txt
  • Place the downloaded client secret json file in the same folder as main.py and rename the file to client_secret.json

  • Run the server

python main.py

Now open your browser and go to http://127.0.0.1:8080


This will bring up a page like this,


This web app basically helps you to generate credentials for your app to use. These credentials are not your username and password, they are your OAuth Credentials that can be made use by Google Apps to manage your account on your behalf. In this case, we want to give authorization to our app so that it can update thumbnails on a video(s).





Click 'Test the auth flow directly' on the web page.


This will create a credentials.txt file in your current working directory. These are the credentials that the app needs.


Purrrfect, now we have everything that we need to start our script.


Clone this GitHub repo


This is the script that can retrieve a recent comment from your video and then change the thumbnail such that it displays this recent comment.


  • Install the required modules

pip install -r requirements.txt
  • Place the credentials.txt and client_secret.json files in the same folder as bot.py

  • Open bot.py and change the VIDEO_ID variable to your video ID.

VIDEO_ID = '' #Put your video id here
  • Change the DEVELOPER_KEY variable to your API KEY

DEVELOPER_KEY = '' #Put your API key here


That's it, now just run the script.

python bot.py

This will start the script which will automatically retrieve a recent comment, write it on the template image, and set this image as the thumbnail. It does so every 8 minutes.





This 8 minute is a calculated number considering the maximum allowed quota for YouTube Data API is 10,000 units per day and the cost for updating the thumbnail every time is 50 units. The idea is that the bot can update the thumbnail 180 times a day (by using 180*50 = 9,000 units of quota) at a rate of 1 update every 8 minutes.


Now, just run this script on the cloud (like Google Cloud, Heroku, Azure, whatever you like!). I'm personally running it on google cloud for now. Costs around 600/month for the VM instance but I'm sure there are some free alternatives (like pythonanywhere but I don't know if this script works there, gotta try).



© 2019 Tech Raj. Designed by Teja Swaroop

  • YouTube
  • Facebook Page
  • Twitter