DmitrMakeev
commited on
Commit
•
09ba895
1
Parent(s):
9eb85b1
Update app.py
Browse files
app.py
CHANGED
@@ -782,7 +782,6 @@ def send_request():
|
|
782 |
|
783 |
DATABASE_NAME = 'data_gc.db'
|
784 |
def update_or_insert_user(db_name, user_data, mapping_template):
|
785 |
-
# Подключение к базе данных
|
786 |
conn = sqlite3.connect(db_name)
|
787 |
cursor = conn.cursor()
|
788 |
|
@@ -793,9 +792,9 @@ def update_or_insert_user(db_name, user_data, mapping_template):
|
|
793 |
return
|
794 |
|
795 |
logging.debug(f"Processing user with email: {email}")
|
796 |
-
|
797 |
# Проверка существования пользователя в базе данных по email
|
798 |
-
cursor.execute("SELECT web_st
|
799 |
user = cursor.fetchone()
|
800 |
logging.debug(f"User found: {user}")
|
801 |
|
@@ -810,16 +809,16 @@ def update_or_insert_user(db_name, user_data, mapping_template):
|
|
810 |
# Обновление значения web_st
|
811 |
cursor.execute("UPDATE contacts SET web_st = ? WHERE email = ?", (web_st_value, email))
|
812 |
conn.commit()
|
|
|
813 |
logging.debug(f"User {email} web_st updated to {web_st_value}")
|
814 |
-
|
815 |
-
# Извлечение старых сообщений
|
816 |
-
old_messages = json.loads(user[1]) if user[1] else []
|
817 |
-
logging.debug(f"Old messages: {old_messages}")
|
818 |
-
|
819 |
else:
|
820 |
-
|
821 |
logging.debug(f"User {email} not found, proceeding with insert")
|
822 |
|
|
|
|
|
|
|
|
|
823 |
# Преобразование данных пользователя на основе шаблона сопоставления
|
824 |
transformed_data = {}
|
825 |
for json_key, db_column in mapping_template.items():
|
@@ -853,40 +852,29 @@ def update_or_insert_user(db_name, user_data, mapping_template):
|
|
853 |
# Добавление значения web_st в данные для вставки
|
854 |
transformed_data['web_st'] = web_st_value
|
855 |
|
856 |
-
#
|
857 |
-
if 'banners' in user_data and isinstance(user_data['banners'], list) and user_data['banners']:
|
858 |
-
transformed_data['b_baners'] = '1'
|
859 |
-
else:
|
860 |
-
transformed_data['b_baners'] = '0'
|
861 |
-
logging.debug(f"Transformed data after processing banners: {transformed_data}")
|
862 |
-
|
863 |
-
# Объединение старых и новых сообщений
|
864 |
-
new_messages = user_data.get('messages', [])
|
865 |
-
all_messages = old_messages + new_messages
|
866 |
-
transformed_data['b_mess'] = json.dumps(all_messages)
|
867 |
-
logging.debug(f"Combined messages: {all_messages}")
|
868 |
-
|
869 |
-
# Обновление или вставка данных в таблицу contacts
|
870 |
if user:
|
871 |
-
|
872 |
-
|
873 |
-
|
874 |
-
|
875 |
-
|
|
|
|
|
|
|
|
|
876 |
else:
|
877 |
-
# Вставка нового пользователя
|
878 |
columns = ', '.join(transformed_data.keys())
|
879 |
-
placeholders = ', '.join(
|
|
|
880 |
insert_values = list(transformed_data.values())
|
881 |
-
|
882 |
-
|
883 |
-
logging.info(f"New user {email} has been added to the database.")
|
884 |
|
885 |
# Подтверждение изменений и закрытие соединения
|
886 |
conn.commit()
|
887 |
conn.close()
|
888 |
-
logging.
|
889 |
-
|
890 |
|
891 |
@app.route('/send_get_request', methods=['GET'])
|
892 |
def send_get_request():
|
@@ -902,14 +890,26 @@ def send_get_request():
|
|
902 |
report = data.get('report', {})
|
903 |
messages = data.get('messages', {})
|
904 |
|
905 |
-
|
906 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
907 |
|
|
|
908 |
processed_emails = set()
|
909 |
for user_id, user_data in users_meta.items():
|
910 |
-
user_data['messages'] =
|
911 |
email = user_data.get('email')
|
912 |
-
if email
|
913 |
update_or_insert_user(DATABASE_NAME, user_data, mapping_template)
|
914 |
processed_emails.add(email)
|
915 |
|
|
|
782 |
|
783 |
DATABASE_NAME = 'data_gc.db'
|
784 |
def update_or_insert_user(db_name, user_data, mapping_template):
|
|
|
785 |
conn = sqlite3.connect(db_name)
|
786 |
cursor = conn.cursor()
|
787 |
|
|
|
792 |
return
|
793 |
|
794 |
logging.debug(f"Processing user with email: {email}")
|
795 |
+
|
796 |
# Проверка существования пользователя в базе данных по email
|
797 |
+
cursor.execute("SELECT web_st FROM contacts WHERE email = ?", (email,))
|
798 |
user = cursor.fetchone()
|
799 |
logging.debug(f"User found: {user}")
|
800 |
|
|
|
809 |
# Обновление значения web_st
|
810 |
cursor.execute("UPDATE contacts SET web_st = ? WHERE email = ?", (web_st_value, email))
|
811 |
conn.commit()
|
812 |
+
conn.close()
|
813 |
logging.debug(f"User {email} web_st updated to {web_st_value}")
|
|
|
|
|
|
|
|
|
|
|
814 |
else:
|
815 |
+
conn.close()
|
816 |
logging.debug(f"User {email} not found, proceeding with insert")
|
817 |
|
818 |
+
# Открываем соединение снова для остальных операций
|
819 |
+
conn = sqlite3.connect(db_name)
|
820 |
+
cursor = conn.cursor()
|
821 |
+
|
822 |
# Преобразование данных пользователя на основе шаблона сопоставления
|
823 |
transformed_data = {}
|
824 |
for json_key, db_column in mapping_template.items():
|
|
|
852 |
# Добавление значения web_st в данные для вставки
|
853 |
transformed_data['web_st'] = web_st_value
|
854 |
|
855 |
+
# Обновление данных пользователя в базе данных
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
856 |
if user:
|
857 |
+
update_query = "UPDATE contacts SET "
|
858 |
+
update_values = []
|
859 |
+
for column, value in transformed_data.items():
|
860 |
+
update_query += f"{column} = ?, "
|
861 |
+
update_values.append(value)
|
862 |
+
update_query = update_query.rstrip(", ") + " WHERE email = ?"
|
863 |
+
update_values.append(email)
|
864 |
+
logging.debug(f"Update query: {update_query} with values: {update_values}")
|
865 |
+
cursor.execute(update_query, update_values)
|
866 |
else:
|
|
|
867 |
columns = ', '.join(transformed_data.keys())
|
868 |
+
placeholders = ', '.join('?' for _ in transformed_data)
|
869 |
+
insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
|
870 |
insert_values = list(transformed_data.values())
|
871 |
+
logging.debug(f"Insert query: {insert_query} with values: {insert_values}")
|
872 |
+
cursor.execute(insert_query, insert_values)
|
|
|
873 |
|
874 |
# Подтверждение изменений и закрытие соединения
|
875 |
conn.commit()
|
876 |
conn.close()
|
877 |
+
logging.debug(f"User with email {email} processed successfully")
|
|
|
878 |
|
879 |
@app.route('/send_get_request', methods=['GET'])
|
880 |
def send_get_request():
|
|
|
890 |
report = data.get('report', {})
|
891 |
messages = data.get('messages', {})
|
892 |
|
893 |
+
report_json_str = report.get('report', '{}')
|
894 |
+
try:
|
895 |
+
report_json = json.loads(report_json_str)
|
896 |
+
except json.JSONDecodeError:
|
897 |
+
report_json = {}
|
898 |
+
|
899 |
+
messages_json_str = report.get('messages', '{}')
|
900 |
+
try:
|
901 |
+
messages_json = json.loads(messages_json_str)
|
902 |
+
except json.JSONDecodeError:
|
903 |
+
messages_json = {}
|
904 |
+
|
905 |
+
users_meta = report_json.get('usersMeta', {})
|
906 |
|
907 |
+
# Обновление или добавление каждого пользователя в базу данных data_gc.db
|
908 |
processed_emails = set()
|
909 |
for user_id, user_data in users_meta.items():
|
910 |
+
user_data['messages'] = messages_json
|
911 |
email = user_data.get('email')
|
912 |
+
if email not in processed_emails:
|
913 |
update_or_insert_user(DATABASE_NAME, user_data, mapping_template)
|
914 |
processed_emails.add(email)
|
915 |
|