diff --git a/plugins/ShrinkUrl/config.py b/plugins/ShrinkUrl/config.py index b211098be..dc80bd0c2 100644 --- a/plugins/ShrinkUrl/config.py +++ b/plugins/ShrinkUrl/config.py @@ -42,11 +42,11 @@ def configure(advanced): conf.supybot.plugins.ShrinkUrl.shrinkSnarfer.setValue(True) class ShrinkService(registry.OnlySomeStrings): - """Valid values include 'tiny', 'ur1', and 'x0'.""" - validStrings = ('tiny', 'ur1', 'x0') + """Valid values include 'tiny', 'ur1', 'x0', and 'bitly'.""" + validStrings = ('tiny', 'ur1', 'x0', 'bitly') class ShrinkCycle(registry.SpaceSeparatedListOfStrings): - """Valid values include 'ln', 'tiny', 'ur1', and 'x0'.""" + """Valid values include 'ln', 'tiny', 'ur1', 'x0', and 'bitly'.""" Value = ShrinkService def __init__(self, *args, **kwargs): @@ -97,4 +97,13 @@ conf.registerChannelValue(ShrinkUrl, 'serviceRotation', ShrinkCycle([], _("""If set to a non-empty value, specifies the list of services to rotate through for the shrinkSnarfer and outFilter."""))) +conf.registerGroup(ShrinkUrl, 'bitly') +conf.registerGlobalValue(ShrinkUrl.bitly, 'login', + registry.String('', _("""Determines the API Login string used for + shortening using the Bit.ly service."""), private=True)) +conf.registerGlobalValue(ShrinkUrl.bitly, 'apiKey', + registry.String('', _("""Determines the API Key string used for + shortening using the Bit.ly service."""), private=True)) + + # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: diff --git a/plugins/ShrinkUrl/plugin.py b/plugins/ShrinkUrl/plugin.py index 3cce73182..fba4dd140 100644 --- a/plugins/ShrinkUrl/plugin.py +++ b/plugins/ShrinkUrl/plugin.py @@ -255,6 +255,39 @@ class ShrinkUrl(callbacks.PluginRegexp): irc.error(str(e)) x0 = thread(wrap(x0, ['httpUrl'])) + _bitlyApi = 'http://api.bit.ly/shorten?format=txt&login=%s&apiKey=%s&longUrl=%s' + @retry + def _getbitlyUrl(self, url): + try: + return self.db.get('bitly', url) + except KeyError: + text = utils.web.getUrl(self._bitlyApi % + (self.registryValue('bitly.login'), + self.registryValue('bitly.apiKey'), + url)).decode() + if not text.startswith('http'): + raise ShrinkError(text) + self.db.set('bitly', url, text) + return text + + @internationalizeDocstring + def bitly(self, irc, msg, args, url): + """ + + Returns an bitly version of . + """ + if not (self.registryValue('bitly.login') or + self.registryValue('bitly.apiKey')): + irc.error(_("""Bit.ly Requested but login and apiKey are empty.""")) + try: + bitlyurl = self._getbitlyUrl(url) + m = irc.reply(bitlyurl) + if m is not None: + m.tag('shrunken') + except ShrinkError as e: + irc.error(str(e)) + bitly = thread(wrap(bitly, ['httpUrl'])) + @retry def _getExpandUrl(self, url): url = utils.web.urlquote(url)