ShrinkUrl: Uniformly handle errors while shrinking via ShrinkError.

Signed-off-by: James Vega <jamessan@users.sourceforge.net>
This commit is contained in:
James Vega 2009-10-04 22:08:55 -04:00
parent c055b16ea8
commit 200f716011

View File

@ -62,6 +62,9 @@ class CdbShrunkenUrlDB(object):
ShrunkenUrlDB = plugins.DB('ShrinkUrl', {'cdb': CdbShrunkenUrlDB}) ShrunkenUrlDB = plugins.DB('ShrinkUrl', {'cdb': CdbShrunkenUrlDB})
class ShrinkError(Exception):
pass
class ShrinkUrl(callbacks.PluginRegexp): class ShrinkUrl(callbacks.PluginRegexp):
regexps = ['shrinkSnarfer'] regexps = ['shrinkSnarfer']
def __init__(self, irc): def __init__(self, irc):
@ -87,7 +90,7 @@ class ShrinkUrl(callbacks.PluginRegexp):
try: try:
shortUrl = getattr(self, '_get%sUrl' % cmd)(url) shortUrl = getattr(self, '_get%sUrl' % cmd)(url)
text = text.replace(url, shortUrl) text = text.replace(url, shortUrl)
except (utils.web.Error, AttributeError): except (utils.web.Error, AttributeError, ShrinkError):
pass pass
newMsg = ircmsgs.privmsg(channel, text, msg=msg) newMsg = ircmsgs.privmsg(channel, text, msg=msg)
newMsg.tag('shrunken') newMsg.tag('shrunken')
@ -118,7 +121,7 @@ class ShrinkUrl(callbacks.PluginRegexp):
if len(url) >= minlen: if len(url) >= minlen:
try: try:
shorturl = getattr(self, '_get%sUrl' % cmd)(url) shorturl = getattr(self, '_get%sUrl' % cmd)(url)
except (utils.web.Error, AttributeError): except (utils.web.Error, AttributeError, ShrinkError):
self.log.info('Couldn\'t get shorturl for %u', url) self.log.info('Couldn\'t get shorturl for %u', url)
return return
if self.registryValue('shrinkSnarfer.showDomain', channel): if self.registryValue('shrinkSnarfer.showDomain', channel):
@ -138,16 +141,16 @@ class ShrinkUrl(callbacks.PluginRegexp):
def _getLnUrl(self, url): def _getLnUrl(self, url):
url = utils.web.urlquote(url) url = utils.web.urlquote(url)
try: try:
return (self.db.get('ln', url), '200') return self.db.get('ln', url)
except KeyError: except KeyError:
text = utils.web.getUrl('http://ln-s.net/home/api.jsp?url=' + url) text = utils.web.getUrl('http://ln-s.net/home/api.jsp?url=' + url)
(code, lnurl) = text.split(None, 1) (code, text) = text.split(None, 1)
lnurl = lnurl.strip() text = text.strip()
if code == '200': if code == '200':
self.db.set('ln', url, lnurl) self.db.set('ln', url, text)
return text
else: else:
lnurl = None raise ShrinkError, text
return (lnurl, code)
def ln(self, irc, msg, args, url): def ln(self, irc, msg, args, url):
"""<url> """<url>
@ -157,13 +160,13 @@ class ShrinkUrl(callbacks.PluginRegexp):
if len(url) < 17: if len(url) < 17:
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) try:
if lnurl is not None: lnurl = self._getLnUrl(url)
m = irc.reply(lnurl) m = irc.reply(lnurl)
if m is not None: if m is not None:
m.tag('shrunken') m.tag('shrunken')
else: except ShrinkError, e:
irc.error(error) irc.error(str(e))
ln = thread(wrap(ln, ['url'])) ln = thread(wrap(ln, ['url']))
_tinyRe = re.compile(r'<blockquote><b>(http://tinyurl\.com/\w+)</b>') _tinyRe = re.compile(r'<blockquote><b>(http://tinyurl\.com/\w+)</b>')
@ -174,8 +177,7 @@ class ShrinkUrl(callbacks.PluginRegexp):
s = utils.web.getUrl('http://tinyurl.com/create.php?url=' + url) s = utils.web.getUrl('http://tinyurl.com/create.php?url=' + url)
m = self._tinyRe.search(s) m = self._tinyRe.search(s)
if m is None: if m is None:
tinyurl = None raise ShrinkError, 'Could not parse the TinyURL.com results.'
else:
tinyurl = m.group(1) tinyurl = m.group(1)
self.db.set('tiny', url, tinyurl) self.db.set('tiny', url, tinyurl)
return tinyurl return tinyurl
@ -188,14 +190,13 @@ class ShrinkUrl(callbacks.PluginRegexp):
if len(url) < 20: if len(url) < 20:
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
try:
tinyurl = self._getTinyUrl(url) tinyurl = self._getTinyUrl(url)
if tinyurl is not None:
m = irc.reply(tinyurl) m = irc.reply(tinyurl)
if m is not None: if m is not None:
m.tag('shrunken') m.tag('shrunken')
else: except ShrinkError, e:
s = 'Could not parse the TinyURL.com results page.' irc.errorPossibleBug(str(e))
irc.errorPossibleBug(s)
tiny = thread(wrap(tiny, ['url'])) tiny = thread(wrap(tiny, ['url']))