This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/fact.py
2012-04-02 09:17:55 -07:00

34 lines
714 B
Python
Executable file

import re
from util import hook
from util import http
from util import misc
from BeautifulSoup import BeautifulSoup
@hook.command(autohelp=False)
def fact(inp, say=False, nick=False):
".fact -- Gets a random fact from OMGFACTS."
fact = None
while fact is None:
try:
fact, link = get_fact()
except:
pass
return u"%s [ %s ]" % (fact, link)
def get_fact():
page = http.get('http://www.omg-facts.com/random')
soup = BeautifulSoup(page)
container = soup.find('a', {'class': 'surprise'})
link = container['href']
fact = misc.strip_html(container.renderContents())
if fact:
return (fact, link)
else:
raise nofact