2012-02-23 14:41:13 +01:00
|
|
|
# Plugin by Lukeroge
|
|
|
|
|
2012-03-27 21:47:46 +02:00
|
|
|
from util import hook, http
|
2012-02-23 11:49:06 +01:00
|
|
|
from BeautifulSoup import BeautifulSoup
|
2012-03-27 21:47:46 +02:00
|
|
|
from collections import defaultdict
|
2012-02-23 11:49:06 +01:00
|
|
|
|
2012-03-27 21:47:46 +02:00
|
|
|
fml_cache = defaultdict()
|
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-04-23 01:58:19 +02:00
|
|
|
page = http.get('http://www.fmylife.com/random/')
|
2012-02-23 11:49:06 +01:00
|
|
|
soup = BeautifulSoup(page)
|
|
|
|
|
2012-03-27 21:47:46 +02:00
|
|
|
for e in soup.findAll('div', {'class': 'post article'}):
|
|
|
|
id = int(e['id'])
|
2012-04-23 01:58:19 +02:00
|
|
|
# get the text of the FML
|
2012-03-27 22:18:25 +02:00
|
|
|
text = ''.join(e.find('p').findAll(text=True))
|
2012-03-30 01:58:47 +02:00
|
|
|
text = http.unescape(text)
|
2012-04-23 01:58:19 +02:00
|
|
|
# append to the dictionary
|
2012-03-27 21:47:46 +02:00
|
|
|
fml_cache[id] = text
|
|
|
|
|
|
|
|
# do an initial refresh of the cache
|
|
|
|
refresh_cache()
|
|
|
|
|
|
|
|
|
|
|
|
@hook.command(autohelp=False)
|
|
|
|
def fml(inp, reply=None):
|
2012-05-16 05:07:27 +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
|
|
|
|
id, text = fml_cache.popitem()
|
|
|
|
# reply with the fml we grabbed
|
|
|
|
reply('(#%d) %s' % (id, text))
|
|
|
|
# refresh fml cache if its getting empty
|
|
|
|
if len(fml_cache) < 3:
|
|
|
|
refresh_cache()
|