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

34 lines
714 B
Python
Raw Normal View History

2011-11-30 12:47:45 +01:00
import re
2012-04-02 18:17:55 +02:00
from util import hook
from util import http
from util import misc
2011-11-30 12:47:45 +01:00
from BeautifulSoup import BeautifulSoup
2012-04-02 18:17:55 +02:00
2011-11-30 12:47:45 +01:00
@hook.command(autohelp=False)
def fact(inp, say=False, nick=False):
2012-02-28 03:03:43 +01:00
".fact -- Gets a random fact from OMGFACTS."
2011-11-30 12:47:45 +01:00
2012-02-02 14:05:11 +01:00
fact = None
while fact is None:
try:
fact, link = get_fact()
except:
2012-02-29 09:29:53 +01:00
pass
2011-11-30 12:47:45 +01:00
2012-02-29 09:29:53 +01:00
return u"%s [ %s ]" % (fact, link)
2011-11-30 12:47:45 +01:00
2012-04-02 18:17:55 +02:00
2012-02-02 14:05:11 +01:00
def get_fact():
page = http.get('http://www.omg-facts.com/random')
soup = BeautifulSoup(page)
2012-04-02 18:17:55 +02:00
container = soup.find('a', {'class': 'surprise'})
2011-11-30 12:47:45 +01:00
link = container['href']
fact = misc.strip_html(container.renderContents())
2012-02-02 14:05:11 +01:00
if fact:
return (fact, link)
else:
raise nofact