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/fmylife.py
Fletcher Boyd 7ab4f756fe .format
2013-09-05 09:46:49 +08:00

29 lines
805 B
Python
Executable file

from util import hook, http
fml_cache = []
def refresh_cache():
""" gets a page of random FMLs and puts them into a dictionary """
soup = http.get_soup('http://www.fmylife.com/random/')
for e in soup.find_all('div', {'class': 'post article'}):
id = int(e['id'])
text = ''.join(e.find('p').find_all(text=True))
fml_cache.append((id, text))
# do an initial refresh of the cache
refresh_cache()
@hook.command(autohelp=False)
def fml(inp, reply=None):
"""fml -- Gets a random quote from fmyfife.com."""
# grab the last item in the fml cache and remove it
id, text = fml_cache.pop()
# reply with the fml we grabbed
reply('(#{}) {}'.format(id, text))
# refresh fml cache if its getting empty
if len(fml_cache) < 3:
refresh_cache()