DmitrMakeev commited on
Commit
140d0d1
1 Parent(s): 69797d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -59
app.py CHANGED
@@ -714,7 +714,7 @@ def data_gc_tab_out():
714
 
715
 
716
 
717
-
718
 
719
 
720
 
@@ -724,7 +724,20 @@ def data_gc_tab_out():
724
 
725
  DATABASE_NAME = 'data_gc.db'
726
 
727
- def update_or_insert_user(db_name, user_data, mapping_template):
 
 
 
 
 
 
 
 
 
 
 
 
 
728
  conn = sqlite3.connect(db_name)
729
  cursor = conn.cursor()
730
 
@@ -744,11 +757,7 @@ def update_or_insert_user(db_name, user_data, mapping_template):
744
  if user:
745
  current_web_st = user[0] if user[0] is not None and user[0] != "" else 0
746
  web_st_value = int(current_web_st) + 1
747
- logging.debug(f"Calculated web_st_value: {web_st_value}")
748
-
749
  current_messages = user[1] if user[1] is not None else ""
750
- logging.debug(f"Current messages: {current_messages}")
751
-
752
  cursor.execute("UPDATE contacts SET web_st = ? WHERE email = ?", (web_st_value, email))
753
  conn.commit()
754
  conn.close()
@@ -763,7 +772,6 @@ def update_or_insert_user(db_name, user_data, mapping_template):
763
  transformed_data = {}
764
  for json_key, db_column in mapping_template.items():
765
  value = user_data.get(json_key, "")
766
-
767
  if isinstance(value, list):
768
  if all(isinstance(item, str) for item in value):
769
  transformed_data[db_column] = "; ".join(value)
@@ -823,49 +831,9 @@ def update_or_insert_user(db_name, user_data, mapping_template):
823
  conn.close()
824
  logging.debug(f"User with email {email} processed successfully")
825
 
826
- def send_to_google_forms(data):
827
- form_url = (
828
- "https://docs.google.com/forms/d/e/1FAIpQLSc-JbmXvgpgGq6KrkXsYSsfMACVMyIDnNqrHy6jImGeSRcpiQ/viewform?"
829
- "usp=pp_url"
830
- f"&name={data.get('name', '')}"
831
- f"&email={data.get('email', '')}"
832
- f"&phone={data.get('phone', '')}"
833
- f"&vk_id={data.get('vk_id', '')}"
834
- f"&chat_id={data.get('chat_id', '')}"
835
- f"&ws_st={data.get('ws_st', '')}"
836
- f"&ws_stop={data.get('ws_stop', '')}"
837
- f"&web_st={data.get('web_st', '')}"
838
- f"&fin_prog={data.get('fin_prog', '')}"
839
- f"&b_city={data.get('b_city', '')}"
840
- f"&b_fin={data.get('b_fin', '')}"
841
- f"&b_ban={data.get('b_ban', '')}"
842
- f"&b_ign={data.get('b_ign', '')}"
843
- f"&b_baners={data.get('b_baners', '')}"
844
- f"&b_butt={data.get('b_butt', '')}"
845
- f"&b_mess={data.get('b_mess', '')}"
846
- f"&shop_st={data.get('shop_st', '')}"
847
- f"&curator={data.get('curator', '')}"
848
- f"&pr1={data.get('pr1', '')}"
849
- f"&pr2={data.get('pr2', '')}"
850
- f"&pr3={data.get('pr3', '')}"
851
- f"&pr4={data.get('pr4', '')}"
852
- f"&pr5={data.get('pr5', '')}"
853
- f"&gc_url={data.get('gc_url', '')}"
854
- f"&key_pr={data.get('key_pr', '')}"
855
- f"&n_con={data.get('n_con', '')}"
856
- f"&canal={data.get('canal', '')}"
857
- f"&data_t={data.get('data_t', '')}"
858
- f"&utm_source={data.get('utm_source', '')}"
859
- f"&utm_medium={data.get('utm_medium', '')}"
860
- f"&utm_campaign={data.get('utm_campaign', '')}"
861
- f"&utm_term={data.get('utm_term', '')}"
862
- f"&utm_content={data.get('utm_content', '')}"
863
- )
864
- response = requests.post(form_url)
865
- if response.status_code == 200:
866
- logging.info("Data successfully sent to Google Forms")
867
- else:
868
- logging.error(f"Failed to send data to Google Forms: {response.status_code}")
869
 
870
  @app.route('/send_request', methods=['POST'])
871
  def send_request():
@@ -883,6 +851,9 @@ def send_request():
883
  else:
884
  return jsonify({'error': 'Failed to fetch data from the API'}), response.status_code
885
 
 
 
 
886
  @app.route('/send_get_request', methods=['GET'])
887
  def send_get_request():
888
  token = request.args.get('token')
@@ -891,7 +862,7 @@ def send_get_request():
891
 
892
  try:
893
  response = requests.get(url, headers={'X-Token': token})
894
- response.raise_for_status()
895
  data = response.json()
896
 
897
  if data is None or 'report' not in data:
@@ -923,8 +894,7 @@ def send_get_request():
923
  user_data['messages'] = user_messages
924
  email = user_data.get('email')
925
  if email and email not in processed_emails:
