This is just a silly experiment inspired by this video from Ben Awad where he reverse-engineered the TikTok API to update his profile with live stats of one of his TikTok videos.
I wanted to try this out too and thought it might work on the dev.to platform too. I looked up the API documentation and found the endpoints that I need to use to make this work!
I don't have any bad intentions and don't want to hit the API too often. Some of the API endpoints have rate limits too, therefore I am making sure that I only make requests when it is necessary and every 30 seconds.
Here is the code that I used to make this work:
phiilu / dev.to-reactions-in-article-title
Using the dev.to API to update the article with the count of reactions.
import axios from 'axios';
import dotenv from 'dotenv';
dotenv.config();
const POST_ID = process.env.POST_ID;
const BASE_URL = `https://dev.to/api`;
const client = axios.create({
baseURL: BASE_URL, headers: {
'api-key': process.env.API_KEY
}
})
let currentReactionsCount = 0;
const API = {
article(id = POST_ID) {
return client.get(`/articles/${id}`);
},
updateArticle(id = POST_ID, body) {
return client.put(`/articles/${id}`, body);
}
};
const sleep = async (amount = 30000) => new Promise(resolve => setTimeout(resolve, amount))
while (true) {
try {
const { data } = await API.article();
const { public_reactions_count } = data;
const newTitle = `This post has ${public_reactions_count} reactions! - Using the dev.to API to update the post title with the reactions count!`
if (currentReactionsCount !== public_reactions_count) {
await API.updateArticle(POST_ID, { title: newTitle });
currentReactionsCount = public_reactions_count;
console.log(`Article updated - Reactions: ${currentReactionsCount}`)
} else {
console.log(`Reaction count was the same`)
}
} catch (error) {
console.log(error.message)
} finally {
await sleep();
}
}
You can create a new API key inside your account settings and the API documentation can be found here.
Now if this is working this post should get updated every 30 seconds with the current reaction count!
Edit:
I noticed that the API might not return up-to-date data and therefore takes maybe longer than 30 seconds.
Top comments (0)