From 9fe59f2949f0ac7df792b2bb85f65dc64c6bf412 Mon Sep 17 00:00:00 2001 From: a5225662 Date: Thu, 23 Mar 2023 22:47:26 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E6=A8=A1=E5=9D=97=EF=BC=8C=E4=BD=BF=E7=94=A8?= =?UTF-8?q?md5=E9=AA=8C=E8=AF=81=E5=AE=9E=E7=8E=B0=E7=83=AD=E5=8A=A0?= =?UTF-8?q?=E8=BD=BDconfig.json=E5=8F=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- channel/wechat/wechat_channel.py | 4 ++ common/time_check.py | 72 ++++++++++++++++++++++++++++++++ config-template.json | 5 ++- config.py | 3 +- 4 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 common/time_check.py diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index 45fd537..bb97dfb 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -12,6 +12,7 @@ from concurrent.futures import ThreadPoolExecutor from common.log import logger from common.tmp_dir import TmpDir from config import conf +from common.time_check import time_checker import requests import io import time @@ -66,6 +67,8 @@ class WechatChannel(Channel): else: self._do_send_text(query, from_user_id) + + @time_checker def handle_text(self, msg): logger.debug("[WX]receive text msg: " + json.dumps(msg, ensure_ascii=False)) content = msg['Text'] @@ -109,6 +112,7 @@ class WechatChannel(Channel): thread_pool.submit(self._do_send_text, content, to_user_id) + @time_checker def handle_group(self, msg): logger.debug("[WX]receive group msg: " + json.dumps(msg, ensure_ascii=False)) group_name = msg['User'].get('NickName', None) diff --git a/common/time_check.py b/common/time_check.py new file mode 100644 index 0000000..4e2c826 --- /dev/null +++ b/common/time_check.py @@ -0,0 +1,72 @@ +import time,re,hashlib +from config import load_config,md5,conf +import config + + +def get_file_md5(file_name): + """ + 计算文件的md5 + :param file_name: + :return m.hexdigest(): + """ + m = hashlib.md5() #创建md5对象 + with open(file_name,'rb') as fobj: + while True: + data = fobj.read(1024) + if not data: + break + m.update(data) #更新md5对象 + + return m.hexdigest() #返回md5值 + + +def time_checker(f): + # print(args[0]()) + def wrapTheFunction(self, *args, **kwargs): + global md5 # 从config.py拿来一个全局变量md5 默认是False + if md5 == None: + _config = conf() + elif md5 == get_file_md5("./config.json"): + _config = conf() + # chat_time_module = _config["chat_time_module"] + # chat_start_time = _config["chat_start_time"] + # chat_stopt_time = _config["chat_stop_time"] + else: + print("检测到配置文件变化") + _config = load_config() # 启动时间支持热更改 修改config.json文件后即可生效 + md5 = get_file_md5("./config.json") + # config.md5 = get_file_md5("./config.json") + + chat_time_module = _config["chat_time_module"] + chat_start_time = _config["chat_start_time"] + chat_stopt_time = _config["chat_stop_time"] + # print(md5,chat_time_module,chat_start_time,chat_stopt_time) + + if chat_time_module: + time_regex = re.compile(r'^([01]?[0-9]|2[0-4])(:)([0-5][0-9])$') #时间匹配,包含24:00 + + starttime_format_check = time_regex.match(chat_start_time) # 检查停止时间格式 + stoptime_format_check = time_regex.match(chat_stopt_time) # 检查停止时间格式 + chat_time_check = chat_start_time < chat_stopt_time # 确定启动时间<停止时间 + + if starttime_format_check and stoptime_format_check and chat_time_check: + # print('服务启动时间:{}'.format(CHAT_START_TIME)) + # print('服务结束时间:{}'.format(CHAT_STOP_TIME)) + if chat_start_time>"23:59": + print('启动时间可能存在问题,请修改') + else: + print("时间格式不正确,请在config.json中修改您的CHAT_START_TIME/CHAT_STOP_TIME,否则可能会影响您正常使用,程序正在自动退出") + + now_time = time.strftime("%H:%M", time.localtime()) + if chat_start_time <= now_time <= chat_stopt_time: + # print("在服务时间内") + # 正常请求并返回 + f(self, *args, **kwargs) + return None + else: + print('不在服务时间内,禁止访问') + return None + else: + f(self, *args, **kwargs) + return wrapTheFunction + diff --git a/config-template.json b/config-template.json index 359de50..b6fdc97 100644 --- a/config-template.json +++ b/config-template.json @@ -11,6 +11,9 @@ "voice_reply_voice": false, "conversation_max_tokens": 1000, "expires_in_seconds": 3600, - "character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。" + "character_desc": "你是ChatGPT, 一个由OpenAI训练的大型语言模型, 你旨在回答并解决人们的任何问题,并且可以使用多种语言与人交流。", + "chat_time_module": false, + "chat_start_time": "00:00", + "chat_stop_time": "24:00" } diff --git a/config.py b/config.py index 9cce2f0..86b2523 100644 --- a/config.py +++ b/config.py @@ -5,7 +5,7 @@ import os from common.log import logger config = {} - +md5 = None def load_config(): global config @@ -17,6 +17,7 @@ def load_config(): # 将json字符串反序列化为dict类型 config = json.loads(config_str) logger.info("[INIT] load config: {}".format(config)) + return config From 18e9aca3b170dbdb1d9897bc20139ffd16408ecf Mon Sep 17 00:00:00 2001 From: a5225662 Date: Fri, 24 Mar 2023 00:01:46 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=B0=83=E7=94=A8#=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=EF=BC=8C=E6=9B=BF=E4=BB=A3md5=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=EF=BC=8C=E5=87=8F=E5=B0=91=E9=87=8D=E5=A4=8D=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=EF=BC=8C=E6=8F=90=E9=AB=98=E7=A8=8B=E5=BA=8F=E6=95=88?= =?UTF-8?q?=E7=8E=87=EF=BC=8C=E5=B0=86print=E6=9B=BF=E6=8D=A2=E4=B8=BAlogg?= =?UTF-8?q?er?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/time_check.py | 67 +++++++++++--------------------------------- config.py | 2 -- 2 files changed, 17 insertions(+), 52 deletions(-) diff --git a/common/time_check.py b/common/time_check.py index 4e2c826..56ea336 100644 --- a/common/time_check.py +++ b/common/time_check.py @@ -1,46 +1,13 @@ import time,re,hashlib -from config import load_config,md5,conf import config - - -def get_file_md5(file_name): - """ - 计算文件的md5 - :param file_name: - :return m.hexdigest(): - """ - m = hashlib.md5() #创建md5对象 - with open(file_name,'rb') as fobj: - while True: - data = fobj.read(1024) - if not data: - break - m.update(data) #更新md5对象 - - return m.hexdigest() #返回md5值 - +from common.log import logger def time_checker(f): - # print(args[0]()) - def wrapTheFunction(self, *args, **kwargs): - global md5 # 从config.py拿来一个全局变量md5 默认是False - if md5 == None: - _config = conf() - elif md5 == get_file_md5("./config.json"): - _config = conf() - # chat_time_module = _config["chat_time_module"] - # chat_start_time = _config["chat_start_time"] - # chat_stopt_time = _config["chat_stop_time"] - else: - print("检测到配置文件变化") - _config = load_config() # 启动时间支持热更改 修改config.json文件后即可生效 - md5 = get_file_md5("./config.json") - # config.md5 = get_file_md5("./config.json") - + def _time_checker(self, *args, **kwargs): + _config = config.conf() chat_time_module = _config["chat_time_module"] chat_start_time = _config["chat_start_time"] chat_stopt_time = _config["chat_stop_time"] - # print(md5,chat_time_module,chat_start_time,chat_stopt_time) if chat_time_module: time_regex = re.compile(r'^([01]?[0-9]|2[0-4])(:)([0-5][0-9])$') #时间匹配,包含24:00 @@ -49,24 +16,24 @@ def time_checker(f): stoptime_format_check = time_regex.match(chat_stopt_time) # 检查停止时间格式 chat_time_check = chat_start_time < chat_stopt_time # 确定启动时间<停止时间 - if starttime_format_check and stoptime_format_check and chat_time_check: - # print('服务启动时间:{}'.format(CHAT_START_TIME)) - # print('服务结束时间:{}'.format(CHAT_STOP_TIME)) - if chat_start_time>"23:59": - print('启动时间可能存在问题,请修改') - else: - print("时间格式不正确,请在config.json中修改您的CHAT_START_TIME/CHAT_STOP_TIME,否则可能会影响您正常使用,程序正在自动退出") + # 时间格式检查 + if not (starttime_format_check and stoptime_format_check and chat_time_check): + logger.warn('时间格式不正确,请在config.json中修改您的CHAT_START_TIME/CHAT_STOP_TIME,否则可能会影响您正常使用,开始({})-结束({})'.format(starttime_format_check,stoptime_format_check)) + if chat_start_time>"23:59": + logger.error('启动时间可能存在问题,请修改!') + # 服务时间检查 now_time = time.strftime("%H:%M", time.localtime()) - if chat_start_time <= now_time <= chat_stopt_time: - # print("在服务时间内") - # 正常请求并返回 + if chat_start_time <= now_time <= chat_stopt_time: # 服务时间内,正常返回回答 f(self, *args, **kwargs) return None else: - print('不在服务时间内,禁止访问') - return None + if args[0]['Content'] == "#更新配置": # 不在服务时间内也可以更新配置 + f(self, *args, **kwargs) + else: + logger.info('非服务时间内,不接受访问') + return None else: - f(self, *args, **kwargs) - return wrapTheFunction + f(self, *args, **kwargs) # 未开启时间模块则直接回答 + return _time_checker diff --git a/config.py b/config.py index 86b2523..1a5a1f2 100644 --- a/config.py +++ b/config.py @@ -5,7 +5,6 @@ import os from common.log import logger config = {} -md5 = None def load_config(): global config @@ -17,7 +16,6 @@ def load_config(): # 将json字符串反序列化为dict类型 config = json.loads(config_str) logger.info("[INIT] load config: {}".format(config)) - return config