Created
May 12, 2011 21:17
-
-
Save dpflug/969462 to your computer and use it in GitHub Desktop.
URL titles for SuperBot2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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