From 16573eb482f024e701d31d099e1be000e7fc74c6 Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Tue, 4 Sep 2012 09:58:59 +1200 Subject: [PATCH] Moved fact.py over to BS4 (I need to rewrite fact.py soon, it's a bit outdated and somewhat broken), removed old htmlstipper library --- plugins/fact.py | 12 +++++------- plugins/util/misc.py | 42 ------------------------------------------ 2 files changed, 5 insertions(+), 49 deletions(-) delete mode 100755 plugins/util/misc.py diff --git a/plugins/fact.py b/plugins/fact.py index 64d2387..f042cdb 100755 --- a/plugins/fact.py +++ b/plugins/fact.py @@ -1,8 +1,6 @@ import re -from util import hook -from util import http -from util import misc -from BeautifulSoup import BeautifulSoup +from util import hook, http +from bs4 import BeautifulSoup @hook.command(autohelp=False) @@ -22,10 +20,10 @@ def fact(inp, say=False, nick=False): def get_fact(): page = http.get('http://www.omg-facts.com/random') soup = BeautifulSoup(page) - container = soup.find('a', {'class': 'surprise'}) - link = container['href'] + response = soup.find('a', {'class': 'surprise'}) + link = response['href'] - fact = misc.strip_html(container.renderContents()) + fact = response.contents[0] if fact: return (fact, link) diff --git a/plugins/util/misc.py b/plugins/util/misc.py deleted file mode 100755 index 9def21d..0000000 --- a/plugins/util/misc.py +++ /dev/null @@ -1,42 +0,0 @@ -from HTMLParser import HTMLParser -import htmlentitydefs -import errno -import re - -class HTMLStripper(HTMLParser): - - def __init__(self, data): - HTMLParser.__init__(self) - self._stripped = [] - self.feed(data) - - def handle_starttag(self, tag, attrs): - if tag.lower() == 'br': - self._stripped.append('\n') - - def handle_charref(self, name): - try: - if name.lower().startswith('x'): - char = int(name[1:], 16) - else: - char = int(name) - self._stripped.append(unichr(char)) - except Exception, error: - return - - def handle_entityref(self, name): - try: - char = unichr(htmlentitydefs.name2codepoint[name]) - except Exception, error: - char = u'&%s;' % name - self._stripped.append(char) - - def handle_data(self, data): - self._stripped.append(data) - - @property - def stripped(self): - return ''.join(self._stripped) - -def strip_html(data): - return HTMLStripper(data).stripped