askcompu

Untitled

Jul 21st, 2015
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.63 KB | None | 0 0
  1. #!/usr/bin/env python
  2. """
  3. remind.py - Phenny Reminder Module
  4. Copyright 2011, Sean B. Palmer, inamidst.com
  5. Licensed under the Eiffel Forum License 2.
  6.  
  7. http://inamidst.com/phenny/
  8. """
  9.  
  10. import os, re, time, threading
  11.  
  12. def filename(self):
  13.     name = self.nick + '-' + self.config.host + '.reminders.db'
  14.     return os.path.join(os.path.expanduser('~/.phenny'), name)
  15.  
  16. def load_database(name):
  17.     data = {}
  18.     if os.path.isfile(name):
  19.         f = open(name, 'r')
  20.         for line in f:
  21.             unixtime, channel, nick, message = line.split('\t')
  22.             message = message.rstrip('\n')
  23.             t = int(unixtime)
  24.             reminder = (channel, nick, message)
  25.             try: data[t].append(reminder)
  26.             except KeyError: data[t] = [reminder]
  27.         f.close()
  28.     return data
  29.  
  30. def dump_database(name, data):
  31.     f = open(name, 'w')
  32.     for unixtime, reminders in data.items():
  33.         for channel, nick, message in reminders:
  34.             f.write('%s\t%s\t%s\t%s\n' % (unixtime, channel, nick, message))
  35.     f.close()
  36.  
  37. def setup(phenny):
  38.     phenny.rfn = filename(phenny)
  39.     phenny.rdb = load_database(phenny.rfn)
  40.  
  41.     def monitor(phenny):
  42.         time.sleep(5)
  43.         while True:
  44.             now = int(time.time())
  45.             unixtimes = [int(key) for key in phenny.rdb]
  46.             oldtimes = [t for t in unixtimes if t <= now]
  47.             if oldtimes:
  48.                 for oldtime in oldtimes:
  49.                     for (channel, nick, message) in phenny.rdb[oldtime]:
  50.                         if message:
  51.                             phenny.msg(channel, nick + ': ' + message)
  52.                         else: phenny.msg(channel, nick + '!')
  53.                     del phenny.rdb[oldtime]
  54.                 dump_database(phenny.rfn, phenny.rdb)
  55.             time.sleep(2.5)
  56.  
  57.     targs = (phenny,)
  58.     t = threading.Thread(target=monitor, args=targs)
  59.     t.start()
  60.  
  61. scaling = {
  62.     'years': 365.25 * 24 * 3600,
  63.     'year': 365.25 * 24 * 3600,
  64.     'yrs': 365.25 * 24 * 3600,
  65.     'y': 365.25 * 24 * 3600,
  66.  
  67.     'months': 29.53059 * 24 * 3600,
  68.     'month': 29.53059 * 24 * 3600,
  69.     'mo': 29.53059 * 24 * 3600,
  70.  
  71.     'weeks': 7 * 24 * 3600,
  72.     'week': 7 * 24 * 3600,
  73.     'wks': 7 * 24 * 3600,
  74.     'wk': 7 * 24 * 3600,
  75.     'w': 7 * 24 * 3600,
  76.  
  77.     'days': 24 * 3600,
  78.     'day': 24 * 3600,
  79.     'd': 24 * 3600,
  80.  
  81.     'hours': 3600,
  82.     'hour': 3600,
  83.     'hrs': 3600,
  84.     'hr': 3600,
  85.     'h': 3600,
  86.  
  87.     'minutes': 60,
  88.     'minute': 60,
  89.     'mins': 60,
  90.     'min': 60,
  91.     'm': 60,
  92.  
  93.     'seconds': 1,
  94.     'second': 1,
  95.     'secs': 1,
  96.     'sec': 1,
  97.     's': 1
  98. }
  99.  
  100. periods = '|'.join(list(scaling.keys()))
  101. p_command = r'\.in ([0-9]+(?:\.[0-9]+)?)\s?((?:%s)\b)?:?\s?(.*)' % periods
  102. r_command = re.compile(p_command)
  103.  
  104. def remind(phenny, input):
  105.     m = r_command.match(input.bytes)
  106.     if not m:
  107.         return phenny.say("You did it wrong.")
  108.     length, scale, message = m.groups()
  109.  
  110.     length = float(length)
  111.     factor = scaling.get(scale, 60)
  112.     duration = length * factor
  113.  
  114.     if duration % 1:
  115.         duration = int(duration) + 1
  116.     else: duration = int(duration)
  117.  
  118.     t = int(time.time()) + duration
  119.     reminder = (input.sender, input.nick, message)
  120.  
  121.     try: phenny.rdb[t].append(reminder)
  122.     except KeyError: phenny.rdb[t] = [reminder]
  123.  
  124.     dump_database(phenny.rfn, phenny.rdb)
  125.  
  126.     if duration >= 60:
  127.         w = ''
  128.         if duration >= 3600 * 12:
  129.             w += time.strftime(' on %d %b %Y', time.gmtime(t))
  130.         w += time.strftime(' at %H:%MZ', time.gmtime(t))
  131.         phenny.reply('Okay, will remind%s' % w)
  132.     else: phenny.reply('Okay, will remind in %s secs' % duration)
  133. remind.commands = ['in']
  134.  
  135. if __name__ == '__main__':
  136.     print(__doc__.strip())
Advertisement
Add Comment
Please, Sign In to add comment