DmitrMakeev commited on
Commit
ce55078
·
verified ·
1 Parent(s): 842bd8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -24
app.py CHANGED
@@ -763,6 +763,22 @@ def data_gc_tab_out():
763
 
764
 
765
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
766
 
767
 
768
 
@@ -775,64 +791,137 @@ def update_or_insert_user(db_name, user_data, mapping_template):
775
  conn = sqlite3.connect(db_name)
776
  cursor = conn.cursor()
777
 
 
778
  email = user_data.get('email')
779
  if not email:
780
  logging.error(f"User data missing email: {user_data}")
781
  return
782
 
 
 
 
783
  cursor.execute("SELECT web_st FROM contacts WHERE email = ?", (email,))
784
  user = cursor.fetchone()
 
785
 
786
- web_st_value = 1
 
787
  if user:
 
788
  current_web_st = user[0] if user[0] is not None and user[0] != "" else 0
789
  web_st_value = int(current_web_st) + 1
 
 
 
790
  cursor.execute("UPDATE contacts SET web_st = ? WHERE email = ?", (web_st_value, email))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
791
  else:
792
- transformed_data = {db_column: str(user_data.get(json_key, "")) for json_key, db_column in mapping_template.items()}
793
- transformed_data['web_st'] = web_st_value
794
  columns = ', '.join(transformed_data.keys())
795
  placeholders = ', '.join('?' for _ in transformed_data)
796
  insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
797
- cursor.execute(insert_query, list(transformed_data.values()))
 
 
798
 
 
799
  conn.commit()
800
  conn.close()
801
  logging.debug(f"User with email {email} processed successfully")
802
 
803
- @app.route('/send_request', methods=['POST'])
804
- def send_request():
805
- token = request.form.get('token')
806
- min_date = request.form.get('minDate')
807
- limit = request.form.get('limit', '20')
808
- webinar_type = request.form.get('type', 'LiveWebinars')
809
- url = f'https://online.bizon365.ru/api/v1/webinars/reports/getlist?minDate={min_date}&limit={limit}&type={webinar_type}'
810
-
811
- response = requests.get(url, headers={'X-Token': token})
812
-
813
- if response.status_code == 200:
814
- data = response.json()
815
- webinar_ids = [item['webinarId'] for item in data['list']]
816
- return jsonify(webinar_ids)
817
- else:
818
- return jsonify({'error': 'Failed to fetch data from the API'}), response.status_code
819
 
820
  @app.route('/send_get_request', methods=['GET'])
821
  def send_get_request():
822
  token = request.args.get('token')
823
- webinar_id = request.args.get('webinarId')
824
- url = f'https://online.bizon365.ru/api/v1/webinars/reports/get?webinarId={webinar_id}'
825
 
826
  response = requests.get(url, headers={'X-Token': token})
827
 
828
  if response.status_code == 200:
829
  data = response.json()
830
- report_json = json.loads(data.get('report', {}).get('report', '{}'))
831
- messages_json = json.loads(data.get('report', {}).get('messages', '{}'))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
832
  users_meta = report_json.get('usersMeta', {})
833
 
 
834
  processed_emails = set()
835
  for user_id, user_data in users_meta.items():
 
836
  user_messages = messages_json.get(user_id, [])
837
  user_data['messages'] = user_messages
838
  email = user_data.get('email')
 
763
 
764
 
765
 
766
+
767
+
768
+ @app.route('/send_request', methods=['POST'])
769
+ def send_request():
770
+ token = request.form.get('token')
771
+ min_date = request.form.get('minDate')
772
+ url = f'https://online.bizon365.ru/api/v1/webinars/reports/getlist?minDate={min_date}'
773
+
774
+ response = requests.get(url, headers={'X-Token': token})
775
+
776
+ if response.status_code == 200:
777
+ data = response.json()
778
+ webinar_ids = [item['webinarId'] for item in data['list']]
779
+ return jsonify(webinar_ids)
780
+ else:
781
+ return jsonify({'error': 'Failed to fetch data from the API'}), response.status_code
782
 
783
 
784
 
 
791
  conn = sqlite3.connect(db_name)
792
  cursor = conn.cursor()
793
 
794
+ # Получение email пользователя из данных
795
  email = user_data.get('email')
796
  if not email:
797
  logging.error(f"User data missing email: {user_data}")
798
  return
799
 
800
+ logging.debug(f"Processing user with email: {email}")
801
+
802
+ # Проверка существования пользователя в базе данных по email
803
  cursor.execute("SELECT web_st FROM contacts WHERE email = ?", (email,))
804
  user = cursor.fetchone()
805
+ logging.debug(f"User found: {user}")
806
 
807
+ # Вынесение увеличения значения web_st в отдельный блок
808
+ web_st_value = 1 # Инициализация значения web_st
809
  if user:
810
+ # Проверка текущего значения web_st и его инкрементация
811
  current_web_st = user[0] if user[0] is not None and user[0] != "" else 0
812
  web_st_value = int(current_web_st) + 1
