DmitrMakeev
commited on
Commit
•
00e9060
1
Parent(s):
1bb98d6
Update app.py
Browse files
app.py
CHANGED
@@ -101,22 +101,6 @@ for db in DATABASES:
|
|
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 |
|
@@ -775,81 +759,94 @@ def send_request():
|
|
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 = "data_gc.db"
|
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 = data.get('report', {})
|
829 |
-
messages = data.get('messages', {})
|
830 |
-
|
831 |
report_json_str = report.get('report', '{}')
|
|
|
|
|
832 |
try:
|
833 |
report_json = json.loads(report_json_str)
|
834 |
except json.JSONDecodeError:
|
835 |
report_json = {}
|
836 |
|
837 |
-
messages_json_str = report.get('messages', '{}')
|
838 |
try:
|
839 |
messages_json = json.loads(messages_json_str)
|
840 |
except json.JSONDecodeError:
|
841 |
messages_json = {}
|
842 |
|
843 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
844 |
|
845 |
-
#
|
|
|
846 |
for user_id, user_data in users_meta.items():
|
847 |
-
user_data
|
848 |
|
849 |
-
|
850 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
851 |
|
852 |
-
return jsonify(
|
853 |
else:
|
854 |
return jsonify({'error': 'Failed to fetch data from the API'}), response.status_code
|
855 |
|
|
|
101 |
|
102 |
|
103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
|
106 |
|
|
|
759 |
|
760 |
|
761 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
762 |
@app.route('/send_get_request', methods=['GET'])
|
763 |
def send_get_request():
|
764 |
token = request.args.get('token')
|
765 |
webinarId = request.args.get('webinarId')
|
766 |
url = f'https://online.bizon365.ru/api/v1/webinars/reports/get?webinarId={webinarId}'
|
|
|
767 |
response = requests.get(url, headers={'X-Token': token})
|
|
|
768 |
if response.status_code == 200:
|
769 |
data = response.json()
|
|
|
770 |
report = data.get('report', {})
|
|
|
|
|
771 |
report_json_str = report.get('report', '{}')
|
772 |
+
messages_json_str = report.get('messages', '{}')
|
773 |
+
|
774 |
try:
|
775 |
report_json = json.loads(report_json_str)
|
776 |
except json.JSONDecodeError:
|
777 |
report_json = {}
|
778 |
|
|
|
779 |
try:
|
780 |
messages_json = json.loads(messages_json_str)
|
781 |
except json.JSONDecodeError:
|
782 |
messages_json = {}
|
783 |
|
784 |
+
result = {
|
785 |
+
'report': report_json,
|
786 |
+
'messages': messages_json
|
787 |
+
}
|
788 |
+
|
789 |
+
# Маппинг полей
|
790 |
+
template = {
|
791 |
+
"username": "name",
|
792 |
+
"phone": "phone",
|
793 |
+
"email": "email",
|
794 |
+
"city": "b_city",
|
795 |
+
"finished": "b_fin",
|
796 |
+
"ban": "b_ban",
|
797 |
+
"ignore": "b_ign",
|
798 |
+
"banners": "b_baners", # Если массив заполнен пишем -1, если пустой пишем - 0
|
799 |
+
"buttons": "b_butt", # Если массив заполнен пишем -1, если пустой пишем - 0
|
800 |
+
"messages": "b_mess",
|
801 |
+
"0": "web_st", # С первого раза и с каждым обновлением пользователя, увеличиваем на 1
|
802 |
+
}
|
803 |
|
804 |
+
# Получение usersMeta и email
|
805 |
+
users_meta = report_json.get('usersMeta', {})
|
806 |
for user_id, user_data in users_meta.items():
|
807 |
+
email = user_data.get('email', '')
|
808 |
|
809 |
+
if not email:
|
810 |
+
continue
|
811 |
+
|
812 |
+
# Проверка наличия пользователя по email
|
813 |
+
conn = sqlite3.connect('data_gc.db')
|
814 |
+
cursor = conn.cursor()
|
815 |
+
cursor.execute("SELECT * FROM contacts WHERE email = ?", (email,))
|
816 |
+
existing_user = cursor.fetchone()
|
817 |
+
|
818 |
+
# Подготовка данных для вставки/обновления
|
819 |
+
mapped_data = {}
|
820 |
+
for key, value in template.items():
|
821 |
+
if key in user_data:
|
822 |
+
if key in ["banners", "buttons"]:
|
823 |
+
mapped_data[value] = -1 if user_data[key] else 0
|
824 |
+
elif key == "0":
|
825 |
+
if existing_user:
|
826 |
+
mapped_data[value] = existing_user[8] + 1 # Увеличиваем на 1 значение поля web_st
|
827 |
+
else:
|
828 |
+
mapped_data[value] = 1 # Изначальное значение
|
829 |
+
else:
|
830 |
+
mapped_data[value] = user_data[key]
|
831 |
+
elif key == "messages":
|
832 |
+
mapped_data[value] = json.dumps(messages_json.get(user_id, []))
|
833 |
+
|
834 |
+
# Вставка или обновление пользователя
|
835 |
+
if existing_user:
|
836 |
+
placeholders = ", ".join([f"{field} = ?" for field in mapped_data.keys()])
|
837 |
+
values = list(mapped_data.values())
|
838 |
+
values.append(email)
|
839 |
+
cursor.execute(f"UPDATE contacts SET {placeholders} WHERE email = ?", values)
|
840 |
+
else:
|
841 |
+
fields = ", ".join(mapped_data.keys())
|
842 |
+
placeholders = ", ".join(["?" for _ in mapped_data.values()])
|
843 |
+
values = list(mapped_data.values())
|
844 |
+
cursor.execute(f"INSERT INTO contacts ({fields}) VALUES ({placeholders})", values)
|
845 |
+
|
846 |
+
conn.commit()
|
847 |
+
conn.close()
|
848 |
|
849 |
+
return jsonify(result)
|
850 |
else:
|
851 |
return jsonify({'error': 'Failed to fetch data from the API'}), response.status_code
|
852 |
|