DmitrMakeev commited on
Commit
dae356e
1 Parent(s): f9a6d6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -66
app.py CHANGED
@@ -219,74 +219,65 @@ def export_user():
219
 
220
 
221
 
222
- def load_data_from_json(json_data):
223
- # Debug print to inspect json_data
224
- print(json.dumps(json_data, indent=4, ensure_ascii=False))
 
 
 
 
 
 
 
 
225
 
 
226
  if 'info' not in json_data or 'items' not in json_data['info'] or 'fields' not in json_data['info']:
227
  raise ValueError("Invalid JSON structure")
228
 
229
  items = json_data['info']['items']
230
  fields = json_data['info']['fields']
231
 
232
- for db in DATABASES:
233
- conn = sqlite3.connect(db)
234
- cursor = conn.cursor()
235
 
236
- for item in items:
237
- user_data = dict(zip(fields, item))
238
- # Проверяем наличие значений для полей и устанавливаем значения по умолчанию, если они отсутствуют
239
- user_data.setdefault('vk_id', '')
240
- user_data.setdefault('chat_id', '')
241
- user_data.setdefault('ws_st', '')
242
- user_data.setdefault('ws_stop', '')
243
- user_data.setdefault('web_st', '')
244
- user_data.setdefault('fin_prog', '')
245
- user_data.setdefault('b_city', '')
246
- user_data.setdefault('b_fin', '')
247
- user_data.setdefault('b_ban', '')
248
- user_data.setdefault('b_ign', '')
249
- user_data.setdefault('b_baners', '')
250
- user_data.setdefault('b_butt', '')
251
- user_data.setdefault('b_mess', '')
252
- user_data.setdefault('shop_st', '')
253
- user_data.setdefault('curator', '')
254
- user_data.setdefault('pr1', '')
255
- user_data.setdefault('pr2', '')
256
- user_data.setdefault('pr3', '')
257
- user_data.setdefault('pr4', '')
258
- user_data.setdefault('pr5', '')
259
- user_data.setdefault('ad_url', '')
260
- user_data.setdefault('key_pr', '')
261
- user_data.setdefault('n_con', '')
262
- user_data.setdefault('canal', '')
263
- user_data.setdefault('data_t', '')
264
-
265
- # Убираем плюс в начале телефона, если он присутствует
266
- if 'Телефон' in user_data and user_data['Телефон'].startswith('+'):
267
- user_data['Телефон'] = user_data['Телефон'][1:]
268
-
269
- query = '''
270
  INSERT INTO contacts (
271
  name, phone, email, vk_id, chat_id, ws_statys, ws_stop, web_statys, fin_progress, b_city, b_fin, b_ban, b_ign, b_baners, b_butt, b_mess, shop_st, shop_statys_full, pr1, pr2, pr3, pr4, pr5, ad_url, curator, key_pr, n_con, canal, data_t
272
  ) VALUES (
273
  :Имя, :Телефон, :Email, :vk_id, :chat_id, :ws_st, :ws_stop, :web_st, :fin_prog, :b_city, :b_fin, :b_ban, :b_ign, :b_baners, :b_butt, :b_mess, :shop_st, :shop_st, :pr1, :pr2, :pr3, :pr4, :pr5, :ad_url, :curator, :key_pr, :n_con, :canal, :data_t
274
  )
275
  '''
276
- cursor.execute(query, user_data)
277
-
278
- conn.commit()
279
- conn.close()
280
-
281
-
282
-
283
-
284
-
285
-
286
-
287
-
288
-
289
 
 
 
290
 
291
 
292
 
@@ -299,15 +290,6 @@ def load_data_from_json(json_data):
299
 
300
 
301
 
302
- def send_second_request(export_id):
303
- api_key = api_key_sys
304
- api_url_template = 'https://school.riverpsy.com/pl/api/account/exports/{export_id}?key={api_key}'
305
- api_url = api_url_template.format(export_id=export_id, api_key=api_key)
306
- response = requests.get(api_url)
307
- if response.status_code == 200:
308
- return response.json()
309
- else:
310
- raise Exception(f"Failed to fetch data, status code: {response.status_code}")
311
 
312
 
313
  @app.route('/start', methods=['GET'])
@@ -316,18 +298,17 @@ def start():
316
  api_key_sys_control = request.args.get('api_sys')
317
 
318
  if export_id is None:
319
- return jsonify({"error": "export_id is required"}), 400
320
 
321
  if api_key_sys_control != api_key_sys:
322
- return jsonify({"error": "Unauthorized access"}), 403
323
 
324
  try:
325
  json_data = send_second_request(export_id)
326
- print(json.dumps(json_data, indent=4, ensure_ascii=False)) # Debug print to inspect JSON data received
327
  load_data_from_json(json_data)
