From 7cc8feaf3208d7f249c9df1b06e4e46a3499e926 Mon Sep 17 00:00:00 2001 From: Joe Julian Date: Mon, 28 May 2012 19:58:15 +0200 Subject: [PATCH] ShrinkUrl: Support for goo.gl url shortener. --- plugins/ShrinkUrl/config.py | 4 ++-- plugins/ShrinkUrl/plugin.py | 33 +++++++++++++++++++++++++++++++++ plugins/ShrinkUrl/test.py | 5 +++++ 3 files changed, 40 insertions(+), 2 deletions(-) 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')