DmitrMakeev
commited on
Commit
•
8779c70
1
Parent(s):
738b53b
Update app.py
Browse files
app.py
CHANGED
@@ -99,20 +99,6 @@ for db in DATABASES:
|
|
99 |
init_db(db)
|
100 |
|
101 |
|
102 |
-
# Шаблон маппинга
|
103 |
-
template = {
|
104 |
-
"username": "name",
|
105 |
-
"phone": "phone",
|
106 |
-
"email": "email",
|
107 |
-
"city": "b_city",
|
108 |
-
"finished": "b_fin",
|
109 |
-
"ban": "b_ban",
|
110 |
-
"ignore": "b_ign",
|
111 |
-
"banners": "b_baners", # Если массив заполнен пишем -1, если пустой пишем - 0
|
112 |
-
"buttons": "b_butt", # Если массив заполнен пишем -1, если пустой пишем - 0
|
113 |
-
"messages": "b_mess",
|
114 |
-
"0": "web_st" # С первого раза и с каждым обновлением пользователя, увеличиваем на 1
|
115 |
-
}
|
116 |
|
117 |
|
118 |
|
@@ -777,81 +763,90 @@ def send_request():
|
|
777 |
|
778 |
|
779 |
|
780 |
-
|
781 |
-
def update_or_insert_user(db_name, user_data, mapping_template):
|
782 |
-
conn = sqlite3.connect(db_name)
|
783 |
-
cursor = conn.cursor()
|
784 |
-
|
785 |
-
email = user_data.get('email')
|
786 |
-
|
787 |
-
# Проверка существования пользователя
|
788 |
-
cursor.execute("SELECT * FROM contacts WHERE email = ?", (email,))
|
789 |
-
user = cursor.fetchone()
|
790 |
-
|
791 |
-
# Преобразование данных пользователя на основе шаблона
|
792 |
-
transformed_data = {}
|
793 |
-
for json_key, db_column in mapping_template.items():
|
794 |
-
value = user_data.get(json_key, "")
|
795 |
-
if isinstance(value, list):
|
796 |
-
transformed_data[db_column] = "-1" if value else "0"
|
797 |
-
else:
|
798 |
-
transformed_data[db_column] = value
|
799 |
-
|
800 |
-
if user:
|
801 |
-
# Обновление существующего пользователя
|
802 |
-
update_query = "UPDATE contacts SET "
|
803 |
-
update_values = []
|
804 |
-
for column, value in transformed_data.items():
|
805 |
-
update_query += f"{column} = ?, "
|
806 |
-
update_values.append(value)
|
807 |
-
update_query = update_query.rstrip(", ") + " WHERE email = ?"
|
808 |
-
update_values.append(email)
|
809 |
-
cursor.execute(update_query, update_values)
|
810 |
-
else:
|
811 |
-
# Добавление нового пользователя
|
812 |
-
columns = ', '.join(transformed_data.keys())
|
813 |
-
placeholders = ', '.join('?' for _ in transformed_data)
|
814 |
-
insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
|
815 |
-
cursor.execute(insert_query, list(transformed_data.values()))
|
816 |
-
|
817 |
-
conn.commit()
|
818 |
-
conn.close()
|
819 |
|
820 |
@app.route('/send_get_request', methods=['GET'])
|
821 |
def send_get_request():
|
822 |
token = request.args.get('token')
|
823 |
webinarId = request.args.get('webinarId')
|
824 |
url = f'https://online.bizon365.ru/api/v1/webinars/reports/get?webinarId={webinarId}'
|
825 |
-
|
826 |
response = requests.get(url, headers={'X-Token': token})
|
827 |
-
|
828 |
if response.status_code == 200:
|
829 |
data = response.json()
|
830 |
-
|
831 |
-
|
832 |
-
|
833 |
-
|
834 |
-
|
835 |
-
|
836 |
-
except json.JSONDecodeError:
|
837 |
-
report_json = {}
|
838 |
-
|
839 |
-
try:
|
840 |
-
messages_json = json.loads(messages_json_str)
|
841 |
-
except json.JSONDecodeError:
|
842 |
-
messages_json = {}
|
843 |
-
|
844 |
-
user_data = report_json.get('usersMeta', {}).get('HXJY5fEdC', {})
|
845 |
-
user_data['messages'] = messages_json
|
846 |
-
|
847 |
-
# Обновление или добавление пользователя во все базы данных
|
848 |
-
for db in DATABASES:
|
849 |
-
update_or_insert_user(db, user_data, template)
|
850 |
-
|
851 |
return jsonify({'status': 'success'})
|
852 |
else:
|
853 |
return jsonify({'error': 'Failed to fetch data from the API'}), response.status_code
|
854 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
855 |
|
856 |
|
857 |
|
|
|
99 |
init_db(db)
|
100 |
|
101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
|
104 |
|
|
|
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 |
+
def update_or_insert_user(email, user_data):
|
786 |
+
template = {
|
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 |
+
for db in DATABASES:
|
801 |
+
conn = sqlite3.connect(db)
|
802 |
+
cursor = conn.cursor()
|
803 |
+
cursor.execute('SELECT * FROM contacts WHERE email = ?', (email,))
|
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 |
+
conn.commit()
|
848 |
+
conn.close()
|
849 |
+
|
850 |
|
851 |
|
852 |
|