Jan 4th, 2023
If you run a blog or website, it's important to keep your readers informed of new content by providing an RSS feed. But manually updating the feed can be a tedious process. In this post, I'll show you how to automate the process using a combination of shell and Python scripts.
Setting up the RSS Feed
First, I create the RSS feed itself. I based my feed on the formatting described in this tutorial: How to Create an RSS Feed.
I then created an rss.xml
file in my blogposts directory and formatted it according to the tutorial.
Then, I got to work on automating the input of blog post details.
Automating the Process
I started by writing a shell script that calls a Python script and passes the first argument (the name of an HTML file for a blog post) to the script:
#!/bin/bash python3 /Users/SEBARRETT/Code/updateRss.py "$1"
Next, I wrote a Python script that does the following:
- Takes the name of an HTML file for a blog post as an argument
- Uses Beautiful Soup to parse the HTML file and extract the
div
element with the class "blog" - Returns the title and contents of the blog post
- Inserts the title and contents into the
rss.xml
file as a newitem
element, below theatom
element and above all the otheritem
elements
The complete Python script can be found here:
To parse the HTML document, the script uses Beautiful Soup to extract the div
element with the class "blog" and its contents.
I had experience using BS4 from my previous work on my Vault research project, so this was a breeze.
The extracted title and contents are then inserted into the rss.xml
file as a new item
element, below the atom
element and above all the other item
elements.
The item
element includes the title of the post, a link to the post, the publication date using the datetime
library, and the main contents of the post.
After testing the script, it was successfully able to update the rss.xml
file with the new blog post information.
To make the shell script executable, the command chmod +x updateRSS.sh
was used.
An alias for the script was also added to the zsh
configuration file.
In the future, I plan to have the script automatically detect when a new blog post has been added using git add
and grep
, and then run the script automatically to update the RSS feed.
If you'd like to follow our RSS feed, the URL is sethbarrett.xyz/blogposts/rss.xml
.