DmitrMakeev commited on
Commit
cbcf70a
1 Parent(s): 5b105d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -160
app.py CHANGED
@@ -282,20 +282,6 @@ def initialize_requests():
282
 
283
 
284
 
285
- def send_test_data():
286
- GOOGLE_FORM_URL = (
287
- "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}"
288
- )
289
-
290
- response = requests.post(GOOGLE_FORM_URL)
291
-
292
- if response.status_code == 200:
293
- print("Data sent successfully.")
294
- else:
295
- print(f"Failed to send data: {response.status_code}, {response.text}")
296
-
297
- send_test_data()
298
-
299
 
300
 
301
 
@@ -735,105 +721,12 @@ def data_gc_tab_out():
735
  DATABASE_NAME = 'data_gc.db'
736
 
737
 
 
 
 
738
 
739
 
740
- # URL Google Формы как переменная
741
- GOOGLE_FORM_URL = (
742
- "https://docs.google.com/formResponse/d/e/1FAIpQLSc-JbmXvgpgGq6KrkXsYSsfMACVMyIDnNqrHy6jImGeSRcpiQ/formResponse"
743
- "?usp=pp_url&entry.1556100878={name}&entry.1477412341={email}&entry.1634985541={phone}&entry.1736544219={vk_id}"
744
- "&entry.62153872={chat_id}&entry.1913752768={ws_st}&entry.1768186232={ws_stop}&entry.1198983592={web_st}"
745
- "&entry.994770784={fin_prog}&entry.910932310={b_city}&entry.1923801792={b_fin}&entry.2005444720={b_ban}"
746
- "&entry.741087361={b_ign}&entry.1316159837={b_baners}&entry.355123557={b_butt}&entry.395996312={b_mess}"
747
- "&entry.646571729={shop_st}&entry.578527800={curator}&entry.1936838964={pr1}&entry.1375537366={pr2}"
748
- "&entry.1249356084={pr3}&entry.752547226={pr4}&entry.704766458={pr5}&entry.1837661={gc_url}&entry.398837750={key_pr}"
749
- "&entry.225564240={n_con}&entry.1642320872={canal}&entry.1581826411={data_t}&entry.311131724={utm_source}"
750
- "&entry.1904279859={utm_medium}&entry.740234546={utm_campaign}&entry.880981295={utm_term}&entry.431306383={utm_content}"
751
- )
752
-
753
- # Определяем шаблон для маппинга
754
- mapping_template_log = {
755
- 'name': 'name',
756
- 'email': 'email',
757
- 'phone': 'phone',
758
- 'vk_id': 'vk_id',
759
- 'chat_id': 'chat_id',
760
- 'ws_st': 'ws_st',
761
- 'ws_stop': 'ws_stop',
762
- 'web_st': 'web_st',
763
- 'fin_prog': 'fin_prog',
764
- 'b_city': 'b_city',
765
- 'b_fin': 'b_fin',
766
- 'b_ban': 'b_ban',
767
- 'b_ign': 'b_ign',
768
- 'b_baners': 'b_baners',
769
- 'b_butt': 'b_butt',
770
- 'b_mess': 'b_mess',
771
- 'shop_st': 'shop_st',
772
- 'curator': 'curator',
773
- 'pr1': 'pr1',
774
- 'pr2': 'pr2',
775
- 'pr3': 'pr3',
776
- 'pr4': 'pr4',
777
- 'pr5': 'pr5',
778
- 'gc_url': 'gc_url',
779
- 'key_pr': 'key_pr',
780
- 'n_con': 'n_con',
781
- 'canal': 'canal',
782
- 'data_t': 'data_t',
783
- 'utm_source': 'utm_source',
784
- 'utm_medium': 'utm_medium',
785
- 'utm_campaign': 'utm_campaign',
786
- 'utm_term': 'utm_term',
787
- 'utm_content': 'utm_content'
788
- }
789
-
790
-
791
-
792
- def send_data_to_google_form(data):
793
- # Используем переменную GOOGLE_FORM_URL для формирования ссылки
794
- google_form_url = GOOGLE_FORM_URL.format(
795
- name=data.get('name', ''),
796
- email=data.get('email', ''),
797
- phone=data.get('phone', ''),
798
- vk_id=data.get('vk_id', ''),
799
- chat_id=data.get('chat_id', ''),
800
- ws_st=data.get('ws_st', ''),
801
- ws_stop=data.get('ws_stop', ''),
802
- web_st=data.get('web_st', ''),
803
- fin_prog=data.get('fin_prog', ''),
804
- b_city=data.get('b_city', ''),
805
- b_fin=data.get('b_fin', ''),
806
- b_ban=data.get('b_ban', ''),
807
- b_ign=data.get('b_ign', ''),
808
- b_baners=data.get('b_baners', ''),
809
- b_butt=data.get('b_butt', ''),
810
- b_mess=data.get('b_mess', ''),
811
- shop_st=data.get('shop_st', ''),
812
- curator=data.get('curator', ''),
813
- pr1=data.get('pr1', ''),
814
- pr2=data.get('pr2', ''),
815
- pr3=data.get('pr3', ''),
816
- pr4=data.get('pr4', ''),
817
- pr5=data.get('pr5', ''),
818
- gc_url=data.get('gc_url', ''),
819
- key_pr=data.get('key_pr', ''),
820
- n_con=data.get('n_con', ''),
821
- canal=data.get('canal', ''),
822
- data_t=data.get('data_t', ''),
823
- utm_source=data.get('utm_source', ''),
824
- utm_medium=data.get('utm_medium', ''),
825
- utm_campaign=data.get('utm_campaign', ''),
826
- utm_term=data.get('utm_term', ''),
827
- utm_content=data.get('utm_content', '')
828
- )
829
-
830
- response = requests.post(google_form_url)
831
- if response.status_code != 200:
832
- logging.error(f"Failed to send data to Google Form: {response.status_code}, {response.text}")
833
- else:
834
- logging.info(f"Data sent to Google Form successfully: {response.status_code}")
835
-
836
- def update_or_insert_user(db_name, user_data, mapping_template_log):
837
  conn = sqlite3.connect(db_name)
