Rewrote fact.py (screw this shitty website, so many failure points)

This commit is contained in:
Luke Rogers 2012-09-05 11:04:01 +12:00
parent 47b7836088
commit 62cf93bd52

View file

@ -1,31 +1,36 @@
import re from util import hook, http, web
from util import hook, http
from bs4 import BeautifulSoup
@hook.command(autohelp=False) @hook.command(autohelp=False)
def fact(inp, say=False, nick=False): def fact(inp, say=False, nick=False):
"fact -- Gets a random fact from OMGFACTS." "fact -- Gets a random fact from OMGFACTS."
fact = None attempts = 0
while fact is None:
while True:
try: try:
fact, link = get_fact() soup = http.get_soup('http://www.omg-facts.com/random')
except: except:
pass if attempts > 2:
return "Could not find a fact!"
else:
attempts += 1
continue
return u"%s [ %s ]" % (fact, link) response = soup.find('a', {'class': 'surprise'})
link = response['href']
#fact = response.contents[0]
fact = ''.join(response.find(text=True))
if fact:
print fact
fact = http.unescape(fact.decode("utf-8")).strip()
break
else:
if attempts > 2:
return "Could not find a fact!"
else:
attempts += 1
continue
def get_fact(): return "%s - %s" % (fact, web.isgd(link))
page = http.get('http://www.omg-facts.com/random')
soup = BeautifulSoup(page)
response = soup.find('a', {'class': 'surprise'})
link = response['href']
fact = response.contents[0]
if fact:
return (fact, link)
else:
raise nofact