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 – a library to generate RSS 2.0 feeds in Python. It is in my opinion straightforward to use.
Installation
First clone the repo, run:
$ git clone https://github.com/svpino/rfeed.git
Sample outputs:
Cloning into 'rfeed'... remote: Counting objects: 213, done. remote: Total 213 (delta 0), reused 0 (delta 0), pack-reused 213 Receiving objects: 100% (213/213), 40.81 KiB | 0 bytes/s, done. Resolving deltas: 100% (136/136), 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
Sample outputs:
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
Example
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() |
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()
Where,
- 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
OR
$ python test.py > /var/www/nfs/atom/updated.xml
For more info see rfeed on github.