Improved title plugin
This commit is contained in:
parent
cf377e1800
commit
7d9b3c35fe
1 changed files with 8 additions and 5 deletions
|
@ -1,4 +1,5 @@
|
||||||
from util import hook, http, urlnorm
|
from util import hook, http, urlnorm
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
|
|
||||||
@hook.command
|
@hook.command
|
||||||
|
@ -7,13 +8,15 @@ def title(inp):
|
||||||
url = urlnorm.normalize(inp.encode('utf-8'), assume_scheme="http")
|
url = urlnorm.normalize(inp.encode('utf-8'), assume_scheme="http")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
page = http.get_html(url)
|
page = http.open(url)
|
||||||
|
real_url = page.geturl()
|
||||||
|
soup = BeautifulSoup(page.read())
|
||||||
except (http.HTTPError, http.URLError):
|
except (http.HTTPError, http.URLError):
|
||||||
return "Could not fetch page."
|
return "Could not fetch page."
|
||||||
|
|
||||||
try:
|
title = soup.find('title').contents[0]
|
||||||
title = page.find(".//title").text
|
|
||||||
except AttributeError:
|
if not title:
|
||||||
return "Could not find title."
|
return "Could not find title."
|
||||||
|
|
||||||
return http.unescape(title)
|
return u"{} [{}]".format(title, real_url)
|
||||||
|
|
Reference in a new issue