diff --git a/plugins/MoobotFactoids.py b/plugins/MoobotFactoids.py index 999217424..fe8dc0ba7 100644 --- a/plugins/MoobotFactoids.py +++ b/plugins/MoobotFactoids.py @@ -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): + """ + + Lists the most factoids. list the + most frequently requested factoids. lists the author with + the most factoids. 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): """ diff --git a/test/test_MoobotFactoids.py b/test/test_MoobotFactoids.py index bb24a739f..e7332776a 100644 --- a/test/test_MoobotFactoids.py +++ b/test/test_MoobotFactoids.py @@ -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 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 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 moo')