time_check.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import re
  2. import time
  3. import config
  4. from common.log import logger
  5. def time_checker(f):
  6. def _time_checker(self, *args, **kwargs):
  7. _config = config.conf()
  8. chat_time_module = _config.get("chat_time_module", False)
  9. if chat_time_module:
  10. chat_start_time = _config.get("chat_start_time", "00:00")
  11. chat_stop_time = _config.get("chat_stop_time", "24:00")
  12. time_regex = re.compile(r"^([01]?[0-9]|2[0-4])(:)([0-5][0-9])$")
  13. if not (time_regex.match(chat_start_time) and time_regex.match(chat_stop_time)):
  14. logger.warning("时间格式不正确,请在config.json中修改CHAT_START_TIME/CHAT_STOP_TIME。")
  15. return None
  16. now_time = time.strptime(time.strftime("%H:%M"), "%H:%M")
  17. chat_start_time = time.strptime(chat_start_time, "%H:%M")
  18. chat_stop_time = time.strptime(chat_stop_time, "%H:%M")
  19. # 结束时间小于开始时间,跨天了
  20. if chat_stop_time < chat_start_time and (chat_start_time <= now_time or now_time <= chat_stop_time):
  21. f(self, *args, **kwargs)
  22. # 结束大于开始时间代表,没有跨天
  23. elif chat_start_time < chat_stop_time and chat_start_time <= now_time <= chat_stop_time:
  24. f(self, *args, **kwargs)
  25. else:
  26. # 定义匹配规则,如果以 #reconf 或者 #更新配置 结尾, 非服务时间可以修改开始/结束时间并重载配置
  27. pattern = re.compile(r"^.*#(?:reconf|更新配置)$")
  28. if args and pattern.match(args[0].content):
  29. f(self, *args, **kwargs)
  30. else:
  31. logger.info("非服务时间内,不接受访问")
  32. return None
  33. else:
  34. f(self, *args, **kwargs) # 未开启时间模块则直接回答
  35. return _time_checker