2005-02-01 10:09:49 +01:00
|
|
|
###
|
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
2010-07-11 15:48:16 +02:00
|
|
|
# Copyright (c) 2009-2010, James Vega
|
2005-02-01 10:09:49 +01:00
|
|
|
# All rights reserved.
|
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
# modification, are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# * Redistributions of source code must retain the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer.
|
|
|
|
# * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
# this list of conditions, and the following disclaimer in the
|
|
|
|
# documentation and/or other materials provided with the distribution.
|
|
|
|
# * Neither the name of the author of this software nor the name of
|
|
|
|
# contributors to this software may be used to endorse or promote products
|
|
|
|
# derived from this software without specific prior written consent.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
# POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
###
|
|
|
|
|
|
|
|
import re
|
2012-05-28 19:58:15 +02:00
|
|
|
import json
|
2012-08-11 11:07:40 +02:00
|
|
|
import urllib
|
2005-02-01 10:09:49 +01:00
|
|
|
|
|
|
|
import supybot.conf as conf
|
|
|
|
import supybot.utils as utils
|
|
|
|
from supybot.commands import *
|
|
|
|
import supybot.ircmsgs as ircmsgs
|
|
|
|
import supybot.plugins as plugins
|
|
|
|
import supybot.ircutils as ircutils
|
|
|
|
import supybot.callbacks as callbacks
|
2010-10-20 09:10:03 +02:00
|
|
|
from supybot.i18n import PluginInternationalization, internationalizeDocstring
|
|
|
|
_ = PluginInternationalization('ShrinkUrl')
|
2005-02-01 10:09:49 +01:00
|
|
|
|
|
|
|
class CdbShrunkenUrlDB(object):
|
|
|
|
def __init__(self, filename):
|
2009-09-30 02:11:47 +02:00
|
|
|
self.dbs = {}
|
|
|
|
cdb = conf.supybot.databases.types.cdb
|
2012-09-02 10:06:33 +02:00
|
|
|
def register_service(service):
|
2009-09-30 02:11:47 +02:00
|
|
|
dbname = filename.replace('.db', service.capitalize() + '.db')
|
|
|
|
self.dbs[service] = cdb.connect(dbname)
|
2012-09-02 10:06:33 +02:00
|
|
|
for service in conf.supybot.plugins.ShrinkUrl.default.validStrings:
|
|
|
|
register_service(service)
|
|
|
|
register_service('Expand')
|
2005-02-01 10:09:49 +01:00
|
|
|
|
2009-09-30 02:11:47 +02:00
|
|
|
def get(self, service, url):
|
|
|
|
return self.dbs[service][url]
|
2005-02-01 10:09:49 +01:00
|
|
|
|
2009-09-30 02:11:47 +02:00
|
|
|
def set(self, service, url, shrunkurl):
|
|
|
|
self.dbs[service][url] = shrunkurl
|
2005-02-01 10:09:49 +01:00
|
|
|
|
|
|
|
def close(self):
|
2009-09-30 02:11:47 +02:00
|
|
|
for service in self.dbs:
|
|
|
|
self.dbs[service].close()
|
2005-02-01 10:09:49 +01:00
|
|
|
|
|
|
|
def flush(self):
|
2009-09-30 02:11:47 +02:00
|
|
|
for service in self.dbs:
|
|
|
|
self.dbs[service].flush()
|
2005-02-01 10:09:49 +01:00
|
|
|
|
|
|
|
ShrunkenUrlDB = plugins.DB('ShrinkUrl', {'cdb': CdbShrunkenUrlDB})
|
|
|
|
|
2009-10-05 04:08:55 +02:00
|
|
|
class ShrinkError(Exception):
|
|
|
|
pass
|
|
|
|
|
2005-02-09 08:04:04 +01:00
|
|
|
class ShrinkUrl(callbacks.PluginRegexp):
|
2005-02-01 10:09:49 +01:00
|
|
|
regexps = ['shrinkSnarfer']
|
|
|
|
def __init__(self, irc):
|
|
|
|
self.__parent = super(ShrinkUrl, self)
|
|
|
|
self.__parent.__init__(irc)
|
|
|
|
self.db = ShrunkenUrlDB()
|
|
|
|
|
|
|
|
def die(self):
|
|
|
|
self.db.close()
|
|
|
|
|
2005-02-17 23:39:44 +01:00
|
|
|
def callCommand(self, command, irc, msg, *args, **kwargs):
|
2005-02-01 10:09:49 +01:00
|
|
|
try:
|
2005-02-17 23:39:44 +01:00
|
|
|
self.__parent.callCommand(command, irc, msg, *args, **kwargs)
|
2005-02-01 10:09:49 +01:00
|
|
|
except utils.web.Error, e:
|
|
|
|
irc.error(str(e))
|
|
|
|
|
|
|
|
def _outFilterThread(self, irc, msg):
|
|
|
|
(channel, text) = msg.args
|
|
|
|
for m in utils.web.httpUrlRe.finditer(text):
|
|
|
|
url = m.group(1)
|
|
|
|
if len(url) > self.registryValue('minimumLength', channel):
|
2010-07-11 15:48:16 +02:00
|
|
|
try:
|
|
|
|
cmd = self.registryValue('serviceRotation',
|
|
|
|
channel, value=False)
|
|
|
|
cmd = cmd.getService().capitalize()
|
|
|
|
except ValueError:
|
|
|
|
cmd = self.registryValue('default', channel).capitalize()
|
2005-02-01 10:09:49 +01:00
|
|
|
try:
|
2009-10-05 03:54:20 +02:00
|
|
|
shortUrl = getattr(self, '_get%sUrl' % cmd)(url)
|
2005-02-01 10:09:49 +01:00
|
|
|
text = text.replace(url, shortUrl)
|
2009-10-05 04:08:55 +02:00
|
|
|
except (utils.web.Error, AttributeError, ShrinkError):
|
2005-02-01 10:09:49 +01:00
|
|
|
pass
|
|
|
|
newMsg = ircmsgs.privmsg(channel, text, msg=msg)
|
|
|
|
newMsg.tag('shrunken')
|
|
|
|
irc.queueMsg(newMsg)
|
|
|
|
|
|
|
|
def outFilter(self, irc, msg):
|
|
|
|
channel = msg.args[0]
|
|
|
|
if msg.command == 'PRIVMSG' and irc.isChannel(channel):
|
|
|
|
if not msg.shrunken:
|
|
|
|
if self.registryValue('outFilter', channel):
|
|
|
|
if utils.web.httpUrlRe.search(msg.args[1]):
|
|
|
|
self._outFilterThread(irc, msg)
|
|
|
|
return None
|
|
|
|
return msg
|
|
|
|
|
|
|
|
def shrinkSnarfer(self, irc, msg, match):
|
|
|
|
channel = msg.args[0]
|
|
|
|
if not irc.isChannel(channel):
|
|
|
|
return
|
|
|
|
if self.registryValue('shrinkSnarfer', channel):
|
|
|
|
url = match.group(0)
|
|
|
|
r = self.registryValue('nonSnarfingRegexp', channel)
|
|
|
|
if r and r.search(url) is not None:
|
2005-02-01 10:41:39 +01:00
|
|
|
self.log.debug('Matched nonSnarfingRegexp: %u', url)
|
2005-02-01 10:09:49 +01:00
|
|
|
return
|
|
|
|
minlen = self.registryValue('minimumLength', channel)
|
2010-07-11 15:48:16 +02:00
|
|
|
try:
|
|
|
|
cmd = self.registryValue('serviceRotation',
|
|
|
|
channel, value=False)
|
|
|
|
cmd = cmd.getService().capitalize()
|
|
|
|
except ValueError:
|
|
|
|
cmd = self.registryValue('default', channel).capitalize()
|
2005-02-01 10:09:49 +01:00
|
|
|
if len(url) >= minlen:
|
2009-10-05 03:54:20 +02:00
|
|
|
try:
|
|
|
|
shorturl = getattr(self, '_get%sUrl' % cmd)(url)
|
2009-10-05 04:08:55 +02:00
|
|
|
except (utils.web.Error, AttributeError, ShrinkError):
|
2005-02-01 10:41:39 +01:00
|
|
|
self.log.info('Couldn\'t get shorturl for %u', url)
|
2005-02-01 10:09:49 +01:00
|
|
|
return
|
2009-03-08 05:46:50 +01:00
|
|
|
if self.registryValue('shrinkSnarfer.showDomain', channel):
|
|
|
|
domain = ' (at %s)' % utils.web.getDomain(url)
|
|
|
|
else:
|
|
|
|
domain = ''
|
2005-03-10 00:29:49 +01:00
|
|
|
if self.registryValue('bold'):
|
2009-03-08 05:46:50 +01:00
|
|
|
s = format('%u%s', ircutils.bold(shorturl), domain)
|
2005-03-10 00:29:49 +01:00
|
|
|
else:
|
2009-03-08 05:46:50 +01:00
|
|
|
s = format('%u%s', shorturl, domain)
|
2005-06-01 23:08:30 +02:00
|
|
|
m = irc.reply(s, prefixNick=False)
|
2009-10-05 03:54:20 +02:00
|
|
|
if m is not None:
|
|
|
|
m.tag('shrunken')
|
2005-02-01 10:09:49 +01:00
|
|
|
shrinkSnarfer = urlSnarfer(shrinkSnarfer)
|
2009-10-16 03:56:26 +02:00
|
|
|
shrinkSnarfer.__doc__ = utils.web._httpUrlRe
|
2005-02-01 10:09:49 +01:00
|
|
|
|
|
|
|
def _getLnUrl(self, url):
|
|
|
|
url = utils.web.urlquote(url)
|
|
|
|
try:
|
2009-10-05 04:08:55 +02:00
|
|
|
return self.db.get('ln', url)
|
2005-02-01 10:09:49 +01:00
|
|
|
except KeyError:
|
|
|
|
text = utils.web.getUrl('http://ln-s.net/home/api.jsp?url=' + url)
|
2012-08-05 16:58:09 +02:00
|
|
|
text = text.decode()
|
2009-10-05 04:08:55 +02:00
|
|
|
(code, text) = text.split(None, 1)
|
|
|
|
text = text.strip()
|
2005-02-01 10:09:49 +01:00
|
|
|
if code == '200':
|
2009-10-05 04:08:55 +02:00
|
|
|
self.db.set('ln', url, text)
|
|
|
|
return text
|
2005-02-01 10:09:49 +01:00
|
|
|
else:
|
2009-10-05 04:08:55 +02:00
|
|
|
raise ShrinkError, text
|
2005-02-01 10:09:49 +01:00
|
|
|
|
2010-10-20 09:10:03 +02:00
|
|
|
@internationalizeDocstring
|
2005-02-01 10:09:49 +01:00
|
|
|
def ln(self, irc, msg, args, url):
|
|
|
|
"""<url>
|
|
|
|
|
|
|
|
Returns an ln-s.net version of <url>.
|
|
|
|
"""
|
2009-10-05 04:08:55 +02:00
|
|
|
try:
|
|
|
|
lnurl = self._getLnUrl(url)
|
2005-02-01 10:09:49 +01:00
|
|
|
m = irc.reply(lnurl)
|
2009-10-05 03:54:20 +02:00
|
|
|
if m is not None:
|
|
|
|
m.tag('shrunken')
|
2009-10-05 04:08:55 +02:00
|
|
|
except ShrinkError, e:
|
|
|
|
irc.error(str(e))
|
2005-02-01 10:09:49 +01:00
|
|
|
ln = thread(wrap(ln, ['url']))
|
|
|
|
|
|
|
|
def _getTinyUrl(self, url):
|
|
|
|
try:
|
2009-09-30 02:11:47 +02:00
|
|
|
return self.db.get('tiny', url)
|
2005-02-01 10:09:49 +01:00
|
|
|
except KeyError:
|
2009-11-05 04:37:53 +01:00
|
|
|
text = utils.web.getUrl('http://tinyurl.com/api-create.php?url=' + url)
|
2012-08-05 16:58:09 +02:00
|
|
|
text = text.decode()
|
2009-11-05 04:37:53 +01:00
|
|
|
if text.startswith('Error'):
|
|
|
|
raise ShrinkError, text[5:]
|
|
|
|
self.db.set('tiny', url, text)
|
|
|
|
return text
|
2005-02-01 10:09:49 +01:00
|
|
|
|
2010-10-20 09:10:03 +02:00
|
|
|
@internationalizeDocstring
|
2005-02-01 10:09:49 +01:00
|
|
|
def tiny(self, irc, msg, args, url):
|
|
|
|
"""<url>
|
|
|
|
|
|
|
|
Returns a TinyURL.com version of <url>
|
|
|
|
"""
|
2009-10-05 04:08:55 +02:00
|
|
|
try:
|
|
|
|
tinyurl = self._getTinyUrl(url)
|
2005-02-01 10:09:49 +01:00
|
|
|
m = irc.reply(tinyurl)
|
2007-06-14 14:43:09 +02:00
|
|
|
if m is not None:
|
|
|
|
m.tag('shrunken')
|
2009-10-05 04:08:55 +02:00
|
|
|
except ShrinkError, e:
|
|
|
|
irc.errorPossibleBug(str(e))
|
2005-02-01 10:09:49 +01:00
|
|
|
tiny = thread(wrap(tiny, ['url']))
|
|
|
|
|
2009-10-05 03:06:48 +02:00
|
|
|
_xrlApi = 'http://metamark.net/api/rest/simple'
|
|
|
|
def _getXrlUrl(self, url):
|
|
|
|
quotedurl = utils.web.urlquote(url)
|
|
|
|
try:
|
|
|
|
return self.db.get('xrl', quotedurl)
|
|
|
|
except KeyError:
|
|
|
|
data = utils.web.urlencode({'long_url': url})
|
2012-08-05 16:58:09 +02:00
|
|
|
text = utils.web.getUrl(self._xrlApi, data=data).decode()
|
2009-10-05 03:06:48 +02:00
|
|
|
if text.startswith('ERROR:'):
|
|
|
|
raise ShrinkError, text[6:]
|
|
|
|
self.db.set('xrl', quotedurl, text)
|
|
|
|
return text
|
|
|
|
|
2010-10-20 09:10:03 +02:00
|
|
|
@internationalizeDocstring
|
2009-10-05 03:06:48 +02:00
|
|
|
def xrl(self, irc, msg, args, url):
|
|
|
|
"""<url>
|
|
|
|
|
|
|
|
Returns an xrl.us version of <url>.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
xrlurl = self._getXrlUrl(url)
|
|
|
|
m = irc.reply(xrlurl)
|
|
|
|
if m is not None:
|
|
|
|
m.tag('shrunken')
|
|
|
|
except ShrinkError, e:
|
|
|
|
irc.error(str(e))
|
|
|
|
xrl = thread(wrap(xrl, ['url']))
|
2005-02-01 10:09:49 +01:00
|
|
|
|
2012-05-28 19:58:15 +02:00
|
|
|
_gooApi = 'https://www.googleapis.com/urlshortener/v1/url'
|
|
|
|
def _getGooUrl(self, url):
|
|
|
|
try:
|
|
|
|
return self.db.get('goo', url)
|
|
|
|
except KeyError:
|
2012-08-04 21:33:58 +02:00
|
|
|
text = utils.web.getUrl(self._gooApi,
|
2012-05-28 19:58:15 +02:00
|
|
|
headers={'content-type':'application/json'},
|
2012-08-05 16:58:09 +02:00
|
|
|
data=json.dumps({'longUrl': url}).encode())
|
|
|
|
googl = json.loads(text.decode())['id']
|
2012-05-28 19:58:15 +02:00
|
|
|
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']))
|
|
|
|
|
2012-08-11 11:07:40 +02:00
|
|
|
_ur1Api = 'http://ur1.ca/'
|
|
|
|
_ur1Regexp = re.compile(r'<a href="(?P<url>[^"]+)">')
|
|
|
|
def _getUr1Url(self, url):
|
|
|
|
try:
|
|
|
|
return self.db.get('ur1ca', utils.web.urlquote(url))
|
|
|
|
except KeyError:
|
|
|
|
parameters = utils.web.urlencode({'longurl': url})
|
|
|
|
response = utils.web.getUrl(self._ur1Api, data=parameters)
|
|
|
|
ur1ca = self._ur1Regexp.search(response.decode()).group('url')
|
|
|
|
if len(ur1ca) > 0 :
|
|
|
|
self.db.set('ur1', url, ur1ca)
|
|
|
|
return ur1ca
|
|
|
|
else:
|
|
|
|
raise ShrinkError, text
|
|
|
|
|
|
|
|
def ur1(self, irc, msg, args, url):
|
|
|
|
"""<url>
|
|
|
|
|
|
|
|
Returns an ur1 version of <url>.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
ur1url = self._getUr1Url(url)
|
|
|
|
m = irc.reply(ur1url)
|
|
|
|
if m is not None:
|
|
|
|
m.tag('shrunken')
|
|
|
|
except ShrinkError, e:
|
|
|
|
irc.error(str(e))
|
|
|
|
ur1 = thread(wrap(ur1, ['url']))
|
|
|
|
|
2009-10-09 04:58:57 +02:00
|
|
|
_x0Api = 'http://api.x0.no/?%s'
|
|
|
|
def _getX0Url(self, url):
|
|
|
|
try:
|
|
|
|
return self.db.get('x0', url)
|
|
|
|
except KeyError:
|
2012-08-05 16:58:09 +02:00
|
|
|
text = utils.web.getUrl(self._x0Api % url).decode()
|
2009-10-09 04:58:57 +02:00
|
|
|
if text.startswith('ERROR:'):
|
|
|
|
raise ShrinkError, text[6:]
|
|
|
|
self.db.set('x0', url, text)
|
|
|
|
return text
|
|
|
|
|
2010-10-20 09:10:03 +02:00
|
|
|
@internationalizeDocstring
|
2009-10-09 04:58:57 +02:00
|
|
|
def x0(self, irc, msg, args, url):
|
|
|
|
"""<url>
|
|
|
|
|
|
|
|
Returns an x0.no version of <url>.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
x0url = self._getX0Url(url)
|
|
|
|
m = irc.reply(x0url)
|
|
|
|
if m is not None:
|
|
|
|
m.tag('shrunken')
|
|
|
|
except ShrinkError, e:
|
|
|
|
irc.error(str(e))
|
|
|
|
x0 = thread(wrap(x0, ['url']))
|
|
|
|
|
2012-09-02 10:06:33 +02:00
|
|
|
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']))
|
|
|
|
|
2005-02-01 10:09:49 +01:00
|
|
|
Class = ShrinkUrl
|
|
|
|
|
2006-02-11 16:52:51 +01:00
|
|
|
# vim:set shiftwidth=4 softtabstop=4 expandtab textwidth=79:
|