This repository has been archived on 2023-04-13. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
CloudBot/disabled_stuff/fact.py

38 lines
926 B
Python
Raw Permalink Normal View History

from util import hook, http, web
2011-12-01 00:47:45 +13:00
2012-04-02 09:17:55 -07:00
2011-12-01 00:47:45 +13:00
@hook.command(autohelp=False)
2014-02-14 00:47:01 +13:00
def fact(inp):
2013-09-04 18:30:04 +08:00
"""fact -- Gets a random fact from OMGFACTS."""
2011-12-01 00:47:45 +13:00
attempts = 0
2012-09-05 21:35:44 +12:00
# all of this is because omgfacts is fail
while True:
2012-02-03 02:05:11 +13:00
try:
soup = http.get_soup('http://www.omg-facts.com/random')
2012-02-03 02:05:11 +13:00
except:
if attempts > 2:
return "Could not find a fact!"
else:
attempts += 1
continue
response = soup.find('a', {'class': 'surprise'})
link = response['href']
2014-02-14 00:47:01 +13:00
fact_data = ''.join(response.find(text=True))
2014-02-14 00:47:01 +13:00
if fact_data:
fact_data = fact_data.strip()
break
else:
if attempts > 2:
return "Could not find a fact!"
else:
attempts += 1
continue
2013-08-02 12:30:16 +12:00
url = web.try_isgd(link)
2014-02-14 00:47:01 +13:00
return "{} - {}".format(fact_data, url)