1 min read

Snakes and stones

Well, I came across this blog post by ‘PragDave’ where he writes a quick Ruby script that extracts the latest 5 entries from an RSS feed, writes it to a file and FTPs it to a remote box.

Seems simple enough but somehow the script just wasn’t readable to me!!

So, I thought of writing my own quick hack in Python to do a simple comparison and so I wrote the following script in about 8 min. Most of the time was actually simply downloading FeedParser and running sudo python setup.py install :)

Here’s the script:

[python] #!/usr/bin/python import feedparser, ftplib d = feedparser.parse('http://www.swaroopch.info/feed/rss2/') entries = min(len(d.entries), 5) # choose minimum entries tmpfile = file('/tmp/topfive', 'w') for i in range(0, entries): print >> tmpfile, '''                 %s                    %s             ''' % (d.entries[i].link, d.entries[i].title, d.entries[i].description) tmpfile.close() ftp = ftplib.FTP('ftp.swaroopch.info') ftp.login('username', 'password') ftp.cwd('public_html/files/tmp') ftp.storbinary('STOR topfive', file('/tmp/topfive')) ftp.quit() [/python]

You can also see the output file.

This piece of code is a lot more readable, cleaner and faster for me to write. Also, I have never used the ftplib and feedparser modules before, this is my first time ever. It took just a few minutes for me to start using them both and they’re doing all the hard work in this script.

After this comparison, I don’t think there’s an incentive for me to try Ruby, but of course, Ruby has other stuff going for it. However, as Bruce Eckel puts it, "if Ruby pushes the right buttons for you, great. It’s probably the tool that will make you most productive right now, and that’s what you should use. It doesn’t really matter whether I am a fan (yet)."

Sidenote: I tried to post a comment or trackback to PragDave, but I couldn’t find a way. If somebody can find a way to do that, please let me know.