1
0
Fork 0
nemubot/modules/sleepytime.py

51 lines
1.8 KiB
Python
Raw Normal View History

2012-07-16 22:45:41 +00:00
# coding=utf-8
2014-08-27 23:39:31 +00:00
"""as http://sleepyti.me/, give you the best time to go to bed"""
2012-07-16 22:45:41 +00:00
import re
import imp
2014-09-30 21:51:14 +00:00
from datetime import datetime, timedelta, timezone
2012-07-16 22:45:41 +00:00
from nemubot.hooks import hook
2014-08-13 13:53:55 +00:00
nemubotversion = 3.4
2012-07-16 22:45:41 +00:00
from nemubot.module.more import Response
2014-11-13 01:51:49 +00:00
def help_full():
2014-11-13 01:51:49 +00:00
return ("If you would like to sleep soon, use !sleepytime to know the best"
" time to wake up; use !sleepytime hh:mm if you want to wake up at"
" hh:mm")
2012-07-16 22:45:41 +00:00
2015-11-02 19:19:12 +00:00
@hook.command("sleepytime")
def cmd_sleep(msg):
2015-07-10 21:09:54 +00:00
if len(msg.args) and re.match("[0-9]{1,2}[h':.,-]([0-9]{1,2})?[m'\":.,-]?",
msg.args[0]) is not None:
2012-08-16 03:42:54 +00:00
# First, parse the hour
2015-07-10 21:09:54 +00:00
p = re.match("([0-9]{1,2})[h':.,-]([0-9]{1,2})?[m':.,-]?", msg.args[0])
2014-09-30 21:51:14 +00:00
f = [datetime(datetime.now(timezone.utc).year,
datetime.now(timezone.utc).month,
datetime.now(timezone.utc).day,
2012-08-16 03:42:54 +00:00
hour=int(p.group(1)))]
if p.group(2) is not None:
2014-11-13 01:51:49 +00:00
f[0] += timedelta(minutes=int(p.group(2)))
2012-08-16 03:42:54 +00:00
g = list()
2014-11-13 01:51:49 +00:00
for i in range(6):
f.append(f[i] - timedelta(hours=1, minutes=30))
2012-08-16 03:42:54 +00:00
g.append(f[i+1].strftime("%H:%M"))
return Response("You should try to fall asleep at one of the following"
2014-07-25 16:02:30 +00:00
" times: %s" % ', '.join(g), channel=msg.channel)
2012-08-16 03:42:54 +00:00
# Just get awake times
2012-07-16 22:45:41 +00:00
else:
2014-09-30 21:51:14 +00:00
f = [datetime.now(timezone.utc) + timedelta(minutes=15)]
2012-08-16 03:42:54 +00:00
g = list()
2014-11-13 01:51:49 +00:00
for i in range(6):
f.append(f[i] + timedelta(hours=1, minutes=30))
2012-08-16 03:42:54 +00:00
g.append(f[i+1].strftime("%H:%M"))
return Response("If you head to bed right now, you should try to wake"
" up at one of the following times: %s" %
2014-07-25 16:02:30 +00:00
', '.join(g), channel=msg.channel)