Catch a webutils.WebError, fix whitespace, and remove a urlquote

This commit is contained in:
James Vega 2004-10-04 18:15:06 +00:00
parent 6a1b9ccaeb
commit b4580384ee

View File

@ -90,10 +90,10 @@ conf.registerChannelValue(conf.supybot.plugins.ShrinkUrl, 'default',
class CdbShrunkenUrlDB(object): class CdbShrunkenUrlDB(object):
def __init__(self, filename): def __init__(self, filename):
self.tinyDb = conf.supybot.databases.types.cdb.connect( self.tinyDb = conf.supybot.databases.types.cdb.connect(
filename.replace('.db', '.Tiny.db')) filename.replace('.db', '.Tiny.db'))
self.lnDb = conf.supybot.databases.types.cdb.connect( self.lnDb = conf.supybot.databases.types.cdb.connect(
filename.replace('.db', '.Ln.db')) filename.replace('.db', '.Ln.db'))
def getTiny(self, url): def getTiny(self, url):
return self.tinyDb[url] return self.tinyDb[url]
@ -115,14 +115,14 @@ class CdbShrunkenUrlDB(object):
self.lnDb.flush() self.lnDb.flush()
ShrunkenUrlDB = plugins.DB('ShrinkUrl', {'cdb': CdbShrunkenUrlDB}) ShrunkenUrlDB = plugins.DB('ShrinkUrl', {'cdb': CdbShrunkenUrlDB})
class ShrinkUrl(callbacks.PrivmsgCommandAndRegexp): class ShrinkUrl(callbacks.PrivmsgCommandAndRegexp):
regexps = ['shrinkSnarfer'] regexps = ['shrinkSnarfer']
def __init__(self): def __init__(self):
self.db = ShrunkenUrlDB() self.db = ShrunkenUrlDB()
self.__parent = super(ShrinkUrl, self) self.__parent = super(ShrinkUrl, self)
self.__parent.__init__() self.__parent.__init__()
def die(self): def die(self):
self.db.close() self.db.close()
@ -138,12 +138,15 @@ class ShrinkUrl(callbacks.PrivmsgCommandAndRegexp):
for m in webutils.httpUrlRe.finditer(text): for m in webutils.httpUrlRe.finditer(text):
url = m.group(1) url = m.group(1)
if len(url) > self.registryValue('minimumLength', channel): if len(url) > self.registryValue('minimumLength', channel):
cmd = self.registryValue('default', channel) cmd = self.registryValue('default', channel)
if cmd == 'ln': try:
(shortUrl, _) = self._getLnUrl(url) if cmd == 'ln':
elif cmd == 'tiny': (shortUrl, _) = self._getLnUrl(url)
shortUrl = self._getTinyUrl(url) elif cmd == 'tiny':
text = text.replace(url, shortUrl) shortUrl = self._getTinyUrl(url)
text = text.replace(url, shortUrl)
except webutils.WebError:
pass
newMsg = ircmsgs.privmsg(channel, text, msg=msg) newMsg = ircmsgs.privmsg(channel, text, msg=msg)
newMsg.tag('shrunken') newMsg.tag('shrunken')
irc.queueMsg(newMsg) irc.queueMsg(newMsg)
@ -157,7 +160,7 @@ class ShrinkUrl(callbacks.PrivmsgCommandAndRegexp):
self._outFilterThread(irc, msg) self._outFilterThread(irc, msg)
return None return None
return msg return msg
def shrinkSnarfer(self, irc, msg, match): def shrinkSnarfer(self, irc, msg, match):
r"https?://[^\])>\s]{13,}" r"https?://[^\])>\s]{13,}"
channel = msg.args[0] channel = msg.args[0]
@ -170,18 +173,18 @@ class ShrinkUrl(callbacks.PrivmsgCommandAndRegexp):
self.log.debug('Matched nonSnarfingRegexp: %r', url) self.log.debug('Matched nonSnarfingRegexp: %r', url)
return return
minlen = self.registryValue('minimumLength', channel) minlen = self.registryValue('minimumLength', channel)
cmd = self.registryValue('default', channel) cmd = self.registryValue('default', channel)
if len(url) >= minlen: if len(url) >= minlen:
shorturl = None shorturl = None
if cmd == 'tiny': if cmd == 'tiny':
shorturl = self._getTinyUrl(url) shorturl = self._getTinyUrl(url)
elif cmd == 'ln': elif cmd == 'ln':
(shorturl, _) = self._getLnUrl(url) (shorturl, _) = self._getLnUrl(url)
if shorturl is None: if shorturl is None:
self.log.info('Couldn\'t get shorturl for %r', url) self.log.info('Couldn\'t get shorturl for %r', url)
return return
domain = webutils.getDomain(url) domain = webutils.getDomain(url)
s = '%s (at %s)' % (ircutils.bold(shorturl), domain) s = '%s (at %s)' % (ircutils.bold(shorturl), domain)
m = irc.reply(s, prefixName=False) m = irc.reply(s, prefixName=False)
if m is None: if m is None:
print irc, irc.__class__ print irc, irc.__class__
@ -192,15 +195,14 @@ class ShrinkUrl(callbacks.PrivmsgCommandAndRegexp):
try: try:
return self.db.getLn(url) return self.db.getLn(url)
except KeyError: except KeyError:
url = webutils.urlquote(url) text = webutils.getUrl('http://ln-s.net/home/api.jsp?url=%s' % url)
text = webutils.getUrl('http://ln-s.net/home/api.jsp?url=%s' % url) (code, lnurl) = text.split(None, 1)
(code, lnurl) = text.split(None, 1) lnurl = lnurl.strip()
lnurl = lnurl.strip() if code == '200':
if code == '200': self.db.setLn(url, lnurl)
self.db.setLn(url, lnurl) else:
else: lnurl = None
lnurl = None return (lnurl, code)
return (lnurl, code)
def ln(self, irc, msg, args, url): def ln(self, irc, msg, args, url):
"""<url> """<url>
@ -211,8 +213,8 @@ class ShrinkUrl(callbacks.PrivmsgCommandAndRegexp):
irc.error('Stop being a lazy-biotch and type the URL yourself.') irc.error('Stop being a lazy-biotch and type the URL yourself.')
return return
(lnurl, error) = self._getLnUrl(url) (lnurl, error) = self._getLnUrl(url)
if lnurl is not None: if lnurl is not None:
domain = webutils.getDomain(url) domain = webutils.getDomain(url)
m = irc.reply(lnurl) m = irc.reply(lnurl)
m.tag('shrunken') m.tag('shrunken')
else: else: