DmitrMakeev
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -782,131 +782,6 @@ def send_request():
|
|
782 |
|
783 |
DATABASE_NAME = 'data_gc.db'
|
784 |
|
785 |
-
def update_or_insert_user(db_name, user_data, mapping_template):
|
786 |
-
conn = sqlite3.connect(db_name)
|
787 |
-
cursor = conn.cursor()
|
788 |
-
|
789 |
-
email = user_data.get('email')
|
790 |
-
if not email:
|
791 |
-
logging.error(f"User data missing email: {user_data}")
|
792 |
-
return
|
793 |
-
|
794 |
-
logging.debug(f"Processing user with email: {email}")
|
795 |
-
|
796 |
-
cursor.execute("SELECT web_st FROM contacts WHERE email = ?", (email,))
|
797 |
-
user = cursor.fetchone()
|
798 |
-
logging.debug(f"User found: {user}")
|
799 |
-
|
800 |
-
web_st_value = 1
|
801 |
-
if user:
|
802 |
-
current_web_st = user[0] if user[0] is not None and user[0] != "" else 0
|
803 |
-
web_st_value = int(current_web_st) + 1
|
804 |
-
logging.debug(f"Calculated web_st_value: {web_st_value}")
|
805 |
-
|
806 |
-
cursor.execute("UPDATE contacts SET web_st = ? WHERE email = ?", (web_st_value, email))
|
807 |
-
conn.commit()
|
808 |
-
conn.close()
|
809 |
-
logging.debug(f"User {email} web_st updated to {web_st_value}")
|
810 |
-
else:
|
811 |
-
conn.close()
|
812 |
-
logging.debug(f"User {email} not found, proceeding with insert")
|
813 |
-
|
814 |
-
conn = sqlite3.connect(db_name)
|
815 |
-
cursor = conn.cursor()
|
816 |
-
|
817 |
-
transformed_data = {}
|
818 |
-
for json_key, db_column in mapping_template.items():
|
819 |
-
value = user_data.get(json_key, "")
|
820 |
-
if isinstance(value, list):
|
821 |
-
transformed_data[db_column] = json.dumps(value) if value else ""
|
822 |
-
else:
|
823 |
-
transformed_data[db_column] = str(value)
|
824 |
-
logging.debug(f"Transformed data: {transformed_data}")
|
825 |
-
|
826 |
-
required_fields = [
|
827 |
-
"vk_id", "chat_id", "ws_st", "ws_stop", "web_st", "fin_prog",
|
828 |
-
"b_city", "b_fin", "b_ban", "b_ign", "b_baners", "b_butt", "b_mess",
|
829 |
-
"shop_st", "curator", "pr1", "pr2", "pr3", "pr4", "pr5", "ad_url",
|
830 |
-
"key_pr", "n_con", "canal", "data_t"
|
831 |
-
]
|
832 |
-
for field in required_fields:
|
833 |
-
if field not in transformed_data:
|
834 |
-
transformed_data[field] = ""
|
835 |
-
logging.debug(f"Transformed data after adding required fields: {transformed_data}")
|
836 |
-
|
837 |
-
if 'phone' in user_data:
|
838 |
-
phone = user_data['phone']
|
839 |
-
if phone.startswith('+'):
|
840 |
-
phone = phone[1:]
|
841 |
-
transformed_data['phone'] = phone
|
842 |
-
logging.debug(f"Transformed data after phone processing: {transformed_data}")
|
843 |
-
|
844 |
-
transformed_data['web_st'] = web_st_value
|
845 |
-
|
846 |
-
if user:
|
847 |
-
update_query = "UPDATE contacts SET "
|
848 |
-
update_values = []
|
849 |
-
for column, value in transformed_data.items():
|
850 |
-
update_query += f"{column} = ?, "
|
851 |
-
update_values.append(value)
|
852 |
-
update_query = update_query.rstrip(", ") + " WHERE email = ?"
|
853 |
-
update_values.append(email)
|
854 |
-
logging.debug(f"Update query: {update_query} with values: {update_values}")
|
855 |
-
cursor.execute(update_query, update_values)
|
856 |
-
else:
|
857 |
-
columns = ', '.join(transformed_data.keys())
|
858 |
-
placeholders = ', '.join('?' for _ in transformed_data)
|
859 |
-
insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
|
860 |
-
insert_values = list(transformed_data.values())
|
861 |
-
logging.debug(f"Insert query: {insert_query} with values: {insert_values}")
|
862 |
-
cursor.execute(insert_query, insert_values)
|
863 |
-
|
864 |
-
conn.commit()
|
865 |
-
conn.close()
|
866 |
-
logging.debug(f"User with email {email} processed successfully")
|
867 |
-
|
868 |
-
app = Flask(__name__)
|
869 |
-
|
870 |
-
@app.route('/send_get_request', methods=['GET'])
|
871 |
-
def send_get_request():
|
872 |
-
token = request.args.get('token')
|
873 |
-
webinarId = request.args.get('webinarId')
|
874 |
-
url = f'https://online.bizon365.ru/api/v1/webinars/reports/get?webinarId={webinarId}'
|
875 |
-
|
876 |
-
response = requests.get(url, headers={'X-Token': token})
|
877 |
-
|
878 |
-
if response.status_code == 200:
|
879 |
-
data = response.json()
|
880 |
-
|
881 |
-
report = data.get('report', {})
|
882 |
-
messages = data.get('messages', {})
|
883 |
-
|
884 |
-
report_json_str = report.get('report', '{}')
|
885 |
-
try:
|
886 |
-
report_json = json.loads(report_json_str)
|
887 |
-
except json.JSONDecodeError:
|
888 |
-
report_json = {}
|
889 |
-
|
890 |
-
messages_json_str = report.get('messages', '{}')
|
891 |
-
try:
|
892 |
-
messages_json = json.loads(messages_json_str)
|
893 |
-
except json.JSONDecodeError:
|
894 |
-
messages_json = {}
|
895 |
-
|
896 |
-
users_meta = report_json.get('usersMeta', {})
|
897 |
-
|
898 |
-
processed_emails = set()
|
899 |
-
for user_id, user_data in users_meta.items():
|
900 |
-
user_data['messages'] = messages.get(user_id, [])
|
901 |
-
email = user_data.get('email')
|
902 |
-
if email not in processed_emails:
|
903 |
-
update_or_insert_user(DATABASE_NAME, user_data, mapping_template)
|
904 |
-
processed_emails.add(email)
|
905 |
-
|
906 |
-
return jsonify({'status': 'User data saved successfully'})
|
907 |
-
else:
|
908 |
-
return jsonify({'error': 'Failed to fetch data from the API'}), response.status_code
|
909 |
-
|
910 |
|
911 |
|
912 |
|
|
|
782 |
|
783 |
DATABASE_NAME = 'data_gc.db'
|
784 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
785 |
|
786 |
|
787 |
|