mirror of
https://github.com/Mikaela/Limnoria.git
synced 2025-01-26 20:14:20 +01:00
Added more tests and the lasturls command (and --nolimit option to the lasturl command.
This commit is contained in:
parent
376e4043f0
commit
37f88d5e21
@ -132,6 +132,16 @@ class URLSnarfer(callbacks.Privmsg, ChannelDBHandler):
|
|||||||
self.nextMsgs.setdefault(key, []).append((url, added))
|
self.nextMsgs.setdefault(key, []).append((url, added))
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
|
def _formatUrl(self, url, added, addedBy):
|
||||||
|
#debug.printf((url, added, addedBy))
|
||||||
|
when = time.strftime(conf.humanTimestampFormat,
|
||||||
|
time.localtime(int(added)))
|
||||||
|
return '<%s> (added by %s at %s)' % (url, addedBy, when)
|
||||||
|
|
||||||
|
def _formatUrlWithId(self, id, url, added, addedBy):
|
||||||
|
#debug.printf((id, url, added, addedBy))
|
||||||
|
return '#%s: %s' % (id, self._formatUrl(url, added, addedBy))
|
||||||
|
|
||||||
def randomurl(self, irc, msg, args):
|
def randomurl(self, irc, msg, args):
|
||||||
"""[<channel>]
|
"""[<channel>]
|
||||||
|
|
||||||
@ -148,11 +158,25 @@ class URLSnarfer(callbacks.Privmsg, ChannelDBHandler):
|
|||||||
if cursor.rowcount == 0:
|
if cursor.rowcount == 0:
|
||||||
irc.reply(msg, 'I have no URLs in my database for %s' % channel)
|
irc.reply(msg, 'I have no URLs in my database for %s' % channel)
|
||||||
else:
|
else:
|
||||||
(id, url, added, addedBy) = cursor.fetchone()
|
irc.reply(msg, self._formatUrlWithId(*cursor.fetchone()))
|
||||||
when = time.strftime(conf.humanTimestampFormat,
|
|
||||||
time.localtime(int(added)))
|
def geturl(self, irc, msg, args):
|
||||||
s = '<%s> (added by %s at %s)' % (url, addedBy, when)
|
"""[<channel>] <id>
|
||||||
irc.reply(msg, s)
|
|
||||||
|
Gets the URL with id <id> from the URL database for <channel>.
|
||||||
|
<channel> is only necessary if not sent in the channel itself.
|
||||||
|
"""
|
||||||
|
channel = privmsgs.getChannel(msg, args)
|
||||||
|
db = self.getDb(channel)
|
||||||
|
cursor = db.cursor()
|
||||||
|
id = privmsgs.getArgs(args)
|
||||||
|
cursor.execute("""SELECT url, added, added_by
|
||||||
|
FROM urls
|
||||||
|
WHERE id=%s""", id)
|
||||||
|
if cursor.rowcount == 0:
|
||||||
|
irc.reply(msg, 'No URL was found with that id.')
|
||||||
|
else:
|
||||||
|
irc.reply(msg, self._formatUrl(*cursor.fetchone()))
|
||||||
|
|
||||||
def numurls(self, irc, msg, args):
|
def numurls(self, irc, msg, args):
|
||||||
"""[<channel>]
|
"""[<channel>]
|
||||||
@ -168,22 +192,31 @@ class URLSnarfer(callbacks.Privmsg, ChannelDBHandler):
|
|||||||
irc.reply(msg, 'I have %s %s in my database.' % \
|
irc.reply(msg, 'I have %s %s in my database.' % \
|
||||||
(count, int(count) == 1 and 'URL' or 'URLs'))
|
(count, int(count) == 1 and 'URL' or 'URLs'))
|
||||||
|
|
||||||
|
def lasturls(self, irc, msg, args):
|
||||||
|
args.append('--nolimit')
|
||||||
|
self.lasturl(irc, msg, args)
|
||||||
|
|
||||||
def lasturl(self, irc, msg, args):
|
def lasturl(self, irc, msg, args):
|
||||||
"""[<channel>] [--{from,with,at,proto,near}=<value>]
|
"""[<channel>] [--{from,with,at,proto,near}=<value>] [--nolimit]
|
||||||
|
|
||||||
Gives the last URL matching the given criteria. --from is from whom
|
Gives the last URL matching the given criteria. --from is from whom
|
||||||
the URL came; --at is the site of the URL; --proto is the protocol the
|
the URL came; --at is the site of the URL; --proto is the protocol the
|
||||||
URL used; --with is something inside the URL; --near is a string in the
|
URL used; --with is something inside the URL; --near is a string in the
|
||||||
messages before and after the link. <channel> is only necessary if the
|
messages before and after the link. If --nolimit is given, returns as
|
||||||
|
many URLs as can fit in the message. <channel> is only necessary if the
|
||||||
message isn't sent in the channel itself.
|
message isn't sent in the channel itself.
|
||||||
"""
|
"""
|
||||||
channel = privmsgs.getChannel(msg, args)
|
channel = privmsgs.getChannel(msg, args)
|
||||||
(optlist, rest) = getopt.getopt(args, '', ['from=', 'with=', 'at=',
|
(optlist, rest) = getopt.getopt(args, '', ['from=', 'with=', 'at=',
|
||||||
'proto=', 'near='])
|
'proto=', 'near=',
|
||||||
|
'nolimit'])
|
||||||
criteria = ['1=1']
|
criteria = ['1=1']
|
||||||
formats = []
|
formats = []
|
||||||
|
nolimit = False
|
||||||
for (option, argument) in optlist:
|
for (option, argument) in optlist:
|
||||||
option = option[2:] # Strip off the --.
|
option = option[2:] # Strip off the --.
|
||||||
|
if option == 'nolimit':
|
||||||
|
nolimit = True
|
||||||
if option == 'from':
|
if option == 'from':
|
||||||
criteria.append('added_by LIKE %s')
|
criteria.append('added_by LIKE %s')
|
||||||
formats.append(argument)
|
formats.append(argument)
|
||||||
@ -214,10 +247,15 @@ class URLSnarfer(callbacks.Privmsg, ChannelDBHandler):
|
|||||||
criterion = ' AND '.join(criteria)
|
criterion = ' AND '.join(criteria)
|
||||||
sql = """SELECT url, added, added_by
|
sql = """SELECT url, added, added_by
|
||||||
FROM urls
|
FROM urls
|
||||||
WHERE %s ORDER BY id DESC""" % criterion
|
WHERE %s ORDER BY id DESC
|
||||||
|
LIMIT 10""" % criterion
|
||||||
cursor.execute(sql, *formats)
|
cursor.execute(sql, *formats)
|
||||||
if cursor.rowcount == 0:
|
if cursor.rowcount == 0:
|
||||||
irc.reply(msg, 'No URLs matched that criteria.')
|
irc.reply(msg, 'No URLs matched that criteria.')
|
||||||
|
else:
|
||||||
|
if nolimit:
|
||||||
|
urls = ['<%s>' % t[0] for t in cursor.fetchall()]
|
||||||
|
s = ircutils.privmsgPayload(urls, ', ', 400)
|
||||||
else:
|
else:
|
||||||
(url, added, added_by) = cursor.fetchone()
|
(url, added, added_by) = cursor.fetchone()
|
||||||
timestamp = time.strftime('%I:%M %p, %B %d, %Y',
|
timestamp = time.strftime('%I:%M %p, %B %d, %Y',
|
||||||
|
@ -76,6 +76,8 @@ class URLSnarferTestCase(ChannelPluginTestCase):
|
|||||||
self.assertRegexp('numurls', str(counter))
|
self.assertRegexp('numurls', str(counter))
|
||||||
self.feedMsg(url)
|
self.feedMsg(url)
|
||||||
counter += 1
|
counter += 1
|
||||||
|
self.assertNotError('geturl %s' % counter)
|
||||||
|
|
||||||
self.assertRegexp('numurls', str(counter))
|
self.assertRegexp('numurls', str(counter))
|
||||||
self.assertRegexp('lasturl', re.escape(urls[-1]))
|
self.assertRegexp('lasturl', re.escape(urls[-1]))
|
||||||
self.assertRegexp('lasturl --proto https', re.escape(urls[-3]))
|
self.assertRegexp('lasturl --proto https', re.escape(urls[-3]))
|
||||||
|
Loading…
Reference in New Issue
Block a user