Updated .seen, added time since to response

master
Matthew Ramina 2013-11-28 21:49:55 -05:00 committed by mutantmonkey
parent b62dc01bd7
commit cbcb25a64b
1 changed files with 35 additions and 11 deletions

View File

@ -7,22 +7,24 @@ Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/ http://inamidst.com/phenny/
""" """
import time, os, shelve import time, os, shelve, datetime
from tools import deprecated from tools import deprecated
@deprecated def f_seen(phenny, input):
def f_seen(self, origin, match, args):
""".seen <nick> - Reports when <nick> was last seen.""" """.seen <nick> - Reports when <nick> was last seen."""
nick = match.group(2).lower() nick = input.group(2).lower()
if not hasattr(self, 'seen'): if not hasattr(phenny, 'seen'):
return self.msg(origin.sender, '?') return phenny.msg(input.sender, '?')
if nick in self.seen: if nick in phenny.seen:
channel, t = self.seen[nick] channel, t = phenny.seen[nick]
dt = timesince(datetime.datetime.utcfromtimestamp(t))
t = time.strftime('%Y-%m-%d %H:%M:%S UTC', time.gmtime(t)) t = time.strftime('%Y-%m-%d %H:%M:%S UTC', time.gmtime(t))
msg = "I last saw %s at %s on %s" % (nick, t, channel) msg = "I last saw %s at %s (%s) on %s" % (nick, t, dt, channel)
self.msg(origin.sender, str(origin.nick) + ': ' + msg) phenny.reply(msg)
else: self.msg(origin.sender, "Sorry, I haven't seen %s around." % nick) else: phenny.reply("Sorry, I haven't seen %s around." % nick)
f_seen.name = 'seen'
f_seen.example = '.seen firespeaker'
f_seen.rule = (['seen'], r'(\S+)') f_seen.rule = (['seen'], r'(\S+)')
@deprecated @deprecated
@ -41,5 +43,27 @@ def f_note(self, origin, match, args):
f_note.rule = r'(.*)' f_note.rule = r'(.*)'
f_note.priority = 'low' f_note.priority = 'low'
def timesince(td):
seconds = int(abs(datetime.datetime.utcnow() - td).total_seconds())
periods = [
('year', 60*60*24*365),
('month', 60*60*24*30),
('day', 60*60*24),
('hour', 60*60),
('minute', 60),
('second', 1)
]
strings = []
for period_name, period_seconds in periods:
if seconds > period_seconds and len(strings) < 2:
period_value, seconds = divmod(seconds, period_seconds)
if period_value == 1:
strings.append("%s %s" % (period_value, period_name))
else:
strings.append("%s %ss" % (period_value, period_name))
return "just now" if len(strings) < 1 else " and ".join(strings) + " ago"
if __name__ == '__main__': if __name__ == '__main__':
print(__doc__.strip()) print(__doc__.strip())