mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-02 17:29:22 +01:00
Added the 'list' function, added code to prevent duplicate grabs being added to
the database.
This commit is contained in:
parent
def70e1ff5
commit
6092ce484a
@ -105,20 +105,27 @@ class QuoteGrabs(plugins.ChannelDBHandler,
|
|||||||
WHERE nick=%s
|
WHERE nick=%s
|
||||||
ORDER BY id DESC LIMIT 1""", msg.nick)
|
ORDER BY id DESC LIMIT 1""", msg.nick)
|
||||||
if cursor.rowcount == 0:
|
if cursor.rowcount == 0:
|
||||||
self._grab(msg, irc.prefix)
|
self._grab(irc, msg, irc.prefix)
|
||||||
self._sendGrabMsg(irc, msg)
|
self._sendGrabMsg(irc, msg)
|
||||||
else:
|
else:
|
||||||
last = int(cursor.fetchone()[0])
|
last = int(cursor.fetchone()[0])
|
||||||
elapsed = int(time.time()) - last
|
elapsed = int(time.time()) - last
|
||||||
if random.random()*elapsed > grabTime/2:
|
if random.random()*elapsed > grabTime/2:
|
||||||
self._grab(msg, irc.prefix)
|
self._grab(irc, msg, irc.prefix)
|
||||||
self._sendGrabMsg(irc, msg)
|
self._sendGrabMsg(irc, msg)
|
||||||
|
|
||||||
def _grab(self, msg, addedBy):
|
def _grab(self, irc, msg, addedBy):
|
||||||
channel = msg.args[0]
|
channel = msg.args[0]
|
||||||
db = self.getDb(channel)
|
db = self.getDb(channel)
|
||||||
cursor = db.cursor()
|
cursor = db.cursor()
|
||||||
text = ircmsgs.prettyPrint(msg)
|
text = ircmsgs.prettyPrint(msg)
|
||||||
|
# Check to see if the latest quotegrab is identical
|
||||||
|
cursor.execute("""SELECT quote FROM quotegrabs
|
||||||
|
WHERE nick=%s
|
||||||
|
ORDER BY id DESC LIMIT 1""", msg.nick)
|
||||||
|
if cursor.rowcount != 0:
|
||||||
|
if text == cursor.fetchone()[0]:
|
||||||
|
return
|
||||||
cursor.execute("""INSERT INTO quotegrabs
|
cursor.execute("""INSERT INTO quotegrabs
|
||||||
VALUES (NULL, %s, %s, %s, %s, %s)""",
|
VALUES (NULL, %s, %s, %s, %s, %s)""",
|
||||||
msg.nick, msg.prefix, addedBy, int(time.time()), text)
|
msg.nick, msg.prefix, addedBy, int(time.time()), text)
|
||||||
@ -142,7 +149,7 @@ class QuoteGrabs(plugins.ChannelDBHandler,
|
|||||||
return
|
return
|
||||||
for m in reviter(irc.state.history):
|
for m in reviter(irc.state.history):
|
||||||
if m.command == 'PRIVMSG' and m.nick == nick:
|
if m.command == 'PRIVMSG' and m.nick == nick:
|
||||||
self._grab(m, msg.prefix)
|
self._grab(irc, m, msg.prefix)
|
||||||
irc.reply(msg, conf.replySuccess)
|
irc.reply(msg, conf.replySuccess)
|
||||||
return
|
return
|
||||||
irc.error(msg, 'I couldn\'t find a proper message to grab.')
|
irc.error(msg, 'I couldn\'t find a proper message to grab.')
|
||||||
@ -166,6 +173,33 @@ class QuoteGrabs(plugins.ChannelDBHandler,
|
|||||||
text = cursor.fetchone()[0]
|
text = cursor.fetchone()[0]
|
||||||
irc.reply(msg, text)
|
irc.reply(msg, text)
|
||||||
|
|
||||||
|
def list(self, irc, msg, args):
|
||||||
|
"""<nick>
|
||||||
|
|
||||||
|
Returns a list of shortened quotes that have been grabbed for <nick>
|
||||||
|
as well as the id of each quote. These ids can be used to get the
|
||||||
|
full quote.
|
||||||
|
"""
|
||||||
|
channel = privmsgs.getChannel(msg, args)
|
||||||
|
nick = privmsgs.getArgs(args)
|
||||||
|
db = self.getDb(channel)
|
||||||
|
cursor = db.cursor()
|
||||||
|
cursor.execute("""SELECT id, quote FROM quotegrabs
|
||||||
|
WHERE nick=%s
|
||||||
|
ORDER BY id ASC""", nick)
|
||||||
|
if cursor.rowcount == 0:
|
||||||
|
irc.error(msg, 'I couldn\'t find any quotegrabs for %s' % nick)
|
||||||
|
else:
|
||||||
|
l = []
|
||||||
|
for (id, quote) in cursor.fetchall():
|
||||||
|
# strip the nick from the quote
|
||||||
|
quote = quote.replace('<%s> ' % nick, '', 1)
|
||||||
|
item_str = utils.ellipsisify('#%s: %s' % (id, quote), 50)
|
||||||
|
l.append(item_str)
|
||||||
|
irc.reply(msg, utils.commaAndify(l))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Class = QuoteGrabs
|
Class = QuoteGrabs
|
||||||
|
|
||||||
|
@ -55,6 +55,25 @@ class QuoteGrabsTestCase(ChannelPluginTestCase, PluginDocumentation):
|
|||||||
self.assertNotError('grab foo')
|
self.assertNotError('grab foo')
|
||||||
self.assertResponse('quote foo', '* foo moos')
|
self.assertResponse('quote foo', '* foo moos')
|
||||||
|
|
||||||
|
def testList(self):
|
||||||
|
testPrefix = 'foo!bar@baz'
|
||||||
|
self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'test',
|
||||||
|
prefix=testPrefix))
|
||||||
|
self.assertNotError('grab foo')
|
||||||
|
self.assertResponse('quotegrabs list foo', '#1: test')
|
||||||
|
self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'a' * 80,
|
||||||
|
prefix=testPrefix))
|
||||||
|
self.assertNotError('grab foo')
|
||||||
|
self.assertResponse('quotegrabs list foo', '#1: test and #2: %s...' %\
|
||||||
|
('a'*43)) # 50 - length of "#2: ..."
|
||||||
|
|
||||||
|
def testDuplicateGrabs(self):
|
||||||
|
testPrefix = 'foo!bar@baz'
|
||||||
|
self.irc.feedMsg(ircmsgs.privmsg(self.channel, 'test',
|
||||||
|
prefix=testPrefix))
|
||||||
|
self.assertNotError('grab foo')
|
||||||
|
self.assertNotError('grab foo') # note: NOT an error, still won't dupe
|
||||||
|
self.assertResponse('quotegrabs list foo', '#1: test')
|
||||||
|
|
||||||
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
# vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78:
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user