yangtb24 commited on
Commit
cd0a165
·
verified ·
1 Parent(s): c9b38ca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -20
app.py CHANGED
@@ -92,26 +92,26 @@ def save_data_to_webdav():
92
  try:
93
  data = {
94
  'chatHistories': chatHistories,
95
- 'GROUP_SETTINGS': GROUP_SETTINGS,
96
- 'USER_SETTINGS': USER_SETTINGS,
97
  'USER_LAST_ACTIVE': {str(k): v.isoformat() for k, v in USER_LAST_ACTIVE.items()},
98
  'GROUP_ACTIVE_USERS': {str(k): list(v) for k, v in GROUP_ACTIVE_USERS.items()},
99
  'GROUP_INFO': {
100
- str(k): {**v, 'last_active': v['last_active'].isoformat()}
101
  for k, v in GROUP_INFO.items()
102
  },
103
  'BANNED_USERS': {str(k): v.isoformat() for k, v in BANNED_USERS.items()}
104
  }
105
-
106
  json_data = json.dumps(data, indent=4)
107
-
108
  response = requests.put(
109
- f"{WEBDAV_URL}/tg_bot/bot_data.json",
110
- data=json_data,
111
  auth=HTTPBasicAuth(WEBDAV_USERNAME, WEBDAV_PASSWORD),
112
  timeout=30
113
  )
114
-
115
  if response.status_code in [200, 201, 204]:
116
  print(f"数据已成功保存到WebDAV: {datetime.now()}")
117
  else:
@@ -121,24 +121,25 @@ def save_data_to_webdav():
121
 
122
  def load_data_from_webdav():
123
  global chatHistories, GROUP_SETTINGS, USER_SETTINGS, USER_LAST_ACTIVE, GROUP_ACTIVE_USERS, GROUP_INFO, BANNED_USERS
124
-
125
  print(f"开始从WebDAV加载数据: {datetime.now()}")
126
  try:
127
- response = requests.get(f"{WEBDAV_URL}/tg_bot/bot_data.json",
128
  auth=HTTPBasicAuth(WEBDAV_USERNAME, WEBDAV_PASSWORD),
129
  timeout=30)
130
-
131
  if response.status_code == 200:
132
  data = json.loads(response.text)
133
-
134
  chatHistories = data['chatHistories']
135
- GROUP_SETTINGS = {int(k): v for k, v in data['GROUP_SETTINGS'].items()}
136
- USER_SETTINGS = {int(k): v for k, v in data['USER_SETTINGS'].items()}
137
- USER_LAST_ACTIVE = {int(k): datetime.fromisoformat(v) for k, v in data['USER_LAST_ACTIVE'].items()}
138
- GROUP_ACTIVE_USERS = {int(k): set(v) for k, v in data['GROUP_ACTIVE_USERS'].items()}
139
- GROUP_INFO = {int(k): {**v, 'last_active': datetime.fromisoformat(v['last_active'])} for k, v in data['GROUP_INFO'].items()}
140
- BANNED_USERS = {int(k): datetime.fromisoformat(v) for k, v in data['BANNED_USERS'].items()}
141
-
 
142
  print(f"数据已从WebDAV成功加载: {datetime.now()}")
143
  else:
144
  print(f"从WebDAV加载数据失败: {response.status_code}, {response.text}")
@@ -146,6 +147,8 @@ def load_data_from_webdav():
146
  print(f"加载数据时发生错误: {e}")
147
  print("使用初始值")
148
 
 
 
149
  def periodic_save():
150
  print("定期保存线程已启动")
151
  while True:
@@ -599,8 +602,9 @@ async def banUser(chatId, userId):
599
  async def unbanUser(chatId, userId):
600
  if userId in BANNED_USERS:
601
  del BANNED_USERS[userId]
 
602
  await sendTelegramMessage(chatId, f"用户 {userId} 已被解禁。")
