DmitrMakeev commited on
Commit
c2d4a11
1 Parent(s): c6b468b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -213,19 +213,23 @@ def export_user():
213
 
214
 
215
  def send_second_request(export_id):
216
- api_key = api_key_sys
217
- api_url_template = 'https://school.riverpsy.com/pl/api/account/exports/{export_id}?key={api_key}'
218
- api_url = api_url_template.format(export_id=export_id, api_key=api_key)
219
- response = requests.get(api_url)
220
- if response.status_code == 200:
221
- return response.json()
222
- else:
223
- raise Exception(f"Failed to fetch data, status code: {response.status_code}")
 
 
224
 
225
  def load_data_from_json(json_data):
226
- data = json_data
227
- items = data['info']['items']
228
- fields = data['info']['fields']
 
 
229
 
230
  db = 'data_gc.db' # Указываем конкретную базу данных
231
  conn = sqlite3.connect(db)
@@ -271,17 +275,18 @@ def start():
271
  api_key_sys_control = request.args.get('api_sys')
272
 
273
  if export_id is None:
274
- return jsonify({"error": "export_id is required"}), 400
275
 
276
  if api_key_sys_control != api_key_sys:
277
- return jsonify({"error": "Unauthorized access"}), 403
278
 
279
  try:
280
  json_data = send_second_request(export_id)
281
  load_data_from_json(json_data)
282
  return "Data loaded successfully", 200
283
  except Exception as e:
284
- return jsonify({"error": str(e)}), 500
 
285
 
286
 
287
 
 
213
 
214
 
215
  def send_second_request(export_id):
216
+ if export_id is None:
217
+ raise Exception("export_id is None")
218
+ # Формирование URL для второго запроса
219
+ export_url_template = f"https://school.riverpsy.com/pl/api/account/exports/{export_id}?key=jqgxSMUnHWoKUcxF3MHSb77VUMk7HpFbO9SHnfVYwHtwqe1S81lqeKxrLPoSPWCephtYQuJwMFsCXEFmyByXdruDpDFgf6L7ij66K9ji0Kf2qAIwbTqEyJGB5MOHwyHl"
220
+ try:
221
+ response = requests.get(export_url_template)
222
+ response.raise_for_status()
223
+ return response.json() # Возвращаем JSON-ответ сервера
224
+ except requests.RequestException as e:
225
+ raise Exception(f"Ошибка при выполнении запроса: {e}")
226
 
227
  def load_data_from_json(json_data):
228
+ if 'info' not in json_data or 'items' not in json_data['info'] or 'fields' not in json_data['info']:
229
+ raise ValueError("Invalid JSON structure")
230
+
231
+ items = json_data['info']['items']
232
+ fields = json_data['info']['fields']
233
 
234
  db = 'data_gc.db' # Указываем конкретную базу данных
235
  conn = sqlite3.connect(db)
 
275
  api_key_sys_control = request.args.get('api_sys')
276
 
277
  if export_id is None:
278
+ return json.dumps({"error": "export_id is required"}), 400
279
 
280
  if api_key_sys_control != api_key_sys:
281
+ return json.dumps({"error": "Unauthorized access"}), 403
282
 
283
  try:
284
  json_data = send_second_request(export_id)
285
  load_data_from_json(json_data)
286
  return "Data loaded successfully", 200
287
  except Exception as e:
288
+ return json.dumps({"error": str(e)}), 500
289
+
290
 
291
 
292