This Post Has 2,451 Views, 168 Reactions And 26 Comments
Robert
Posted on June 3, 2020
In this post, I'll be teaching you how to update a DEV post title, The Tom Scott way.
YouTuber Tom Scott's latest video titled, "This Video Has X Views", continually updates itself to reflect its own view count.
Basically what he does is run a background job that hits the YouTube API to update the title with the latest view count every few minutes.
Here's another one from Mr. Beast but instead of updating the title of the video, the thumbnail gets updated every few minutes with the amount of money given away.
Getting Started
We will use Firebase Cloud Functions to make a script that will update our DEV post title every x minutes. Also, Node.js and axios to get and update a DEV article.
Tip: To get a post ID, right-click and inspect your dev post page and look for the data-article-id attribute.
Next, open file firebase/index.js and insert the following implementation:
constfunctions=require("firebase-functions");constaxios=require("axios");constarticlesEndpoint="https://dev.to/api/articles";// A recursive function that checks if a post// is present in the current response and returns it.asyncfunctiongetPost(postId,pageNo=1){const{data}=awaitaxios.get(`${articlesEndpoint}/me/all?per_page=10&page=${pageNo}`,{headers:{"api-key":functions.config().dev.api_key,},});constpost=data.find((i)=>i.id==postId);if(!post){returngetPost(postId,pageNo+1);}returnpost;}// A snippet taken from SO that adds commas to a number.// https://stackoverflow.com/a/2901298/4698790functionaddCommas(x){returnx.toString().replace(/\B(?=(\d{3})+(?!\d))/g,",");}// A function that updates a post title// to the current views, reactions and comments// count by its id.asyncfunctionupdatePost(postId){const{page_views_count,public_reactions_count,comments_count,}=awaitgetPost(postId);constdata={title:`This Post Has ${addCommas(page_views_count)} Views, ${addCommas(public_reactions_count)} Reactions And ${addCommas(comments_count)} Comments`,};constres=awaitaxios.put(`${articlesEndpoint}/${postId}`,data,{headers:{"api-key":functions.config().dev.api_key,},});returnres.data;}// A function that uses the scheduler and// pubsub APIs to run a given code every// 3 minutes.exports.updatePostTitle=functions.pubsub.schedule("every 3 minutes").onRun(()=>{constpostId=functions.config().dev.post_id;returnupdatePost(postId);});
The area of focus in the code above is the getPost, updatePost, and the updatePostTitle functions.
The getPost function is a recursive function that gets the user's articles and runs until the given post id is found. We do this because currently, the DEV API endpoint https://dev.to/api/articles/YOUR_POST_ID_HERE doesn't return the page_views_count property that we need to add to the title of the post.
The updatePost function updates a post's title by including the current views, reactions and comments count of the post. The format looks like this.
This Post Has 1,001 Views, 532 Reactions And 69 Comments
Lastly, the updatePostTitle, is a cloud function that uses the Pub/Sub and the Cloud Scheduler APIs to update the post title every 3 minutes.
4. Deployment
To deploy our Cloud Function, simply run:
$ firebase deploy --only functions
Now that your deployment is complete, you can go to your Firebase console and find your function.
Enable billing on your project by switching to the Blaze or Flame plan. See pricing for more details. This is required to be able to do requests to non-Google services.
Configure this sample to use your project using firebase use --add and select your project.
Install dependencies locally by running: cd functions; npm install; cd -