From 7d9b3c35fe5bd39b2216e4579c89c39206c39478 Mon Sep 17 00:00:00 2001 From: Luke Rogers Date: Fri, 12 Oct 2012 23:01:37 +1300 Subject: [PATCH] Improved title plugin --- plugins/title.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/plugins/title.py b/plugins/title.py index f1bfdb3..c8e0c67 100755 --- a/plugins/title.py +++ b/plugins/title.py @@ -1,4 +1,5 @@ from util import hook, http, urlnorm +from bs4 import BeautifulSoup @hook.command @@ -7,13 +8,15 @@ def title(inp): url = urlnorm.normalize(inp.encode('utf-8'), assume_scheme="http") try: - page = http.get_html(url) + page = http.open(url) + real_url = page.geturl() + soup = BeautifulSoup(page.read()) except (http.HTTPError, http.URLError): return "Could not fetch page." - try: - title = page.find(".//title").text - except AttributeError: + title = soup.find('title').contents[0] + + if not title: return "Could not find title." - return http.unescape(title) + return u"{} [{}]".format(title, real_url)