DmitrMakeev commited on
Commit
9ac14d9
1 Parent(s): 65d94dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -7
app.py CHANGED
@@ -907,14 +907,22 @@ def send_get_request():
907
  webinarId = request.args.get('webinarId')
908
  url = f'https://online.bizon365.ru/api/v1/webinars/reports/get?webinarId={webinarId}'
909
 
910
- response = requests.get(url, headers={'X-Token': token})
911
-
912
- if response.status_code == 200:
913
  data = response.json()
914
 
 
 
 
 
915
  report = data.get('report', {})
916
  messages = data.get('messages', {})
917
 
 
 
 
 
918
  report_json_str = report.get('report', '{}')
919
  try:
920
  report_json = json.loads(report_json_str)
@@ -929,10 +937,8 @@ def send_get_request():
929
 
930
  users_meta = report_json.get('usersMeta', {})
931
 
932
- # Обновление или добавление каждого пользователя в базу данных
933
  processed_emails = set()
934
  for user_id, user_data in users_meta.items():
935
- # Получаем только сообщения для текущего пользователя
936
  user_messages = messages_json.get(user_id, [])
937
  user_data['messages'] = user_messages
938
  email = user_data.get('email')
@@ -941,8 +947,9 @@ def send_get_request():
941
  processed_emails.add(email)
942
 
943
  return jsonify({'status': 'User data saved successfully'})
944
- else:
945
- return jsonify({'error': 'Failed to fetch data from the API'}), response.status_code
 
946
 
947
 
948
 
 
907
  webinarId = request.args.get('webinarId')
908
  url = f'https://online.bizon365.ru/api/v1/webinars/reports/get?webinarId={webinarId}'
909
 
910
+ try:
911
+ response = requests.get(url, headers={'X-Token': token})
912
+ response.raise_for_status() # Проверка на ошибки HTTP
913
  data = response.json()
914
 
915
+ # Убедитесь, что report существует в данных
916
+ if data is None or 'report' not in data:
917
+ return jsonify({'error': 'No report data found'}), 500
918
+
919
  report = data.get('report', {})
920
  messages = data.get('messages', {})
921
 
922
+ # Проверка на None перед использованием
923
+ if report is None:
924
+ return jsonify({'error': 'No report data found in the response'}), 500
925
+
926
  report_json_str = report.get('report', '{}')
927
  try:
928
  report_json = json.loads(report_json_str)
 
937
 
938
  users_meta = report_json.get('usersMeta', {})
939
 
 
940
  processed_emails = set()
941
  for user_id, user_data in users_meta.items():
 
942
  user_messages = messages_json.get(user_id, [])
943
  user_data['messages'] = user_messages
944
  email = user_data.get('email')
 
947
  processed_emails.add(email)
948
 
949
  return jsonify({'status': 'User data saved successfully'})
950
+
951
+ except requests.exceptions.RequestException as e:
952
+ return jsonify({'error': f'API request failed: {str(e)}'}), 500
953
 
954
 
955