diff --git a/plugins/ShrinkUrl/config.py b/plugins/ShrinkUrl/config.py index 5a2b6355e..dc473d45a 100644 --- a/plugins/ShrinkUrl/config.py +++ b/plugins/ShrinkUrl/config.py @@ -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): diff --git a/plugins/ShrinkUrl/plugin.py b/plugins/ShrinkUrl/plugin.py index 471400186..0a72e0c15 100644 --- a/plugins/ShrinkUrl/plugin.py +++ b/plugins/ShrinkUrl/plugin.py @@ -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): + """ + + Returns an goo.gl version of . + """ + 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: diff --git a/plugins/ShrinkUrl/test.py b/plugins/ShrinkUrl/test.py index caf606668..629486c2c 100644 --- a/plugins/ShrinkUrl/test.py +++ b/plugins/ShrinkUrl/test.py @@ -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')