From e9749386f5d549353f486b265fd396bae426a6e6 Mon Sep 17 00:00:00 2001 From: Womsxd <45663319+Womsxd@users.noreply.github.com> Date: Tue, 26 Apr 2022 16:15:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E5=8F=AA=E8=AF=BB=E6=83=85=E5=86=B5?= =?UTF-8?q?=E5=81=9A=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.py | 33 +++++++++++++++++++++++---------- index.py | 6 +++--- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/config.py b/config.py index 2eab14a..6a8bc51 100644 --- a/config.py +++ b/config.py @@ -75,6 +75,7 @@ def load_config(): def save_config(): + global serverless if not serverless: log.info("云函数执行,无法保存") return None @@ -83,16 +84,23 @@ def save_config(): data["mihoyobbs_Login_ticket"] = login_ticket data["mihoyobbs_Stuid"] = stuid data["mihoyobbs_Stoken"] = stoken - f.seek(0) - f.truncate() temp_text = json.dumps(data, sort_keys=False, indent=4, separators=(', ', ': ')) - f.write(temp_text) - f.flush() + try: + f.seek(0) + f.truncate() + f.write(temp_text) + f.flush() + except OSError: + serverless = True + log.info("Cookie保存失败") + exit(-1) + else: + log.info("Config保存完毕") f.close() - log.info("Config保存完毕") def clear_cookies(): + global serverless if not serverless: log.info("云函数执行,无法保存") return None @@ -103,10 +111,15 @@ def clear_cookies(): data["mihoyobbs_Stuid"] = "" data["mihoyobbs_Stoken"] = "" data["mihoyobbs_Cookies"] = "CookieError" - f.seek(0) - f.truncate() temp_text = json.dumps(data, sort_keys=False, indent=4, separators=(', ', ': ')) - f.write(temp_text) - f.flush() + try: + f.seek(0) + f.truncate() + f.write(temp_text) + f.flush() + except OSError: + serverless = True + log.info("Cookie删除失败") + else: + log.info("Cookie删除完毕") f.close() - log.info("Cookie删除完毕") diff --git a/index.py b/index.py index d28e258..972af5c 100644 --- a/index.py +++ b/index.py @@ -1,12 +1,12 @@ import main import push +import config import main_multi from error import CookieError -from config import serverless def main_handler(event: dict, context: dict): - serverless = True + config.serverless = True try: status_code, push_message = main.main() except CookieError: @@ -17,7 +17,7 @@ def main_handler(event: dict, context: dict): def main_handler_mulit(event: dict, context: dict): - serverless = True + config.serverless = True # 多用户需要传递True表示自动执行,不需要手动进行确认 main_multi.main_multi(True) print("云函数多用户测试支持!")