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

38 lines
924 B
Python
Raw Normal View History

from util import hook, http, web
2011-11-30 12:47:45 +01:00
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):
2013-09-04 12:30:04 +02:00
"""fact -- Gets a random fact from OMGFACTS."""
2011-11-30 12:47:45 +01:00
attempts = 0
2012-09-05 11:35:44 +02:00
# all of this is because omgfacts is fail
while True:
2012-02-02 14:05:11 +01:00
try:
soup = http.get_soup('http://www.omg-facts.com/random')
2012-02-02 14:05:11 +01:00
except:
if attempts > 2:
return "Could not find a fact!"
else:
attempts += 1
continue
response = soup.find('a', {'class': 'surprise'})
link = response['href']
fact = ''.join(response.find(text=True))
if fact:
2012-09-06 04:08:59 +02:00
fact = fact.strip()
break
else:
if attempts > 2:
return "Could not find a fact!"
else:
attempts += 1
continue
2013-08-02 02:30:16 +02:00
url = web.try_isgd(link)
2013-09-05 03:46:49 +02:00
return "{} - {}".format(fact, url)