Junk Food for the Brain

Open Source and Awesomesauce :)

F*** My Life via Bash the Pythonic Way

| Comments

Being a little bored today, I decided to surf around. Thanks to my friend Thaweesak, I got this site called F*** My Life.

A few hilarious stories and minutes later, I realised they had a web api. Thats when I got an epiphany!!! Why not get a random story from here to cheer me up on demand? And for a linux junkie, what better way than through a terminal?

Since I have been experimenting with python for awhile, what better way to practice a new language than something fun?

Here’s my script:-

#!/usr/bin/env python
# Ugly script to get a random quote from www.fmylife.com
# Yeah, so ugly I dont even bother catching exceptions
#
# Meant to run on python 2.5++ as I'm using the inbuilt ElementTree package
#  
import urllib
import xml.etree.ElementTree as ET
import cStringIO  
FML_SITE_RANDOM = 'http://api.betacie.com/view/random'
paramsDict = { 'language':'en', 'key':'readonly'}
encodedParams = urllib.urlencode(paramsDict)  
try:
    fmldata = urllib.urlopen(FML_SITE_RANDOM, encodedParams).read()
    fmldata = ET.XML(fmldata)
    author = fmldata.findtext('items/item/author').strip()
    if len(author) == 0:
        author = 'anonymous'

    peopleAgreed = fmldata.findtext('items/item/agree').strip()
    peopleDisagreed = fmldata.findtext('items/item/deserved').strip()
    story = fmldata.findtext('items/item/text').strip()

    print "Think your life sucks?\n"
    print 'This happened to ' + author + '.'
    print ''
    print story + '\n'
    print peopleAgreed + ' reader(s) agreed'
    print peopleDisagreed + ' reader(s) have seen worse. '
    print
    print 'F*** My Life'
    print 'http://www.fmylife.com'  

except:
    msg = '''Whoops!!! Some error, probably your connection. I guess
    your life sucks after all.... :('''
    print msg

Just chmod 755 it and place it in your ~/bin directory. Then just type in fml in your terminal for some random F*** ups.

Assuming you have Fedora 10 or Python 2.5++ installed, it should work perfectly.

Here’s a shot of it running:-

[caption id=”attachment_62” align=”aligncenter” width=”300” caption=”My F*** My Life script in action.”]My F*** My Life script in
action.[/caption]

Sources I used to create this program:-

The wonders of Open Source Technology. w00t!!! :)