This week I want to focus on helping myself be more effective and automated. So we are going to build an RSS reader that functions as an automated social media publishing tool.
This tool will be built in node.js
and will have the following sequence for now:
- Get all posts from a website RSS feed
- Loop through the posts and see if they are already published (we keep track of this in a json file)
- If not, we get the content of the post and publish on Facebook
- We also post this on Twitter
Today we will start this process and see how we can parse a websites RSS feed
.
RSS reader app in node.js.
Note: Ensure you have
node.js
installed on your machine. See nodejs website for the installation procedure.
To create a basic app in node.js
we run the following command in our terminal:
mkdir rss-app && cd rss-app && npm init
We are doing three things here:
mkdir rss-app
this created a directory called rss-appcd rss-app
this command tells us tochange directory
into a folder called rss-appnpm init
this command comes withnode.js
and creates a blank template. It will ask a couple of questions that you can enter through.
Read more: Basic Node.js Express application
Creating our node.js entry point
We start by creating the basic entry point for our app. Every node.js
project needs an entry point.
touch index.js
Touch means we create a new file, and it’s called index.js
Next, we install our rss parser
npm i rss-parser
This tells node to install a package called rss-parser
. You can find the package here: rss-parser
Read the XML feed
The RSS feed for my website can be found on the following URL: https://daily-dev-tips.com/sitemap.xml
Open your index.js
file in your favorite editor and place the following code in it.
let Parser = require('rss-parser');
let parser = new Parser();
(async () => {
let feed = await parser.parseURL('https://daily-dev-tips.com/sitemap.xml');
console.log(feed.title);
feed.items.forEach((item) => {
console.log(item.title);
});
})();
Let’s go through this code:
let Parser = require('rss-parser');
let parser = new Parser();
The first line tells node.js
we will use the rss-parser package.
The second line defines a new parser.
(async () => {})();
This block is a shorthand for an asynchronous function. Which we need because we are going to use the await
function.
let feed = await parser.parseURL('https://daily-dev-tips.com/sitemap.xml');
console.log(feed.title);
Here we tell the rss-parser package to parse a new XML feed from my website’s feed URL. Next, we console log the feed’s title.
feed.items.forEach((item) => {
console.log(item.title);
});
This is a JavaScript loop that will loop through the feed’s items.
It will console log each item’s title.
Run the RSS app
To test and run our app, go back to the terminal and run the following command:
node index.js
This tells node to run the file called index.js
It will output this:
Daily Dev Tips
String replace in Vanilla JS
Weekend tip: Watch the Vue documentary
Promise chains in JavaScript
Match all urls from a string in vanilla JS
Conclusion
You can find the GitHub demo project here: RSS-App.
We can now read our RSS feed and parse its data in node.js
tomorrow we will start doing something with this data.
In the meantime feel free to send me a message on Facebook or Twitter