Generating RSS from a directory every time it changes

Translation of the original in Catalan. Read it in: CatalàCastellano

For internal matters of the eBosc network, I was interested in generating an RSS file every time a directory changed its content. Next I explain how I solved the matter.

For internal matters of the eBosc network, I was interested in generating an RSS file every time a directory changed its content. Next I explain how I solved the matter.

So I installed dnotify which allowed me to execute a command every time a directory was modified and I could leave it running as a daemon (-b parameter from version 0.18 onwards).

dnotify -b -C /directory -e /path/generarRSS.sh

If it detects that a new file has been created in the directory, the indicated command is launched, in this case it is a small shell script that I have made myself, with the following content:

#!/bin/shls --color=no --sort=time -1 /path/ | /root/bin/rsspipe.py --limit 20 --url "http://localhost/" --title="Llistat del directori" --link="http://localhost/" --description="Descripció del contingut del directori" /path/fitxer.xml

As you can see the only thing I do is a listing sorted by time and then I send the output of the directory listing to the RSSPipe program. In reality this is a version that I have modified myself, since the original version did not do everything I wanted. Even though I have no idea about python without looking at any manual I have been able to modify the code, since it was easy to deduce what had to be touched for it to do what I wanted.

Code of my version of the RSSPipe:

#! /usr/bin/python## rsspipe.py v0#   <http://rentzsch.com/code/rsspipe>#   Copyright (c) 2004 Jonathan 'Wolf' Rentzsch.#   Some rights reserved: <http://creativecommons.org/licenses/by/2.0/>"""Reads lines from stdin, outputting an RSS 0.92 file containing the last N lines as items.usage example:        tail -f /var/log/httpd/access_log|python rsspipe.py --title 'http traffic' httpd_access.xml"""	import sys, cgifrom optparse import OptionParser	parser = OptionParser()parser.add_option("-t", "--title", dest="title", help="feed title", default="rsspipe")parser.add_option("-l", "--link", dest="link", help="feed link", default="http://rentzsch.com/code/rsspipe")parser.add_option("-d", "--description", dest="description", help="feed description", default="rsspipe")parser.add_option("-i", "--limit", dest="limit", help="number of lines to export as RSS items", default=100)parser.add_option("-u", "--url", dest="url", help="la url on es pot descarregar el fitxer", default="ftp://localhost/")(options, args) = parser.parse_args()	outputFile = open(args[0],'w')lineQueue = []i=0;while i<int(options.limit):        readline = sys.stdin.readline()        if readline != '':                i=i+1;                if len(lineQueue) >= int(options.limit):                        del lineQueue[0]                lineQueue.append(readline.rstrip())	                outputString = '<?xml version="1.0"?>\n<rss version="0.92">\n<channel>\n\t<title>' + options.title + '</title>\n\t<link>' + options.link + '</link>\n\t<description>' + options.description + '</description>\n';                for theLine in lineQueue:                        encodedLine = cgi.escape(theLine)                        outputString = outputString + '<item>\n\t<title>' + encodedLine + '</title>\n\t<link>' + options.url + encodedLine + '</link>\n\t<description>' + encodedLine + '</description>\n</item>\n';                outputString = outputString + '\n</channel>\n</rss>'                outputFile.seek(0)                outputFile.truncate(0)                outputFile.write(outputString)                outputFile.flush()	

The changes I have made to the RSSpipe are the following:

  • I have added a new parameter (-url) that serves to indicate the path of the file. I need it to reference how to download the file for example via HTTP or FTP.
  • Instead of making the program run infinitely I have told it that it only runs to generate the lines it has to, not infinitely, as was originally planned.
  • Inside each item the link tag now references the URL of the -url parameter plus the item in question, so the link is already usable.
  • I have cleaned the output of the RSS file which was very ugly. Everything was on a single line, I have put the line breaks and indented it.