Greatly improved plugins/fmylife.py :)

This commit is contained in:
Luke Rogers 2012-02-24 02:41:13 +13:00
parent 62437817f4
commit a4f9c2d070

View file

@ -1,24 +1,39 @@
# Plugin by Lukeroge
# <lukeroge@gmail.com> <https://github.com/lukeroge/CloudBot/>
import re import re
from util import hook, http, misc from util import hook, http, misc
from urllib2 import HTTPError from urllib2 import HTTPError
from urlparse import urljoin
from BeautifulSoup import BeautifulSoup from BeautifulSoup import BeautifulSoup
url = 'http://www.fmylife.com/random' base_url = 'http://www.fmylife.com/'
@hook.command(autohelp=False) @hook.command(autohelp=False)
def fml(inp): def fml(inp):
".fml -- gets a random quote from fmyfife.com" ".fml [id] -- gets a random quote from fmyfife.com"
try: if inp:
page = http.get(url) if not inp.isdigit():
except (HTTPError, IOError): return "Invalid ID!"
return "I tried to use .fml, but it was broken. FML" try:
page = http.get(urljoin(base_url, inp))
except (HTTPError, IOError):
return "Could not fetch #%s. FML" % inp
else:
try:
page = http.get(urljoin(base_url, 'random'))
except (HTTPError, IOError):
return "I tried to use .fml, but it was broken. FML"
soup = BeautifulSoup(page) soup = BeautifulSoup(page)
soup.find('div', id='submit').extract() soup.find('div', id='submit').extract()
post = soup.body.find('div', 'post') post = soup.body.find('div', 'post')
id = int(post.find('a', 'fmllink')['href'].split('/')[-1]) try:
id = int(post.find('a', 'fmllink')['href'].split('/')[-1])
except TypeError:
return "Could not fetch #%s. FML" % inp
body = misc.strip_html(' '.join(link.renderContents() for link in post('a', 'fmllink'))) body = misc.strip_html(' '.join(link.renderContents() for link in post('a', 'fmllink')))
return '(#%d) %s' % (id, body) return '(#%d) %s' % (id, body)