328
  return "Data loaded successfully", 200
329
  except Exception as e:
330
- return jsonify({"error": str(e)}), 500
331
 
332
 
333
 
 
219
 
220
 
221
 
222
+ def send_second_request(export_id):
223
+ if export_id is None:
224
+ raise Exception("export_id is None")
225
+ # Формирование URL для второго запроса
226
+ export_url_template = f"https://school.riverpsy.com/pl/api/account/exports/{export_id}?key=jqgxSMUnHWoKUcxF3MHSb77VUMk7HpFbO9SHnfVYwHtwqe1S81lqeKxrLPoSPWCephtYQuJwMFsCXEFmyByXdruDpDFgf6L7ij66K9ji0Kf2qAIwbTqEyJGB5MOHwyHl"
227
+ try:
228
+ response = requests.get(export_url_template)
229
+ response.raise_for_status()
230
+ return response.json() # Возвращаем JSON-ответ сервера
231
+ except requests.RequestException as e:
232
+ raise Exception(f"Ошибка при выполнении запроса: {e}")
233
 
234
+ def load_data_from_json(json_data):
235
  if 'info' not in json_data or 'items' not in json_data['info'] or 'fields' not in json_data['info']:
236
  raise ValueError("Invalid JSON structure")
237
 
238
  items = json_data['info']['items']
239
  fields = json_data['info']['fields']
240
 
241
+ db = 'data_gc.db' # Указываем конкретную базу данных
242
+ conn = sqlite3.connect(db)
243
+ cursor = conn.cursor()
244
 
245
+ for item in items:
246
+ user_data = dict(zip(fields, item))
247
+ # Проверяем наличие значений для полей и устанавливаем значения по умолчанию, если они отсутствуют
248
+ user_data.setdefault('vk_id', '')
249
+ user_data.setdefault('chat_id', '')
250
+ user_data.setdefault('ws_st', '')
251
+ user_data.setdefault('ws_stop', '')
252
+ user_data.setdefault('web_st', '')
253
+ user_data.setdefault('fin_prog', '')
254
+ user_data.setdefault('shop_st', '')
255
+ user_data.setdefault('curator', '')
256
+ user_data.setdefault('pr1', '')
257
+ user_data.setdefault('pr2', '')
258
+ user_data.setdefault('pr3', '')
259
+ user_data.setdefault('pr4', '')
260
+ user_data.setdefault('pr5', '')
261
+ user_data.setdefault('ad_url', '')
262
+ user_data.setdefault('key_pr', '')
263
+ user_data.setdefault('n_con', '')
264
+ user_data.setdefault('canal', '')
265
+ user_data.setdefault('data_t', '')
266
+ # Убираем плюс в начале телефона, если он присутствует
267
+ if 'Телефон' in user_data and user_data['Телефон'].startswith('+'):
268
+ user_data['Телефон'] = user_data['Телефон'][1:]
269
+
270
+ query = '''
 
 
 
 
 
 
 
 
271
  INSERT INTO contacts (
272
  name, phone, email, vk_id, chat_id, ws_statys, ws_stop, web_statys, fin_progress, b_city, b_fin, b_ban, b_ign, b_baners, b_butt, b_mess, shop_st, shop_statys_full, pr1, pr2, pr3, pr4, pr5, ad_url, curator, key_pr, n_con, canal, data_t
273
  ) VALUES (
274
  :Имя, :Телефон, :Email, :vk_id, :chat_id, :ws_st, :ws_stop, :web_st, :fin_prog, :b_city, :b_fin, :b_ban, :b_ign, :b_baners, :b_butt, :b_mess, :shop_st, :shop_st, :pr1, :pr2, :pr3, :pr4, :pr5, :ad_url, :curator, :key_pr, :n_con, :canal, :data_t
275
  )
276
  '''
277
+ cursor.execute(query, user_data)
 
 
 
 
 
 
 
 
 
 
 
 
278
 
279
+ conn.commit()
280
+ conn.close()
281
 
282
 
283
 
 
290
 
291
 
292
 
 
 
 
 
 
 
 
 
 
293
 
294
 
295
  @app.route('/start', methods=['GET'])
 
298
  api_key_sys_control = request.args.get('api_sys')
299
 
300
  if export_id is None:
301
+ return json.dumps({"error": "export_id is required"}), 400
302
 
303
  if api_key_sys_control != api_key_sys:
304
+ return json.dumps({"error": "Unauthorized access"}), 403
305
 
306
  try:
307
  json_data = send_second_request(export_id)
 
308
  load_data_from_json(json_data)
309
  return "Data loaded successfully", 200
310
  except Exception as e:
311
+ return json.dumps({"error": str(e)}), 500
312
 
313
 
314