DmitrMakeev commited on
Commit
a7d1e77
1 Parent(s): e025122

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -130
app.py CHANGED
@@ -1657,18 +1657,16 @@ 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:
1664
- for key, value in item.items():
1665
- headers = key.split(';')
1666
- row = value.split(';')
1667
- parsed_data.append(dict(zip(headers, row)))
1668
  return parsed_data
1669
 
1670
  def clean_phone_number(phone_number):
1671
- # Удаляем все символы, кроме цифр
1672
  return re.sub(r'\D', '', phone_number)
1673
 
1674
  def insert_data(data, verify_phone, add_curator):
@@ -1677,12 +1675,11 @@ def insert_data(data, verify_phone, add_curator):
1677
  cursor = conn.cursor()
1678
 
1679
  for row in data:
1680
- name = row.get('Name', '')
1681
- phone = row.get('Phone', '').lstrip('+')
1682
  email = row.get('Email', '')
1683
- data_t = row.get('Date', '').strip('"')
1684
 
1685
- # Очистка номера телефона
1686
  phone = clean_phone_number(phone)
1687
 
1688
  cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone))
@@ -1704,7 +1701,7 @@ def insert_data(data, verify_phone, add_curator):
1704
  ws_st = row.get('ws_st', '')
1705
 
1706
  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']
1707
- 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('shop_st', ''), 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', ''), data_t, row.get('utm_source', ''), row.get('utm_medium', ''), row.get('utm_campaign', ''), row.get('utm_term', ''), row.get('utm_content', '')]
1708
 
1709
  placeholders = ', '.join(['?' for _ in columns])
1710
  columns_str = ', '.join(columns)
@@ -1716,7 +1713,6 @@ def insert_data(data, verify_phone, add_curator):
1716
 
1717
  try:
1718
  cursor.execute(query, values)
1719
- # Отправка данных в Google Forms
1720
  user_data = dict(zip(columns, values))
1721
  send_to_google_forms(user_data, gog_url)
1722
  except Exception as e:
@@ -1761,124 +1757,6 @@ def se_upl_csv():
1761
 
1762
 
1763
 
