DmitrMakeev
commited on
Commit
•
336cc17
1
Parent(s):
ac4eec7
Update app.py
Browse files
app.py
CHANGED
@@ -1783,6 +1783,196 @@ def download_csv():
|
|
1783 |
|
1784 |
|
1785 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1786 |
@app.route('/gc_in', methods=['GET'])
|
1787 |
def add_data_gc_in():
|
1788 |
global current_curator_index
|
|
|
1783 |
|
1784 |
|
1785 |
|
1786 |
+
|
1787 |
+
|
1788 |
+
|
1789 |
+
|
1790 |
+
|
1791 |
+
|
1792 |
+
|
1793 |
+
|
1794 |
+
|
1795 |
+
|
1796 |
+
|
1797 |
+
|
1798 |
+
|
1799 |
+
|
1800 |
+
|
1801 |
+
|
1802 |
+
|
1803 |
+
|
1804 |
+
|
1805 |
+
|
1806 |
+
|
1807 |
+
|
1808 |
+
|
1809 |
+
|
1810 |
+
|
1811 |
+
|
1812 |
+
|
1813 |
+
|
1814 |
+
|
1815 |
+
|
1816 |
+
|
1817 |
+
|
1818 |
+
|
1819 |
+
|
1820 |
+
|
1821 |
+
|
1822 |
+
|
1823 |
+
|
1824 |
+
|
1825 |
+
|
1826 |
+
|
1827 |
+
|
1828 |
+
|
1829 |
+
|
1830 |
+
|
1831 |
+
|
1832 |
+
|
1833 |
+
|
1834 |
+
|
1835 |
+
|
1836 |
+
|
1837 |
+
|
1838 |
+
|
1839 |
+
|
1840 |
+
|
1841 |
+
|
1842 |
+
|
1843 |
+
|
1844 |
+
|
1845 |
+
|
1846 |
+
DATABASES = ['data_gc.db']
|
1847 |
+
curators = ["Anna", "Ekaterina", "Ivan", "Maria", "Sergey", "Olga", "Alex", "Natalia", "Dmitry", "Elena"]
|
1848 |
+
current_curator_index = 0
|
1849 |
+
|
1850 |
+
def j_verify_phone_number(phone_number):
|
1851 |
+
full_url_ver = f"{wa_url}{wa_ak}{ws_url_ver}{wa_api_key}"
|
1852 |
+
payload = {"phoneNumber": phone_number}
|
1853 |
+
headers = {'Content-Type': 'application/json'}
|
1854 |
+
response = requests.post(full_url_ver, headers=headers, json=payload)
|
1855 |
+
if response.status_code == 200:
|
1856 |
+
response_body = response.json()
|
1857 |
+
return response_body.get('existsWhatsapp', 'false')
|
1858 |
+
else:
|
1859 |
+
return "Error"
|
1860 |
+
|
1861 |
+
def j_parse_json_data(data):
|
1862 |
+
return json.loads(data)
|
1863 |
+
|
1864 |
+
def j_clean_phone_number(phone_number):
|
1865 |
+
return re.sub(r'\D', '', phone_number)
|
1866 |
+
|
1867 |
+
def j_insert_data(data, verify_phone, add_curator):
|
1868 |
+
global current_curator_index
|
1869 |
+
for db_name in DATABASES:
|
1870 |
+
conn = sqlite3.connect(db_name)
|
1871 |
+
cursor = conn.cursor()
|
1872 |
+
|
1873 |
+
for row in data:
|
1874 |
+
name = row.get('Имя', '')
|
1875 |
+
phone = row.get('WhatsApp', '').lstrip('+')
|
1876 |
+
email = row.get('Email', '')
|
1877 |
+
data_t = row.get('Дата', '').strip('"')
|
1878 |
+
|
1879 |
+
phone = j_clean_phone_number(phone)
|
1880 |
+
|
1881 |
+
cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone))
|
1882 |
+
user_exists = cursor.fetchone()
|
1883 |
+
|
1884 |
+
if user_exists:
|
1885 |
+
print(f"User with email {email} or phone {phone} already exists. Skipping insert.")
|
1886 |
+
continue
|
1887 |
+
|
1888 |
+
if add_curator == "1":
|
1889 |
+
curator = curators[current_curator_index]
|
1890 |
+
current_curator_index = (current_curator_index + 1) % len(curators)
|
1891 |
+
else:
|
1892 |
+
curator = row.get('Куратор', '')
|
1893 |
+
|
1894 |
+
if verify_phone == "1":
|
1895 |
+
ws_st = j_verify_phone_number(phone)
|
1896 |
+
else:
|
1897 |
+
ws_st = row.get('ws_st', '')
|
1898 |
+
|
1899 |
+
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']
|
1900 |
+
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('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', '')]
|
1901 |
+
|
1902 |
+
placeholders = ', '.join(['?' for _ in columns])
|
1903 |
+
columns_str = ', '.join(columns)
|
1904 |
+
|
1905 |
+
query = f'''
|
1906 |
+
INSERT INTO contacts ({columns_str})
|
1907 |
+
VALUES ({placeholders})
|
1908 |
+
'''
|
1909 |
+
|
1910 |
+
try:
|
1911 |
+
cursor.execute(query, values)
|
1912 |
+
user_data = dict(zip(columns, values))
|
1913 |
+
send_to_google_forms(user_data, gog_url)
|
1914 |
+
except Exception as e:
|
1915 |
+
print(f"Error inserting row: {row}")
|
1916 |
+
print(f"Error message: {str(e)}")
|
1917 |
+
conn.rollback()
|
1918 |
+
raise
|
1919 |
+
|
1920 |
+
conn.commit()
|
1921 |
+
conn.close()
|
1922 |
+
|
1923 |
+
@app.route('/j_upload_json', methods=['POST'])
|
1924 |
+
def j_upload_json():
|
1925 |
+
if 'file' not in request.files:
|
1926 |
+
return jsonify({"error": "No file part"}), 400
|
1927 |
+
file = request.files['file']
|
1928 |
+
if file.filename == '':
|
1929 |
+
return jsonify({"error": "No selected file"}), 400
|
1930 |
+
if file and file.filename.endswith('.json'):
|
1931 |
+
stream = io.StringIO(file.stream.read().decode("UTF8"), newline=None)
|
1932 |
+
data = stream.getvalue()
|
1933 |
+
parsed_data = j_parse_json_data(data)
|
1934 |
+
verify_phone = request.form.get('verify_phone', '0')
|
1935 |
+
add_curator = request.form.get('add_curator', '0')
|
1936 |
+
print(f"Verify Phone: {verify_phone}")
|
1937 |
+
print(f"Add Curator: {add_curator}")
|
1938 |
+
j_insert_data(parsed_data, verify_phone, add_curator)
|
1939 |
+
return jsonify({"message": "Data uploaded and inserted successfully"})
|
1940 |
+
return jsonify({"error": "Invalid file format"}), 400
|
1941 |
+
|
1942 |
+
@app.route('/j_upl_json', methods=['GET'])
|
1943 |
+
def j_se_upl_json():
|
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('j_upl_json.html')
|
1948 |
+
|
1949 |
+
|
1950 |
+
|
1951 |
+
|
1952 |
+
|
1953 |
+
|
1954 |
+
|
1955 |
+
|
1956 |
+
|
1957 |
+
|
1958 |
+
|
1959 |
+
|
1960 |
+
|
1961 |
+
|
1962 |
+
|
1963 |
+
|
1964 |
+
|
1965 |
+
|
1966 |
+
|
1967 |
+
|
1968 |
+
|
1969 |
+
|
1970 |
+
|
1971 |
+
|
1972 |
+
|
1973 |
+
|
1974 |
+
|
1975 |
+
|
1976 |
@app.route('/gc_in', methods=['GET'])
|
1977 |
def add_data_gc_in():
|
1978 |
global current_curator_index
|