This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/plugins/title.py

23 lines
571 B
Python
Raw Normal View History

2012-05-16 21:45:59 +02:00
from util import hook, http, urlnorm
2012-10-12 12:01:37 +02:00
from bs4 import BeautifulSoup
2011-11-20 10:23:31 +01:00
2012-03-23 06:14:58 +01:00
@hook.command
def title(inp):
2013-09-04 12:30:04 +02:00
"""title <url> -- gets the title of a web page"""
2012-10-12 11:34:48 +02:00
url = urlnorm.normalize(inp.encode('utf-8'), assume_scheme="http")
try:
2012-10-12 12:01:37 +02:00
page = http.open(url)
real_url = page.geturl()
soup = BeautifulSoup(page.read())
2012-10-12 11:34:48 +02:00
except (http.HTTPError, http.URLError):
return "Could not fetch page."
2012-03-23 06:14:58 +01:00
2012-10-12 12:01:37 +02:00
title = soup.find('title').contents[0]
if not title:
return "Could not find title."
2012-10-12 12:01:37 +02:00
return u"{} [{}]".format(title, real_url)