926
- update_or_insert_user(DATABASE_NAME, user_data, mapping_template)
927
- send_to_google_forms(user_data)
928
  processed_emails.add(email)
929
 
930
  return jsonify({'status': 'User data saved successfully'})
@@ -973,18 +943,14 @@ def webhookbz():
973
  user_data['messages'] = user_messages
974
  email = user_data.get('email')
975
  if email and email not in processed_emails:
976
- update_or_insert_user(DATABASE_NAME, user_data, mapping_template)
977
- send_to_google_forms(user_data)
978
  processed_emails.add(email)
979
 
980
  return jsonify({'status': 'User data saved successfully'})
981
  else:
982
  return jsonify({'error': 'Failed to fetch data from the API'}), response.status_code
983
 
984
-
985
-
986
-
987
-
988
 
989
 
990
 
 
714
 
715
 
716
 
717
+ gog_url = "https://docs.google.com/formResponse/d/e/1FAIpQLSc-JbmXvgpgGq6KrkXsYSsfMACVMyIDnNqrHy6jImGeSRcpiQ/{viewform}?usp=pp_url&entry.1556100878={name}&entry.1477412341={email}&entry.1634985541={phone}&entry.1736544219={vk_id}&entry.62153872={chat_id}&entry.1913752768={ws_st}&entry.1768186232={ws_stop}&entry.1198983592={web_st}&entry.994770784={fin_prog}&entry.910932310={b_city}&entry.1923801792={b_fin}&entry.2005444720={b_ban}&entry.741087361={b_ign}&entry.1316159837={b_baners}&entry.355123557={b_butt}&entry.395996312={b_mess}&entry.646571729={shop_st}&entry.578527800={curator}&entry.1936838964={pr1}&entry.1375537366={pr2}&entry.1249356084={pr3}&entry.752547226={pr4}&entry.704766458={pr5}&entry.1837661={gc_url}&entry.398837750={key_pr}&entry.225564240={n_con}&entry.1642320872={canal}&entry.1581826411={data_t}&entry.311131724={utm_source}&entry.1904279859={utm_medium}&entry.740234546={utm_campaign}&entry.880981295={utm_term}&entry.431306383={utm_content}"
718
 
719
 
720
 
 
724
 
725
  DATABASE_NAME = 'data_gc.db'
726
 
727
+ def send_to_google_forms(user_data, gog_url):
728
+ # Формирование URL с параметрами
729
+ url = gog_url.format(**user_data)
730
+
731
+ # Отправка POST-запроса
732
+ response = requests.post(url)
733
+
734
+ if response.status_code == 200:
735
+ logging.debug(f"Data sent to Google Forms successfully for user: {user_data.get('email')}")
736
+ else:
737
+ logging.error(f"Failed to send data to Google Forms for user: {user_data.get('email')}. Response: {response.text}")
738
+
739
+
740
+ def update_or_insert_user(db_name, user_data, mapping_template, gog_url):
741
  conn = sqlite3.connect(db_name)
742
  cursor = conn.cursor()
743
 
 
757
  if user:
758
  current_web_st = user[0] if user[0] is not None and user[0] != "" else 0
759
  web_st_value = int(current_web_st) + 1
 
 
760
  current_messages = user[1] if user[1] is not None else ""
 
 
761
  cursor.execute("UPDATE contacts SET web_st = ? WHERE email = ?", (web_st_value, email))
762
  conn.commit()
763
  conn.close()
 
772
  transformed_data = {}
773
  for json_key, db_column in mapping_template.items():
774
  value = user_data.get(json_key, "")
 
775
  if isinstance(value, list):
776
  if all(isinstance(item, str) for item in value):
777
  transformed_data[db_column] = "; ".join(value)
 
831
  conn.close()
832
  logging.debug(f"User with email {email} processed successfully")
833
 
834
+ # Отправка данных в Google Forms
835
+ send_to_google_forms(transformed_data, gog_url)
836
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
837
 
838
  @app.route('/send_request', methods=['POST'])
839
  def send_request():
 
851
  else:
852
  return jsonify({'error': 'Failed to fetch data from the API'}), response.status_code
853
 
854
+
855
+
856
+
857
  @app.route('/send_get_request', methods=['GET'])
858
  def send_get_request():
859
  token = request.args.get('token')
 
862
 
863
  try:
864
  response = requests.get(url, headers={'X-Token': token})
865
+ response.raise_for_status() # Проверка на ошибки HTTP
866
  data = response.json()
867
 
868
  if data is None or 'report' not in data:
 
894
  user_data['messages'] = user_messages
895
  email = user_data.get('email')
896
  if email and email not in processed_emails:
897
+ update_or_insert_user(DATABASE_NAME, user_data, mapping_template, gog_url)
 
898
  processed_emails.add(email)
899
 
900
  return jsonify({'status': 'User data saved successfully'})
 
943
  user_data['messages'] = user_messages
944
  email = user_data.get('email')
945
  if email and email not in processed_emails:
946
+ update_or_insert_user(DATABASE_NAME, user_data, mapping_template, gog_url)
 
947
  processed_emails.add(email)
948
 
949
  return jsonify({'status': 'User data saved successfully'})
950
  else:
951
  return jsonify({'error': 'Failed to fetch data from the API'}), response.status_code
952
 
953
+
 
 
 
954
 
955
 
956