DmitrMakeev commited on
Commit
dbdcf4a
·
verified ·
1 Parent(s): 2eb948a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -0
app.py CHANGED
@@ -284,6 +284,81 @@ def biz_v():
284
 
285
 
286
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287
  @app.route('/upload_csv', methods=['POST'])
288
  def upload_csv():
289
  if 'file' not in request.files:
 
284
 
285
 
286
 
287
+ def clean_phone_number_ss(phone_number):
288
+ return re.sub(r'\D', '', phone_number)
289
+
290
+ DATABASE2 = 'data_gc.db'
291
+
292
+ def verify_phone_number(phone_number):
293
+ full_url_ver = f"{wa_url}{wa_ak}{ws_url_ver}{wa_api_key}"
294
+ payload = {"phoneNumber": phone_number}
295
+ headers = {'Content-Type': 'application/json'}
296
+ response = requests.post(full_url_ver, headers=headers, json=payload)
297
+ if response.status_code == 200:
298
+ response_body = response.json()
299
+ return response_body.get('existsWhatsapp', 'false')
300
+ else:
301
+ return "false"
302
+
303
+ def parse_csv_data(data):
304
+ parsed_data = []
305
+ for item in data:
306
+ for key, value in item.items():
307
+ headers = key.split(';')
308
+ row = value.split(';')
309
+ parsed_data.append(dict(zip(headers, row)))
310
+ return parsed_data
311
+
312
+ def insert_data(data, verify_phone, add_curator):
313
+ global current_curator_index
314
+ with sqlite3.connect(DATABASE2) as conn:
315
+ cursor = conn.cursor()
316
+
317
+ for row in data:
318
+ name = row.get('Name', '')
319
+ phone = row.get('Phone', '').lstrip('+')
320
+ email = row.get('Email', '')
321
+ data_t = row.get('Date', '').strip('"')
322
+
323
+ cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone))
324
+ user_exists = cursor.fetchone()
325
+
326
+ if user_exists:
327
+ print(f"User with email {email} or phone {phone} already exists. Skipping insert.")
328
+ continue
329
+
330
+ if add_curator == "1":
331
+ curator = curators[current_curator_index]
332
+ current_curator_index = (current_curator_index + 1) % len(curators)
333
+ else:
334
+ curator = row.get('curator', '')
335
+
336
+ if verify_phone == "1":
337
+ ws_st = verify_phone_number(phone)
338
+ else:
339
+ ws_st = row.get('ws_st', '')
340
+
341
+ columns = ['name', 'phone', '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', 'orders', 'curator', 'pr1', 'pr2', 'pr3', 'pr4', 'pr5', 'gc_url', 'key_pr', 'n_con', 'canal', 'data_on', 'data_t', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'gcpc']
342
+ values = [name, phone, email, row.get('vk_id', ''), row.get('chat_id', ''), ws_st, row.get('ws_stop', ''), row.get('web_st', 0), row.get('fin_prog', 0), row.get('b_city', ''), row.get('b_fin', ''), row.get('b_ban', ''), row.get('b_ign', ''), row.get('b_baners', ''), row.get('b_butt', ''), row.get('b_mess', ''), row.get('orders', ''), curator, row.get('pr1', ''), row.get('pr2', ''), row.get('pr3', ''), row.get('pr4', ''), row.get('pr5', ''), row.get('gc_url', ''), row.get('key_pr', ''), row.get('n_con', ''), row.get('canal', ''), row.get('data_on', ''), row.get('data_t', ''), row.get('utm_source', ''), row.get('utm_medium', ''), row.get('utm_campaign', ''), row.get('utm_term', ''), row.get('utm_content', ''), row.get('gcpc', '')]
343
+
344
+ placeholders = ', '.join(['?' for _ in columns])
345
+ columns_str = ', '.join(columns)
346
+
347
+ query = f'''
348
+ INSERT INTO contacts ({columns_str})
349
+ VALUES ({placeholders})
350
+ '''
351
+
352
+ try:
353
+ cursor.execute(query, values)
354
+ except Exception as e:
355
+ print(f"Error inserting row: {row}")
356
+ print(f"Error message: {str(e)}")
357
+ conn.rollback()
358
+ raise
359
+
360
+ conn.commit()
361
+
362
  @app.route('/upload_csv', methods=['POST'])
363
  def upload_csv():
364
  if 'file' not in request.files: