PDA

View Full Version : Get your horoscope from The Onion


kmj
07-10-2002, 06:53 PM
here's a messy script that I just wrote up; soon to be converted to a moobot module:


# onionhr.py - get the current horoscope for a given sign from theonion.com
# Keith Jones
#

import httplib
import re
import sys
import string

if len(sys.argv) != 2:
print 'usage: python onionhr sign'
sys.exit()

param = sys.argv[1]

# first, go to the onion mainpage, so we can find the volume and issue number,
# so we know where to find the current horoscope
#
conn = httplib.HTTPConnection('www.theonion.com')
conn.request('GET', "")

response = conn.getresponse()
if response.status != 200:
print "bad response from the onion:", response.status
sys.exit()

# find the volume and issue
data = response.read()
match = re.search('http://www.theonion.com/onion(\d{4})/index.html', data)
volume = match.group(1)

# Now make the connection with theonion's horoscope page
conn = httplib.HTTPConnection('www.theonion.com')
conn.request('GET', '/onion' + volume + '/horoscopes_' + volume + '.html')
response = conn.getresponse()
if response.status != 200:
print "bad response getting horoscopes:", response.status
sys.exit()

#parse out the info for the given sign
data = response.read()
data = re.sub('<.*?>', '', data)

match = re.search(param, data, re.IGNORECASE)
if match is None:
print "sorry, couldn't find", param
sys.exit()

pos = string.find(data, '\n', match.start())
if pos == -1:
print "aw, crap!"
sys.exit()

pos2 = string.find(data, '\n', pos+1)
if pos2 == -1:
print "aw, crap!(2)"
sys.exit()

print data[pos:pos2]

inkedmn
07-10-2002, 07:08 PM
heh, sweet :)

gufmn
07-10-2002, 07:48 PM
Ummm.......this is all I get:

"usage: python onionhr sign"

from the first if statement.

/me scratches head.

kmj
07-10-2002, 07:55 PM
run python onionhr.py leo

gufmn
07-10-2002, 08:05 PM
ah....very nice :)

kmj
07-10-2002, 09:04 PM
:D thanks. I hate how the scripts depend on whether you run it with 'python...' or just the script name.. Irritates me. Perhaps I should write a wrapper module that lets you get the args with a single consistent interface.

gufmn
07-10-2002, 09:37 PM
Well if you plan on adding it to moobot, it's fine. However, running from a command line would be easier with some raw_input interface stuff.