The title of this blog won’t be exactly right. It only updates every few minutes so don’t bother refreshing, refreshing, and refreshing. You won’t actually be able to see it ticking up second-by-second.
If it’s actually 100% spot on it would be a miracle, but if it’s close then the code I’ve written is still working. But at some point, that code will break and the title of this blog will slip more and more out of touch with reality.
The man in the picture above is Tom Scott and that image is from his wildly successful YouTube video: This Video Has X Views
Now that’s not the actual name of the video. In fact, it’s nearly impossible to give you the exact name because the name of the video changes every few minutes.
You see Tom created a piece of code that changes the title every couple of minutes based on how many views the video had at that current time.
I loved the idea of this video and I decided to recreate it on a different platform.
So let me give you the story of how I recreated his video on Medium, and how all of this is even possible.
That is currently how many views Tom Scott’s video has as I’m writing this. This means the title of his video should be:
This Video Has 27,099,286 Views.
And you know what… It’s exactly right! Just look at the screenshot I took below.
Now if you’re skeptical and think Tom is just manually changing the title, just look at the date the video was posted. This video was posted 10 months ago. Nobody in their right mind would continually update the title of one video for 10 months.
So how is he doing it?
In his own words…
“ There are two main approaches you can take to accomplish this”
In his video, he took one of the approaches and I decided to take the other.
Let’s start with his approach.
API stands for Application Programming Interface, and nearly every major web service has one including Medium. It’s a way for bits of code to pass data and instructions back and forth between services safely.
What Tom did to change his video title was use YouTube’s API. He simply created a piece of code that sends a single request to YouTube with the video ID, the stuff to change, and the credentials to prove he’s allowed to change his own video.
YouTube in return sends back a single answer in the form of a number. If the number returned is a response code of 200 it means that everything worked and the title was successfully changed.
This approach if you know what you’re doing really isn’t complicated. In fact, in his video, he briefly displays the code he used and it’s only about 50 lines.
This again is just one of the two approaches he mentions. Now let me show you the second approach, the one I used to change the title of this blog.
2. Web Scraping
If you follow me and what I write first of all thank you, and second you have probably heard me mention web scraping before.
I have done many projects with web scraping, and wrote how using it can automate tasks in your everyday life.
But if you have never heard of it before let me explain it in simple terms. Web scraping is basically a piece of code that impersonates a human. Everything that someone can do on their computer or laptop, we can write a piece of code to do it as well.
This includes clicking anywhere on a page, inputting text into a search box, scrolling, etc. This can all be done with just a few lines of code, and this was Tom’s second approach to changing the title on his video, and what I did.
This method does have advantages and disadvantages when compared against the API method.
The disadvantage to using web scraping is that the code can easily break. I’ll go more into the details of my code in a bit, but what you have to know is that it doesn’t react well to changes.
If Medium were to change the format of their website or just how it looks, more than likely my code would break and it would stop updating the title. But as long as Medium doesn’t go through that change my code should keep updating the title every few minutes.
The advantage of using web scraping over an API, in this case, is that an API has limitations. You can only do what the web service’s API will allow you to do. Web scraping on the other hand has no limitations and can do whatever a human can do online.
For example, using web scraping I can write a piece of code that not only changes the title but the body of text you’re reading right now, and that’s exactly what I’m going to do.
Take a look at the line below. This line will display the username of the last person who gave a clap for this blog.
Right now I have no idea what the name is but if you want to see your name there, just give a clap on this blog below and hope nobody else does the same after you, before the blog updates.
The last person who clapped was Trevor Ram
What Does My Code Actually Do
Now is the time where I’m going to give you the 7 exact steps my code goes through to change the title of this blog.
If you want to see my actual full code, keep reading till the end where I tell you exactly where you can find it.
- My code will first open up a browser and go to the sign-in page on Medium.
- I sign into my account using Facebook, so my code will input my account name and password and press the login button to log in to Medium.
- This brings my code to the home page on Medium where it will press on my account settings and select my stats.
- The stats section is where you can see all the stories you published and their specific stats. My code will get a list of all my stories and select the one with “This Blog” in the title and will store the number of views the blog has.
- This now brings the code to this exact page, where it will first scroll down and store the number of claps the blog has and the last person who clapped for it.
- It will then find and click the edit story button and change the title to whatever the number of views and claps I have stored.
- Lastly, my code will press the save and publish button.
I really hope that when you clicked on this blog and read it, the code was not broken and the stats in the title are decently accurate.
I know you can not actually see the views that this article has only the number of claps, but I ensure you if the number of claps is close to being accurate so are the views.
I want to give a huge shout out to Tom Scott without him and his video I honestly would have never thought to do this. I’ve been referencing his video throughout the entire blog so I think it’s time where if you want to see it for yourselves I’ll leave a link to it below.
Access The Code Used To Create This Article
If you want access to a fully cleaned and commented version of the code used to update this blog you can join my group “Clean Code Z” whereby joining you will gain access to nice clean versions of the code I use for all my articles and videos.
Feel feel to join here: Clean Code Z