ShrinkUrl: Support for goo.gl url shortener.

This commit is contained in:
Joe Julian 2012-05-28 19:58:15 +02:00 committed by Valentin Lorentz
parent 2486be4464
commit 7cc8feaf32
3 changed files with 40 additions and 2 deletions

View File

@ -43,10 +43,10 @@ def configure(advanced):
class ShrinkService(registry.OnlySomeStrings):
"""Valid values include 'ln', 'tiny', 'xrl', and 'x0'."""
validStrings = ('ln', 'tiny', 'xrl', 'x0')
validStrings = ('ln', 'tiny', 'xrl', 'goo', 'x0')
class ShrinkCycle(registry.SpaceSeparatedListOfStrings):
"""Valid values include 'ln', 'tiny', 'xrl', and 'x0'."""
"""Valid values include 'ln', 'tiny', 'xrl', 'goo', and 'x0'."""
Value = ShrinkService
def __init__(self, *args, **kwargs):

View File

@ -29,6 +29,8 @@
###
import re
import json
import httplib2
import supybot.conf as conf
import supybot.utils as utils
@ -232,6 +234,37 @@ class ShrinkUrl(callbacks.PluginRegexp):
irc.error(str(e))
xrl = thread(wrap(xrl, ['url']))
_gooApi = 'https://www.googleapis.com/urlshortener/v1/url'
def _getGooUrl(self, url):
url = utils.web.urlquote(url)
try:
return self.db.get('goo', url)
except KeyError:
text = httplib2.Http().request(self._gooApi,
'POST',
headers={'content-type':'application/json'},
body=json.dumps({'longUrl': url}))[1]
googl = json.loads(text)['id']
if len(googl) > 0 :
self.db.set('goo', url, googl)
return googl
else:
raise ShrinkError, text
def goo(self, irc, msg, args, url):
"""<url>
Returns an goo.gl version of <url>.
"""
try:
goourl = self._getGooUrl(url)
m = irc.reply(goourl)
if m is not None:
m.tag('shrunken')
except ShrinkError, e:
irc.error(str(e))
goo = thread(wrap(goo, ['url']))
_x0Api = 'http://api.x0.no/?%s'
def _getX0Url(self, url):
try:

View File

@ -43,6 +43,8 @@ class ShrinkUrlTestCase(ChannelPluginTestCase):
(udUrl, r'http://ln-s.net/2\$K')],
'xrl': [(sfUrl, r'http://xrl.us/bfq8ik'),
(udUrl, r'http://xrl.us/bfnyji')],
'goo': [(sfUrl, r'http://goo.gl/3c59N'),
(udUrl, r'http://goo.gl/ocTga')],
'x0': [(sfUrl, r'http://x0.no/0l2j'),
(udUrl, r'http://x0.no/0l2k')]
}
@ -97,6 +99,9 @@ class ShrinkUrlTestCase(ChannelPluginTestCase):
def testXrlsnarf(self):
self._snarf('xrl')
def testGoosnarf(self):
self._snarf('goo')
def testX0snarf(self):
self._snarf('x0')