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