Random tweaks

This commit is contained in:
lukeroge 2012-04-23 11:58:19 +12:00
parent 2e597038ea
commit 1ca04b6b3e

View file

@ -1,24 +1,23 @@
# Plugin by Lukeroge # Plugin by Lukeroge
from util import hook, http from util import hook, http
from urlparse import urljoin
from BeautifulSoup import BeautifulSoup from BeautifulSoup import BeautifulSoup
from collections import defaultdict from collections import defaultdict
base_url = 'http://www.fmylife.com/'
fml_cache = defaultdict() fml_cache = defaultdict()
def refresh_cache(): def refresh_cache():
""" gets a page of random FMLs and puts them into a dictionary """ """ gets a page of random FMLs and puts them into a dictionary """
page = http.get(urljoin(base_url, 'random')) page = http.get('http://www.fmylife.com/random/')
soup = BeautifulSoup(page) soup = BeautifulSoup(page)
for e in soup.findAll('div', {'class': 'post article'}): for e in soup.findAll('div', {'class': 'post article'}):
id = int(e['id']) id = int(e['id'])
# get the text of the FML
text = ''.join(e.find('p').findAll(text=True)) text = ''.join(e.find('p').findAll(text=True))
text = http.unescape(text) text = http.unescape(text)
# append to the dictionary
fml_cache[id] = text fml_cache[id] = text
# do an initial refresh of the cache # do an initial refresh of the cache