| from flask import Flask, request, render_template_string, render_template, jsonify, Response |
| import sqlite3 |
| import os |
| import random |
| import requests |
| import time |
| import re |
| import json |
| import base64 |
| import logging |
| import csv |
| import io |
| from urllib.parse import quote |
|
|
| from datetime import datetime |
| import pytz |
|
|
| from unidecode import unidecode |
|
|
| from base import replace_null_with_empty_string |
|
|
| api_key_sys = os.getenv('api_key_sys') |
| |
| gc_url_gru = os.getenv('gc_url_gru') |
|
|
| gc_url_export = os.getenv('gc_url_export') |
|
|
| start_up = os.getenv('start_up') |
|
|
| gc_url = os.getenv('gc_url') |
| gc_url_form = os.getenv('gc_url_form') |
| gc_api = os.getenv('gc_api') |
|
|
| wa_url = os.getenv('wa_url') |
| wa_api_key = os.getenv('wa_api_key') |
| wa_ak = os.getenv('ws_ak') |
| ws_url_mes = "/sendMessage/" |
| ws_url_ver = "/checkWhatsapp/" |
|
|
| up_db = os.getenv('up_db') |
|
|
| id_gru = os.getenv('id_gru') |
| date_from = "2022-01-01" |
|
|
|
|
| export_id = "" |
|
|
| code_executed = False |
|
|
|
|
| status = "active" |
|
|
|
|
|
|
| current_curator_index = 0 |
| verifikation_start = "1" |
| curator_on_off = "0" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| app = Flask(__name__, template_folder="./") |
| app.config['DEBUG'] = True |
| UPLOAD_FOLDER = 'static' |
| logging.basicConfig(level=logging.DEBUG) |
| if not os.path.exists(UPLOAD_FOLDER): |
| os.makedirs(UPLOAD_FOLDER) |
|
|
| DATABASES = ['data_gc.db', 'data1.db', 'data2.db', 'data3.db', 'data4.db', 'data5.db'] |
|
|
|
|
|
|
| def init_db(db_name): |
| conn = sqlite3.connect(db_name) |
| cursor = conn.cursor() |
| cursor.execute(''' |
| CREATE TABLE IF NOT EXISTS contacts ( |
| id INTEGER PRIMARY KEY AUTOINCREMENT, |
| name TEXT NOT NULL, |
| phone TEXT NOT NULL, |
| email TEXT NOT NULL, |
| vk_id TEXT, |
| chat_id TEXT, |
| ws_st TEXT, |
| ws_stop TEXT, |
| web_st INTEGER, |
| fin_prog INTEGER, |
| b_city TEXT, |
| b_fin TEXT, |
| b_ban TEXT, |
| b_ign TEXT, |
| b_baners TEXT, |
| b_butt TEXT, |
| b_mess TEXT, |
| shop_st TEXT, |
| curator TEXT, |
| pr1 TEXT, |
| pr2 TEXT, |
| pr3 TEXT, |
| pr4 TEXT, |
| pr5 TEXT, |
| gc_url TEXT, |
| key_pr TEXT, |
| n_con TEXT, |
| canal TEXT, |
| data_on TEXT, |
| data_t TEXT, |
| utm_source TEXT, |
| utm_medium TEXT, |
| utm_campaign TEXT, |
| utm_term TEXT, |
| utm_content TEXT, |
| gcpc TEXT |
| ) |
| ''') |
| conn.commit() |
| conn.close() |
|
|
| for db in DATABASES: |
| init_db(db) |
|
|
|
|
|
|
|
|
| template = { |
| "username": "name", |
| "phone": "phone", |
| "email": "email", |
| "city": "b_city", |
| "finished": "b_fin", |
| "ban": "b_ban", |
| "ignore": "b_ign", |
| "banners": "b_baners", |
| "buttons": "b_butt", |
| "messages": "b_mess" |
| } |
|
|
| mapping_template = { |
| "username": "name", |
| "phone": "phone", |
| "email": "email", |
| "city": "b_city", |
| "finished": "b_fin", |
| "ban": "b_ban", |
| "ignore": "b_ign", |
| "banners": "b_baners", |
| "buttons": "b_butt", |
| "messages": "b_mess" |
| } |
|
|
|
|
|
|
|
|
| mapping_template_cur = { |
| 'name': 'Name', |
| 'phone': 'Phone', |
| 'email': 'Email' |
| } |
|
|
|
|
|
|
| |
| def fetch(url): |
| try: |
| response = requests.get(url) |
| response.raise_for_status() |
| print(f"GET запрос к {url} выполнен успешно.") |
| return response.json() |
| except requests.RequestException as e: |
| print(f"Ошибка при выполнении GET запроса к {url}: {e}") |
| return None |
|
|
| |
| def send_notification(url, data): |
| try: |
| response = requests.post(url, json=data) |
| response.raise_for_status() |
| print(f"POST запрос к {url} выполнен успешно.") |
| return response.json() |
| except requests.RequestException as e: |
| print(f"Ошибка при выполнении POST запроса к {url}: {e}") |
| return None |
|
|
| |
| def initialize_requests(): |
| global code_executed, export_id |
| print(f"Функция initialize_requests вызвана. start_up: {start_up}, code_executed: {code_executed}") |
|
|
| if start_up == '1' and not code_executed: |
| try: |
| |
| url_template = f"{gc_url_gru}/{id_gru}/users?key={gc_api}&created_at[from]={date_from}&status={status}" |
| data = fetch(url_template) |
| if data and data.get("success"): |
| export_id = data.get("info", {}).get("export_id", "") |
| print("Export ID:", export_id) |
|
|
| |
| notification_url = "https://skyauto.me/cllbck/217669590/29245685/bGZuMDRZZUpLZ3VJR2oxcC9CQmh0UT0?api=1&uid=535939344" |
| notification_data = { |
| "message": "Первый запрос был выполнен", |
| "export_id": export_id |
| } |
| notification_response = send_notification(notification_url, notification_data) |
| print("Ответ от сервера оповещения:", notification_response) |
|
|
| code_executed = True |
| else: |
| raise Exception(f"Ошибка в ответе от сервера: {data.get('error_message') if data else 'Нет данных'}") |
| except Exception as e: |
| print(f"Ошибка: {e}") |
| else: |
| print("Системная переменная start_up не равна '1' или код уже выполнялся.") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
| @app.route('/export_user', methods=['GET']) |
| def export_user(): |
| try: |
| export_id = request.args.get('export_id') |
| if not export_id: |
| raise Exception("export_id не найден в параметрах запроса") |
|
|
| print(f"Получен export_id: {export_id}") |
|
|
| |
| third_url_template = f"{gc_url_export}/{export_id}?key={gc_api}" |
| response = fetch(third_url_template) |
| if response and response.get("success"): |
| print("Ответ сервера:") |
| print(response) |
| return jsonify(response), 200 |
| else: |
| raise Exception(f"Ошибка в ответе от сервера: {response.get('error_message') if response else 'Нет данных'}") |
| except Exception as e: |
| print(f"Ошибка: {e}") |
| return jsonify({"error": str(e)}), 500 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| def send_second_request(export_id): |
| if export_id is None: |
| raise Exception("export_id is None") |
| |
| export_url_template = f"https://school.riverpsy.com/pl/api/account/exports/{export_id}?key=jqgxSMUnHWoKUcxF3MHSb77VUMk7HpFbO9SHnfVYwHtwqe1S81lqeKxrLPoSPWCephtYQuJwMFsCXEFmyByXdruDpDFgf6L7ij66K9ji0Kf2qAIwbTqEyJGB5MOHwyHl" |
| try: |
| response = requests.get(export_url_template) |
| response.raise_for_status() |
| return response.json() |
| except requests.RequestException as e: |
| raise Exception(f"Ошибка при выполнении запроса: {e}") |
|
|
| def load_data_from_json(json_data): |
| if 'info' not in json_data or 'items' not in json_data['info'] or 'fields' not in json_data['info']: |
| raise ValueError("Invalid JSON structure") |
|
|
| items = json_data['info']['items'] |
| fields = json_data['info']['fields'] |
|
|
| db = 'data_gc.db' |
| conn = sqlite3.connect(db) |
| cursor = conn.cursor() |
|
|
| for item in items: |
| user_data = dict(zip(fields, item)) |
| |
| user_data.setdefault('vk_id', '') |
| user_data.setdefault('chat_id', '') |
| user_data.setdefault('ws_st', '') |
| user_data.setdefault('ws_stop', '') |
| user_data.setdefault('web_st', '') |
| user_data.setdefault('fin_prog', '') |
| user_data.setdefault('b_city', '') |
| user_data.setdefault('b_fin', '') |
| user_data.setdefault('b_ban', '') |
| user_data.setdefault('b_ign', '') |
| user_data.setdefault('b_baners', '') |
| user_data.setdefault('b_butt', '') |
| user_data.setdefault('b_mess', '') |
| user_data.setdefault('shop_st', '') |
| user_data.setdefault('curator', '') |
| user_data.setdefault('pr1', '') |
| user_data.setdefault('pr2', '') |
| user_data.setdefault('pr3', '') |
| user_data.setdefault('pr4', '') |
| user_data.setdefault('pr5', '') |
| user_data.setdefault('gc_url', '') |
| user_data.setdefault('key_pr', '') |
| user_data.setdefault('n_con', '') |
| user_data.setdefault('canal', '') |
| user_data.setdefault('data_t', '') |
| |
| if 'Телефон' in user_data and user_data['Телефон'].startswith('+'): |
| user_data['Телефон'] = user_data['Телефон'][1:] |
|
|
| query = ''' |
| 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, pr1, pr2, pr3, pr4, pr5, gc_url, curator, key_pr, n_con, canal, data_t |
| ) VALUES ( |
| :Имя, :Телефон, :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, :pr1, :pr2, :pr3, :pr4, :pr5, :gc_url, :curator, :key_pr, :n_con, :canal, :data_t |
| ) |
| ''' |
| cursor.execute(query, user_data) |
|
|
| conn.commit() |
| conn.close() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| @app.route('/start', methods=['GET']) |
| def start(): |
| export_id = request.args.get('export_id') |
| api_key_sys_control = request.args.get('api_sys') |
|
|
| if export_id is None: |
| return json.dumps({"error": "export_id is required"}), 400 |
|
|
| if api_key_sys_control != api_key_sys: |
| return json.dumps({"error": "Unauthorized access"}), 403 |
|
|
| try: |
| json_data = send_second_request(export_id) |
| load_data_from_json(json_data) |
| return "Data loaded successfully", 200 |
| except Exception as e: |
| return json.dumps({"error": str(e)}), 500 |
|
|
|
|
|
|
|
|
|
|
| |
| def randomize_message(template): |
| def replace_placeholder(match): |
| options = match.group(1).split('|') |
| return random.choice(options) |
|
|
| return re.sub(r'\{([^}]+)\}', replace_placeholder, template) |
|
|
| def clean_phone_number(phone): |
| if phone.startswith('+'): |
| return phone[1:] |
| return phone |
|
|
| def send_message(chat_id, message): |
| |
| |
| full_url = f"{wa_url}{wa_ak}{ws_url_mes}{wa_api_key}" |
| payload = { |
| "chatId": chat_id, |
| "message": message |
| } |
| headers = { |
| 'Content-Type': 'application/json' |
| } |
| response = requests.request("POST", full_url, headers=headers, json=payload) |
| try: |
| response_json = response.json() |
| except ValueError: |
| response_json = {"error": "Invalid JSON response"} |
| return response_json |
|
|
| def check_and_send_mailings(mesage_db1, clean_db): |
| try: |
| results = [] |
| for database in DATABASES: |
| conn = sqlite3.connect(database) |
| cursor = conn.cursor() |
| cursor.execute('SELECT name, phone FROM contacts') |
| contacts = cursor.fetchall() |
| conn.close() |
|
|
| for contact in contacts: |
| name = contact[0] |
| chat_id = f"{clean_phone_number(contact[1])}@c.us" |
| message = randomize_message(mesage_db1) |
| message = message.replace('[[nemes]]', name) |
| send_result = send_message(chat_id, message) |
| results.append({ |
| "chat_id": chat_id, |
| "message": message, |
| "result": send_result |
| }) |
|
|
| if clean_db == '1': |
| conn = sqlite3.connect(database) |
| cursor = conn.cursor() |
| cursor.execute('DELETE FROM contacts') |
| conn.commit() |
| conn.close() |
|
|
| return jsonify({"status": "success", "results": results}), 200 |
| except Exception as e: |
| print(f"Error sending mailings: {e}") |
| return jsonify({"status": "error", "message": str(e)}), 500 |
|
|
| |
| @app.route('/start_db', methods=['GET']) |
| def start_mailings(): |
| mesage_db1 = request.args.get('mesage') |
| clean_db = request.args.get('clean_db') |
| api_sys_control = request.args.get('api_sys') |
| |
|
|
| if not mesage_db1: |
| return "Parameter 'mesage' is required.", 400 |
|
|
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
|
|
| return check_and_send_mailings(mesage_db1, clean_db) |
|
|
|
|
|
|
|
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
| |
|
|
|
|
|
|
| @app.route('/data_gc') |
| def show_data_gc(): |
| try: |
| api_sys_control = request.args.get('api_sys') |
|
|
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
|
|
| conn = sqlite3.connect('data_gc.db') |
| cursor = conn.cursor() |
| cursor.execute('SELECT name, phone, email FROM contacts') |
| contacts = cursor.fetchall() |
| cursor.execute('SELECT COUNT(*) FROM contacts') |
| total_users = cursor.fetchone()[0] |
| conn.close() |
|
|
| return render_template('data_gc.html', contacts=contacts, total_users=total_users) |
| except Exception as e: |
| print(f"Error showing contacts: {e}") |
| return "Internal Server Error", 500 |
|
|
|
|
| @app.route('/data_ras') |
| def show_data_ras(): |
| try: |
| api_sys_control = request.args.get('api_sys') |
|
|
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
|
|
| conn = sqlite3.connect('data1.db') |
| cursor = conn.cursor() |
| cursor.execute('SELECT name, phone, email FROM contacts') |
| contacts = cursor.fetchall() |
| cursor.execute('SELECT COUNT(*) FROM contacts') |
| total_users = cursor.fetchone()[0] |
| conn.close() |
|
|
| return render_template('data_ras.html', contacts=contacts, total_users=total_users) |
| except Exception as e: |
| print(f"Error showing contacts: {e}") |
| return "Internal Server Error", 500 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| @app.route('/data_gc_tab', methods=['GET']) |
| def data_gc_tab(): |
| api_sys_control = request.args.get('api_sys') |
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
| return render_template('data_gc_tab.html') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| @app.route('/data_gc_tab_out', methods=['GET']) |
| def data_gc_tab_out(): |
| try: |
| api_sys_control = request.args.get('api_sys') |
|
|
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
|
|
| conn = sqlite3.connect('data_gc.db') |
| cursor = conn.cursor() |
| cursor.execute(''' |
| SELECT id, 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_on, data_t, utm_source, utm_medium, utm_campaign, utm_term, utm_content, gcpc |
| FROM contacts |
| ''') |
| contacts = cursor.fetchall() |
| conn.close() |
|
|
| contacts_json = [{ |
| 'id': contact[0], 'name': contact[1], 'phone': contact[2], 'email': contact[3], |
| 'vk_id': contact[4], 'chat_id': contact[5], 'ws_st': contact[6], 'ws_stop': contact[7], |
| 'web_st': contact[8], 'fin_prog': contact[9], 'b_city': contact[10], 'b_fin': contact[11], |
| 'b_ban': contact[12], 'b_ign': contact[13], 'b_baners': contact[14], 'b_butt': contact[15], |
| 'b_mess': contact[16], 'shop_st': contact[17], 'curator': contact[18], 'pr1': contact[19], |
| 'pr2': contact[20], 'pr3': contact[21], 'pr4': contact[22], 'pr5': contact[23], |
| 'gc_url': contact[24], 'key_pr': contact[25], 'n_con': contact[26], 'canal': contact[27],'data_on': contact[28], |
| 'data_t': contact[29],'utm_source': contact[30], 'utm_medium': contact[31], 'utm_campaign': contact[32], |
| 'utm_term': contact[33], 'utm_content': contact[34], 'gcpc': contact[34] |
| } for contact in contacts] |
| return jsonify(contacts_json), 200 |
| except Exception as e: |
| error_message = f"Error getting data from data_gc: {e}" |
| print(error_message) |
| return error_message, 500 |
|
|
|
|
|
|
| |
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| DATABASE_NAME = 'data_gc.db' |
|
|
|
|
|
|
|
|
|
|
|
|
| def update_or_insert_user(db_name, user_data, mapping_template): |
| conn = sqlite3.connect(db_name) |
| cursor = conn.cursor() |
|
|
| |
| email = user_data.get('email') |
| if not email: |
| logging.error(f"User data missing email: {user_data}") |
| return |
|
|
| logging.debug(f"Processing user with email: {email}") |
| |
| |
| cursor.execute("SELECT web_st FROM contacts WHERE email = ?", (email,)) |
| user = cursor.fetchone() |
| logging.debug(f"User found: {user}") |
|
|
| |
| web_st_value = 1 |
| if user: |
| |
| current_web_st = user[0] if user[0] is not None and user[0] != "" else 0 |
| web_st_value = int(current_web_st) + 1 |
| logging.debug(f"Calculated web_st_value: {web_st_value}") |
|
|
| |
| cursor.execute("UPDATE contacts SET web_st = ? WHERE email = ?", (web_st_value, email)) |
| conn.commit() |
| conn.close() |
| logging.debug(f"User {email} web_st updated to {web_st_value}") |
| else: |
| conn.close() |
| logging.debug(f"User {email} not found, proceeding with insert") |
|
|
| |
| conn = sqlite3.connect(db_name) |
| cursor = conn.cursor() |
|
|
| |
| transformed_data = {} |
| for json_key, db_column in mapping_template.items(): |
| value = user_data.get(json_key, "") |
| |
| if isinstance(value, list): |
| |
| if all(isinstance(item, str) for item in value): |
| transformed_data[db_column] = "; ".join(value) |
| else: |
| logging.error(f"Expected list of strings for key {json_key}, but got: {value}") |
| transformed_data[db_column] = "" |
| else: |
| transformed_data[db_column] = str(value) |
| logging.debug(f"Transformed data: {transformed_data}") |
|
|
| |
|
|
| required_fields = [ |
| "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_on", "data_t", 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'gcpc' |
| ] |
|
|
| for field in required_fields: |
| if field not in transformed_data: |
| transformed_data[field] = "" |
| logging.debug(f"Transformed data after adding required fields: {transformed_data}") |
|
|
| |
| if 'phone' in user_data: |
| phone = user_data['phone'] |
| if phone.startswith('+'): |
| phone = phone[1:] |
| transformed_data['phone'] = phone |
| logging.debug(f"Transformed data after phone processing: {transformed_data}") |
|
|
| |
| transformed_data['web_st'] = web_st_value |
|
|
| |
| if user: |
| update_query = "UPDATE contacts SET " |
| update_values = [] |
| for column, value in transformed_data.items(): |
| update_query += f"{column} = ?, " |
| update_values.append(value) |
| update_query = update_query.rstrip(", ") + " WHERE email = ?" |
| update_values.append(email) |
| logging.debug(f"Update query: {update_query} with values: {update_values}") |
| cursor.execute(update_query, update_values) |
| else: |
| columns = ', '.join(transformed_data.keys()) |
| placeholders = ', '.join('?' for _ in transformed_data) |
| insert_query = f"INSERT INTO contacts ({columns}) VALUES ({placeholders})" |
| insert_values = list(transformed_data.values()) |
| logging.debug(f"Insert query: {insert_query} with values: {insert_values}") |
| cursor.execute(insert_query, insert_values) |
|
|
| |
| conn.commit() |
| conn.close() |
| logging.debug(f"User with email {email} processed successfully") |
|
|
|
|
| @app.route('/send_request', methods=['POST']) |
| def send_request(): |
| token = request.form.get('token') |
| min_date = request.form.get('minDate') |
| type = request.form.get('type') |
| url = f'https://online.bizon365.ru/api/v1/webinars/reports/getlist?minDate={min_date}&type={type}' |
| |
| response = requests.get(url, headers={'X-Token': token}) |
| |
| if response.status_code == 200: |
| data = response.json() |
| webinar_ids = [item['webinarId'] for item in data['list']] |
| return jsonify(webinar_ids) |
| else: |
| return jsonify({'error': 'Failed to fetch data from the API'}), response.status_code |
|
|
|
|
|
|
|
|
| @app.route('/send_get_request', methods=['GET']) |
| def send_get_request(): |
| token = request.args.get('token') |
| webinarId = request.args.get('webinarId') |
| url = f'https://online.bizon365.ru/api/v1/webinars/reports/get?webinarId={webinarId}' |
|
|
| try: |
| response = requests.get(url, headers={'X-Token': token}) |
| response.raise_for_status() |
| data = response.json() |
|
|
| |
| if data is None or 'report' not in data: |
| return jsonify({'error': 'No report data found'}), 500 |
|
|
| report = data.get('report', {}) |
| messages = data.get('messages', {}) |
|
|
| |
| if report is None: |
| return jsonify({'error': 'No report data found in the response'}), 500 |
|
|
| report_json_str = report.get('report', '{}') |
| try: |
| report_json = json.loads(report_json_str) |
| except json.JSONDecodeError: |
| report_json = {} |
|
|
| messages_json_str = report.get('messages', '{}') |
| try: |
| messages_json = json.loads(messages_json_str) |
| except json.JSONDecodeError: |
| messages_json = {} |
|
|
| users_meta = report_json.get('usersMeta', {}) |
|
|
| processed_emails = set() |
| for user_id, user_data in users_meta.items(): |
| user_messages = messages_json.get(user_id, []) |
| user_data['messages'] = user_messages |
| email = user_data.get('email') |
| if email and email not in processed_emails: |
| update_or_insert_user(DATABASE_NAME, user_data, mapping_template) |
| processed_emails.add(email) |
|
|
| return jsonify({'status': 'User data saved successfully'}) |
| |
| except requests.exceptions.RequestException as e: |
| return jsonify({'error': f'API request failed: {str(e)}'}), 500 |
|
|
| api_bz = "SkrIONpr3ByeSIuEaBhr1bB8u4aBhSJfH8uEpB2rk7rI_ETrn" |
|
|
|
|
| @app.route('/webhookbz', methods=['POST']) |
| def webhookbz(): |
| api_sys_control = request.args.get('api_sys') |
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
|
|
| data = request.json |
| webinar_id = data.get('webinarId') |
|
|
| if not webinar_id: |
| return jsonify({'error': 'webinarId is required'}), 400 |
|
|
| url = f'https://online.bizon365.ru/api/v1/webinars/reports/get?webinarId={webinar_id}' |
| response = requests.get(url, headers={'X-Token': api_key_sys}) |
|
|
| if response.status_code == 200: |
| data = response.json() |
|
|
| report = data.get('report', {}) |
| messages = data.get('messages', {}) |
|
|
| report_json_str = report.get('report', '{}') |
| try: |
| report_json = json.loads(report_json_str) |
| except json.JSONDecodeError: |
| report_json = {} |
|
|
| messages_json_str = report.get('messages', '{}') |
| try: |
| messages_json = json.loads(messages_json_str) |
| except json.JSONDecodeError: |
| messages_json = {} |
|
|
| users_meta = report_json.get('usersMeta', {}) |
|
|
| processed_emails = set() |
| for user_id, user_data in users_meta.items(): |
| user_messages = messages_json.get(user_id, []) |
| user_data['messages'] = user_messages |
| email = user_data.get('email') |
| if email and email not in processed_emails: |
| update_or_insert_user(DATABASE_NAME, user_data, mapping_template) |
| processed_emails.add(email) |
|
|
| return jsonify({'status': 'User data saved successfully'}) |
| else: |
| return jsonify({'error': 'Failed to fetch data from the API'}), response.status_code |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| @app.route('/biz_v', methods=['GET']) |
| def biz_v(): |
| api_sys_control = request.args.get('api_sys') |
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
| return render_template('biz_v.html') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| @app.route('/ver', methods=['GET']) |
| def veref(): |
| api_sys_control = request.args.get('api_sys') |
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
| return render_template('ver.html') |
|
|
| @app.route('/se_mes', methods=['GET']) |
| def se_mes(): |
| api_sys_control = request.args.get('api_sys') |
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
| return render_template('se_mes.html') |
|
|
| @app.route('/se_mes_im', methods=['GET']) |
| def se_mes_im(): |
| api_sys_control = request.args.get('api_sys') |
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
| return render_template('se_mes_im.html') |
|
|
| @app.route('/se_mes_ran', methods=['GET']) |
| def se_mes_ran(): |
| api_sys_control = request.args.get('api_sys') |
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
| return render_template('se_mes_ran.html') |
|
|
| @app.route('/se_mes_im_ran', methods=['GET']) |
| def se_mes_im_ran(): |
| api_sys_control = request.args.get('api_sys') |
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
| return render_template('se_mes_im_ran.html') |
|
|
| @app.route('/se_mes_im2', methods=['GET']) |
| def se_mes_im2(): |
| api_sys_control = request.args.get('api_sys') |
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
| return render_template('se_mes_im2.html') |
|
|
| @app.route('/se_mes_f', methods=['GET']) |
| def se_mes_f(): |
| api_sys_control = request.args.get('api_sys') |
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
| return render_template('se_mes_f.html') |
|
|
| @app.route('/up_gr', methods=['GET']) |
| def up_gr(): |
| api_sys_control = request.args.get('api_sys') |
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
| return render_template('up_gr.html') |
|
|
| @app.route('/up_user_gp', methods=['GET']) |
| def up_user_gp(): |
| api_sys_control = request.args.get('api_sys') |
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
| return render_template('up_user_gp.html') |
|
|
| @app.route('/del_user_gp', methods=['GET']) |
| def del_user_gp(): |
| api_sys_control = request.args.get('api_sys') |
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
| return render_template('del_user_gp.html') |
|
|
| @app.route('/up_ad', methods=['GET']) |
| def up_ad(): |
| api_sys_control = request.args.get('api_sys') |
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
| return render_template('up_ad.html') |
|
|
| @app.route('/del_ad', methods=['GET']) |
| def del_ad(): |
| api_sys_control = request.args.get('api_sys') |
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
| return render_template('del_ad.html') |
|
|
| @app.route('/se_opr', methods=['GET']) |
| def se_opr(): |
| api_sys_control = request.args.get('api_sys') |
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
| return render_template('se_opr.html') |
|
|
| @app.route('/online', methods=['GET']) |
| def online(): |
| api_sys_control = request.args.get('api_sys') |
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
| return render_template('online.html') |
|
|
| @app.route('/se_mes_f_gc', methods=['GET']) |
| def se_mes_f_gc(): |
| api_sys_control = request.args.get('api_sys') |
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
| return render_template('se_mes_f_gc.html') |
|
|
|
|
|
|
|
|
| @app.route('/bas_vk_tab', methods=['GET']) |
| def bas_vk_otob(): |
| api_sys_control = request.args.get('api_sys') |
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
| return render_template('bas_vk_tab.html') |
|
|
|
|
|
|
| @app.route('/pages_gen', methods=['GET']) |
| def bas_pages_gen(): |
| api_sys_control = request.args.get('api_sys') |
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
| return render_template('pages_gen.html') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
| @app.route('/total_users', methods=['GET']) |
| def total_users(): |
| try: |
| api_sys_control = request.args.get('api_sys') |
|
|
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
|
|
| total_users_gc = 0 |
| total_users_ras = 0 |
|
|
| conn_gc = sqlite3.connect('data_gc.db') |
| cursor_gc = conn_gc.cursor() |
| cursor_gc.execute('SELECT COUNT(*) FROM contacts') |
| total_users_gc = cursor_gc.fetchone()[0] |
| conn_gc.close() |
|
|
| conn_ras = sqlite3.connect('data1.db') |
| cursor_ras = conn_ras.cursor() |
| cursor_ras.execute('SELECT COUNT(*) FROM contacts') |
| total_users_ras = cursor_ras.fetchone()[0] |
| conn_ras.close() |
|
|
| return jsonify({ |
| "total_users_gc": total_users_gc, |
| "total_users_ras": total_users_ras |
| }), 200 |
| except Exception as e: |
| print(f"Error getting total users: {e}") |
| return "Internal Server Error", 500 |
|
|
| |
| @app.route('/all_users_gc', methods=['GET']) |
| def all_users_gc(): |
| try: |
| api_sys_control = request.args.get('api_sys') |
|
|
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
|
|
| conn = sqlite3.connect('data_gc.db') |
| cursor = conn.cursor() |
| cursor.execute('SELECT name, phone, email FROM contacts') |
| contacts = cursor.fetchall() |
| conn.close() |
|
|
| return jsonify(contacts), 200 |
| except Exception as e: |
| print(f"Error getting all users from data_gc: {e}") |
| return "Internal Server Error", 500 |
|
|
| |
| @app.route('/all_users_ras', methods=['GET']) |
| def all_users_ras(): |
| try: |
| api_sys_control = request.args.get('api_sys') |
|
|
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
|
|
| conn = sqlite3.connect('data1.db') |
| cursor = conn.cursor() |
| cursor.execute('SELECT name, phone, email FROM contacts') |
| contacts = cursor.fetchall() |
| conn.close() |
|
|
| return jsonify(contacts), 200 |
| except Exception as e: |
| print(f"Error getting all users from data_ras: {e}") |
| return "Internal Server Error", 500 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
| |
| @app.route('/gc_db_no_email', methods=['GET']) |
| def gc_db_no_email(): |
| try: |
| api_sys_control = request.args.get('api_sys') |
|
|
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
|
|
| |
| name_d = request.args.get('name', '') |
| phone_d = request.args.get('phone', '') |
| pr1_d = request.args.get('pr1', '') |
| pr2_d = request.args.get('pr2', '') |
| pr3_d = request.args.get('pr3', '') |
|
|
| |
| conn = sqlite3.connect('data_gc.db') |
| cursor = conn.cursor() |
| cursor.execute('SELECT email FROM contacts WHERE phone = ?', (phone_d,)) |
| result = cursor.fetchone() |
| email_d = result[0] if result else '' |
| conn.close() |
|
|
| |
| json_data = { |
| "user": { |
| "email": email_d, |
| "phone": phone_d, |
| "first_name": name_d, |
| "addfields": { |
| "pr1": pr1_d, |
| "pr2": pr2_d, |
| "pr3": pr3_d |
| } |
| }, |
| "system": { |
| "refresh_if_exists": 1 |
| }, |
| "session": { |
| "utm_source": "", |
| "utm_medium": "", |
| "utm_content": "", |
| "utm_campaign": "", |
| "utm_group": "", |
| "gcpc": "", |
| "gcao": "", |
| "referer": "" |
| } |
| } |
|
|
| |
| json_str = json.dumps(json_data) |
| params_d = base64.b64encode(json_str.encode('utf-8')).decode('utf-8') |
|
|
| |
| data = { |
| 'key': gc_api, |
| 'action': 'add', |
| 'params': params_d |
| } |
|
|
| |
| response = requests.post(gc_url, data=data) |
|
|
| |
| return { |
| 'status_code': response.status_code, |
| 'response_body': response.text |
| } |
| except Exception as e: |
| print(f"Error in gc_db_no_email: {e}") |
| return "Internal Server Error", 500 |
|
|
| @app.route('/gc_db_email', methods=['GET']) |
| def gc_db_email(): |
| try: |
| api_sys_control = request.args.get('api_sys') |
|
|
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
|
|
| |
| name_d = request.args.get('name', '') |
| email_d = request.args.get('email', '') |
| phone_d = request.args.get('phone', '') |
| pr1_d = request.args.get('pr1', '') |
| pr2_d = request.args.get('pr2', '') |
| pr3_d = request.args.get('pr3', '') |
|
|
| |
| json_data = { |
| "user": { |
| "email": email_d, |
| "phone": phone_d, |
| "first_name": name_d, |
| "addfields": { |
| "pr1": pr1_d, |
| "pr2": pr2_d, |
| "pr3": pr3_d |
| } |
| }, |
| "system": { |
| "refresh_if_exists": 1 |
| }, |
| "session": { |
| "utm_source": "", |
| "utm_medium": "", |
| "utm_content": "", |
| "utm_campaign": "", |
| "utm_group": "", |
| "gcpc": "", |
| "gcao": "", |
| "referer": "" |
| } |
| } |
|
|
| |
| json_str = json.dumps(json_data) |
| params_d = base64.b64encode(json_str.encode('utf-8')).decode('utf-8') |
|
|
| |
| data = { |
| 'key': gc_api, |
| 'action': action_d, |
| 'params': params_d |
| } |
|
|
| |
| response = requests.post(gc_url, data=data) |
|
|
| |
| return { |
| 'status_code': response.status_code, |
| 'response_body': response.text |
| } |
| except Exception as e: |
| print(f"Error in gc_db_email: {e}") |
| return "Internal Server Error", 500 |
| |
| @app.route('/gc_forms', methods=['GET']) |
| def gc_forms(): |
| try: |
| api_sys_control = request.args.get('api_sys') |
|
|
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
|
|
| |
| name_d = request.args.get('name', '') |
| email_d = request.args.get('email', '') |
| phone_d = request.args.get('phone', '') |
| cod_pred = request.args.get('cod_pred', '') |
| price = request.args.get('price', '') |
| utm_source = request.args.get('utm_source', '') |
| utm_medium = request.args.get('utm_medium', '') |
| utm_content = request.args.get('utm_content', '') |
| utm_campaign = request.args.get('utm_campaign', '') |
| utm_group = request.args.get('utm_group', '') |
| gcpc = request.args.get('gcpc', '') |
| |
| json_data = { |
| "user": { |
| "email": email_d, |
| "phone": phone_d, |
| "first_name": name_d, |
| "addfields": { |
| "pr1": "", |
| "pr2": "", |
| "pr3": "" |
| } |
| }, |
| "system": { |
| "refresh_if_exists": 1 |
| }, |
| "session": { |
| "utm_source": utm_source, |
| "utm_medium": utm_medium, |
| "utm_content": utm_content, |
| "utm_campaign": utm_campaign, |
| "utm_group": utm_group, |
| "gcpc": gcpc, |
| "gcao": "", |
| "referer": "" |
| }, |
| "deal":{ |
| "offer_code": cod_pred, |
| "deal_cost": price |
| } |
| } |
|
|
| |
| json_str = json.dumps(json_data) |
| params_d = base64.b64encode(json_str.encode('utf-8')).decode('utf-8') |
|
|
| |
| data = { |
| 'key': gc_api, |
| 'action': action_d, |
| 'params': params_d |
| } |
|
|
| |
| response = requests.post(gc_url_form, data=data) |
|
|
| |
| return { |
| 'status_code': response.status_code, |
| 'response_body': response.text |
| } |
| except Exception as e: |
| print(f"Error in gc_db_email: {e}") |
| return "Internal Server Error", 500 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
| curators = ["Anna", "Ekaterina", "Ivan", "Maria", "Sergey", "Olga", "Alex", "Natalia", "Dmitry", "Elena"] |
|
|
|
|
| mt_avp = { |
| 'name': 'name', |
| 'phone': 'phone', |
| 'email': 'email', |
| 'shop_st': 'shop_st' |
| } |
|
|
| mt_bhelp = { |
| 'name': 'name', |
| 'phone': 'phone', |
| 'email': 'email', |
| 'ad': 'ws_stop' |
| } |
|
|
| mt_gc = { |
| 'name': 'name', |
| 'phone': 'phone', |
| 'email': 'email', |
| 'ad': 'web_st' |
| } |
|
|
| mt_tl = { |
| 'name': 'name', |
| 'phone': 'phone', |
| 'email': 'email', |
| 'ad': 'ad_url' |
| } |
|
|
| mapp_templates = { |
| 'avp': mt_avp, |
| 'bhelp': mt_bhelp, |
| 'gc': mt_gc, |
| 'ad': mt_tl |
| } |
|
|
| DATABASE_NAME3 = 'data_gc.db' |
|
|
| def add_or_update_contact(contact_data): |
| conn = sqlite3.connect(DATABASE_NAME3) |
| cursor = conn.cursor() |
|
|
| email = contact_data.get('email') |
| if not email: |
| logging.error(f"Missing email in contact data: {contact_data}") |
| return |
|
|
| utc_now = datetime.utcnow() |
| msk_tz = pytz.timezone('Europe/Moscow') |
| msk_now = utc_now.replace(tzinfo=pytz.utc).astimezone(msk_tz) |
| contact_data['data_t'] = msk_now.strftime('%Y-%m-%d %H:%M:%S') |
|
|
| |
| fields = [ |
| '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_on', 'data_t', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'gcpc' |
| ] |
|
|
| |
| for field in fields: |
| if field not in contact_data: |
| contact_data[field] = '' |
|
|
| cursor.execute("SELECT id FROM contacts WHERE email = ?", (email,)) |
| contact = cursor.fetchone() |
|
|
| if contact: |
| update_fields = [f"{field} = ?" for field in fields if contact_data[field] != ''] |
| update_values = [contact_data[field] for field in fields if contact_data[field] != ''] |
| update_values.append(contact[0]) |
| update_query = f"UPDATE contacts SET {', '.join(update_fields)} WHERE id = ?" |
| cursor.execute(update_query, update_values) |
| else: |
| insert_query = f"INSERT INTO contacts ({', '.join(fields)}) VALUES ({', '.join(['?' for _ in fields])})" |
| cursor.execute(insert_query, tuple(contact_data[field] for field in fields)) |
|
|
| conn.commit() |
| conn.close() |
|
|
| @app.route('/add_data_ver_cur', methods=['GET']) |
| def add_data_ver_cur(): |
| global current_curator_index |
|
|
| veref_on_off = request.args.get('ver', '0') |
| curator_on_off = request.args.get('cur', '0') |
| |
| template_key = request.args.get('template_key', 'avp') |
| mapping_template_cur = mapp_templates.get(template_key, mt_avp) |
|
|
| user_data = {mapping_template_cur[key]: request.args.get(key, "") for key in mapping_template_cur} |
|
|
| logging.debug(f"Received data: {user_data}") |
|
|
| if curator_on_off == "1": |
| user_data['curator'] = curators[current_curator_index] |
|
|
| if veref_on_off == "true": |
| phone_number = user_data.get('phone', '') |
| if not phone_number: |
| logging.error("Phone number is empty") |
| return jsonify({'status': 'error', 'message': 'Phone number is empty'}), 400 |
| |
| phone_verification_response = verify_phone_number(phone_number) |
| if phone_verification_response is not None: |
| user_data['ws_st'] = phone_verification_response |
|
|
| try: |
| add_or_update_contact(user_data) |
| if curator_on_off == "1": |
| current_curator_index = (current_curator_index + 1) % len(curators) |
| |
| return jsonify({'status': 'success', 'message': f'User added with curator {user_data.get("curator", "not assigned")}'}) |
| except Exception as e: |
| logging.error(f"Error adding user: {e}") |
| return jsonify({'status': 'error', 'message': str(e)}), 500 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| def clean_phone_number_ss(phone_number): |
| return re.sub(r'\D', '', phone_number) |
|
|
| DATABASE2 = 'data_gc.db' |
|
|
| def verify_phone_number(phone_number): |
| full_url_ver = f"{wa_url}{wa_ak}{ws_url_ver}{wa_api_key}" |
| payload = {"phoneNumber": phone_number} |
| headers = {'Content-Type': 'application/json'} |
| response = requests.post(full_url_ver, headers=headers, json=payload) |
| if response.status_code == 200: |
| response_body = response.json() |
| return response_body.get('existsWhatsapp', 'false') |
| else: |
| return "false" |
|
|
| def parse_csv_data(data): |
| parsed_data = [] |
| for item in data: |
| for key, value in item.items(): |
| headers = key.split(';') |
| row = value.split(';') |
| parsed_data.append(dict(zip(headers, row))) |
| return parsed_data |
|
|
| def insert_data(data, verify_phone, add_curator): |
| global current_curator_index |
| with sqlite3.connect(DATABASE2) as conn: |
| cursor = conn.cursor() |
|
|
| for row in data: |
| name = row.get('Name', '') |
| phone = row.get('Phone', '').lstrip('+') |
| email = row.get('Email', '') |
| data_t = row.get('Date', '').strip('"') |
|
|
| cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone)) |
| user_exists = cursor.fetchone() |
|
|
| if user_exists: |
| print(f"User with email {email} or phone {phone} already exists. Skipping insert.") |
| continue |
|
|
| if add_curator == "1": |
| curator = curators[current_curator_index] |
| current_curator_index = (current_curator_index + 1) % len(curators) |
| else: |
| curator = row.get('curator', '') |
|
|
| if verify_phone == "1": |
| ws_st = verify_phone_number(phone) |
| else: |
| ws_st = row.get('ws_st', '') |
|
|
| columns = ['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_on', 'data_t', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'gcpc'] |
| values = [name, phone, email, row.get('vk_id', ''), row.get('chat_id', ''), ws_st, row.get('ws_stop', ''), row.get('web_st', 0), row.get('fin_prog', 0), row.get('b_city', ''), row.get('b_fin', ''), row.get('b_ban', ''), row.get('b_ign', ''), row.get('b_baners', ''), row.get('b_butt', ''), row.get('b_mess', ''), row.get('shop_st', ''), curator, row.get('pr1', ''), row.get('pr2', ''), row.get('pr3', ''), row.get('pr4', ''), row.get('pr5', ''), row.get('gc_url', ''), row.get('key_pr', ''), row.get('n_con', ''), row.get('canal', ''), row.get('data_on', ''), row.get('data_t', ''), row.get('utm_source', ''), row.get('utm_medium', ''), row.get('utm_campaign', ''), row.get('utm_term', ''), row.get('utm_content', ''), row.get('gcpc', '')] |
|
|
| placeholders = ', '.join(['?' for _ in columns]) |
| columns_str = ', '.join(columns) |
|
|
| query = f''' |
| INSERT INTO contacts ({columns_str}) |
| VALUES ({placeholders}) |
| ''' |
|
|
| try: |
| cursor.execute(query, values) |
| except Exception as e: |
| print(f"Error inserting row: {row}") |
| print(f"Error message: {str(e)}") |
| conn.rollback() |
| raise |
|
|
| conn.commit() |
|
|
| @app.route('/upload_csv', methods=['POST']) |
| def upload_csv(): |
| if 'file' not in request.files: |
| return jsonify({"error": "No file part"}), 400 |
| file = request.files['file'] |
| if file.filename == '': |
| return jsonify({"error": "No selected file"}), 400 |
| if file and file.filename.endswith('.csv'): |
| stream = io.StringIO(file.stream.read().decode("UTF8"), newline=None) |
| csv_input = csv.DictReader(stream) |
| data = [row for row in csv_input] |
| parsed_data = parse_csv_data(data) |
| verify_phone = request.form.get('verify_phone', '0') |
| add_curator = request.form.get('add_curator', '0') |
| print(f"Verify Phone: {verify_phone}") |
| print(f"Add Curator: {add_curator}") |
| insert_data(parsed_data, verify_phone, add_curator) |
| return jsonify({"message": "Data uploaded and inserted successfully"}) |
| return jsonify({"error": "Invalid file format"}), 400 |
|
|
| @app.route('/upl_csv', methods=['GET']) |
| def se_upl_csv(): |
| api_sys_control = request.args.get('api_sys') |
| if api_sys_control != api_key_sys: |
| return "EUR 22", 200 |
| return render_template('upl_csv.html') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| DATABASE = 'data_gc.db' |
|
|
| |
| def clean_phone_number_j(phone_number): |
| return re.sub(r'\D', '', phone_number) |
|
|
| |
| def insert_data_j(data): |
| conn = sqlite3.connect(DATABASE) |
| cursor = conn.cursor() |
|
|
| for row in data: |
| name = row.get('name', '') |
| phone = row.get('phone', '').lstrip('+') |
| email = row.get('email', '') |
| data_t = row.get('data_t', '').strip('"') |
|
|
| |
| phone = clean_phone_number_j(phone) |
|
|
| cursor.execute("SELECT 1 FROM contacts WHERE email = ? OR phone = ?", (email, phone)) |
| user_exists = cursor.fetchone() |
|
|
| if user_exists: |
| print(f"User with email {email} or phone {phone} already exists. Skipping insert.") |
| continue |
|
|
| columns = ['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_on', 'data_t', 'utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'gcpc'] |
| values = [name, phone, email, row.get('vk_id', ''), row.get('chat_id', ''), row.get('ws_st', ''), row.get('ws_stop', ''), row.get('web_st', 0), row.get('fin_prog', 0), row.get('b_city', ''), row.get('b_fin', ''), row.get('b_ban', ''), row.get('b_ign', ''), row.get('b_baners', ''), row.get('b_butt', ''), row.get('b_mess', ''), row.get('shop_st', ''), row.get('curator', ''), row.get('pr1', ''), row.get('pr2', ''), row.get('pr3', ''), row.get('pr4', ''), row.get('pr5', ''), row.get('gc_url', ''), row.get('key_pr', ''), row.get('n_con', ''), row.get('canal', ''), row.get('data_on', ''), row.get('data_t', ''), row.get('utm_source', ''), row.get('utm_medium', ''), row.get('utm_campaign', ''), row.get('utm_term', ''), row.get('utm_content', ''), row.get('gcpc', '')] |
|
|
| placeholders = ', '.join(['?' for _ in columns]) |
| columns_str = ', '.join(columns) |
|
|
| query = f''' |
| INSERT INTO contacts ({columns_str}) |
| VALUES ({placeholders}) |
| ''' |
|
|
| try: |
| cursor.execute(query, values) |
| except Exception as e: |
| print(f"Error inserting row: {row}") |
| print(f"Error message: {str(e)}") |
| conn.rollback() |
| continue |
|
|
| conn.commit() |
| conn.close() |
|
|
| |
| @app.route('/upload_json', methods=['POST']) |
| def upload_json(): |
| if 'file' not in request.files: |
| return jsonify({"error": "No file part"}), 400 |
| file = request.files['file'] |
| if file.filename == '': |
| return jsonify({"error": "No selected file"}), 400 |
| if file and file.filename.endswith('.json'): |
| data = json.load(file) |
| insert_data_j(data) |
| return jsonify({"message": "Data uploaded and inserted successfully"}) |
| return jsonify({"error": "Invalid file format"}), 400 |
|
|
| |
| @app.route('/upl_json', methods=['GET']) |
| def display_form(): |
| return render_template('upload_json.html') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| DATABASE6 = 'data_gc.db' |
|
|
|
|
|
|
| |
| def clean_phone_number_ss(phone_number): |
| return re.sub(r'\D', '', phone_number) |
|
|
| |
| @app.route('/order', methods=['GET']) |
| def from_shop_st(): |
| try: |
| api_sys_control = request.args.get('api_sys') |
|
|
| if api_sys_control != api_key_sys: |
| return json.dumps({"error": "Unauthorized access"}), 403 |
|
|
| name = request.args.get('name', '') |
| email = request.args.get('email', '') |
| phone = request.args.get('phone', '').lstrip('+') |
| order = request.args.get('order', '') |
| status = request.args.get('status', '') |
| del_flag = request.args.get('del', '') |
| n_con_flag = request.args.get('n_con', '') |
|
|
| if not email or not phone: |
| return json.dumps({"error": "Email and phone are required"}), 400 |
|
|
| |
| phone = clean_phone_number_ss(phone) |
|
|
| conn = sqlite3.connect(DATABASE6) |
| cursor = conn.cursor() |
|
|
| cursor.execute("SELECT * FROM contacts WHERE email = ? OR phone = ?", (email, phone)) |
| result = cursor.fetchone() |
|
|
| if result: |
| shop_st = result[17] if result[17] else '{}' |
| shop_st_data = json.loads(shop_st) |
| else: |
| shop_st_data = {} |
|
|
| if del_flag == '1': |
| shop_st_data = {} |
| elif order and status: |
| shop_st_data[order] = status |
|
|
| shop_st_json = json.dumps(shop_st_data) |
|
|
| if result: |
| |
| current_data = { |
| 'name': result[1], |
| 'phone': result[2], |
| 'email': result[3], |
| 'shop_st': shop_st_json |
| } |
|
|
| |
| if name: |
| current_data['name'] = name |
| if phone: |
| current_data['phone'] = phone |
| if email: |
| current_data['email'] = email |
|
|
| cursor.execute(""" |
| UPDATE contacts |
| SET name = ?, phone = ?, email = ?, shop_st = ? |
| WHERE email = ? OR phone = ? |
| """, (current_data['name'], current_data['phone'], current_data['email'], current_data['shop_st'], email, phone)) |
| else: |
| cursor.execute(""" |
| INSERT INTO contacts (name, phone, email, shop_st) |
| VALUES (?, ?, ?, ?) |
| """, (name, phone, email, shop_st_json)) |
|
|
| conn.commit() |
| conn.close() |
|
|
| return json.dumps(shop_st_data), 200 |
|
|
| except Exception as e: |
| return json.dumps({"error": str(e)}), 500 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| |
| @app.route('/order2', methods=['GET']) |
| def from_shop_st2(): |
| try: |
| api_sys_control = request.args.get('api_sys') |
|
|
| if api_sys_control != api_key_sys: |
| return json.dumps({"error": "Unauthorized access"}), 403 |
|
|
| name = request.args.get('name', '') |
| email = request.args.get('email', '') |
| phone = request.args.get('phone', '').lstrip('+') |
| order = request.args.get('order', '') |
| status = request.args.get('status', '') |
| del_flag = request.args.get('del', '') |
| n_con_flag = request.args.get('n_con', '') |
|
|
| if not email or not phone: |
| return json.dumps({"error": "Email and phone are required"}), 400 |
|
|
| |
| phone = clean_phone_number_ss(phone) |
|
|
| conn = sqlite3.connect(DATABASE6) |
| cursor = conn.cursor() |
|
|
| cursor.execute("SELECT * FROM contacts WHERE email = ? OR phone = ?", (email, phone)) |
| result = cursor.fetchone() |
|
|
| if result: |
| shop_st = result[17] if result[17] else '{}' |
| shop_st_data = json.loads(shop_st) |
| else: |
| shop_st_data = {} |
|
|
| if del_flag == '1': |
| shop_st_data = {} |
| elif order and status: |
| shop_st_data[order] = status |
|
|
| shop_st_json = json.dumps(shop_st_data) |
|
|
| |
| columns_to_update = ['name', 'phone', 'email', 'shop_st', 'n_con'] |
| values_to_update = [name, phone, email, shop_st_json, n_con_flag] |
|
|
| if result: |
| |
| set_clause = ', '.join([f"{col} = ?" for col in columns_to_update]) |
| query = f"UPDATE contacts SET {set_clause} WHERE email = ? OR phone = ?" |
| cursor.execute(query, values_to_update + [email, phone]) |
| else: |
| |
| query = f"INSERT INTO contacts ({', '.join(columns_to_update)}) VALUES ({', '.join(['?' for _ in columns_to_update])})" |
| cursor.execute(query, values_to_update) |
|
|
| conn.commit() |
| conn.close() |
|
|
| return json.dumps(shop_st_data), 200 |
|
|
| except Exception as e: |
| return json.dumps({"error": str(e)}), 500 |
|
|
|
|
|
|
|
|
|
|
| |
| @app.route('/order3', methods=['GET']) |
| def from_shop_st3(): |
| try: |
| api_sys_control = request.args.get('api_sys') |
|
|
| if api_sys_control != api_key_sys: |
| return json.dumps({"error": "Unauthorized access"}), 403 |
|
|
| name = request.args.get('name', '') |
| email = request.args.get('email', '') |
| phone = request.args.get('phone', '').lstrip('+') |
| order = request.args.get('order', '') |
| status = request.args.get('status', '') |
| del_flag = request.args.get('del', '') |
| n_con_flag = request.args.get('n_con', '') |
|
|
| if not email or not phone: |
| return json.dumps({"error": "Email and phone are required"}), 400 |
|
|
| |
| phone = clean_phone_number_ss(phone) |
|
|
| conn = sqlite3.connect(DATABASE6) |
| cursor = conn.cursor() |
|
|
| cursor.execute("SELECT * FROM contacts WHERE email = ? OR phone = ?", (email, phone)) |
| result = cursor.fetchone() |
|
|
| if result: |
| shop_st = result[17] if result[17] else '{}' |
| shop_st_data = json.loads(shop_st) |
| else: |
| shop_st_data = {} |
|
|
| if del_flag == '1': |
| shop_st_data = {} |
| elif order and status: |
| shop_st_data[order] = status |
|
|
| shop_st_json = json.dumps(shop_st_data) |
|
|
| |
| columns_to_update = ['name', 'phone', 'email', 'shop_st', 'n_con'] |
| values_to_update = [name, phone, email, shop_st_json, n_con_flag] |
|
|
| if result: |
| |
| set_clause = ', '.join([f"{col} = ?" for col in columns_to_update]) |
| query = f"UPDATE contacts SET {set_clause} WHERE email = ? OR phone = ?" |
| cursor.execute(query, values_to_update + [email, phone]) |
| else: |
| |
| query = f"INSERT INTO contacts ({', '.join(columns_to_update)}) VALUES ({', '.join(['?' for _ in columns_to_update])})" |
| cursor.execute(query, values_to_update) |
|
|
| conn.commit() |
|
|
| |
| replace_null_with_empty_string(conn) |
|
|
| conn.close() |
|
|
| return json.dumps(shop_st_data), 200 |
|
|
| except Exception as e: |
| return json.dumps({"error": str(e)}), 500 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| @app.route('/gc_in', methods=['GET']) |
| def add_data_gc_in(): |
| global current_curator_index |
|
|
| veref_on_off = request.args.get('ver', '0') |
| curator_on_off = request.args.get('cur', '0') |
| |
| template_key = request.args.get('template_key', 'avp') |
| mapping_template_cur = mapp_templates.get(template_key, mt_avp) |
|
|
| user_data = {mapping_template_cur[key]: request.args.get(key, "") for key in mapping_template_cur} |
|
|
| if curator_on_off == "1": |
| user_data['curator'] = curators[current_curator_index] |
|
|
| if veref_on_off == "1": |
| phone_verification_response = verify_phone_number(user_data.get('phone', '')) |
| if phone_verification_response is not None: |
| user_data['ws_st'] = phone_verification_response |
|
|
| try: |
| add_or_update_contact(user_data) |
| if curator_on_off == "1": |
| current_curator_index = (current_curator_index + 1) % len(curators) |
| return jsonify({'status': 'success', 'message': f'User added with curator {user_data.get("curator", "not assigned")}'}) |
| except Exception as e: |
| logging.error(f"Error adding user: {e}") |
| return jsonify({'status': 'error', 'message': str(e)}), 500 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| @app.route('/tl_help.js') |
| def serve_vk_bridge(): |
| script_content = """ |
| function mySuccessFunction(form) { |
| if (!form) return; |
| if (form instanceof jQuery) { |
| form = form.get(0); |
| } |
| |
| var obj = {}; |
| var inputs = form.elements; |
| Array.prototype.forEach.call(inputs, function(input) { |
| if (input.type === 'radio') { |
| if (input.checked) obj[input.name] = input.value; |
| } else { |
| obj[input.name] = input.value; |
| } |
| }); |
| |
| var email = obj["Email"] || ""; |
| var phone = obj["Phone"] || ""; |
| var name = obj["Name"] || ""; |
| console.log("name:", name); |
| console.log("email:", email); |
| console.log("phone:", phone); |
| |
| var urlParams = new URLSearchParams(window.location.search); |
| var utm_source = urlParams.get('utm_source') || "0"; |
| var utm_medium = urlParams.get('utm_medium') || "0"; |
| var utm_campaign = urlParams.get('utm_campaign') || "0"; |
| var utm_content = urlParams.get('utm_content') || "0"; |
| var utm_term = urlParams.get('utm_term') || "0"; |
| var gcpc = urlParams.get('gcpc') || "0"; |
| var redirectUrl; |
| if (form.id === formId1) { |
| redirectUrl = new URL(redirectUrl1); |
| } else if (form.id === formId2) { |
| redirectUrl = new URL(redirectUrl2); |
| } else if (form.id === formId3) { |
| redirectUrl = new URL(redirectUrl3); |
| } else { |
| console.error('Неизвестный ID формы:', form.id); |
| return; |
| } |
| var queryString = '?ups=' + encodeURIComponent(ups); |
| queryString += '&name=' + encodeURIComponent(name); |
| queryString += '&email=' + encodeURIComponent(email); |
| queryString += '&phone=' + encodeURIComponent(phone); |
| queryString += '&utm_source=' + encodeURIComponent(utm_source); |
| queryString += '&utm_medium=' + encodeURIComponent(utm_medium); |
| queryString += '&utm_campaign=' + encodeURIComponent(utm_campaign); |
| queryString += '&utm_content=' + encodeURIComponent(utm_content); |
| queryString += '&utm_term=' + encodeURIComponent(utm_term); |
| queryString += '&gcpc=' + encodeURIComponent(gcpc); |
| console.log('Сформированный URL:', redirectUrl.toString() + queryString); |
| window.open(redirectUrl.toString() + queryString, '_blank'); |
| } |
| if (document.readyState !== 'loading') { |
| us_sendFormAfterSuccess(); |
| } else { |
| document.addEventListener('DOMContentLoaded', us_sendFormAfterSuccess); |
| } |
| function us_sendFormAfterSuccess() { |
| var forms = document.querySelectorAll('.js-form-proccess'); |
| Array.prototype.forEach.call(forms, function(form) { |
| form.addEventListener('tildaform:aftersuccess', function(e) { |
| e.preventDefault(); |
| mySuccessFunction(form); |
| }); |
| }); |
| } |
| """ |
| return Response(script_content, mimetype='application/javascript') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| initialize_requests() |
|
|
|
|
|
|
| if __name__ == '__main__': |
| |
| app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 7860))) |