ShrinkUrl plugin, add BitLy support.

Login+ApiKey required for it to function.
This commit is contained in:
Michael Telatynski 2015-06-08 17:57:13 +01:00
parent 7c4a28d7fd
commit 147cccdf6c
2 changed files with 45 additions and 3 deletions

View File

@ -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:

View File

@ -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):
"""<url>
Returns an bitly version of <url>.
"""
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)