ShrinkUrl: Implement @expand. Closes GH-392.

This commit is contained in:
Valentin Lorentz 2012-09-02 10:06:33 +02:00
parent de61bb6b93
commit e5e2db15b2
2 changed files with 37 additions and 1 deletions

View File

@ -46,9 +46,12 @@ class CdbShrunkenUrlDB(object):
def __init__(self, filename): def __init__(self, filename):
self.dbs = {} self.dbs = {}
cdb = conf.supybot.databases.types.cdb cdb = conf.supybot.databases.types.cdb
for service in conf.supybot.plugins.ShrinkUrl.default.validStrings: def register_service(service):
dbname = filename.replace('.db', service.capitalize() + '.db') dbname = filename.replace('.db', service.capitalize() + '.db')
self.dbs[service] = cdb.connect(dbname) self.dbs[service] = cdb.connect(dbname)
for service in conf.supybot.plugins.ShrinkUrl.default.validStrings:
register_service(service)
register_service('Expand')
def get(self, service, url): def get(self, service, url):
return self.dbs[service][url] return self.dbs[service][url]
@ -320,6 +323,32 @@ class ShrinkUrl(callbacks.PluginRegexp):
irc.error(str(e)) irc.error(str(e))
x0 = thread(wrap(x0, ['url'])) x0 = thread(wrap(x0, ['url']))
def _getExpandUrl(self, url):
url = utils.web.urlquote(url)
try:
return self.db.get('Expand', url)
except KeyError:
text = utils.web.getUrl('http://api.longurl.org/v2/expand?url=' + url)
text = text.decode()
text = text.split('<![CDATA[', 1)[1].split(']]>', 1)[0]
self.db.set('Expand', url, text)
return text
@internationalizeDocstring
def expand(self, irc, msg, args, url):
"""<url>
Returns an expanded version of <url>.
"""
try:
expandurl = self._getExpandUrl(url)
m = irc.reply(expandurl)
if m is not None:
m.tag('shrunken')
except ShrinkError, e:
irc.error(str(e))
expand = thread(wrap(expand, ['url']))
Class = ShrinkUrl Class = ShrinkUrl
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:

View File

@ -130,4 +130,11 @@ class ShrinkUrlTestCase(ChannelPluginTestCase):
shrink.minimumLength.setValue(origLen) shrink.minimumLength.setValue(origLen)
shrink.nonSnarfingRegexp.setValue(origRegexp) shrink.nonSnarfingRegexp.setValue(origRegexp)
def testExpand(self):
self.assertResponse('expand http://x0.no/0l2k', self.udUrl)
self.assertResponse('expand http://x0.no/0l2k', self.udUrl)
self.assertResponse('expand http://x0.no/0l2j', self.sfUrl)
self.assertResponse('expand http://x0.no/0l2j', self.sfUrl)
self.assertResponse('expand http://x0.no/0l2k', self.udUrl)
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: # vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79: