Advertisement
Guest User

Untitled

a guest
Sep 17th, 2014
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 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. storage = {}
  13.  
  14. def setup(phenny):
  15.  
  16. def monitor(phenny):
  17. global storage
  18. #NICKTRACKER: Use currently logged-in nicks as fallback targets.
  19. time.sleep(5)
  20. while True:
  21. now = int(time.time())
  22. unixtimes = [int(key) for key in storage]
  23. oldtimes = [t for t in unixtimes if t <= now]
  24. if oldtimes:
  25. for oldtime in oldtimes:
  26. for (channel, nick, message) in storage[oldtime]:
  27. if message:
  28. phenny.msg(channel, nick + ': ' + message)
  29. else: phenny.msg(channel, nick + '!')
  30. del storage[oldtime]
  31. time.sleep(2.5)
  32.  
  33. targs = (phenny,)
  34. t = threading.Thread(target=monitor, args=targs)
  35. t.daemon = True
  36. t.start()
  37.  
  38.  
  39. scaling = {
  40. 'years': 365.25 * 24 * 3600,
  41. 'year': 365.25 * 24 * 3600,
  42. 'yrs': 365.25 * 24 * 3600,
  43. 'y': 365.25 * 24 * 3600,
  44.  
  45. 'months': 29.53059 * 24 * 3600,
  46. 'month': 29.53059 * 24 * 3600,
  47. 'mo': 29.53059 * 24 * 3600,
  48.  
  49. 'weeks': 7 * 24 * 3600,
  50. 'week': 7 * 24 * 3600,
  51. 'wks': 7 * 24 * 3600,
  52. 'wk': 7 * 24 * 3600,
  53. 'w': 7 * 24 * 3600,
  54.  
  55. 'days': 24 * 3600,
  56. 'day': 24 * 3600,
  57. 'd': 24 * 3600,
  58.  
  59. 'hours': 3600,
  60. 'hour': 3600,
  61. 'hrs': 3600,
  62. 'hr': 3600,
  63. 'h': 3600,
  64.  
  65. 'minutes': 60,
  66. 'minute': 60,
  67. 'mins': 60,
  68. 'min': 60,
  69. 'm': 60,
  70.  
  71. 'seconds': 1,
  72. 'second': 1,
  73. 'secs': 1,
  74. 'sec': 1,
  75. 's': 1
  76. }
  77.  
  78. periods = '|'.join(scaling.keys())
  79. p_command = r'\.in ([0-9]+(?:\.[0-9]+)?)\s?((?:%s)\b)?:?\s?(.*)' % periods
  80. r_command = re.compile(p_command)
  81.  
  82. def remind(phenny, input):
  83. m = r_command.match(input.bytes)
  84. if not m:
  85. return phenny.reply("Sorry, didn't understand the input.")
  86. length, scale, message = m.groups()
  87.  
  88. length = float(length)
  89. factor = scaling.get(scale, 60)
  90. duration = length * factor
  91.  
  92. # Ludicriously out-of-bounds trapping
  93. if duration > 9000000000 or duration < 1:
  94. return( phenny.reply("That's a bit unlikely, don't you think?") )
  95.  
  96. if duration % 1:
  97. duration = int(duration) + 1
  98. else: duration = int(duration)
  99.  
  100.  
  101. #NICKTRACKER: Store in the canonical name
  102. t = int(time.time()) + duration
  103. reminder = (input.sender, input.nick, message)
  104.  
  105. try: storage[t].append(reminder)
  106. except KeyError: storage[t] = [reminder]
  107.  
  108. try:
  109. if duration >= 60:
  110. w = ''
  111. if duration >= 3600 * 12:
  112. w += time.strftime(' on %d %b %Y', time.gmtime(t))
  113. w += time.strftime(' at %H:%MZ', time.gmtime(t))
  114. phenny.reply('Okay, will remind%s' % w)
  115. else: phenny.reply('Okay, will remind in %s secs' % duration)
  116. except ValueError:
  117. phenny.reply("That's kind of unlikely, don't you think?")
  118. remind.commands = ['in']
  119.  
  120. if __name__ == '__main__':
  121. print __doc__.strip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement