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]
# 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]