Update app.py
Browse files
app.py
CHANGED
@@ -88,51 +88,63 @@ WEBDAV_USERNAME = os.environ.get('WEBDAV_USERNAME')
|
|
88 |
WEBDAV_PASSWORD = os.environ.get('WEBDAV_PASSWORD')
|
89 |
|
90 |
def save_data_to_webdav():
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
def load_data_from_webdav():
|
113 |
global chatHistories, GROUP_SETTINGS, USER_SETTINGS, USER_LAST_ACTIVE, GROUP_ACTIVE_USERS, GROUP_INFO, BANNED_USERS
|
114 |
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
chatHistories = data['chatHistories']
|
122 |
-
GROUP_SETTINGS = data['GROUP_SETTINGS']
|
123 |
-
USER_SETTINGS = data['USER_SETTINGS']
|
124 |
-
USER_LAST_ACTIVE = {k: datetime.fromisoformat(v) for k, v in data['USER_LAST_ACTIVE'].items()}
|
125 |
-
GROUP_ACTIVE_USERS = {k: set(v) for k, v in data['GROUP_ACTIVE_USERS'].items()}
|
126 |
-
GROUP_INFO = {k: {**v, 'last_active': datetime.fromisoformat(v['last_active'])} for k, v in data['GROUP_INFO'].items()}
|
127 |
-
BANNED_USERS = {k: datetime.fromisoformat(v) for k, v in data['BANNED_USERS'].items()}
|
128 |
|
129 |
-
|
130 |
-
|
131 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
def periodic_save():
|
|
|
134 |
while True:
|
135 |
-
time.sleep(
|
136 |
save_data_to_webdav()
|
137 |
|
138 |
# 在程序启动时加载数据
|
@@ -142,6 +154,7 @@ load_data_from_webdav()
|
|
142 |
save_thread = threading.Thread(target=periodic_save)
|
143 |
save_thread.daemon = True
|
144 |
save_thread.start()
|
|
|
145 |
|
146 |
def make_telegram_request(method, data=None):
|
147 |
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/{method}"
|
|
|
88 |
WEBDAV_PASSWORD = os.environ.get('WEBDAV_PASSWORD')
|
89 |
|
90 |
def save_data_to_webdav():
|
91 |
+
print(f"开始保存数据到WebDAV: {datetime.now()}")
|
92 |
+
try:
|
93 |
+
data = {
|
94 |
+
'chatHistories': chatHistories,
|
95 |
+
'GROUP_SETTINGS': GROUP_SETTINGS,
|
96 |
+
'USER_SETTINGS': USER_SETTINGS,
|
97 |
+
'USER_LAST_ACTIVE': {k: v.isoformat() for k, v in USER_LAST_ACTIVE.items()},
|
98 |
+
'GROUP_ACTIVE_USERS': {k: list(v) for k, v in GROUP_ACTIVE_USERS.items()},
|
99 |
+
'GROUP_INFO': {k: {**v, 'last_active': v['last_active'].isoformat()} for k, v in GROUP_INFO.items()},
|
100 |
+
'BANNED_USERS': {k: v.isoformat() for k, v in BANNED_USERS.items()}
|
101 |
+
}
|
102 |
+
|
103 |
+
json_data = json.dumps(data)
|
104 |
+
|
105 |
+
response = requests.put(f"{WEBDAV_URL}/tg_bot/bot_data.json",
|
106 |
+
data=json_data,
|
107 |
+
auth=HTTPBasicAuth(WEBDAV_USERNAME, WEBDAV_PASSWORD),
|
108 |
+
timeout=30) # 添加超时
|
109 |
+
|
110 |
+
if response.status_code in [200, 201, 204]:
|
111 |
+
print(f"数据已成功保存到WebDAV: {datetime.now()}")
|
112 |
+
else:
|
113 |
+
print(f"保存到WebDAV失败: {response.status_code}, {response.text}")
|
114 |
+
except Exception as e:
|
115 |
+
print(f"保存数据时发生错误: {e}")
|
116 |
|
117 |
def load_data_from_webdav():
|
118 |
global chatHistories, GROUP_SETTINGS, USER_SETTINGS, USER_LAST_ACTIVE, GROUP_ACTIVE_USERS, GROUP_INFO, BANNED_USERS
|
119 |
|
120 |
+
print(f"开始从WebDAV加载数据: {datetime.now()}")
|
121 |
+
try:
|
122 |
+
response = requests.get(f"{WEBDAV_URL}/tg_bot/bot_data.json",
|
123 |
+
auth=HTTPBasicAuth(WEBDAV_USERNAME, WEBDAV_PASSWORD),
|
124 |
+
timeout=30) # 添加超时
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
|
126 |
+
if response.status_code == 200:
|
127 |
+
data = json.loads(response.text)
|
128 |
+
|
129 |
+
chatHistories = data['chatHistories']
|
130 |
+
GROUP_SETTINGS = data['GROUP_SETTINGS']
|
131 |
+
USER_SETTINGS = data['USER_SETTINGS']
|
132 |
+
USER_LAST_ACTIVE = {k: datetime.fromisoformat(v) for k, v in data['USER_LAST_ACTIVE'].items()}
|
133 |
+
GROUP_ACTIVE_USERS = {k: set(v) for k, v in data['GROUP_ACTIVE_USERS'].items()}
|
134 |
+
GROUP_INFO = {k: {**v, 'last_active': datetime.fromisoformat(v['last_active'])} for k, v in data['GROUP_INFO'].items()}
|
135 |
+
BANNED_USERS = {k: datetime.fromisoformat(v) for k, v in data['BANNED_USERS'].items()}
|
136 |
+
|
137 |
+
print(f"数据已从WebDAV成功加载: {datetime.now()}")
|
138 |
+
else:
|
139 |
+
print(f"从WebDAV加载数据失败: {response.status_code}, {response.text}")
|
140 |
+
except Exception as e:
|
141 |
+
print(f"加载数据时发生错误: {e}")
|
142 |
+
print("使用初始值")
|
143 |
|
144 |
def periodic_save():
|
145 |
+
print("定期保存线程已启动")
|
146 |
while True:
|
147 |
+
time.sleep(60) # 等待60秒
|
148 |
save_data_to_webdav()
|
149 |
|
150 |
# 在程序启动时加载数据
|
|
|
154 |
save_thread = threading.Thread(target=periodic_save)
|
155 |
save_thread.daemon = True
|
156 |
save_thread.start()
|
157 |
+
print("定期保存线程已创建并启动")
|
158 |
|
159 |
def make_telegram_request(method, data=None):
|
160 |
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/{method}"
|