603
  print(f"用户 {userId} 在群组 {chatId} 中被解禁。")
604
 
605
  if __name__ == '__main__':
606
- app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))
 
92
  try:
93
  data = {
94
  'chatHistories': chatHistories,
95
+ 'GROUP_SETTINGS': {str(k): v for k, v in GROUP_SETTINGS.items()},
96
+ 'USER_SETTINGS': {str(k): v for k, v in USER_SETTINGS.items()},
97
  'USER_LAST_ACTIVE': {str(k): v.isoformat() for k, v in USER_LAST_ACTIVE.items()},
98
  'GROUP_ACTIVE_USERS': {str(k): list(v) for k, v in GROUP_ACTIVE_USERS.items()},
99
  'GROUP_INFO': {
100
+ str(k): {**v, 'last_active': v['last_active'].isoformat()}
101
  for k, v in GROUP_INFO.items()
102
  },
103
  'BANNED_USERS': {str(k): v.isoformat() for k, v in BANNED_USERS.items()}
104
  }
105
+
106
  json_data = json.dumps(data, indent=4)
107
+
108
  response = requests.put(
109
+ f"{WEBDAV_URL}/tg_bot/bot_data.json",
110
+ data=json_data,
111
  auth=HTTPBasicAuth(WEBDAV_USERNAME, WEBDAV_PASSWORD),
112
  timeout=30
113
  )
114
+
115
  if response.status_code in [200, 201, 204]:
116
  print(f"数据已成功保存到WebDAV: {datetime.now()}")
117
  else:
 
121
 
122
  def load_data_from_webdav():
123
  global chatHistories, GROUP_SETTINGS, USER_SETTINGS, USER_LAST_ACTIVE, GROUP_ACTIVE_USERS, GROUP_INFO, BANNED_USERS
124
+
125
  print(f"开始从WebDAV加载数据: {datetime.now()}")
126
  try:
127
+ response = requests.get(f"{WEBDAV_URL}/tg_bot/bot_data.json",
128
  auth=HTTPBasicAuth(WEBDAV_USERNAME, WEBDAV_PASSWORD),
129
  timeout=30)
130
+
131
  if response.status_code == 200:
132
  data = json.loads(response.text)
133
+
134
  chatHistories = data['chatHistories']
135
+ GROUP_SETTINGS = {int(k): v for k, v in data.get('GROUP_SETTINGS', {}).items()}
136
+ USER_SETTINGS = {int(k): v for k, v in data.get('USER_SETTINGS', {}).items()}
137
+ USER_LAST_ACTIVE = {int(k): datetime.fromisoformat(v) for k, v in data.get('USER_LAST_ACTIVE', {}).items()}
138
+ GROUP_ACTIVE_USERS = {int(k): set(v) for k, v in data.get('GROUP_ACTIVE_USERS', {}).items()}
139
+ GROUP_INFO = {int(k): {**v, 'last_active': datetime.fromisoformat(v['last_active'])} for k, v in data.get('GROUP_INFO', {}).items()}
140
+ BANNED_USERS = {int(k): datetime.fromisoformat(v) for k, v in data.get('BANNED_USERS', {}).items()}
141
+
142
+
143
  print(f"数据已从WebDAV成功加载: {datetime.now()}")
144
  else:
145
  print(f"从WebDAV加载数据失败: {response.status_code}, {response.text}")
 
147
  print(f"加载数据时发生错误: {e}")
148
  print("使用初始值")
149
 
150
+
151
+
152
  def periodic_save():
153
  print("定期保存线程已启动")
154
  while True:
 
602
  async def unbanUser(chatId, userId):
603
  if userId in BANNED_USERS:
604
  del BANNED_USERS[userId]
605
+
606
  await sendTelegramMessage(chatId, f"用户 {userId} 已被解禁。")
607
  print(f"用户 {userId} 在群组 {chatId} 中被解禁。")
608
 
609
  if __name__ == '__main__':
610
+ app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860)))