Return to Blog Home

YouTube Comment Synchronizer

Source 

YouTube Comment Synchronizer

Sometimes things change, buy why propagate those changes manually?

For most markers I created, making the comment on the relevent YouTube video was the last thing I did, but for a number of entries I had made changes after - fixing typos, rephrasing things, etc - which would require me to both remember to and actually return to said comment and edit it.

YouTube API

I first went for the YouTube API - I was already using Googles APIs to update my Spreadsheet, so I assumed it would be as simple as checking a box somewhere and running the code...

It was not, despite this being my first usage of the API I was receiving The request cannot be completed because you have exceeded your youtube quota messages, which eventually lead me to finding the correct dashboard showing how many how many quotas I had avaialbe per day: 0. This was supposed to be 10,000 units, but for whatever reason my project was down to 0, so my only solution was to delete and create a new project - which involved me also obtaining new credentials for my sheet updating script too.


Finally I was able to use it, easily getting the plain text of a comment by ID, but when I went to use the updating endpoint I was hit with authorization errors. No big deal I thought, I just didn't grant enough scopes, so after adding every YouTube scope possible I was confused, until I found out that I needed authorization from the author of the video to edit my comments on their video.

This was enough to push me over the edge, and I installed Playwright...

Playwright

It wouldn't be the pretty and official way, but I knew with a little time I would be able to update my own comments on any video - even if I had to update this part of the script every time YouTube updated their UI.

First challenge was logging in, which thankfully was as simple as detecting the login button, and a number of clicks, waits, typing of credentials, and more waiting. To be a little efficient I did decide to cache my cookies every time, and to this date have never needed to login, every usage has been able to use those same cookies.

Fortunately YouTube comments are directly linkable via the lc query parameter, meaning the comment would be at the top of the comments feed, so I only needed to scroll down to the comments feed and it'd always be on screen. From there I was able to click the action menu, the Edit button, fill out the <div contenteditable> they are using as a <textarea />, and clicking the submit button.

Caching

Much like my Spreadsheet updating script, I had no interesting in waiting to update comments that didn't need updating, so I implemented content hashing. This allowed the script to generate a hash for each comments text, and if the previous hash didn't match the newly generated hash, that comment would need to be updated.

Since I already went through all the effort of getting the YouTube API working, I decided to use it to validate that the Playwright script was successfully updating the content, so with that hash state was a boolean flag indicating if the comment content had been validated via the API.

Future

You may be wondering why I didn't automate the initial posting of the comment at this point? Simply put I don't trust my script enough to do that, and would rather manually post the initial comment and save the ID.