diff --git a/modules/seen.py b/modules/seen.py index e4dabbe..e369dc5 100644 --- a/modules/seen.py +++ b/modules/seen.py @@ -7,22 +7,24 @@ Licensed under the Eiffel Forum License 2. http://inamidst.com/phenny/ """ -import time, os, shelve +import time, os, shelve, datetime from tools import deprecated -@deprecated -def f_seen(self, origin, match, args): +def f_seen(phenny, input): """.seen - Reports when was last seen.""" - nick = match.group(2).lower() - if not hasattr(self, 'seen'): - return self.msg(origin.sender, '?') - if nick in self.seen: - channel, t = self.seen[nick] + nick = input.group(2).lower() + if not hasattr(phenny, 'seen'): + return phenny.msg(input.sender, '?') + if nick in phenny.seen: + channel, t = phenny.seen[nick] + dt = timesince(datetime.datetime.utcfromtimestamp(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) - self.msg(origin.sender, str(origin.nick) + ': ' + msg) - else: self.msg(origin.sender, "Sorry, I haven't seen %s around." % nick) + msg = "I last saw %s at %s (%s) on %s" % (nick, t, dt, channel) + phenny.reply(msg) + 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+)') @deprecated @@ -41,5 +43,27 @@ def f_note(self, origin, match, args): f_note.rule = r'(.*)' 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__': print(__doc__.strip())