DmitrMakeev
commited on
Commit
•
dae356e
1
Parent(s):
f9a6d6f
Update app.py
Browse files
app.py
CHANGED
@@ -219,74 +219,65 @@ def export_user():
|
|
219 |
|
220 |
|
221 |
|
222 |
-
def
|
223 |
-
|
224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
233 |
-
|
234 |
-
|
235 |
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
user_data
|
260 |
-
|
261 |
-
|
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 |
-
|
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
|
320 |
|
321 |
if api_key_sys_control != api_key_sys:
|
322 |
-
return
|
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
|
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 |
|