ShrinkUrl: Add ur1.ca support.

Signed-off-by: James McCoy <jamessan@users.sourceforge.net>
This commit is contained in:
Valentin Lorentz 2012-08-11 11:07:40 +02:00 committed by James McCoy
parent a8e3081b18
commit caa36121a7
3 changed files with 37 additions and 3 deletions

View File

@ -40,11 +40,11 @@ def configure(advanced):
conf.supybot.plugins.ShrinkUrl.shrinkSnarfer.setValue(True)
class ShrinkService(registry.OnlySomeStrings):
"""Valid values include 'ln', 'tiny', 'xrl', 'goo', and 'x0'."""
validStrings = ('ln', 'tiny', 'xrl', 'goo', 'x0')
"""Valid values include 'ln', 'tiny', 'xrl', 'goo', 'ur1', and 'x0'."""
validStrings = ('ln', 'tiny', 'xrl', 'goo', 'ur1', 'x0')
class ShrinkCycle(registry.SpaceSeparatedListOfStrings):
"""Valid values include 'ln', 'tiny', 'xrl', 'goo', and 'x0'."""
"""Valid values include 'ln', 'tiny', 'xrl', 'goo', 'ur1', and 'x0'."""
Value = ShrinkService
def __init__(self, *args, **kwargs):

View File

@ -259,6 +259,35 @@ class ShrinkUrl(callbacks.PluginRegexp):
irc.error(str(e))
goo = thread(wrap(goo, ['url']))
_ur1Api = 'http://ur1.ca/'
_ur1Regexp = re.compile(r'<a href="(?P<url>[^"]+)">')
def _getUr1Url(self, url):
try:
return self.db.get('ur1ca', utils.web.urlquote(url))
except KeyError:
parameters = utils.web.urlencode({'longurl': url})
response = utils.web.getUrl(self._ur1Api, data=parameters)
ur1ca = self._ur1Regexp.search(response.decode()).group('url')
if ur1ca > 0 :
self.db.set('ur1', url, ur1ca)
return ur1ca
else:
raise ShrinkError, text
def ur1(self, irc, msg, args, url):
"""<url>
Returns an ur1 version of <url>.
"""
try:
ur1url = self._getUr1Url(url)
m = irc.reply(ur1url)
if m is not None:
m.tag('shrunken')
except ShrinkError, e:
irc.error(str(e))
ur1 = thread(wrap(ur1, ['url']))
_x0Api = 'http://api.x0.no/?%s'
def _getX0Url(self, url):
try:

View File

@ -45,6 +45,8 @@ class ShrinkUrlTestCase(ChannelPluginTestCase):
(udUrl, r'http://xrl.us/bfnyji')],
'goo': [(sfUrl, r'http://goo.gl/3c59N'),
(udUrl, r'http://goo.gl/ocTga')],
'ur1': [(sfUrl, r'http://ur1.ca/9xl25'),
(udUrl, r'http://ur1.ca/9xl9k')],
'x0': [(sfUrl, r'http://x0.no/0l2j'),
(udUrl, r'http://x0.no/0l2k')]
}
@ -102,6 +104,9 @@ class ShrinkUrlTestCase(ChannelPluginTestCase):
def testGoosnarf(self):
self._snarf('goo')
def testUr1snarf(self):
self._snarf('ur1')
def testX0snarf(self):
self._snarf('x0')