Greatly improved plugins/fmylife.py :)
This commit is contained in:
parent
62437817f4
commit
a4f9c2d070
1 changed files with 22 additions and 7 deletions
|
@ -1,17 +1,29 @@
|
||||||
|
# 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"
|
||||||
|
|
||||||
|
if inp:
|
||||||
|
if not inp.isdigit():
|
||||||
|
return "Invalid ID!"
|
||||||
try:
|
try:
|
||||||
page = http.get(url)
|
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):
|
except (HTTPError, IOError):
|
||||||
return "I tried to use .fml, but it was broken. FML"
|
return "I tried to use .fml, but it was broken. FML"
|
||||||
|
|
||||||
|
@ -19,6 +31,9 @@ def fml(inp):
|
||||||
|
|
||||||
soup.find('div', id='submit').extract()
|
soup.find('div', id='submit').extract()
|
||||||
post = soup.body.find('div', 'post')
|
post = soup.body.find('div', 'post')
|
||||||
|
try:
|
||||||
id = int(post.find('a', 'fmllink')['href'].split('/')[-1])
|
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)
|
||||||
|
|
Reference in a new issue