mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-23 11:09:23 +01:00
ShrinkUrl: Support for goo.gl url shortener.
Signed-off-by: James McCoy <jamessan@users.sourceforge.net>
This commit is contained in:
parent
2327317b33
commit
a8e3081b18
@ -40,11 +40,11 @@ def configure(advanced):
|
||||
conf.supybot.plugins.ShrinkUrl.shrinkSnarfer.setValue(True)
|
||||
|
||||
class ShrinkService(registry.OnlySomeStrings):
|
||||
"""Valid values include 'ln', 'tiny', 'xrl', and 'x0'."""
|
||||
validStrings = ('ln', 'tiny', 'xrl', 'x0')
|
||||
"""Valid values include 'ln', 'tiny', 'xrl', 'goo', and '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):
|
||||
|
@ -29,6 +29,7 @@
|
||||
###
|
||||
|
||||
import re
|
||||
import json
|
||||
|
||||
import supybot.conf as conf
|
||||
import supybot.utils as utils
|
||||
@ -227,6 +228,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:
|
||||
headers = utils.web.defaultHeaders.copy()
|
||||
headers['content-type'] = 'application/json'
|
||||
data = json.dumps({'longUrl': url})
|
||||
text = utils.web.getUrl(self._gooApi, data=data, headers=headers)
|
||||
googl = json.loads(text)['id']
|
||||
if googl:
|
||||
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:
|
||||
|
@ -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')
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user