838
  cursor = conn.cursor()
839
 
@@ -843,7 +736,7 @@ def update_or_insert_user(db_name, user_data, mapping_template_log):
843
  return
844
 
845
  logging.debug(f"Processing user with email: {email}")
846
-
847
  cursor.execute("SELECT web_st, b_mess FROM contacts WHERE email = ?", (email,))
848
  user = cursor.fetchone()
849
  logging.debug(f"User found: {user}")
@@ -870,7 +763,7 @@ def update_or_insert_user(db_name, user_data, mapping_template_log):
870
  cursor = conn.cursor()
871
 
872
  transformed_data = {}
873
- for json_key, db_column in mapping_template_log.items():
874
  value = user_data.get(json_key, "")
875
 
876
  if isinstance(value, list):
@@ -883,58 +776,68 @@ def update_or_insert_user(db_name, user_data, mapping_template_log):
883
  transformed_data[db_column] = str(value)
884
  logging.debug(f"Transformed data: {transformed_data}")
885
 
886
- insert_query = f"""
887
- INSERT INTO contacts (name, phone, email, vk_id, chat_id, ws_st, ws_stop, web_st, fin_prog, b_city, b_fin, b_ban, b_ign, b_baners, b_butt, b_mess, shop_st, curator, pr1, pr2, pr3, pr4, pr5, gc_url, key_pr, n_con, canal, data_t, utm_source, utm_medium, utm_campaign, utm_term, utm_content)
888
- VALUES (:name, :phone, :email, :vk_id, :chat_id, :ws_st, :ws_stop, :web_st, :fin_prog, :b_city, :b_fin, :b_ban, :b_ign, :b_baners, :b_butt, :b_mess, :shop_st, :curator, :pr1, :pr2, :pr3, :pr4, :pr5, :gc_url, :key_pr, :n_con, :canal, :data_t, :utm_source, :utm_medium, :utm_campaign, :utm_term, :utm_content)
889
- """
890
-
891
- insert_values = {
892
- 'name': transformed_data.get('name', ''),
893
- 'phone': transformed_data.get('phone', ''),
894
- 'email': transformed_data.get('email', ''),
895
- 'vk_id': transformed_data.get('vk_id', ''),
896
- 'chat_id': transformed_data.get('chat_id', ''),
897
- 'ws_st': transformed_data.get('ws_st', ''),
898
- 'ws_stop': transformed_data.get('ws_stop', ''),
899
- 'web_st': transformed_data.get('web_st', ''),
900
- 'fin_prog': transformed_data.get('fin_prog', ''),
901
- 'b_city': transformed_data.get('b_city', ''),
902
- 'b_fin': transformed_data.get('b_fin', ''),
903
- 'b_ban': transformed_data.get('b_ban', ''),
904
- 'b_ign': transformed_data.get('b_ign', ''),
905
- 'b_baners': transformed_data.get('b_baners', ''),
906
- 'b_butt': transformed_data.get('b_butt', ''),
907
- 'b_mess': transformed_data.get('b_mess', ''),
908
- 'shop_st': transformed_data.get('shop_st', ''),
909
- 'curator': transformed_data.get('curator', ''),
910
- 'pr1': transformed_data.get('pr1', ''),
911
- 'pr2': transformed_data.get('pr2', ''),
912
- 'pr3': transformed_data.get('pr3', ''),
913
- 'pr4': transformed_data.get('pr4', ''),
914
- 'pr5': transformed_data.get('pr5', ''),
915
- 'gc_url': transformed_data.get('gc_url', ''),
916
- 'key_pr': transformed_data.get('key_pr', ''),
917
- 'n_con': transformed_data.get('n_con', ''),
918
- 'canal': transformed_data.get('canal', ''),
919
- 'data_t': transformed_data.get('data_t', ''),
920
- 'utm_source': transformed_data.get('utm_source', ''),
921
- 'utm_medium': transformed_data.get('utm_medium', ''),
922
- 'utm_campaign': transformed_data.get('utm_campaign', ''),
923
- 'utm_term': transformed_data.get('utm_term', ''),
924
- 'utm_content': transformed_data.get('utm_content', '')
925
- }
926
- logging.debug(f"Insert query: {insert_query} with values: {insert_values}")
927
- cursor.execute(insert_query, insert_values)
 
 
928
 
