DmitrMakeev
commited on
Commit
•
e3d9e7e
1
Parent(s):
9f74cf3
Update app.py
Browse files
app.py
CHANGED
@@ -11,15 +11,43 @@ import base64
|
|
11 |
from unidecode import unidecode
|
12 |
|
13 |
api_key_sys = os.getenv('api_key_sys')
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
18 |
id_gru = os.getenv('id_gru')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
wa_url = os.getenv('wa_url')
|
20 |
wa_api_key = os.getenv('wa_api_key')
|
|
|
|
|
21 |
start_up = os.getenv('start_up')
|
|
|
22 |
export_id = ""
|
|
|
|
|
|
|
23 |
json_data = '''
|
24 |
|
25 |
|
@@ -27,6 +55,9 @@ json_data = '''
|
|
27 |
|
28 |
'''
|
29 |
|
|
|
|
|
|
|
30 |
app = Flask(__name__, template_folder="./")
|
31 |
app.config['DEBUG'] = True
|
32 |
UPLOAD_FOLDER = 'static'
|
@@ -67,49 +98,34 @@ def init_db(db_name):
|
|
67 |
for db in DATABASES:
|
68 |
init_db(db)
|
69 |
|
70 |
-
def load_data_from_json():
|
71 |
-
data = json.loads(json_data)
|
72 |
-
items = data['info']['items']
|
73 |
-
fields = data['info']['fields']
|
74 |
|
75 |
-
db = 'data_gc.db' # Указываем конкретную базу данных
|
76 |
-
conn = sqlite3.connect(db)
|
77 |
-
cursor = conn.cursor()
|
78 |
|
79 |
-
for item in items:
|
80 |
-
user_data = dict(zip(fields, item))
|
81 |
-
user_data.setdefault('vk_id', '')
|
82 |
-
user_data.setdefault('chat_id', '')
|
83 |
-
user_data.setdefault('ws_st', '')
|
84 |
-
user_data.setdefault('ws_stop', '')
|
85 |
-
user_data.setdefault('web_st', '')
|
86 |
-
user_data.setdefault('fin_prog', '')
|
87 |
-
user_data.setdefault('shop_st', '')
|
88 |
-
user_data.setdefault('curator', '')
|
89 |
-
user_data.setdefault('pr1', '')
|
90 |
-
user_data.setdefault('pr2', '')
|
91 |
-
user_data.setdefault('pr3', '')
|
92 |
-
user_data.setdefault('pr4', '')
|
93 |
-
user_data.setdefault('pr5', '')
|
94 |
-
user_data.setdefault('ad_url', '')
|
95 |
|
96 |
-
if 'Телефон' in user_data and user_data['Телефон'].startswith('+'):
|
97 |
-
user_data['Телефон'] = user_data['Телефон'][1:]
|
98 |
|
99 |
-
query = '''
|
100 |
-
INSERT INTO contacts (
|
101 |
-
name, phone, email, vk_id, chat_id, ws_statys, ws_stop, web_statys, fin_progress, shop_statys_full, pr1, pr2, pr3, pr4, pr5, ad_url, curator
|
102 |
-
) VALUES (
|
103 |
-
:Имя, :Телефон, :Email, :vk_id, :chat_id, :ws_st, :ws_stop, :web_st, :fin_prog, :shop_st, :pr1, :pr2, :pr3, :pr4, :pr5, :ad_url, :curator
|
104 |
-
)
|
105 |
-
'''
|
106 |
-
cursor.execute(query, user_data)
|
107 |
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
# Если start_up равна '1', выполним запросы
|
112 |
if start_up == '1':
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
# Формирование URL для первого запроса
|
114 |
url_template = f"{gc_url_gru}/{id_gru}/users?key={gc_api}&created_at[from]={date_from}&status={status}"
|
115 |
|
@@ -124,7 +140,7 @@ if start_up == '1':
|
|
124 |
export_id = data.get("info", {}).get("export_id", "")
|
125 |
print("Export ID:", export_id)
|
126 |
|
127 |
-
# Задержка в
|
128 |
time_delay = 1 * 60
|
129 |
time.sleep(time_delay)
|
130 |
|
@@ -147,9 +163,6 @@ if start_up == '1':
|
|
147 |
else:
|
148 |
print("Ошибка при выполнении первого запроса")
|
149 |
print("Статус код:", response.status_code)
|
150 |
-
|
151 |
-
# Загрузка данных из JSON в базу данных
|
152 |
-
load_data_from_json()
|
153 |
else:
|
154 |
print("Системная переменная start_up не равна '1', код не выполняется при старте сервера.")
|
155 |
|
@@ -172,6 +185,71 @@ else:
|
|
172 |
|
173 |
|
174 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
|
176 |
|
177 |
def randomize_message(template):
|
|
|
11 |
from unidecode import unidecode
|
12 |
|
13 |
api_key_sys = os.getenv('api_key_sys')
|
14 |
+
|
15 |
+
|
16 |
+
gc_api = os.getenv('gc_api') #Good
|
17 |
+
gc_url = os.getenv('gc_url_use')
|
18 |
+
|
19 |
+
|
20 |
+
gc_url_gru = os.getenv('gc_url_gru') #Good
|
21 |
+
gc_url_export = os.getenv('gc_url_export')
|
22 |
+
|
23 |
id_gru = os.getenv('id_gru')
|
24 |
+
# Дата
|
25 |
+
date_from = "2022-01-01"
|
26 |
+
|
27 |
+
# Статус
|
28 |
+
status = "active"
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
|
33 |
+
|
34 |
+
|
35 |
+
|
36 |
+
|
37 |
+
|
38 |
+
|
39 |
+
|
40 |
+
|
41 |
wa_url = os.getenv('wa_url')
|
42 |
wa_api_key = os.getenv('wa_api_key')
|
43 |
+
|
44 |
+
|
45 |
start_up = os.getenv('start_up')
|
46 |
+
|
47 |
export_id = ""
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
json_data = '''
|
52 |
|
53 |
|
|
|
55 |
|
56 |
'''
|
57 |
|
58 |
+
|
59 |
+
|
60 |
+
|
61 |
app = Flask(__name__, template_folder="./")
|
62 |
app.config['DEBUG'] = True
|
63 |
UPLOAD_FOLDER = 'static'
|
|
|
98 |
for db in DATABASES:
|
99 |
init_db(db)
|
100 |
|
|
|
|
|
|
|
|
|
101 |
|
|
|
|
|
|
|
102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
103 |
|
|
|
|
|
104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
+
|
107 |
+
|
108 |
+
|
109 |
+
|
110 |
+
|
111 |
+
|
112 |
+
|
113 |
+
|
114 |
+
|
115 |
+
|
116 |
+
|
117 |
+
|
118 |
|
119 |
# Если start_up равна '1', выполним запросы
|
120 |
if start_up == '1':
|
121 |
+
# Ваши API-ключи и другие переменные
|
122 |
+
gc_url_gru = os.getenv('gc_url_gru')
|
123 |
+
gc_api = os.getenv('gc_api')
|
124 |
+
gc_url_export = os.getenv('gc_url_export')
|
125 |
+
id_gru = "2993783"
|
126 |
+
date_from = "2022-01-01"
|
127 |
+
status = "active"
|
128 |
+
|
129 |
# Формирование URL для первого запроса
|
130 |
url_template = f"{gc_url_gru}/{id_gru}/users?key={gc_api}&created_at[from]={date_from}&status={status}"
|
131 |
|
|
|
140 |
export_id = data.get("info", {}).get("export_id", "")
|
141 |
print("Export ID:", export_id)
|
142 |
|
143 |
+
# Задержка в 5 минут перед вторым запросом (в миллисекундах)
|
144 |
time_delay = 1 * 60
|
145 |
time.sleep(time_delay)
|
146 |
|
|
|
163 |
else:
|
164 |
print("Ошибка при выполнении первого запроса")
|
165 |
print("Статус код:", response.status_code)
|
|
|
|
|
|
|
166 |
else:
|
167 |
print("Системная переменная start_up не равна '1', код не выполняется при старте сервера.")
|
168 |
|
|
|
185 |
|
186 |
|
187 |
|
188 |
+
|
189 |
+
|
190 |
+
|
191 |
+
def load_data_from_json():
|
192 |
+
data = json.loads(json_data)
|
193 |
+
items = data['info']['items']
|
194 |
+
fields = data['info']['fields']
|
195 |
+
|
196 |
+
db = 'data_gc.db' # Указываем конкретную базу данных
|
197 |
+
conn = sqlite3.connect(db)
|
198 |
+
cursor = conn.cursor()
|
199 |
+
|
200 |
+
for item in items:
|
201 |
+
user_data = dict(zip(fields, item))
|
202 |
+
# Проверяем наличие значений для полей и устанавливаем значения по умолчанию, если они отсутствуют
|
203 |
+
user_data.setdefault('vk_id', '')
|
204 |
+
user_data.setdefault('chat_id', '')
|
205 |
+
user_data.setdefault('ws_st', '')
|
206 |
+
user_data.setdefault('ws_stop', '')
|
207 |
+
user_data.setdefault('web_st', '')
|
208 |
+
user_data.setdefault('fin_prog', '')
|
209 |
+
user_data.setdefault('shop_st', '')
|
210 |
+
user_data.setdefault('curator', '')
|
211 |
+
user_data.setdefault('pr1', '')
|
212 |
+
user_data.setdefault('pr2', '')
|
213 |
+
user_data.setdefault('pr3', '')
|
214 |
+
user_data.setdefault('pr4', '')
|
215 |
+
user_data.setdefault('pr5', '')
|
216 |
+
user_data.setdefault('ad_url', '')
|
217 |
+
|
218 |
+
# Убираем плюс в начале телефона, если он присутствует
|
219 |
+
if 'Телефон' in user_data and user_data['Телефон'].startswith('+'):
|
220 |
+
user_data['Телефон'] = user_data['Телефон'][1:]
|
221 |
+
|
222 |
+
query = '''
|
223 |
+
INSERT INTO contacts (
|
224 |
+
name, phone, email, vk_id, chat_id, ws_statys, ws_stop, web_statys, fin_progress, shop_statys_full, pr1, pr2, pr3, pr4, pr5, ad_url, curator
|
225 |
+
) VALUES (
|
226 |
+
:Имя, :Телефон, :Email, :vk_id, :chat_id, :ws_st, :ws_stop, :web_st, :fin_prog, :shop_st, :pr1, :pr2, :pr3, :pr4, :pr5, :ad_url, :curator
|
227 |
+
)
|
228 |
+
'''
|
229 |
+
cursor.execute(query, user_data)
|
230 |
+
|
231 |
+
conn.commit()
|
232 |
+
conn.close()
|
233 |
+
|
234 |
+
@app.before_first_request
|
235 |
+
def before_first_request():
|
236 |
+
load_data_from_json()
|
237 |
+
|
238 |
+
|
239 |
+
|
240 |
+
|
241 |
+
|
242 |
+
|
243 |
+
|
244 |
+
|
245 |
+
|
246 |
+
|
247 |
+
|
248 |
+
|
249 |
+
|
250 |
+
|
251 |
+
|
252 |
+
|
253 |
|
254 |
|
255 |
def randomize_message(template):
|