2012-03-27 21:47:46 +02:00
|
|
|
from util import hook, http
|
2012-02-23 11:49:06 +01:00
|
|
|
|
2012-09-03 23:09:57 +02:00
|
|
|
fml_cache = []
|
2012-02-23 11:49:06 +01:00
|
|
|
|
2012-03-27 21:47:46 +02:00
|
|
|
|
|
|
|
def refresh_cache():
|
2012-04-04 01:13:58 +02:00
|
|
|
""" gets a page of random FMLs and puts them into a dictionary """
|
2012-09-05 01:41:32 +02:00
|
|
|
soup = http.get_soup('http://www.fmylife.com/random/')
|
2012-02-23 11:49:06 +01:00
|
|
|
|
2012-09-03 23:09:57 +02:00
|
|
|
for e in soup.find_all('div', {'class': 'post article'}):
|
2014-02-13 03:09:22 +01:00
|
|
|
fml_id = int(e['id'])
|
2012-09-03 23:09:57 +02:00
|
|
|
text = ''.join(e.find('p').find_all(text=True))
|
2014-02-13 03:09:22 +01:00
|
|
|
fml_cache.append((fml_id, text))
|
2012-03-27 21:47:46 +02:00
|
|
|
|
|
|
|
# do an initial refresh of the cache
|
|
|
|
refresh_cache()
|
|
|
|
|
|
|
|
|
|
|
|
@hook.command(autohelp=False)
|
|
|
|
def fml(inp, reply=None):
|
2013-09-04 12:30:04 +02:00
|
|
|
"""fml -- Gets a random quote from fmyfife.com."""
|
2012-03-27 21:47:46 +02:00
|
|
|
|
|
|
|
# grab the last item in the fml cache and remove it
|
2014-02-13 03:09:22 +01:00
|
|
|
fml_id, text = fml_cache.pop()
|
2012-03-27 21:47:46 +02:00
|
|
|
# reply with the fml we grabbed
|
2014-02-13 03:09:22 +01:00
|
|
|
reply('(#{}) {}'.format(fml_id, text))
|
2012-03-27 21:47:46 +02:00
|
|
|
# refresh fml cache if its getting empty
|
|
|
|
if len(fml_cache) < 3:
|
|
|
|
refresh_cache()
|