929
  conn.commit()
930
  conn.close()
931
  logging.debug(f"User with email {email} processed successfully")
932
 
933
- # Отправляем данные в Google Форму после обновления/вставки
934
- send_data_to_google_form(transformed_data)
 
 
 
 
 
 
 
 
935
 
936
  @app.route('/send_request', methods=['POST'])
937
- def send_request():
938
  token = request.form.get('token')
939
  min_date = request.form.get('minDate')
940
  type = request.form.get('type')
@@ -1045,8 +948,43 @@ def webhookbz():
1045
  else:
1046
  return jsonify({'error': 'Failed to fetch data from the API'}), response.status_code
1047
 
1048
-
1049
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1050
 
1051
 
1052
 
 
282
 
283
 
284
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
285
 
286
 
287
 
 
721
  DATABASE_NAME = 'data_gc.db'
722
 
723
 
724
+ DATABASE_NAME = 'data_gc.db'
725
+ DATABASES = ['data_gc.db', 'data1.db', 'data2.db', 'data3.db', 'data4.db', 'data5.db']
726
+ 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}"
727
 
728
 
729
+ def update_or_insert_user(db_name, user_data, mapping_template):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
730
  conn = sqlite3.connect(db_name)
731
  cursor = conn.cursor()
732
 
 
736
  return
737
 
738
  logging.debug(f"Processing user with email: {email}")
739
+
740
  cursor.execute("SELECT web_st, b_mess FROM contacts WHERE email = ?", (email,))
741
  user = cursor.fetchone()
742
  logging.debug(f"User found: {user}")
 
763
  cursor = conn.cursor()
764
 
765
  transformed_data = {}
766
+ for json_key, db_column in mapping_template.items():
767
  value = user_data.get(json_key, "")
768
 
769
  if isinstance(value, list):
 
776
  transformed_data[db_column] = str(value)
777
  logging.debug(f"Transformed data: {transformed_data}")
778
 
