DmitrMakeev
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -99,7 +99,20 @@ for db in DATABASES:
|
|
99 |
init_db(db)
|
100 |
|
101 |
|
102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
104 |
|
105 |
|
@@ -756,49 +769,86 @@ def send_request():
|
|
756 |
|
757 |
|
758 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
759 |
@app.route('/send_get_request', methods=['GET'])
|
760 |
def send_get_request():
|
761 |
token = request.args.get('token')
|
762 |
webinarId = request.args.get('webinarId')
|
763 |
url = f'https://online.bizon365.ru/api/v1/webinars/reports/get?webinarId={webinarId}'
|
764 |
-
|
765 |
response = requests.get(url, headers={'X-Token': token})
|
766 |
-
|
767 |
if response.status_code == 200:
|
768 |
data = response.json()
|
769 |
-
# Извлечение данных отчета
|
770 |
-
report = data.get('report', {})
|
771 |
-
|
772 |
-
# Извлечение полей из отчета
|
773 |
|
774 |
-
report_json_str = report.get('report', '{}')
|
775 |
-
|
776 |
-
|
777 |
try:
|
778 |
report_json = json.loads(report_json_str)
|
779 |
except json.JSONDecodeError:
|
780 |
-
report_json = {}
|
781 |
-
# Извлечение данных отчета
|
782 |
-
messages = data.get('messages', {})
|
783 |
-
|
784 |
-
# Извлечение полей из отчета
|
785 |
|
786 |
-
messages_json_str = report.get('messages', '{}')
|
787 |
-
|
788 |
-
# Обработка строкового JSON в report
|
789 |
try:
|
790 |
messages_json = json.loads(messages_json_str)
|
791 |
except json.JSONDecodeError:
|
792 |
-
messages_json = {}
|
793 |
-
# Возврат результата
|
794 |
-
result = {
|
795 |
|
796 |
-
|
797 |
-
|
798 |
|
799 |
-
|
800 |
-
|
801 |
-
|
|
|
|
|
802 |
else:
|
803 |
return jsonify({'error': 'Failed to fetch data from the API'}), response.status_code
|
804 |
|
@@ -821,20 +871,6 @@ def send_get_request():
|
|
821 |
|
822 |
|
823 |
|
824 |
-
|
825 |
-
|
826 |
-
|
827 |
-
|
828 |
-
|
829 |
-
|
830 |
-
|
831 |
-
|
832 |
-
|
833 |
-
|
834 |
-
|
835 |
-
|
836 |
-
|
837 |
-
|
838 |
|
839 |
|
840 |
|
|
|
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 |
|
|
|
769 |
|
770 |
|
771 |
|
772 |
+
|
773 |
+
|
774 |
+
|
775 |
+
|
776 |
+
|
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 |
+
report_json_str = data.get('report', {}).get('report', '{}')
|
832 |
+
messages_json_str = data.get('messages', '{}')
|
833 |
+
|
834 |
try:
|
835 |
report_json = json.loads(report_json_str)
|
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 |
|
|
|
871 |
|
872 |
|
873 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
874 |
|
875 |
|
876 |
|