I wanted to generate RSS 2.0 feeds in Python. Nothing fancy but for certain tasks I needed it something that is quick and just works out of the box. I found rfeed. It is a library to generate RSS 2.0 feeds in Python. It is in my opinion straightforward to use. This post explains how to generate RSS 2.0 feed quickly using Python.
Generate RSS 2.0 feed quickly using Python
Make sure git is installed. Use the apt command or yum command/dnf command to install the git client:
sudo yum install git ## RHEL/CentOS/Oracle Linux ##
sudo dnf install git ## Fedora Linux (RHEL/CentOS 8.x) ##
sudo apt install git ## Ubuntu/Debian Linux ##
Reading package lists... Done Building dependency tree Reading state information... Done Suggested packages: gettext-base git-daemon-run | git-daemon-sysvinit git-doc git-el git-email git-gui gitk gitweb git-arch git-cvs git-mediawiki git-svn The following NEW packages will be installed: git 0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded. Need to get 3176 kB of archives. After this operation, 24.1 MB of additional disk space will be used. Get:1 http://archive.ubuntu.com/ubuntu xenial-updates/main amd64 git amd64 1:2.7.4-0ubuntu1.6 [3176 kB] Fetched 3176 kB in 1s (2861 kB/s) Selecting previously unselected package git. (Reading database ... 17182 files and directories currently installed.) Preparing to unpack .../git_1%3a2.7.4-0ubuntu1.6_amd64.deb ... Unpacking git (1:2.7.4-0ubuntu1.6) ... Setting up git (1:2.7.4-0ubuntu1.6) ...
Next, clone the repo, run:
$ git clone https://github.com/svpino/rfeed.git
Cloning into 'rfeed'... remote: Enumerating objects: 228, done. remote: Total 228 (delta 0), reused 0 (delta 0), pack-reused 228 Receiving objects: 100% (228/228), 45.60 KiB | 0 bytes/s, done. Resolving deltas: 100% (144/144), done. Checking connectivity... done.
The library is a single file rfeed.py, so you could simply copy it wherever you need it. You can also install it using the following command:
$ cd rfeed/
$ python setup.py install
running install running build running build_py creating build creating build/lib.linux-x86_64-2.7 copying rfeed.py -> build/lib.linux-x86_64-2.7 running install_lib copying build/lib.linux-x86_64-2.7/rfeed.py -> /usr/local/lib/python2.7/dist-packages byte-compiling /usr/local/lib/python2.7/dist-packages/rfeed.py to rfeed.pyc running install_egg_info Removing /usr/local/lib/python2.7/dist-packages/rfeed-1.0.0.egg-info Writing /usr/local/lib/python2.7/dist-packages/rfeed-1.0.0.egg-info
Let us create a file named test.py:
$ vi test.py
Append the following text into it:
import datetime from rfeed import * # year, month, date, hh, mm, ss item1 = Item( title = "My 10 UNIX Command Line Mistakes", link = "https://www.cyberciti.biz/tips/my-10-unix-command-line-mistakes.html", description = "Here are a few mistakes that I made while working at UNIX/Linux prompt.", author = "Vivek Gite", guid = Guid("https://www.cyberciti.biz/tips/my-10-unix-command-line-mistakes.html"), pubDate = datetime.datetime(2017, 8, 01, 4, 0)) item2 = Item( title = "Top 25 Nginx Web Server Best Security Practices", link = "https://www.cyberciti.biz/tips/linux-unix-bsd-nginx-webserver-security.html", description = "Best Nginx web server hardening and security practice for Linux/Unix sysadmins and developers.", author = "Vivek Gite", guid = Guid("https://www.cyberciti.biz/tips/linux-unix-bsd-nginx-webserver-security.html"), pubDate = datetime.datetime(2017, 8, 01, 4, 2)) feed = Feed( title = "nixCraft Updated Tutorials/Posts", link = "https://www.cyberciti.biz/atom/updated.xml", description = "nixCraft Linux and Unix Sysadmin Blog - Recently updated posts", language = "en-US", lastBuildDate = datetime.datetime.now(), items = [item1, item2]) print feed.rss()
- The main object of the RSS 2.0 feed is the Feed class.
- The Feed class supports a list of Item instances.
- To specify the guid attribute of an item, you can use a Guid instance.
- Item: Represents an item of a feed’s channel.
- To get the final RSS content, you can use the rss() method of the Feed class.
Just run it:
$ python test.py
$ python test.py > /var/www/nfs/atom/updated.xml
rfeed is a library to generate RSS 2.0 feeds in Python. It is extensible, and in my opinion very easy to use. Besides the standard RSS 2.0 specification, it also includes iTunes support for podcast feeds. For more info see rfeed on github.