DmitrMakeev
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -101,6 +101,31 @@ for db in DATABASES:
|
|
101 |
|
102 |
|
103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
|
106 |
|
@@ -750,102 +775,79 @@ def send_request():
|
|
750 |
|
751 |
|
752 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
753 |
|
754 |
-
|
755 |
-
|
756 |
-
|
757 |
-
|
758 |
-
|
759 |
-
|
760 |
-
|
761 |
-
|
762 |
-
|
763 |
-
|
764 |
-
|
765 |
-
|
766 |
|
767 |
@app.route('/send_get_request', methods=['GET'])
|
768 |
def send_get_request():
|
769 |
token = request.args.get('token')
|
770 |
webinarId = request.args.get('webinarId')
|
771 |
url = f'https://online.bizon365.ru/api/v1/webinars/reports/get?webinarId={webinarId}'
|
|
|
772 |
response = requests.get(url, headers={'X-Token': token})
|
|
|
773 |
if response.status_code == 200:
|
774 |
data = response.json()
|
775 |
-
report = data.get('report', {})
|
776 |
-
users_meta = report.get('usersMeta', {})
|
777 |
-
for user_id, user_data in users_meta.items():
|
778 |
-
email = user_data.get('email')
|
779 |
-
if email:
|
780 |
-
update_or_insert_user(email, user_data)
|
781 |
-
return jsonify({'status': 'success'})
|
782 |
-
else:
|
783 |
-
return jsonify({'error': 'Failed to fetch data from the API'}), response.status_code
|
784 |
|
785 |
-
|
786 |
-
|
787 |
-
"username": "name",
|
788 |
-
"phone": "phone",
|
789 |
-
"email": "email",
|
790 |
-
"city": "b_city",
|
791 |
-
"finished": "b_fin",
|
792 |
-
"ban": "b_ban",
|
793 |
-
"ignore": "b_ign",
|
794 |
-
"banners": "b_baners", # Если массив заполнен пишем -1, если пустой пишем - 0
|
795 |
-
"buttons": "b_butt", # Если массив заполнен пишем -1, если пустой пишем - 0
|
796 |
-
"messages": "b_mess",
|
797 |
-
"0": "web_st", # С первого раза и с каждым обновлением пользователя, увеличиваем на 1
|
798 |
-
}
|
799 |
|
800 |
-
|
801 |
-
|
802 |
-
|
803 |
-
|
804 |
-
existing_user = cursor.fetchone()
|
805 |
-
|
806 |
-
if existing_user:
|
807 |
-
# Обновление данных пользователя
|
808 |
-
update_data = {}
|
809 |
-
for key, value in user_data.items():
|
810 |
-
if key in template:
|
811 |
-
update_data[template[key]] = value
|
812 |
-
update_data['web_st'] = existing_user[8] + 1 if existing_user[8] is not None else 1
|
813 |
-
update_data['b_baners'] = -1 if user_data.get('banners', []) else 0
|
814 |
-
update_data['b_butt'] = -1 if user_data.get('buttons', []) else 0
|
815 |
-
update_query = 'UPDATE contacts SET ' + ', '.join([f'{k} = ?' for k in update_data.keys()]) + ' WHERE email = ?'
|
816 |
-
cursor.execute(update_query, list(update_data.values()) + [email])
|
817 |
-
else:
|
818 |
-
# Добавление нового пользователя
|
819 |
-
insert_data = {
|
820 |
-
"name": user_data.get('username', ''),
|
821 |
-
"phone": user_data.get('phone', ''),
|
822 |
-
"email": user_data.get('email', ''),
|
823 |
-
"b_city": user_data.get('city', ''),
|
824 |
-
"b_fin": user_data.get('finished', ''),
|
825 |
-
"b_ban": user_data.get('ban', ''),
|
826 |
-
"b_ign": user_data.get('ignore', ''),
|
827 |
-
"b_baners": -1 if user_data.get('banners', []) else 0,
|
828 |
-
"b_butt": -1 if user_data.get('buttons', []) else 0,
|
829 |
-
"b_mess": user_data.get('messages', ''),
|
830 |
-
"web_st": 1,
|
831 |
-
"shop_st": '',
|
832 |
-
"curator": '',
|
833 |
-
"pr1": '',
|
834 |
-
"pr2": '',
|
835 |
-
"pr3": '',
|
836 |
-
"pr4": '',
|
837 |
-
"pr5": '',
|
838 |
-
"ad_url": '',
|
839 |
-
"key_pr": '',
|
840 |
-
"n_con": '',
|
841 |
-
"canal": '',
|
842 |
-
"data_t": ''
|
843 |
-
}
|
844 |
-
insert_query = 'INSERT INTO contacts (' + ', '.join(insert_data.keys()) + ') VALUES (' + ', '.join(['?'] * len(insert_data)) + ')'
|
845 |
-
cursor.execute(insert_query, list(insert_data.values()))
|
846 |
|
847 |
-
|
848 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
849 |
|
850 |
|
851 |
|
|
|
101 |
|
102 |
|
103 |
|
104 |
+
template = {
|
105 |
+
"username": "name",
|
106 |
+
"phone": "phone",
|
107 |
+
"email": "email",
|
108 |
+
"city": "b_city",
|
109 |
+
"finished": "b_fin",
|
110 |
+
"ban": "b_ban",
|
111 |
+
"ignore": "b_ign",
|
112 |
+
"banners": "b_baners", # Если массив заполнен пишем -1, если пустой пишем - 0
|
113 |
+
"buttons": "b_butt", # Если массив заполнен пишем -1, если пустой пишем - 0
|
114 |
+
"messages": "b_mess",
|
115 |
+
"0": "web_st" # С первого раза и с каждым обновлением пользователя, увеличиваем на 1
|
116 |
+
}
|
117 |
+
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
|
122 |
+
|
123 |
+
|
124 |
+
|
125 |
+
|
126 |
+
|
127 |
+
|
128 |
+
|
129 |
|
130 |
|
131 |
|
|
|
775 |
|
776 |
|
777 |
|
778 |
+
def update_or_insert_user(db_name, user_data, mapping_template):
|
779 |
+
conn = sqlite3.connect(db_name)
|
780 |
+
cursor = conn.cursor()
|
781 |
+
|
782 |
+
email = user_data.get('email')
|
783 |
+
|
784 |
+
# Проверка существования пользователя
|
785 |
+
cursor.execute("SELECT * FROM contacts WHERE email = ?", (email,))
|
786 |
+
user = cursor.fetchone()
|
787 |
+
|
788 |
+
# Преобразование данных пользователя на основе шаблона
|
789 |
+
transformed_data = {}
|
790 |
+
for json_key, db_column in mapping_template.items():
|
791 |
+
value = user_data.get(json_key, "")
|
792 |
+
if isinstance(value, list):
|
793 |
+
transformed_data[db_column] = "-1" if value else "0"
|
794 |
+
else:
|
795 |
+
transformed_data[db_column] = value
|
796 |
+
|
797 |
+
if user:
|
798 |
+
# Обновление существующего пользователя
|
799 |
+
update_query = "UPDATE contacts SET "
|
800 |
+
update_values = []
|
801 |
+
for column, value in transformed_data.items():
|
802 |
+
update_query += f"{column} = ?, "
|
803 |
+
update_values.append(value)
|
804 |
+
update_query = update_query.rstrip(", ") + " WHERE email = ?"
|
805 |
+
update_values.append(email)
|
806 |
+
cursor.execute(update_query, update_values)
|
807 |
+
else:
|
808 |
+
# Добавление нового пользователя
|
809 |
+
columns = ', '.join(transformed_data.keys())
|
810 |
+
placeholders = ', '.join('?' for _ in transformed_data)
|
811 |
+
insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
|
812 |
+
cursor.execute(insert_query, list(transformed_data.values()))
|
813 |
|
814 |
+
conn.commit()
|
815 |
+
conn.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
816 |
|
817 |
@app.route('/send_get_request', methods=['GET'])
|
818 |
def send_get_request():
|
819 |
token = request.args.get('token')
|
820 |
webinarId = request.args.get('webinarId')
|
821 |
url = f'https://online.bizon365.ru/api/v1/webinars/reports/get?webinarId={webinarId}'
|
822 |
+
|
823 |
response = requests.get(url, headers={'X-Token': token})
|
824 |
+
|
825 |
if response.status_code == 200:
|
826 |
data = response.json()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
827 |
|
828 |
+
report_json_str = data.get('report', {}).get('report', '{}')
|
829 |
+
messages_json_str = data.get('messages', '{}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
830 |
|
831 |
+
try:
|
832 |
+
report_json = json.loads(report_json_str)
|
833 |
+
except json.JSONDecodeError:
|
834 |
+
report_json = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
835 |
|
836 |
+
try:
|
837 |
+
messages_json = json.loads(messages_json_str)
|
838 |
+
except json.JSONDecodeError:
|
839 |
+
messages_json = {}
|
840 |
+
|
841 |
+
user_data = report_json.get('usersMeta', {}).get('HXJY5fEdC', {})
|
842 |
+
user_data['messages'] = messages_json
|
843 |
+
|
844 |
+
# Обновление или добавление пользователя во все базы данных
|
845 |
+
for db in DATABASES:
|
846 |
+
update_or_insert_user(db, user_data, template)
|
847 |
+
|
848 |
+
return jsonify({'status': 'User data saved successfully'})
|
849 |
+
else:
|
850 |
+
return jsonify({'error': 'Failed to fetch data from the API'}), response.status_code
|
851 |
|
852 |
|
853 |
|