Node.js read and write post status to a JSON file

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Your welcome, these silly scripts really come in handy and I use them for a lot of things :D
Chris Bongers , Oh yes! absoloutey..
Your welcome, hope it comes to use one day
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

A while ago, I started building my RSS reader to auto-publish to certain platforms, but we never finished it π.
Today we will be looking into looping over the articles we get via the RSS reader and keeping track of which ones are posted to the socials.
Pre-condition: You need to know how to set up a basic node app π
What you'll learn from this article
Our JSON file is going to be quite easy in structure and will look as follows:
{
"https://daily-dev-tips.com/posts/rss-reader-in-node-js": {
"published": true
},
"https://daily-dev-tips.com/posts/top-10-chrome-extensions-for-developers-π": {
"published": true
}
}
We basically only need to know if an object is already on this list.
First, we need to add the rss-parser package.
npm i rss-parser
Then we can loop through our articles using the sitemap we have.
let Parser = require('rss-parser');
let parser = new Parser();
(async () => {
let feed = await parser.parseURL('https://daily-dev-tips.com/sitemap.xml');
feed.items.forEach(item => {
console.log(item.id);
});
})();
Now we need to make sure we read our JSON file and see if we have already published this article.
First, let's define the file-system.
const fs = require('fs');
Then we can read out actual JSON file
let rawdata = fs.readFileSync('site.json');
let siteData = JSON.parse(rawdata);
This will at first we an empty object {}.
In our loop we need to check if we already published this item. If yes => Don't do anything If no => Do magic and then add to JSON file.β
feed.items.forEach(item => {
let url = item.id;
if (!siteData.url) {
// Do magic posting stuff
siteData[url] = {
'published': true
};
}
});
Once the loop is done, we can save our JSON to the actual file.
fs.writeFileSync('site.json', JSON.stringify(siteData));
Our JSON file will then look something like this.
{
"https://daily-dev-tips.com/posts/vanilla-javascript-canvas-images-to-black-and-white/": {
"published": true
},
"https://daily-dev-tips.com/posts/vanilla-javascript-images-in-canvas/": {
"published": true
},
"https://daily-dev-tips.com/posts/vanilla-javascript-colouring-our-canvas-elements-π/": {
"published": true
}
}
Awesome, we now parsed our RSS feed, read our JSON file, and wrote data to it if not already in there!
You can find this project on GitHub.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter