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("云函数多用户测试支持!")