779
+ required_fields = [
780
+ "vk_id", "chat_id", "ws_st", "ws_stop", "web_st", "fin_prog",
781
+ "b_city", "b_fin", "b_ban", "b_ign", "b_baners", "b_butt", "b_mess",
782
+ "shop_st", "curator", "pr1", "pr2", "pr3", "pr4", "pr5", "gc_url",
783
+ "key_pr", "n_con", "canal", "data_t", 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content'
784
+ ]
785
+ for field in required_fields:
786
+ if field not in transformed_data:
787
+ transformed_data[field] = ""
788
+ logging.debug(f"Transformed data after adding required fields: {transformed_data}")
789
+
790
+ if 'phone' in user_data:
791
+ phone = user_data['phone']
792
+ if phone.startswith('+'):
793
+ phone = phone[1:]
794
+ transformed_data['phone'] = phone
795
+ logging.debug(f"Transformed data after phone processing: {transformed_data}")
796
+
797
+ transformed_data['web_st'] = web_st_value
798
+
799
+ new_messages = transformed_data.get('b_mess', "")
800
+ if current_messages:
801
+ transformed_data['b_mess'] = current_messages + "; " + new_messages
802
+ else:
803
+ transformed_data['b_mess'] = new_messages
804
+ logging.debug(f"Transformed data after message processing: {transformed_data}")
805
+
806
+ if user:
807
+ update_query = "UPDATE contacts SET "
808
+ update_values = []
809
+ for column, value in transformed_data.items():
810
+ update_query += f"{column} = ?, "
811
+ update_values.append(value)
812
+ update_query = update_query.rstrip(", ") + " WHERE email = ?"
813
+ update_values.append(email)
814
+ logging.debug(f"Update query: {update_query} with values: {update_values}")
815
+ cursor.execute(update_query, update_values)
816
+ else:
817
+ columns = ', '.join(transformed_data.keys())
818
+ placeholders = ', '.join('?' for _ in transformed_data)
819
+ insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})"
820
+ insert_values = list(transformed_data.values())
821
+ logging.debug(f"Insert query: {insert_query} with values: {insert_values}")
822
+ cursor.execute(insert_query, insert_values)
823
 
824
  conn.commit()
825
  conn.close()
826
  logging.debug(f"User with email {email} processed successfully")
827
 
828
+ # Отправляем данные в Google форму
829
+ url = generate_gog_url(transformed_data)
830
+ send_request(url, {})
831
+
832
+ def send_request(url, data):
833
+ response = requests.post(url, data=data)
834
+ if response.status_code == 200:
835
+ logging.info(f"Request sent successfully: {response.url}")
836
+ else:
837
+ logging.error(f"Failed to send request: {response.status_code} - {response.text}")
838
 
839
  @app.route('/send_request', methods=['POST'])
840
+ def send_request_route():
841
  token = request.form.get('token')
842
  min_date = request.form.get('minDate')
843
  type = request.form.get('type')
 
948
  else:
949
  return jsonify({'error': 'Failed to fetch data from the API'}), response.status_code
950
 
951
+ def generate_gog_url(user_data):
952
+ data_dict = {
953
+ 'name': quote(user_data.get('name', '')),
954
+ 'email': quote(user_data.get('email', '')),
955
+ 'phone': quote(user_data.get('phone', '')),
956
+ 'vk_id': quote(user_data.get('vk_id', '')),
957
+ 'chat_id': quote(user_data.get('chat_id', '')),
958
+ 'ws_st': quote(user_data.get('ws_st', '')),
959
+ 'ws_stop': quote(user_data.get('ws_stop', '')),
960
+ 'web_st': quote(user_data.get('web_st', '')),
961
+ 'fin_prog': quote(user_data.get('fin_prog', '')),
962
+ 'b_city': quote(user_data.get('b_city', '')),
963
+ 'b_fin': quote(user_data.get('b_fin', '')),
964
+ 'b_ban': quote(user_data.get('b_ban', '')),
965
+ 'b_ign': quote(user_data.get('b_ign', '')),
966
+ 'b_baners': quote(user_data.get('b_baners', '')),
967
+ 'b_butt': quote(user_data.get('b_butt', '')),
968
+ 'b_mess': quote(user_data.get('b_mess', '')),
969
+ 'shop_st': quote(user_data.get('shop_st', '')),
970
+ 'curator': quote(user_data.get('curator', '')),
971
+ 'pr1': quote(user_data.get('pr1', '')),
972
+ 'pr2': quote(user_data.get('pr2', '')),
973
+ 'pr3': quote(user_data.get('pr3', '')),
974
+ 'pr4': quote(user_data.get('pr4', '')),
975
+ 'pr5': quote(user_data.get('pr5', '')),
976
+ 'gc_url': quote(user_data.get('gc_url', '')),
977
+ 'key_pr': quote(user_data.get('key_pr', '')),
978
+ 'n_con': quote(user_data.get('n_con', '')),
979
+ 'canal': quote(user_data.get('canal', '')),
980
+ 'data_t': quote(user_data.get('data_t', '')),
981
+ 'utm_source': quote(user_data.get('utm_source', '')),
982
+ 'utm_medium': quote(user_data.get('utm_medium', '')),
983
+ 'utm_campaign': quote(user_data.get('utm_campaign', '')),
984
+ 'utm_term': quote(user_data.get('utm_term', '')),
985
+ 'utm_content': quote(user_data.get('utm_content', ''))
986
+ }
987
+ return gog_url.format(**data_dict)
988
 
989
 
990