Added @most <popular|authored|recent> (RFE #829830)

This commit is contained in:
James Vega 2003-11-06 17:24:49 +00:00
parent 6ea38aeffc
commit a55193ea62
2 changed files with 72 additions and 4 deletions

View File

@ -68,10 +68,6 @@ def configure(onStart, afterConnect, advanced):
from questions import expect, anything, something, yn
onStart.append('load MoobotFactoids')
example = utils.wrapLines("""
Add an example IRC session using this module here.
""")
allchars = string.maketrans('', '')
class OptionList(object):
@ -474,6 +470,55 @@ class MoobotFactoids(callbacks.PrivmsgCommandAndRegexp):
"""
self._lock(irc, msg, args, False)
def most(self, irc, msg, args):
"""<popular|authored|recent>
Lists the most <popular|authored|recent> factoids. <popular> list the
most frequently requested factoids. <authored> lists the author with
the most factoids. <recent> lists the most recently created factoids.
"""
key = privmsgs.getArgs(args,needed=1)
key = key.lower()
if key == 'popular':
key = 'requested_count'
cursor = self.db.cursor()
cursor.execute("""SELECT key,%s FROM factoids WHERE requested_count
> 0 ORDER by %s DESC LIMIT 10""" % (key, key))
if cursor.rowcount == 0:
irc.reply(msg, 'I can\'t find any factoids.')
else:
popular = ['%s (%s)' % (t[0], t[1]) for t in cursor.fetchall()]
l = len(popular)
irc.reply(msg, 'Top %s %s: %s' % (l, utils.pluralize(l,
'factoid'), utils.commaAndify(popular)))
elif key == 'authored':
key = 'created_by'
cursor = self.db.cursor()
cursor.execute("""SELECT count(key),%s FROM factoids GROUP BY %s
ORDER BY %s DESC LIMIT 10""" % (key, key, key))
if cursor.rowcount == 0:
irc.reply(msg, 'I can\'t find any factoids.')
else:
author = ['%s (%s)' % (ircdb.users.getUser(t[1]).name, t[0])
for t in cursor.fetchall()]
l = len(author)
irc.reply(msg, 'Top %s %s: %s' % (l, utils.pluralize(l,
'author'), utils.commaAndify(author)))
elif key == 'recent':
key = 'created_at'
cursor = self.db.cursor()
cursor.execute("""SELECT key FROM factoids ORDER by %s DESC LIMIT
10""" % key)
if cursor.rowcount == 0:
irc.reply(msg, 'I can\'t find any factoids.')
else:
recent = ['%s' % t[0] for t in cursor.fetchall()]
l = len(recent)
irc.reply(msg, '%s latest %s: %s' % (l, utils.pluralize(l,
'factoid'), utils.commaAndify(recent)))
else:
raise callbacks.ArgumentError
def listauth(self, irc, msg, args):
"""<author name>

View File

@ -151,6 +151,29 @@ if sqlite is not None:
self.assertActionRegexp('moo', '^(moos|woofs)$')
self.assertError('moo =~ s/moo/')
def testMost(self):
# Check an empty database
self.assertResponse('most popular', 'I can\'t find any factoids.')
self.assertResponse('most authored', 'I can\'t find any factoids.')
self.assertResponse('most recent', 'I can\'t find any factoids.')
# Check singularity response
self.assertNotError('moogle is <reply>moo')
self.assertResponse('most popular', 'I can\'t find any factoids.')
self.assertResponse('most authored', 'Top 1 author: tester (1)')
self.assertResponse('most recent', '1 latest factoid: moogle')
self.assertResponse('moogle', 'moo')
self.assertResponse('most popular', 'Top 1 factoid: moogle (1)')
# Check plural response
self.assertNotError('mogle is <reply>mo')
self.assertResponse('most authored', 'Top 1 author: tester (2)')
self.assertResponse('most recent', '2 latest factoids: mogle and '\
'moogle')
self.assertResponse('moogle', 'moo')
self.assertResponse('most popular', 'Top 1 factoid: moogle (2)')
self.assertResponse('mogle', 'mo')
self.assertResponse('most popular', 'Top 2 factoids: moogle (2) '\
'and mogle (1)')
def testListkeys(self):
self.assertResponse('listkeys %', 'No keys matching \'%\' found.')
self.assertNotError('moo is <reply>moo')