2005-02-01 10:09:49 +01:00
|
|
|
###
|
|
|
|
# Copyright (c) 2002-2004, Jeremiah Fincher
|
2012-09-01 16:16:48 +02:00
|
|
|
# Copyright (c) 2009-2010, James McCoy
|
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
|
2014-07-16 07:41:33 +02:00
|
|
|
import sys
|
2013-01-07 20:22:23 +01:00
|
|
|
import time
|
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
|
|
|
|
2013-01-07 20:22:23 +01:00
|
|
|
import supybot.log as log
|
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
|
|
|
|
|
2013-01-07 20:22:23 +01:00
|
|
|
def retry(f):
|
|
|
|
def newf(*args, **kwargs):
|
|
|
|
for x in xrange(0, 3):
|
|
|
|
try:
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
except Exception:
|
|
|
|
log.exception('Shrinking URL failed. Trying again.')
|
|
|
|
time.sleep(1)
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
return newf
|
|
|
|
|
2005-02-09 08:04:04 +01:00
|
|
|
class ShrinkUrl(callbacks.PluginRegexp):
|
2014-11-30 21:07:41 +01:00
|
|
|
"""This plugin features commands to shorten URLs through different services,
|
|
|
|
like tinyurl."""
|
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)
|
2014-01-20 15:49:15 +01:00
|
|
|
except utils.web.Error as e:
|
2005-02-01 10:09:49 +01:00
|
|
|
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
|
|
|
|
2013-01-07 20:22:23 +01:00
|
|
|
@retry
|
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:
|
2014-01-20 15:43:55 +01: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')
|
2014-01-20 15:49:15 +01:00
|
|
|
except ShrinkError as e:
|
2009-10-05 04:08:55 +02:00
|
|
|
irc.error(str(e))
|
2015-01-24 19:14:41 +01:00
|
|
|
ln = thread(wrap(ln, ['httpUrl']))
|
2005-02-01 10:09:49 +01:00
|
|
|
|
2013-01-07 20:22:23 +01:00
|
|
|
@retry
|
2005-02-01 10:09:49 +01:00
|
|
|
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'):
|
2014-01-20 15:43:55 +01:00
|
|
|
raise ShrinkError(text[5:])
|
2009-11-05 04:37:53 +01:00
|
|
|
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')
|
2014-01-20 15:49:15 +01:00
|
|
|
except ShrinkError as e:
|
2009-10-05 04:08:55 +02:00
|
|
|
irc.errorPossibleBug(str(e))
|
2015-01-24 19:14:41 +01:00
|
|
|
tiny = thread(wrap(tiny, ['httpUrl']))
|
2005-02-01 10:09:49 +01:00
|
|
|
|
2012-05-28 19:58:15 +02:00
|
|
|
_gooApi = 'https://www.googleapis.com/urlshortener/v1/url'
|
2013-01-07 20:22:23 +01:00
|
|
|
@retry
|
2012-05-28 19:58:15 +02:00
|
|
|
def _getGooUrl(self, url):
|
|
|
|
try:
|
|
|
|
return self.db.get('goo', url)
|
|
|
|
except KeyError:
|
2012-05-28 19:58:15 +02:00
|
|
|
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)
|
2014-07-16 07:41:33 +02:00
|
|
|
if sys.version_info[0] >= 3 and isinstance(text, bytes):
|
|
|
|
text = text.decode()
|
2012-05-28 19:58:15 +02:00
|
|
|
googl = json.loads(text)['id']
|
|
|
|
if googl:
|
2012-05-28 19:58:15 +02:00
|
|
|
self.db.set('goo', url, googl)
|
|
|
|
return googl
|
|
|
|
else:
|
2014-01-20 15:43:55 +01:00
|
|
|
raise ShrinkError(text)
|
2012-05-28 19:58:15 +02:00
|
|
|
|
|
|
|
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')
|
2014-01-20 15:49:15 +01:00
|
|
|
except ShrinkError as e:
|
2012-05-28 19:58:15 +02:00
|
|
|
irc.error(str(e))
|
2015-01-24 19:14:41 +01:00
|
|
|
goo = thread(wrap(goo, ['httpUrl']))
|
2012-05-28 19:58:15 +02:00
|
|
|
|
2012-08-11 11:07:40 +02:00
|
|
|
_ur1Api = 'http://ur1.ca/'
|
|
|
|
_ur1Regexp = re.compile(r'<a href="(?P<url>[^"]+)">')
|
2013-01-07 20:22:23 +01:00
|
|
|
@retry
|
2012-08-11 11:07:40 +02:00
|
|
|
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')
|
2013-08-24 11:28:29 +02:00
|
|
|
if ur1ca:
|
2012-08-11 11:07:40 +02:00
|
|
|
self.db.set('ur1', url, ur1ca)
|
|
|
|
return ur1ca
|
|
|
|
else:
|
2014-03-05 14:14:36 +01:00
|
|
|
raise ShrinkError(response)
|
2012-08-11 11:07:40 +02:00
|
|
|
|
|
|
|
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')
|
2014-01-20 15:49:15 +01:00
|
|
|
except ShrinkError as e:
|
2012-08-11 11:07:40 +02:00
|
|
|
irc.error(str(e))
|
2015-01-24 19:14:41 +01:00
|
|
|
ur1 = thread(wrap(ur1, ['httpUrl']))
|
2012-08-11 11:07:40 +02:00
|
|
|
|
2009-10-09 04:58:57 +02:00
|
|
|
_x0Api = 'http://api.x0.no/?%s'
|
2013-01-07 20:22:23 +01:00
|
|
|
@retry
|
2009-10-09 04:58:57 +02:00
|
|
|
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:'):
|
2014-01-20 15:43:55 +01:00
|
|
|
raise ShrinkError(text[6:])
|
2009-10-09 04:58:57 +02:00
|
|
|
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')
|
2014-01-20 15:49:15 +01:00
|
|
|
except ShrinkError as e:
|
2009-10-09 04:58:57 +02:00
|
|
|
irc.error(str(e))
|
2015-01-24 19:14:41 +01:00
|
|
|
x0 = thread(wrap(x0, ['httpUrl']))
|
2009-10-09 04:58:57 +02:00
|
|
|
|
2013-01-07 20:22:23 +01:00
|
|
|
@retry
|
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')
|
2014-01-20 15:49:15 +01:00
|
|
|
except ShrinkError as e:
|
2012-09-02 10:06:33 +02:00
|
|
|
irc.error(str(e))
|
2015-01-24 19:14:41 +01:00
|
|
|
expand = thread(wrap(expand, ['httpUrl']))
|
2012-09-02 10:06:33 +02:00
|
|
|
|
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:
|