Skip to content

Instantly share code, notes, and snippets.

@dpflug
Created May 12, 2011 21:17
Show Gist options
  • Save dpflug/969462 to your computer and use it in GitHub Desktop.
Save dpflug/969462 to your computer and use it in GitHub Desktop.
URL titles for SuperBot2
from Hook import bindFunction, requires, prefers
@requires("URLUtils")
@prefers("Colors")
class URLTitle:
@bindFunction(message="(https?://[^\s!,]*)")
def url_title(self, target, colorize, grabTitle, message0):
if toMe:
return # Ignore PMs and notices
print("Called url_title")
title = grabTitle(message0)
print("title is %s" % title)
if title:
if colorize:
return "<{B}Title{}: {C7}%s{}>" % title
else:
return "<Title: %s>" % title
import urllib2
class URLUtils:
def grabTitle(self, url):
"""Return the title of the page if exists."""
try:
title = urllib2.urlopen(url, None, 5).read(5120).split("title>")[1][:-2]
title = re.sub('[\n\r\t ]+', ' ', title).strip()
unicode(title, errors='ignore')
except Exception, e:
raise e
def onEvent(self, event):
event["grabTitle"] = self.grabTitle
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment