DmitrMakeev commited on
Commit
35b861d
1 Parent(s): e882c31

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +108 -6
app.py CHANGED
@@ -1657,7 +1657,7 @@ def verify_phone_number(phone_number):
1657
  return response_body.get('existsWhatsapp', 'false')
1658
  else:
1659
  return "Error"
1660
-
1661
  def parse_csv_data(data):
1662
  parsed_data = []
1663
  for item in data:
@@ -1748,11 +1748,6 @@ def upload_csv():
1748
  return jsonify({"message": "Data uploaded and inserted successfully"})
1749
  return jsonify({"error": "Invalid file format"}), 400
1750
 
1751
-
1752
-
1753
-
1754
-
1755
-
1756
  @app.route('/upl_csv', methods=['GET'])
1757
  def se_upl_csv():
1758
  api_sys_control = request.args.get('api_sys')
@@ -1766,7 +1761,114 @@ def se_upl_csv():
1766
 
1767
 
1768
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1769
 
 
 
 
 
 
 
1770
 
1771
 
1772
 
 
1657
  return response_body.get('existsWhatsapp', 'false')
1658
  else:
1659
  return "Error"
1660
+
1661
  def parse_csv_data(data):
1662
  parsed_data = []
1663
  for item in data:
 
1748
  return jsonify({"message": "Data uploaded and inserted successfully"})
1749
  return jsonify({"error": "Invalid file format"}), 400
1750
 
 
 
 
 
 
1751
  @app.route('/upl_csv', methods=['GET'])
1752
  def se_upl_csv():
1753
  api_sys_control = request.args.get('api_sys')
 
1761
 
1762
 
1763
 
1764
+ DATABASE2 = 'data_gc.db'
1765
+
1766
+ def verify_phone_number_b(phone_number):
1767
+ full_url_ver = f"{wa_url}{wa_ak}{ws_url_ver}{wa_api_key}"
1768
+ payload = {"phoneNumber": phone_number}
1769
+ headers = {'Content-Type': 'application/json'}
1770
+ response = requests.post(full_url_ver, headers=headers, json=payload)
1771
+ if response.status_code == 200:
1772
+ response_body = response.json()
1773
+ return response_body.get('existsWhatsapp', 'false')
1774
+ else:
1775
+ return "Error"
1776
+
1777
+ def parse_csv_data_b(data):
1778
+ parsed_data = []
1779
+ for row in data:
1780
+ parsed_row = {}
1781
+ for key, value in row.items():
1782
+ headers = key.split(',')
1783
+ values = value.split(',')
1784
+ parsed_row.update(dict(zip(headers, values)))
1785
+ parsed_data.append(parsed_row)
1786
+ return parsed_data
1787
+
1788
+ def clean_phone_number_b(phone_number):
1789
+ return re.sub(r'\D', '', phone_number)
1790
+
1791
+ def insert_data_b(data, verify_phone, add_curator):
1792
+ global current_curator_index
1793
+ conn = sqlite3.connect(DATABASE2)
1794
+ cursor = conn.cursor()
1795
+
1796
+ for row in data:
1797
+ name = row.get('Имя', '')
1798
+ phone = row.get('WhatsApp', '').lstrip('+')
1799
+ email = row.get('Email', '')
1800
+ data_t = row.get('Дата', '').strip('"')
1801
+
1802
+ phone = clean_phone_number_b(phone)
1803
+
1804
+ cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone))
1805
+ user_exists = cursor.fetchone()
1806
+
1807
+ if user_exists:
1808
+ print(f"User with email {email} or phone {phone} already exists. Skipping insert.")
1809
+ continue
1810
+
1811
+ if add_curator == "1":
1812
+ curator = curators[current_curator_index]
1813
+ current_curator_index = (current_curator_index + 1) % len(curators)
1814
+ else:
1815
+ curator = row.get('Куратор', '')
1816
+
1817
+ if verify_phone == "1":
1818
+ ws_st = verify_phone_number_b(phone)
1819
+ else:
1820
+ ws_st = row.get('ws_st', '')
1821
+
1822
+ 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', 'shop_st', 'curator', 'pr1', 'pr2', 'pr3', 'pr4', 'pr5', 'gc_url', 'key_pr', 'n_con', 'canal', 'data_t', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content']
1823
+ values = [name, phone, email, row.get('ВКонтакте', ''), row.get('Телеграм', ''), ws_st, row.get('ws_stop', ''), row.get('web_st', 0), row.get('fin_prog', 0), row.get('Город', ''), row.get('b_fin', ''), row.get('b_ban', ''), row.get('b_ign', ''), row.get('b_baners', ''), row.get('b_butt', ''), row.get('Реплики', ''), row.get('Статус покупки', ''), curator, row.get('pr1', ''), row.get('pr2', ''), row.get('pr3', ''), row.get('Канал трафика', ''), row.get('pr5', ''), row.get('GetCurse', ''), row.get('Ключ PR', ''), row.get('n_con', ''), row.get('Канал', ''), data_t, row.get('utm_source', ''), row.get('utm_medium', ''), row.get('utm_campaign', ''), row.get('utm_term', ''), row.get('utm_content', '')]
1824
+
1825
+ placeholders = ', '.join(['?' for _ in columns])
1826
+ columns_str = ', '.join(columns)
1827
+
1828
+ query = f'''
1829
+ INSERT INTO contacts ({columns_str})
1830
+ VALUES ({placeholders})
1831
+ '''
1832
+
1833
+ try:
1834
+ cursor.execute(query, values)
1835
+ user_data = dict(zip(columns, values))
1836
+ send_to_google_forms(user_data, gog_url)
1837
+ except Exception as e:
1838
+ print(f"Error inserting row: {row}")
1839
+ print(f"Error message: {str(e)}")
1840
+ conn.rollback()
1841
+ raise
1842
+
1843
+ conn.commit()
1844
+ conn.close()
1845
+
1846
+ @app.route('/upload_csv_b', methods=['POST'])
1847
+ def upload_csv_b():
1848
+ if 'file' not in request.files:
1849
+ return jsonify({"error": "No file part"}), 400
1850
+ file = request.files['file']
1851
+ if file.filename == '':
1852
+ return jsonify({"error": "No selected file"}), 400
1853
+ if file and file.filename.endswith('.csv'):
1854
+ stream = io.StringIO(file.stream.read().decode("UTF8"), newline=None)
1855
+ csv_input = csv.DictReader(stream)
1856
+ data = [row for row in csv_input]
1857
+ parsed_data = parse_csv_data_b(data)
1858
+ verify_phone = request.form.get('verify_phone', '0')
1859
+ add_curator = request.form.get('add_curator', '0')
1860
+ print(f"Verify Phone: {verify_phone}")
1861
+ print(f"Add Curator: {add_curator}")
1862
+ insert_data_b(parsed_data, verify_phone, add_curator)
1863
+ return jsonify({"message": "Data uploaded and inserted successfully"})
1864
+ return jsonify({"error": "Invalid file format"}), 400
1865
 
1866
+ @app.route('/upl_csv_b', methods=['GET'])
1867
+ def se_upl_csv_b():
1868
+ api_sys_control = request.args.get('api_sys')
1869
+ if api_sys_control != api_key_sys:
1870
+ return "EUR 22", 200
1871
+ return render_template('upl_csv_b.html')
1872
 
1873
 
1874