813
+ logging.debug(f"Calculated web_st_value: {web_st_value}")
814
+
815
+ # Обновление значения web_st
816
  cursor.execute("UPDATE contacts SET web_st = ? WHERE email = ?", (web_st_value, email))
817
+ conn.commit()
818
+ conn.close()
819
+ logging.debug(f"User {email} web_st updated to {web_st_value}")
820
+ else:
821
+ conn.close()
822
+ logging.debug(f"User {email} not found, proceeding with insert")
823
+
824
+ # Открываем соединение снова для остальных операций
825
+ conn = sqlite3.connect(db_name)
826
+ cursor = conn.cursor()
827
+
828
+ # Преобразование данных пользователя на основе шаблона сопоставления
829
+ transformed_data = {}
830
+ for json_key, db_column in mapping_template.items():
831
+ value = user_data.get(json_key, "")
832
+
833
+ if isinstance(value, list):
834
+ # Проверяем тип элементов списка
835
+ if all(isinstance(item, str) for item in value):
836
+ transformed_data[db_column] = "; ".join(value) # Сохраняем сообщения в строку
837
+ else:
838
+ logging.error(f"Expected list of strings for key {json_key}, but got: {value}")
839
+ transformed_data[db_column] = ""
840
+ else:
841
+ transformed_data[db_column] = str(value)
842
+ logging.debug(f"Transformed data: {transformed_data}")
843
+
844
+ # Заполнение обязательных полей значениями по умолчанию
845
+ required_fields = [
846
+ "vk_id", "chat_id", "ws_st", "ws_stop", "web_st", "fin_prog",
847
+ "b_city", "b_fin", "b_ban", "b_ign", "b_baners", "b_butt", "b_mess",
848
+ "shop_st", "curator", "pr1", "pr2", "pr3", "pr4", "pr5", "ad_url",
849
+ "key_pr", "n_con", "canal", "data_t"
850
+ ]
851
+ for field in required_fields:
852
+ if field not in transformed_data:
853
+ transformed_data[field] = ""
854
+ logging.debug(f"Transformed data after adding required fields: {transformed_data}")
855
+
856
+ # Обработка номера телефона, если он есть
857
+ if 'phone' in user_data:
858
+ phone = user_data['phone']
859
+ if phone.startswith('+'):
860
+ phone = phone[1:]
861
+ transformed_data['phone'] = phone
862
+ logging.debug(f"Transformed data after phone processing: {transformed_data}")
863
+
864
+ # Добавление значения web_st в данные для вставки
865
+ transformed_data['web_st'] = web_st_value
866
+
867
+ # Обновление данных пользователя в базе данных
868
+ if user:
869
+ update_query = "UPDATE contacts SET "
870
+ update_values = []
871
+ for column, value in transformed_data.items():
872
+ update_query += f"{column} = ?, "
873
+ update_values.append(value)
874
+ update_query = update_query.rstrip(", ") + " WHERE email = ?"
875
+ update_values.append(email)
876
+ logging.debug(f"Update query: {update_query} with values: {update_values}")
877
+ cursor.execute(update_query, update_values)
878
  else:
 
 
879
  columns = ', '.join(transformed_data.keys())
880
  placeholders = ', '.join('?' for _ in transformed_data)
881
  insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
882
+ insert_values = list(transformed_data.values())
883
+ logging.debug(f"Insert query: {insert_query} with values: {insert_values}")
884
+ cursor.execute(insert_query, insert_values)
885
 
886
+ # Подтверждение изменений и закрытие соединения
887
  conn.commit()
888
  conn.close()
889
  logging.debug(f"User with email {email} processed successfully")
890
 
891
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
892
 
893
  @app.route('/send_get_request', methods=['GET'])
894
  def send_get_request():
895
  token = request.args.get('token')
896
+ webinarId = request.args.get('webinarId')
897
+ url = f'https://online.bizon365.ru/api/v1/webinars/reports/get?webinarId={webinarId}'
898
 
899
  response = requests.get(url, headers={'X-Token': token})
900
 
901
  if response.status_code == 200:
902
  data = response.json()
903
+
904
+ report = data.get('report', {})
905
+ messages = data.get('messages', {})
906
+
907
+ report_json_str = report.get('report', '{}')
908
+ try:
909
+ report_json = json.loads(report_json_str)
910
+ except json.JSONDecodeError:
911
+ report_json = {}
912
+
913
+ messages_json_str = report.get('messages', '{}')
914
+ try:
915
+ messages_json = json.loads(messages_json_str)
916
+ except json.JSONDecodeError:
917
+ messages_json = {}
918
+
919
  users_meta = report_json.get('usersMeta', {})
920
 
921
+ # Обновление или добавление каждого пользователя в базу данных
922
  processed_emails = set()
923
  for user_id, user_data in users_meta.items():
924
+ # Получаем только сообщения для текущего пользователя
925
  user_messages = messages_json.get(user_id, [])
926
  user_data['messages'] = user_messages
927
  email = user_data.get('email')