1764
- DATABASE4 = '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
-
1778
-
1779
- def parse_csv_data_b(data):
1780
- parsed_data = []
1781
- for row in data:
1782
- parsed_row = {}
1783
- for key, value in row.items():
1784
- headers = next(csv.reader([key]))
1785
- values = next(csv.reader([value]))
1786
- parsed_row.update(dict(zip(headers, values)))
1787
- parsed_data.append(parsed_row)
1788
- return parsed_data
1789
-
1790
- def clean_phone_number_b(phone_number):
1791
- return re.sub(r'\D', '', phone_number)
1792
-
1793
- def insert_data_b(data, verify_phone, add_curator):
1794
- global current_curator_index
1795
- conn = sqlite3.connect(DATABASE4)
1796
- cursor = conn.cursor()
1797
-
1798
- for row in data:
1799
- name = row.get('Имя', '')
1800
- phone = row.get('WhatsApp', '').lstrip('+')
1801
- email = row.get('Email', '')
1802
- data_t = row.get('Дата', '').strip('"')
1803
-
1804
- phone = clean_phone_number_b(phone)
1805
-
1806
- cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone))
1807
- user_exists = cursor.fetchone()
1808
-
1809
- if user_exists:
1810
- print(f"User with email {email} or phone {phone} already exists. Skipping insert.")
1811
- continue
1812
-
1813
- if add_curator == "1":
1814
- curator = curators[current_curator_index]
1815
- current_curator_index = (current_curator_index + 1) % len(curators)
1816
- else:
1817
- curator = row.get('Куратор', '')
1818
-
1819
- if verify_phone == "1":
1820
- ws_st = verify_phone_number_b(phone)
1821
- else:
1822
- ws_st = row.get('ws_st', '')
1823
-
1824
- 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']
1825
- 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', '')]
1826
-
1827
- placeholders = ', '.join(['?' for _ in columns])
1828
- columns_str = ', '.join(columns)
1829
-
1830
- query = f'''
1831
- INSERT INTO contacts ({columns_str})
1832
- VALUES ({placeholders})
1833
- '''
1834
-
1835
- try:
1836
- cursor.execute(query, values)
1837
- user_data = dict(zip(columns, values))
1838
- send_to_google_forms(user_data, gog_url)
1839
- except Exception as e:
1840
- print(f"Error inserting row: {row}")
1841
- print(f"Error message: {str(e)}")
1842
- conn.rollback()
1843
- raise
1844
-
1845
- conn.commit()
1846
- conn.close()
1847
-
1848
- @app.route('/upload_csv_b', methods=['POST'])
1849
- def upload_csv_b():
1850
- if 'file' not in request.files:
1851
- return jsonify({"error": "No file part"}), 400
1852
- file = request.files['file']
1853
- if file.filename == '':
1854
- return jsonify({"error": "No selected file"}), 400
1855
- if file and file.filename.endswith('.csv'):
1856
- stream = io.StringIO(file.stream.read().decode("UTF8"), newline=None)
1857
- csv_input = csv.DictReader(stream)
1858
- data = [row for row in csv_input]
1859
- parsed_data = parse_csv_data_b(data)
1860
- verify_phone = request.form.get('verify_phone', '0')
1861
- add_curator = request.form.get('add_curator', '0')
1862
- print(f"Verify Phone: {verify_phone}")
1863
- print(f"Add Curator: {add_curator}")
1864
- insert_data_b(parsed_data, verify_phone, add_curator)
1865
- return jsonify({"message": "Data uploaded and inserted successfully"})
1866
- return jsonify({"error": "Invalid file format"}), 400
1867
-
1868
- @app.route('/upl_csv_b', methods=['GET'])
1869
- def se_upl_csv_b():
1870
- api_sys_control = request.args.get('api_sys')
1871
- if api_sys_control != api_key_sys:
1872
- return "EUR 22", 200
1873
- return render_template('upl_csv_b.html')
1874
-
1875
-
1876
-
1877
-
1878
-
1879
-
1880
-
1881
-
1882
 
1883
 
1884
 
 
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:
1664
+ headers = list(item.keys())
1665
+ row = list(item.values())
1666
+ parsed_data.append(dict(zip(headers, row)))
 
1667
  return parsed_data
1668
 
1669
  def clean_phone_number(phone_number):
 
1670
  return re.sub(r'\D', '', phone_number)
1671
 
1672
  def insert_data(data, verify_phone, add_curator):
 
1675
  cursor = conn.cursor()
1676
 
1677
  for row in data:
1678
+ name = row.get('Имя', '')
1679
+ phone = row.get('WhatsApp', '').lstrip('+')
1680
  email = row.get('Email', '')
1681
+ data_t = row.get('Дата', '').strip('"')
1682
 
 
1683
  phone = clean_phone_number(phone)
1684
 
1685
  cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone))
 
1701
  ws_st = row.get('ws_st', '')
1702
 
1703
  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']
1704
+ values = [name, phone, email, row.get('ВКонтакте', ''), row.get('Телеграм', ''), ws_st, row.get('GetCurse', ''), row.get('Город', ''), row.get('Реплики', ''), row.get('curator', ''), row.get('Статус покупки', ''), row.get('Канал трафика', ''), data_t, row.get('Ключ PR', ''), row.get('Канал', ''), row.get('Дата', '')]
1705
 
1706
  placeholders = ', '.join(['?' for _ in columns])
1707
  columns_str = ', '.join(columns)
 
1713
 
1714
  try:
1715
  cursor.execute(query, values)
 
1716
  user_data = dict(zip(columns, values))
1717
  send_to_google_forms(user_data, gog_url)
1718
  except Exception as e:
 
1757
 
1758
 
1759
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1760
 
1761
 
1762