junk food for the brain …
Posts tagged Python
F*** my life via bash the pythonic way
Mar 1st
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:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#!/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:-
My F*** My Life script in action.
Sources I used to create this program:-
- F*** My Life Web Api
- Python v2.6.1 documentation
- YDN Parse XML using Python
- YDN Make Yahoo! Web Service REST calls with Python
The wonders of Open Source Technology. w00t!!!