Add old link alert to title grabber

master
Andrei 2012-06-27 19:33:09 -04:00 committed by mutantmonkey
parent 78441d5fbf
commit 52aaa0778a
2 changed files with 26 additions and 7 deletions

View File

@ -17,6 +17,7 @@ import time
from html.entities import name2codepoint from html.entities import name2codepoint
import web import web
from tools import deprecated from tools import deprecated
from modules.linx import postedlink
cj = http.cookiejar.LWPCookieJar() cj = http.cookiejar.LWPCookieJar()
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj)) opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
@ -76,9 +77,6 @@ def head(phenny, input):
head.commands = ['head'] head.commands = ['head']
head.example = '.head http://www.w3.org/' head.example = '.head http://www.w3.org/'
r_title = re.compile(r'(?ims)<title[^>]*>(.*?)</title\s*>')
r_entity = re.compile(r'&[A-Za-z0-9#]+;')
@deprecated @deprecated
def f_title(self, origin, match, args): def f_title(self, origin, match, args):
""".title <URI> - Return the title of URI.""" """.title <URI> - Return the title of URI."""
@ -95,6 +93,9 @@ def f_title(self, origin, match, args):
else: self.msg(origin.sender, origin.nick + ': No title found') else: self.msg(origin.sender, origin.nick + ': No title found')
f_title.commands = ['title'] f_title.commands = ['title']
r_title = re.compile(r'(?ims)<title[^>]*>(.*?)</title\s*>')
r_entity = re.compile(r'&[A-Za-z0-9#]+;')
def noteuri(phenny, input): def noteuri(phenny, input):
uri = input.group(1) uri = input.group(1)
if not hasattr(phenny.bot, 'last_seen_uri'): if not hasattr(phenny.bot, 'last_seen_uri'):
@ -108,13 +109,13 @@ def snarfuri(phenny, input):
if re.match(r'(?i)' + phenny.config.prefix + titlecommands, input.group()): if re.match(r'(?i)' + phenny.config.prefix + titlecommands, input.group()):
return return
uri = input.group(1) uri = input.group(1)
title = gettitle(uri) title = gettitle(uri, input.sender)
if title: if title:
phenny.msg(input.sender, '[ ' + title + ' ]') phenny.msg(input.sender, title)
snarfuri.rule = r'.*(http[s]?://[^<> "\x01]+)[,.]?' snarfuri.rule = r'.*(http[s]?://[^<> "\x01]+)[,.]?'
snarfuri.priority = 'low' snarfuri.priority = 'low'
def gettitle(uri): def gettitle(uri, channel):
if not ':' in uri: if not ':' in uri:
uri = 'http://' + uri uri = 'http://' + uri
uri = uri.replace('#!', '?_escaped_fragment_=') uri = uri.replace('#!', '?_escaped_fragment_=')
@ -192,6 +193,12 @@ def gettitle(uri):
if title: if title:
title = title.replace('\n', '') title = title.replace('\n', '')
title = title.replace('\r', '') title = title.replace('\r', '')
channels = ['#vtluug']
if channel in channels:
title = "[ " + title + " ] " + postedlink(uri)
else:
title = "[ " + title + " ] "
else: title = None else: title = None
return title return title

View File

@ -79,5 +79,17 @@ def posted(phenny, input):
posted.rule = (['posted'], r'(.*)') posted.rule = (['posted'], r'(.*)')
def postedlink(url):
""" helper method for gettitle() """
try:
req = web.post("http://linx.li/vtluugpostedurl", {'url': url})
except:
req = ""
return req
postedlink.channels = ['#vtluug']
if __name__ == '__main__': if __name__ == '__main__':
print(__doc__.strip()) print(__doc__.strip())