mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-01-23 02:24:12 +01:00
Added tinyurl stuff
This commit is contained in:
parent
6b4d03fecc
commit
bf12760108
@ -41,11 +41,13 @@ import os
|
||||
import re
|
||||
import time
|
||||
import getopt
|
||||
import urllib2
|
||||
import urlparse
|
||||
|
||||
import sqlite
|
||||
|
||||
import conf
|
||||
import debug
|
||||
import utils
|
||||
import ircmsgs
|
||||
import privmsgs
|
||||
@ -80,11 +82,16 @@ def configure(onStart, afterConnect, advanced):
|
||||
from questions import expect, anything, something, yn
|
||||
onStart.append('load URLSnarfer')
|
||||
|
||||
class URLSnarfer(plugins.ChannelDBHandler, callbacks.Privmsg):
|
||||
class URLSnarfer(plugins.ChannelDBHandler, callbacks.Privmsg,
|
||||
plugins.Toggleable):
|
||||
toggles = plugins.ToggleDictionary({'tinysnarf':True,
|
||||
'tinyreply':True})
|
||||
_maxUrlLen = 46
|
||||
def __init__(self):
|
||||
self.nextMsgs = {}
|
||||
callbacks.Privmsg.__init__(self)
|
||||
plugins.ChannelDBHandler.__init__(self)
|
||||
plugins.Toggleable.__init__(self)
|
||||
|
||||
def makeDb(self, filename):
|
||||
if os.path.exists(filename):
|
||||
@ -103,6 +110,11 @@ class URLSnarfer(plugins.ChannelDBHandler, callbacks.Privmsg):
|
||||
site TEXT,
|
||||
filename TEXT
|
||||
)""")
|
||||
cursor.execute("""CREATE TABLE tinyurls (
|
||||
id INTEGER PRIMARY KEY,
|
||||
url_id INTEGER,
|
||||
tinyurl TEXT
|
||||
)""")
|
||||
db.commit()
|
||||
return db
|
||||
|
||||
@ -134,10 +146,41 @@ class URLSnarfer(plugins.ChannelDBHandler, callbacks.Privmsg):
|
||||
(NULL, %s, %s, %s, %s, %s, '', %s, %s, %s)""",
|
||||
url, added, addedBy, msg.args[1], previousMsg,
|
||||
protocol, site, filename)
|
||||
if self.toggles.get('tinysnarf', channel=msg.args[0]) and\
|
||||
len(url) > self._maxUrlLen:
|
||||
cursor.execute("""SELECT id FROM urls WHERE url=%s AND
|
||||
added=%s AND added_by=%s""", url, added, addedBy)
|
||||
if cursor.rowcount != 0:
|
||||
#debug.printf(url)
|
||||
tinyurl = self._getTinyUrl(url)
|
||||
if tinyurl:
|
||||
id = int(cursor.fetchone()[0])
|
||||
cursor.execute("""INSERT INTO tinyurls VALUES
|
||||
(NULL, %s, %s)""", id, tinyurl)
|
||||
if self.toggles.get('tinyreply', channel=msg.args[0]):
|
||||
irc.queueMsg(callbacks.reply(msg, 'TinyURL: %s' %
|
||||
tinyurl, prefixName=False))
|
||||
key = (msg.nick, channel)
|
||||
self.nextMsgs.setdefault(key, []).append((url, added))
|
||||
db.commit()
|
||||
|
||||
_tinyRe = re.compile(r'23 characters:\n<blockquote>(http://tinyurl.com/\w{4})'\
|
||||
'</blockquote>')
|
||||
def _getTinyUrl(self, url, cmd=False):
|
||||
try:
|
||||
fd = urllib2.urlopen('http://tinyurl.com/create.php?url=%s' % url)
|
||||
s = fd.read()
|
||||
fd.close()
|
||||
m = self._tinyRe.search(s)
|
||||
if m is None:
|
||||
return None
|
||||
return m.group(1)
|
||||
except urllib2.HTTPError, e:
|
||||
if cmd:
|
||||
raise callbacks.Error, e.msg()
|
||||
else:
|
||||
debug.msg(e.msg())
|
||||
|
||||
def _formatUrl(self, url, added, addedBy):
|
||||
#debug.printf((url, added, addedBy))
|
||||
when = time.strftime(conf.humanTimestampFormat,
|
||||
@ -166,6 +209,22 @@ class URLSnarfer(plugins.ChannelDBHandler, callbacks.Privmsg):
|
||||
else:
|
||||
irc.reply(msg, self._formatUrlWithId(*cursor.fetchone()))
|
||||
|
||||
def tinyurl(self, irc, msg, args):
|
||||
"""<url>
|
||||
|
||||
Returns a TinyURL.com version of <url>
|
||||
"""
|
||||
url = privmsgs.getArgs(args)
|
||||
if self.toggles.get('tinysnarf', channel=msg.args[0]) and\
|
||||
self.toggles.get('tinyreply', channel=msg.args[0]):
|
||||
return
|
||||
url = self._getTinyUrl(url)
|
||||
if not url:
|
||||
irc.error(msg, 'Could not parse the TinyURL.com results page. '\
|
||||
'(%s)' % conf.replyPossibleBug)
|
||||
else:
|
||||
irc.reply(msg, url)
|
||||
|
||||
def geturl(self, irc, msg, args):
|
||||
"""[<channel>] <id>
|
||||
|
||||
|
@ -75,6 +75,8 @@ if sqlite is not None:
|
||||
class URLSnarferTestCase(ChannelPluginTestCase, PluginDocumentation):
|
||||
plugins = ('URLSnarfer',)
|
||||
def test(self):
|
||||
self.assertNotError('toggle tinyreply off')
|
||||
self.assertNotError('toggle tinysnarf off')
|
||||
counter = 0
|
||||
self.assertNotError('randomurl')
|
||||
for url in urls:
|
||||
@ -92,14 +94,41 @@ if sqlite is not None:
|
||||
self.assertNotError('randomurl')
|
||||
|
||||
def testDefaultNotFancy(self):
|
||||
self.assertNotError('toggle tinyreply off')
|
||||
self.assertNotError('toggle tinysnarf off')
|
||||
self.feedMsg(urls[0])
|
||||
self.assertResponse('lasturl', urls[0])
|
||||
|
||||
def testAction(self):
|
||||
self.assertNotError('toggle tinyreply off')
|
||||
self.assertNotError('toggle tinysnarf off')
|
||||
self.irc.feedMsg(ircmsgs.action(self.channel, urls[1]))
|
||||
self.assertNotRegexp('lasturl', '\\x01')
|
||||
|
||||
|
||||
def testTinyurl(self):
|
||||
self.assertNotError('toggle tinyreply on')
|
||||
self.assertNotError('toggle tinysnarf off')
|
||||
self.assertRegexp('tinyurl http://sourceforge.net/tracker/?'\
|
||||
'func=add&group_id=58965&atid=489447',
|
||||
r'http://tinyurl.com/\w{4}')
|
||||
self.assertNotError('toggle tinysnarf on')
|
||||
self.assertRegexp('tinyurl http://sourceforge.net/tracker/?'\
|
||||
'func=add&group_id=58965&atid=489447',
|
||||
r'http://tinyurl.com/\w{4}')
|
||||
self.assertNotError('toggle tinyreply off')
|
||||
self.assertRegexp('tinyurl http://sourceforge.net/tracker/?'\
|
||||
'func=add&group_id=58965&atid=489447',
|
||||
r'http://tinyurl.com/\w{4}')
|
||||
|
||||
def testTinysnarf(self):
|
||||
self.assertNotError('toggle tinyreply off')
|
||||
self.assertNotError('toggle tinysnarf on')
|
||||
self.assertNoResponse('http://sourceforge.net/tracker/?'\
|
||||
'func=add&group_id=58965&atid=489447')
|
||||
self.assertNotError('toggle tinyreply on')
|
||||
self.assertRegexp('http://sourceforge.net/tracker/?'\
|
||||
'func=add&group_id=58965&atid=489447',
|
||||
r'TinyURL: http://tinyurl.com/\w{4}')
|
||||
|
||||
|
||||
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||
|
Loading…
Reference in New Issue
Block a user