Swaroop C H

blog books about contact subscribe

Snakes and stones

17 Jan 2005

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:


#!/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, '''
  <div class="blogentries">
       <div class="blogentry">
         <a href="%s"><span class="blogentrytitle">%s</span></a>
         <div class="blogentrydescription">
           %s
         </div>
       </div>
     </div>''' % (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()

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.

Comments

Premshree Pillai says:

Well, that Ruby code could've been written the way you've done it. But using closures is the Ruby Way To Do Things.

Btw, added your feed to LJ => http://www.livejournal.com/userinfo.bml?user=swaroop_ch

Swaroop C H says:

Hey Premshree,

Ok, I guess, I'm not comfortable with the Ruby way as I am with the Python way. No big deal. Use what you like as long as you can get the job done ;)

And thanks for the LJ feed!!

P.S. Can you publicize that? I don't know whether it is useful otherwise....

Feedback

There's no comment box, but please do email me or tweet me your thoughts and criticisms, and I will publish the relevant ones here.