Update app.py
Browse files
app.py
CHANGED
|
@@ -771,103 +771,109 @@ def send_request():
|
|
| 771 |
|
| 772 |
DATABASE_NAME = 'data_gc.db'
|
| 773 |
|
| 774 |
-
def
|
| 775 |
-
conn = sqlite3.connect(
|
| 776 |
-
|
| 777 |
-
|
| 778 |
-
def select_user_by_email(conn, email):
|
| 779 |
-
cur = conn.cursor()
|
| 780 |
-
cur.execute("SELECT web_st FROM contacts WHERE email=?", (email,))
|
| 781 |
-
return cur.fetchone()
|
| 782 |
-
|
| 783 |
-
def update_user(conn, data):
|
| 784 |
-
query = """
|
| 785 |
-
UPDATE contacts SET name = ?, phone = ?, email = ?, b_city = ?, b_fin = ?, b_ban = ?, b_ign = ?, b_baners = ?, b_butt = ?, b_mess = ?,
|
| 786 |
-
vk_id = ?, chat_id = ?, ws_st = ?, ws_stop = ?, web_st = ?, fin_prog = ?, shop_st = ?, curator = ?, pr1 = ?, pr2 = ?, pr3 = ?, pr4 = ?, pr5 = ?,
|
| 787 |
-
ad_url = ?, key_pr = ?, n_con = ?, canal = ?, data_t = ?, web_st = ? WHERE email = ?
|
| 788 |
-
"""
|
| 789 |
-
cur = conn.cursor()
|
| 790 |
-
cur.execute(query, data)
|
| 791 |
-
conn.commit()
|
| 792 |
-
|
| 793 |
-
def insert_user(conn, data):
|
| 794 |
-
query = """
|
| 795 |
-
INSERT INTO contacts (name, phone, email, b_city, b_fin, b_ban, b_ign, b_baners, b_butt, b_mess, vk_id, chat_id, ws_st, ws_stop, web_st,
|
| 796 |
-
fin_prog, shop_st, curator, pr1, pr2, pr3, pr4, pr5, ad_url, key_pr, n_con, canal, data_t, web_st) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
| 797 |
-
"""
|
| 798 |
-
cur = conn.cursor()
|
| 799 |
-
cur.execute(query, data)
|
| 800 |
-
conn.commit()
|
| 801 |
|
| 802 |
-
|
| 803 |
-
email = user_data['email']
|
| 804 |
logging.debug(f"Processing user with email: {email}")
|
| 805 |
|
| 806 |
-
|
|
|
|
| 807 |
logging.debug(f"User found: {user}")
|
| 808 |
-
|
| 809 |
-
transformed_data = {
|
| 810 |
-
|
| 811 |
-
|
| 812 |
-
|
| 813 |
-
|
| 814 |
-
|
| 815 |
-
|
| 816 |
-
'b_ign': user_data.get('b_ign', ''),
|
| 817 |
-
'b_baners': user_data.get('b_baners', '0'),
|
| 818 |
-
'b_butt': user_data.get('b_butt', '-1'),
|
| 819 |
-
'b_mess': user_data.get('b_mess', ''),
|
| 820 |
-
'vk_id': '',
|
| 821 |
-
'chat_id': '',
|
| 822 |
-
'ws_st': '',
|
| 823 |
-
'ws_stop': '',
|
| 824 |
-
'web_st': '',
|
| 825 |
-
'fin_prog': '',
|
| 826 |
-
'shop_st': '',
|
| 827 |
-
'curator': '',
|
| 828 |
-
'pr1': '',
|
| 829 |
-
'pr2': '',
|
| 830 |
-
'pr3': '',
|
| 831 |
-
'pr4': '',
|
| 832 |
-
'pr5': '',
|
| 833 |
-
'ad_url': '',
|
| 834 |
-
'key_pr': '',
|
| 835 |
-
'n_con': '',
|
| 836 |
-
'canal': '',
|
| 837 |
-
'data_t': ''
|
| 838 |
-
}
|
| 839 |
logging.debug(f"Transformed data: {transformed_data}")
|
| 840 |
-
|
| 841 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 842 |
logging.debug(f"Transformed data after phone processing: {transformed_data}")
|
| 843 |
-
|
| 844 |
-
web_st_value = 0
|
| 845 |
-
logging.debug(f"Calculated web_st_value: {web_st_value}")
|
| 846 |
-
|
| 847 |
-
transformed_data['web_st'] = web_st_value
|
| 848 |
-
|
| 849 |
if user:
|
| 850 |
-
|
| 851 |
-
|
| 852 |
-
|
| 853 |
-
else:
|
| 854 |
-
insert_data = list(transformed_data.values())
|
| 855 |
-
logging.debug(f"Insert query: {query} with values: {insert_data}")
|
| 856 |
-
insert_user(conn, insert_data)
|
| 857 |
-
|
| 858 |
-
logging.debug(f"User with email {email} processed successfully")
|
| 859 |
|
| 860 |
-
|
| 861 |
-
|
| 862 |
-
|
| 863 |
-
|
| 864 |
-
|
| 865 |
-
|
| 866 |
-
|
| 867 |
-
|
| 868 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 869 |
conn.close()
|
| 870 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 871 |
|
| 872 |
|
| 873 |
|
|
|
|
| 771 |
|
| 772 |
DATABASE_NAME = 'data_gc.db'
|
| 773 |
|
| 774 |
+
def update_or_insert_user(db_name, user_data, mapping_template):
|
| 775 |
+
conn = sqlite3.connect(db_name)
|
| 776 |
+
cursor = conn.cursor()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 777 |
|
| 778 |
+
email = user_data.get('email')
|
|
|
|
| 779 |
logging.debug(f"Processing user with email: {email}")
|
| 780 |
|
| 781 |
+
cursor.execute("SELECT web_st FROM contacts WHERE email = ?", (email,))
|
| 782 |
+
user = cursor.fetchone()
|
| 783 |
logging.debug(f"User found: {user}")
|
| 784 |
+
|
| 785 |
+
transformed_data = {}
|
| 786 |
+
for json_key, db_column in mapping_template.items():
|
| 787 |
+
value = user_data.get(json_key, "")
|
| 788 |
+
if isinstance(value, list):
|
| 789 |
+
transformed_data[db_column] = "-1" if value else "0"
|
| 790 |
+
else:
|
| 791 |
+
transformed_data[db_column] = str(value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 792 |
logging.debug(f"Transformed data: {transformed_data}")
|
| 793 |
+
|
| 794 |
+
required_fields = [
|
| 795 |
+
"vk_id", "chat_id", "ws_st", "ws_stop", "web_st", "fin_prog",
|
| 796 |
+
"b_city", "b_fin", "b_ban", "b_ign", "b_baners", "b_butt", "b_mess",
|
| 797 |
+
"shop_st", "curator", "pr1", "pr2", "pr3", "pr4", "pr5", "ad_url",
|
| 798 |
+
"key_pr", "n_con", "canal", "data_t"
|
| 799 |
+
]
|
| 800 |
+
|
| 801 |
+
for field in required_fields:
|
| 802 |
+
if field not in transformed_data:
|
| 803 |
+
transformed_data[field] = ""
|
| 804 |
+
logging.debug(f"Transformed data after adding required fields: {transformed_data}")
|
| 805 |
+
|
| 806 |
+
if 'phone' in user_data:
|
| 807 |
+
phone = user_data['phone']
|
| 808 |
+
if phone.startswith('+'):
|
| 809 |
+
phone = phone[1:]
|
| 810 |
+
transformed_data['phone'] = phone
|
| 811 |
logging.debug(f"Transformed data after phone processing: {transformed_data}")
|
| 812 |
+
|
| 813 |
+
web_st_value = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
| 814 |
if user:
|
| 815 |
+
web_st_value = int(user[0]) if user[0] else 0
|
| 816 |
+
web_st_value += 1
|
| 817 |
+
logging.debug(f"Calculated web_st_value: {web_st_value}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 818 |
|
| 819 |
+
update_query = "UPDATE contacts SET "
|
| 820 |
+
update_values = []
|
| 821 |
+
for column, value in transformed_data.items():
|
| 822 |
+
update_query += f"{column} = ?, "
|
| 823 |
+
update_values.append(value)
|
| 824 |
+
update_query += "web_st = ? WHERE email = ?"
|
| 825 |
+
update_values.extend([web_st_value, email])
|
| 826 |
+
logging.debug(f"Update query: {update_query} with values: {update_values}")
|
| 827 |
+
cursor.execute(update_query, update_values)
|
| 828 |
+
|
| 829 |
+
if cursor.rowcount == 0:
|
| 830 |
+
columns = ', '.join(transformed_data.keys()) + ", web_st"
|
| 831 |
+
placeholders = ', '.join('?' for _ in transformed_data) + ", ?"
|
| 832 |
+
insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
|
| 833 |
+
insert_values = list(transformed_data.values()) + [web_st_value]
|
| 834 |
+
logging.debug(f"Insert query: {insert_query} with values: {insert_values}")
|
| 835 |
+
cursor.execute(insert_query, insert_values)
|
| 836 |
+
|
| 837 |
+
conn.commit()
|
| 838 |
conn.close()
|
| 839 |
+
logging.debug(f"User with email {email} processed successfully")
|
| 840 |
+
|
| 841 |
+
@app.route('/send_get_request', methods=['GET'])
|
| 842 |
+
def send_get_request():
|
| 843 |
+
token = request.args.get('token')
|
| 844 |
+
webinarId = request.args.get('webinarId')
|
| 845 |
+
url = f'https://online.bizon365.ru/api/v1/webinars/reports/get?webinarId={webinarId}'
|
| 846 |
+
|
| 847 |
+
response = requests.get(url, headers={'X-Token': token})
|
| 848 |
+
|
| 849 |
+
if response.status_code == 200:
|
| 850 |
+
data = response.json()
|
| 851 |
+
|
| 852 |
+
report = data.get('report', {})
|
| 853 |
+
messages = data.get('messages', {})
|
| 854 |
+
|
| 855 |
+
report_json_str = report.get('report', '{}')
|
| 856 |
+
try:
|
| 857 |
+
report_json = json.loads(report_json_str)
|
| 858 |
+
except json.JSONDecodeError:
|
| 859 |
+
report_json = {}
|
| 860 |
+
|
| 861 |
+
messages_json_str = report.get('messages', '{}')
|
| 862 |
+
try:
|
| 863 |
+
messages_json = json.loads(messages_json_str)
|
| 864 |
+
except json.JSONDecodeError:
|
| 865 |
+
messages_json = {}
|
| 866 |
+
|
| 867 |
+
users_meta = report_json.get('usersMeta', {})
|
| 868 |
+
|
| 869 |
+
# Обновление или добавление каждого пользователя в базу данных data_gc.db
|
| 870 |
+
for user_id, user_data in users_meta.items():
|
| 871 |
+
user_data['messages'] = messages_json
|
| 872 |
+
update_or_insert_user(DATABASE_NAME, user_data, template)
|
| 873 |
+
|
| 874 |
+
return jsonify({'status': 'User data saved successfully'})
|
| 875 |
+
else:
|
| 876 |
+
return jsonify({'error': 'Failed to fetch data from the API'}), response.status_code
|
| 877 |
|
| 878 |
|
| 879 |
|