DmitrMakeev
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -823,15 +823,27 @@ def update_or_insert_user(db_name, user_data, mapping_template):
|
|
823 |
transformed_data['phone'] = phone
|
824 |
logging.debug(f"Transformed data after phone processing: {transformed_data}")
|
825 |
|
|
|
826 |
if user:
|
827 |
-
|
828 |
-
|
829 |
-
|
830 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()) + [
|
835 |
logging.debug(f"Insert query: {insert_query} with values: {insert_values}")
|
836 |
cursor.execute(insert_query, insert_values)
|
837 |
|
@@ -839,44 +851,6 @@ def update_or_insert_user(db_name, user_data, mapping_template):
|
|
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 |
|
881 |
|
882 |
|
|
|
823 |
transformed_data['phone'] = phone
|
824 |
logging.debug(f"Transformed data after phone processing: {transformed_data}")
|
825 |
|
826 |
+
web_st_value = 0
|
827 |
if user:
|
828 |
+
web_st_value = int(user[0]) if user[0] else 0
|
829 |
+
web_st_value += 1
|
830 |
+
logging.debug(f"Calculated web_st_value: {web_st_value}")
|
831 |
+
|
832 |
+
update_query = "UPDATE contacts SET "
|
833 |
+
update_values = []
|
834 |
+
for column, value in transformed_data.items():
|
835 |
+
update_query += f"{column} = ?, "
|
836 |
+
update_values.append(value)
|
837 |
+
update_query += "web_st = ? WHERE email = ?"
|
838 |
+
update_values.extend([web_st_value, email])
|
839 |
+
logging.debug(f"Update query: {update_query} with values: {update_values}")
|
840 |
+
cursor.execute(update_query, update_values)
|
841 |
+
|
842 |
+
if cursor.rowcount == 0:
|
843 |
columns = ', '.join(transformed_data.keys()) + ", web_st"
|
844 |
placeholders = ', '.join('?' for _ in transformed_data) + ", ?"
|
845 |
insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
|
846 |
+
insert_values = list(transformed_data.values()) + [web_st_value]
|
847 |
logging.debug(f"Insert query: {insert_query} with values: {insert_values}")
|
848 |
cursor.execute(insert_query, insert_values)
|
849 |
|
|
|
851 |
conn.close()
|
852 |
logging.debug(f"User with email {email} processed successfully")
|
853 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
854 |
|
855 |
|
856 |
|