Update app.py
Browse files
app.py
CHANGED
|
@@ -1834,30 +1834,15 @@ def download_csv():
|
|
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
-
|
| 1838 |
-
curators = ["Anna", "Ekaterina", "Ivan", "Maria", "Sergey", "Olga", "Alex", "Natalia", "Dmitry", "Elena"]
|
| 1839 |
-
current_curator_index = 0
|
| 1840 |
-
|
| 1841 |
-
# Функция для проверки номера телефона через API
|
| 1842 |
-
def j_verify_phone_number(phone_number):
|
| 1843 |
-
full_url_ver = f"{wa_url}{wa_ak}{ws_url_ver}{wa_api_key}"
|
| 1844 |
-
payload = {"phoneNumber": phone_number}
|
| 1845 |
-
headers = {'Content-Type': 'application/json'}
|
| 1846 |
-
response = requests.post(full_url_ver, headers=headers, json=payload)
|
| 1847 |
-
if response.status_code == 200:
|
| 1848 |
-
response_body = response.json()
|
| 1849 |
-
return response_body.get('existsWhatsapp', 'false')
|
| 1850 |
-
else:
|
| 1851 |
-
return "Error"
|
| 1852 |
|
| 1853 |
# Функция для очистки номера телефона
|
| 1854 |
-
def
|
| 1855 |
return re.sub(r'\D', '', phone_number)
|
| 1856 |
|
| 1857 |
# Функция для вставки данных в базу данных
|
| 1858 |
-
def
|
| 1859 |
-
|
| 1860 |
-
conn = sqlite3.connect(DATABASE5) # Подключаемся к базе данных
|
| 1861 |
cursor = conn.cursor()
|
| 1862 |
|
| 1863 |
for row in data:
|
|
@@ -1867,7 +1852,7 @@ def j_insert_data(data, verify_phone, add_curator):
|
|
| 1867 |
data_t = row.get('data_t', '').strip('"')
|
| 1868 |
|
| 1869 |
# Очистка номера телефона
|
| 1870 |
-
phone =
|
| 1871 |
|
| 1872 |
cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone))
|
| 1873 |
user_exists = cursor.fetchone()
|
|
@@ -1876,19 +1861,8 @@ def j_insert_data(data, verify_phone, add_curator):
|
|
| 1876 |
print(f"User with email {email} or phone {phone} already exists. Skipping insert.")
|
| 1877 |
continue
|
| 1878 |
|
| 1879 |
-
if add_curator == "1":
|
| 1880 |
-
curator = curators[current_curator_index]
|
| 1881 |
-
current_curator_index = (current_curator_index + 1) % len(curators)
|
| 1882 |
-
else:
|
| 1883 |
-
curator = row.get('curator', '')
|
| 1884 |
-
|
| 1885 |
-
if verify_phone == "1":
|
| 1886 |
-
ws_st = j_verify_phone_number(phone)
|
| 1887 |
-
else:
|
| 1888 |
-
ws_st = row.get('ws_st', '')
|
| 1889 |
-
|
| 1890 |
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']
|
| 1891 |
-
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', '')]
|
| 1892 |
|
| 1893 |
placeholders = ', '.join(['?' for _ in columns])
|
| 1894 |
columns_str = ', '.join(columns)
|
|
@@ -1900,29 +1874,18 @@ def j_insert_data(data, verify_phone, add_curator):
|
|
| 1900 |
|
| 1901 |
try:
|
| 1902 |
cursor.execute(query, values)
|
| 1903 |
-
# Отправка данных в Google Forms
|
| 1904 |
-
user_data = dict(zip(columns, values))
|
| 1905 |
-
send_to_google_forms(user_data, gog_url)
|
| 1906 |
except Exception as e:
|
| 1907 |
print(f"Error inserting row: {row}")
|
| 1908 |
print(f"Error message: {str(e)}")
|
| 1909 |
conn.rollback()
|
| 1910 |
-
|
| 1911 |
|
| 1912 |
conn.commit()
|
| 1913 |
conn.close()
|
| 1914 |
|
| 1915 |
-
# Функция для отправки данных в Google Forms
|
| 1916 |
-
def send_to_google_forms(user_data, gog_url):
|
| 1917 |
-
headers = {'Content-Type': 'application/json'}
|
| 1918 |
-
response = requests.post(gog_url, headers=headers, json=user_data)
|
| 1919 |
-
if response.status_code != 200:
|
| 1920 |
-
print(f"Failed to send data to Google Forms. Status code: {response.status_code}")
|
| 1921 |
-
print(f"Response: {response.text}")
|
| 1922 |
-
|
| 1923 |
# Маршрут для загрузки JSON-файла
|
| 1924 |
@app.route('/upload_json', methods=['POST'])
|
| 1925 |
-
def
|
| 1926 |
if 'file' not in request.files:
|
| 1927 |
return jsonify({"error": "No file part"}), 400
|
| 1928 |
file = request.files['file']
|
|
@@ -1930,20 +1893,13 @@ def j_upload_json():
|
|
| 1930 |
return jsonify({"error": "No selected file"}), 400
|
| 1931 |
if file and file.filename.endswith('.json'):
|
| 1932 |
data = json.load(file)
|
| 1933 |
-
|
| 1934 |
-
add_curator = request.form.get('add_curator', '0')
|
| 1935 |
-
print(f"Verify Phone: {verify_phone}")
|
| 1936 |
-
print(f"Add Curator: {add_curator}")
|
| 1937 |
-
j_insert_data(data, verify_phone, add_curator)
|
| 1938 |
return jsonify({"message": "Data uploaded and inserted successfully"})
|
| 1939 |
return jsonify({"error": "Invalid file format"}), 400
|
| 1940 |
|
| 1941 |
# Маршрут для отображения формы загрузки JSON
|
| 1942 |
@app.route('/upl_json', methods=['GET'])
|
| 1943 |
-
def
|
| 1944 |
-
api_sys_control = request.args.get('api_sys')
|
| 1945 |
-
if api_sys_control != api_key_sys:
|
| 1946 |
-
return "EUR 22", 200
|
| 1947 |
return render_template('upload_json.html')
|
| 1948 |
|
| 1949 |
|
|
@@ -1963,8 +1919,6 @@ def j_se_upl_json():
|
|
| 1963 |
|
| 1964 |
|
| 1965 |
|
| 1966 |
-
|
| 1967 |
-
|
| 1968 |
|
| 1969 |
|
| 1970 |
@app.route('/gc_in', methods=['GET'])
|
|
|
|
| 1834 |
|
| 1835 |
|
| 1836 |
|
| 1837 |
+
DATABASE = 'data_gc.db'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1838 |
|
| 1839 |
# Функция для очистки номера телефона
|
| 1840 |
+
def clean_phone_number(phone_number):
|
| 1841 |
return re.sub(r'\D', '', phone_number)
|
| 1842 |
|
| 1843 |
# Функция для вставки данных в базу данных
|
| 1844 |
+
def insert_data(data):
|
| 1845 |
+
conn = sqlite3.connect(DATABASE) # Подключаемся к базе данных
|
|
|
|
| 1846 |
cursor = conn.cursor()
|
| 1847 |
|
| 1848 |
for row in data:
|
|
|
|
| 1852 |
data_t = row.get('data_t', '').strip('"')
|
| 1853 |
|
| 1854 |
# Очистка номера телефона
|
| 1855 |
+
phone = clean_phone_number(phone)
|
| 1856 |
|
| 1857 |
cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone))
|
| 1858 |
user_exists = cursor.fetchone()
|
|
|
|
| 1861 |
print(f"User with email {email} or phone {phone} already exists. Skipping insert.")
|
| 1862 |
continue
|
| 1863 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1864 |
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']
|
| 1865 |
+
values = [name, phone, email, row.get('vk_id', ''), row.get('chat_id', ''), row.get('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', ''), row.get('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', '')]
|
| 1866 |
|
| 1867 |
placeholders = ', '.join(['?' for _ in columns])
|
| 1868 |
columns_str = ', '.join(columns)
|
|
|
|
| 1874 |
|
| 1875 |
try:
|
| 1876 |
cursor.execute(query, values)
|
|
|
|
|
|
|
|
|
|
| 1877 |
except Exception as e:
|
| 1878 |
print(f"Error inserting row: {row}")
|
| 1879 |
print(f"Error message: {str(e)}")
|
| 1880 |
conn.rollback()
|
| 1881 |
+
continue
|
| 1882 |
|
| 1883 |
conn.commit()
|
| 1884 |
conn.close()
|
| 1885 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1886 |
# Маршрут для загрузки JSON-файла
|
| 1887 |
@app.route('/upload_json', methods=['POST'])
|
| 1888 |
+
def upload_json():
|
| 1889 |
if 'file' not in request.files:
|
| 1890 |
return jsonify({"error": "No file part"}), 400
|
| 1891 |
file = request.files['file']
|
|
|
|
| 1893 |
return jsonify({"error": "No selected file"}), 400
|
| 1894 |
if file and file.filename.endswith('.json'):
|
| 1895 |
data = json.load(file)
|
| 1896 |
+
insert_data(data)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1897 |
return jsonify({"message": "Data uploaded and inserted successfully"})
|
| 1898 |
return jsonify({"error": "Invalid file format"}), 400
|
| 1899 |
|
| 1900 |
# Маршрут для отображения формы загрузки JSON
|
| 1901 |
@app.route('/upl_json', methods=['GET'])
|
| 1902 |
+
def display_form():
|
|
|
|
|
|
|
|
|
|
| 1903 |
return render_template('upload_json.html')
|
| 1904 |
|
| 1905 |
|
|
|
|
| 1919 |
|
| 1920 |
|
| 1921 |
|
|
|
|
|
|
|
| 1922 |
|
| 1923 |
|
| 1924 |
@app.route('/gc_in', methods=['GET'])
|