2008-02-21 07:06:33 -05:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
|
|
|
seen.py - Phenny Seen Module
|
|
|
|
Copyright 2008, Sean B. Palmer, inamidst.com
|
|
|
|
Licensed under the Eiffel Forum License 2.
|
|
|
|
|
|
|
|
http://inamidst.com/phenny/
|
|
|
|
"""
|
|
|
|
|
2013-11-28 21:49:55 -05:00
|
|
|
import time, os, shelve, datetime
|
2008-02-21 07:06:33 -05:00
|
|
|
from tools import deprecated
|
|
|
|
|
2013-11-28 21:49:55 -05:00
|
|
|
def f_seen(phenny, input):
|
2012-01-03 14:09:34 -05:00
|
|
|
""".seen <nick> - Reports when <nick> was last seen."""
|
2013-11-28 21:49:55 -05:00
|
|
|
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))
|
2012-01-03 14:09:34 -05:00
|
|
|
t = time.strftime('%Y-%m-%d %H:%M:%S UTC', time.gmtime(t))
|
|
|
|
|
2013-11-28 21:49:55 -05:00
|
|
|
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'
|
2008-02-21 07:06:33 -05:00
|
|
|
f_seen.rule = (['seen'], r'(\S+)')
|
|
|
|
|
|
|
|
@deprecated
|
|
|
|
def f_note(self, origin, match, args):
|
2012-01-03 14:09:34 -05:00
|
|
|
def note(self, origin, match, args):
|
|
|
|
if not hasattr(self.bot, 'seen'):
|
2013-01-02 13:48:06 -05:00
|
|
|
fn = self.nick + '-' + self.config.host + '.seen'
|
2013-01-01 15:19:06 -05:00
|
|
|
path = os.path.join(os.path.expanduser('~/.phenny'), fn)
|
|
|
|
self.bot.seen = shelve.open(path)
|
2012-01-03 14:09:34 -05:00
|
|
|
if origin.sender.startswith('#'):
|
|
|
|
self.seen[origin.nick.lower()] = (origin.sender, time.time())
|
2013-01-01 15:19:06 -05:00
|
|
|
self.seen.sync()
|
2012-01-03 14:09:34 -05:00
|
|
|
|
|
|
|
try: note(self, origin, match, args)
|
|
|
|
except Exception as e: print(e)
|
2008-02-21 07:06:33 -05:00
|
|
|
f_note.rule = r'(.*)'
|
|
|
|
f_note.priority = 'low'
|
|
|
|
|
2013-11-28 21:49:55 -05:00
|
|
|
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"
|
|
|
|
|
2008-02-21 07:06:33 -05:00
|
|
|
if __name__ == '__main__':
|
2012-01-03 14:09:34 -05:00
|
|
|
print(__doc__.strip())
|