Skip to content

Commit

Permalink
ShrinkUrl: Add ur1.ca support.
Browse files Browse the repository at this point in the history
  • Loading branch information
progval committed Aug 11, 2012
1 parent 5dfba06 commit 24d73eb
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions plugins/ShrinkUrl/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ def configure(advanced):

class ShrinkService(registry.OnlySomeStrings):
"""Valid values include 'ln', 'tiny', 'xrl', 'goo', and 'x0'."""
validStrings = ('ln', 'tiny', 'xrl', 'goo', '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):
Expand Down
30 changes: 30 additions & 0 deletions plugins/ShrinkUrl/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import re
import json
import urllib

import supybot.conf as conf
import supybot.utils as utils
Expand Down Expand Up @@ -265,6 +266,35 @@ def goo(self, irc, msg, args, url):
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 len(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:
Expand Down
5 changes: 5 additions & 0 deletions plugins/ShrinkUrl/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')]
}
Expand Down Expand Up @@ -102,6 +104,9 @@ def testXrlsnarf(self):
def testGoosnarf(self):
self._snarf('goo')

def testUr1snarf(self):
self._snarf('ur1')

def testX0snarf(self):
self._snarf('x0')

Expand Down

0 comments on commit 24d73eb

Please sign in to comment.