Added tinyurl stuff

This commit is contained in:
James Vega 2003-11-03 05:39:14 +00:00
parent 6b4d03fecc
commit bf12760108
2 changed files with 90 additions and 2 deletions

View File

@ -41,11 +41,13 @@ import os
import re import re
import time import time
import getopt import getopt
import urllib2
import urlparse import urlparse
import sqlite import sqlite
import conf import conf
import debug
import utils import utils
import ircmsgs import ircmsgs
import privmsgs import privmsgs
@ -80,11 +82,16 @@ def configure(onStart, afterConnect, advanced):
from questions import expect, anything, something, yn from questions import expect, anything, something, yn
onStart.append('load URLSnarfer') 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): def __init__(self):
self.nextMsgs = {} self.nextMsgs = {}
callbacks.Privmsg.__init__(self) callbacks.Privmsg.__init__(self)
plugins.ChannelDBHandler.__init__(self) plugins.ChannelDBHandler.__init__(self)
plugins.Toggleable.__init__(self)
def makeDb(self, filename): def makeDb(self, filename):
if os.path.exists(filename): if os.path.exists(filename):
@ -103,6 +110,11 @@ class URLSnarfer(plugins.ChannelDBHandler, callbacks.Privmsg):
site TEXT, site TEXT,
filename TEXT filename TEXT
)""") )""")
cursor.execute("""CREATE TABLE tinyurls (
id INTEGER PRIMARY KEY,
url_id INTEGER,
tinyurl TEXT
)""")
db.commit() db.commit()
return db return db
@ -134,10 +146,41 @@ class URLSnarfer(plugins.ChannelDBHandler, callbacks.Privmsg):
(NULL, %s, %s, %s, %s, %s, '', %s, %s, %s)""", (NULL, %s, %s, %s, %s, %s, '', %s, %s, %s)""",
url, added, addedBy, msg.args[1], previousMsg, url, added, addedBy, msg.args[1], previousMsg,
protocol, site, filename) 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) key = (msg.nick, channel)
self.nextMsgs.setdefault(key, []).append((url, added)) self.nextMsgs.setdefault(key, []).append((url, added))
db.commit() 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): def _formatUrl(self, url, added, addedBy):
#debug.printf((url, added, addedBy)) #debug.printf((url, added, addedBy))
when = time.strftime(conf.humanTimestampFormat, when = time.strftime(conf.humanTimestampFormat,
@ -166,6 +209,22 @@ class URLSnarfer(plugins.ChannelDBHandler, callbacks.Privmsg):
else: else:
irc.reply(msg, self._formatUrlWithId(*cursor.fetchone())) 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): def geturl(self, irc, msg, args):
"""[<channel>] <id> """[<channel>] <id>

View File

@ -75,6 +75,8 @@ if sqlite is not None:
class URLSnarferTestCase(ChannelPluginTestCase, PluginDocumentation): class URLSnarferTestCase(ChannelPluginTestCase, PluginDocumentation):
plugins = ('URLSnarfer',) plugins = ('URLSnarfer',)
def test(self): def test(self):
self.assertNotError('toggle tinyreply off')
self.assertNotError('toggle tinysnarf off')
counter = 0 counter = 0
self.assertNotError('randomurl') self.assertNotError('randomurl')
for url in urls: for url in urls:
@ -92,14 +94,41 @@ if sqlite is not None:
self.assertNotError('randomurl') self.assertNotError('randomurl')
def testDefaultNotFancy(self): def testDefaultNotFancy(self):
self.assertNotError('toggle tinyreply off')
self.assertNotError('toggle tinysnarf off')
self.feedMsg(urls[0]) self.feedMsg(urls[0])
self.assertResponse('lasturl', urls[0]) self.assertResponse('lasturl', urls[0])
def testAction(self): def testAction(self):
self.assertNotError('toggle tinyreply off')
self.assertNotError('toggle tinysnarf off')
self.irc.feedMsg(ircmsgs.action(self.channel, urls[1])) self.irc.feedMsg(ircmsgs.action(self.channel, urls[1]))
self.assertNotRegexp('lasturl', '\\x01') 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: # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: