DmitrMakeev
commited on
Commit
•
cd24c40
1
Parent(s):
b010f8d
Update app.py
Browse files
app.py
CHANGED
@@ -824,24 +824,14 @@ def update_or_insert_user(db_name, user_data, mapping_template):
|
|
824 |
logging.debug(f"Transformed data after phone processing: {transformed_data}")
|
825 |
|
826 |
# Увеличиваем значение web_st на единицу
|
827 |
-
|
828 |
-
|
829 |
-
|
830 |
-
update_query = "UPDATE contacts SET "
|
831 |
-
update_values = []
|
832 |
-
for column, value in transformed_data.items():
|
833 |
-
update_query += f"{column} = ?, "
|
834 |
-
update_values.append(value)
|
835 |
-
update_query += "web_st = web_st + 1 WHERE email = ?"
|
836 |
-
update_values.append(email)
|
837 |
-
logging.debug(f"Update query: {update_query} with values: {update_values}")
|
838 |
-
cursor.execute(update_query, update_values)
|
839 |
|
840 |
if cursor.rowcount == 0:
|
841 |
columns = ', '.join(transformed_data.keys()) + ", web_st"
|
842 |
placeholders = ', '.join('?' for _ in transformed_data) + ", ?"
|
843 |
insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
|
844 |
-
insert_values = list(transformed_data.values()) + [
|
845 |
logging.debug(f"Insert query: {insert_query} with values: {insert_values}")
|
846 |
cursor.execute(insert_query, insert_values)
|
847 |
|
@@ -849,6 +839,42 @@ def update_or_insert_user(db_name, user_data, mapping_template):
|
|
849 |
conn.close()
|
850 |
logging.debug(f"User with email {email} processed successfully")
|
851 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
852 |
|
853 |
|
854 |
|
|
|
824 |
logging.debug(f"Transformed data after phone processing: {transformed_data}")
|
825 |
|
826 |
# Увеличиваем значение web_st на единицу
|
827 |
+
update_query = "UPDATE contacts SET web_st = web_st + 1 WHERE email = ?"
|
828 |
+
cursor.execute(update_query, (email,))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
829 |
|
830 |
if cursor.rowcount == 0:
|
831 |
columns = ', '.join(transformed_data.keys()) + ", web_st"
|
832 |
placeholders = ', '.join('?' for _ in transformed_data) + ", ?"
|
833 |
insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
|
834 |
+
insert_values = list(transformed_data.values()) + [1]
|
835 |
logging.debug(f"Insert query: {insert_query} with values: {insert_values}")
|
836 |
cursor.execute(insert_query, insert_values)
|
837 |
|
|
|
839 |
conn.close()
|
840 |
logging.debug(f"User with email {email} processed successfully")
|
841 |
|
842 |
+
@app.route('/send_get_request', methods=['GET'])
|
843 |
+
def send_get_request():
|
844 |
+
token = request.args.get('token')
|
845 |
+
webinarId = request.args.get('webinarId')
|
846 |
+
url = f'https://online.bizon365.ru/api/v1/webinars/reports/get?webinarId={webinarId}'
|
847 |
+
|
848 |
+
response = requests.get(url, headers={'X-Token': token})
|
849 |
+
|
850 |
+
if response.status_code == 200:
|
851 |
+
data = response.json()
|
852 |
+
|
853 |
+
report = data.get('report', {})
|
854 |
+
messages = data.get('messages', {})
|
855 |
+
|
856 |
+
report_json_str = report.get('report', '{}')
|
857 |
+
try:
|
858 |
+
report_json = json.loads(report_json_str)
|
859 |
+
except json.JSONDecodeError:
|
860 |
+
report_json = {}
|
861 |
+
|
862 |
+
messages_json_str = report.get('messages', '{}')
|
863 |
+
try:
|
864 |
+
messages_json = json.loads(messages_json_str)
|
865 |
+
except json.JSONDecodeError:
|
866 |
+
messages_json = {}
|
867 |
+
|
868 |
+
users_meta = report_json.get('usersMeta', {})
|
869 |
+
|
870 |
+
# Обновление или добавление каждого пользователя в базу данных data_gc.db
|
871 |
+
for user_id, user_data in users_meta.items():
|
872 |
+
user_data['messages'] = messages_json
|
873 |
+
update_or_insert_user(DATABASE_NAME, user_data, mapping_template)
|
874 |
+
|
875 |
+
return jsonify({'status': 'User data saved successfully'})
|
876 |
+
else:
|
877 |
+
return jsonify({'error': 'Failed to fetch data from the API'}), response.status_code
|
878 |
|
879 |
|
880 |
|