DmitrMakeev commited on
Commit
0d47cfb
·
verified ·
1 Parent(s): 5a1ff71

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +196 -47
app.py CHANGED
@@ -1,5 +1,6 @@
1
  from flask import Flask, request, render_template, jsonify
2
  import requests
 
3
  import os
4
 
5
 
@@ -131,63 +132,211 @@ def save_db():
131
 
132
 
133
 
 
 
 
 
 
134
  @app.route('/save_pl', methods=['POST'])
135
  def save_pl():
136
- show_params = False
137
  list_id1 = '560768'
138
-
139
- # Получение данных из JSON тела запроса
140
  data = request.json
141
  name = data.get('name')
142
  email = data.get('email')
143
  phone = data.get('phone')
144
 
145
- # Подготовка данных для отправки
146
- data = {
147
- "email": email,
148
- "unconfirmed": False,
149
- "values": [
150
- {
151
- "parameter_id": 393120,
152
- "kind": "string",
153
- "list_id": list_id1,
154
- "title": "phone",
155
- "value": phone
156
- },
157
- {
158
- "parameter_id": 393119,
159
- "kind": "string",
160
- "list_id": list_id1,
161
- "title": "name",
162
- "value": name
163
- }
 
 
 
 
 
 
 
 
 
164
  ]
165
- }
166
-
167
- headers = {
168
- 'Authorization': f'Bearer {token}',
169
- 'Content-Type': 'application/json'
170
- }
171
-
172
- # Отправка данных на указанный URL
173
- response = requests.post(f"{base_url}/email/lists/{list_id1}/recipients", json=data, headers=headers)
174
-
175
- # Обработка ответа
176
- if show_params:
177
- system_vars = {
178
- 'base_url': base_url,
179
- 'token': token
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
180
  }
181
- return jsonify({'system_variables': system_vars, 'request_parameters': request.json})
182
- else:
183
- return response.text
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
 
192
 
193
 
 
1
  from flask import Flask, request, render_template, jsonify
2
  import requests
3
+ import datetime
4
  import os
5
 
6
 
 
132
 
133
 
134
 
135
+
136
+
137
+
138
+ # Здесь должно быть определение переменных base_url и token для Notisend API
139
+
140
  @app.route('/save_pl', methods=['POST'])
141
  def save_pl():
 
142
  list_id1 = '560768'
 
 
143
  data = request.json
144
  name = data.get('name')
145
  email = data.get('email')
146
  phone = data.get('phone')
147
 
148
+ # Определяем текущий день недели (0 - понедельник, 1 - вторник, ..., 6 - воскресенье)
149
+ current_day = datetime.datetime.today().weekday()
150
+
151
+ urls = []
152
+
153
+ # Понедельник, пятница, суббота, воскресенье
154
+ if current_day in [0, 4, 5, 6]:
155
+ urls = [
156
+ (f"{base_url}/email/lists/{list_id1}/recipients_mon_fri_sat_sun", {
157
+ "email": email,
158
+ "unconfirmed": True,
159
+ "values": [
160
+ {
161
+ "parameter_id": 393120,
162
+ "kind": "string",
163
+ "list_id": list_id1,
164
+ "title": "phone",
165
+ "value": phone
166
+ },
167
+ {
168
+ "parameter_id": 393119,
169
+ "kind": "string",
170
+ "list_id": list_id1,
171
+ "title": "name",
172
+ "value": name
173
+ }
174
+ ]
175
+ })
176
  ]
177
+ # Вторник
178
+ elif current_day == 1:
179
+ current_time = datetime.datetime.now().time()
180
+ if current_time < datetime.time(19, 0): # Утро до 19:00
181
+ urls = [
182
+ (f"{base_url}/email/lists/{list_id1}/recipients_tue_morning", {
183
+ "email": email,
184
+ "unconfirmed": True,
185
+ "values": [
186
+ {
187
+ "parameter_id": 393120,
188
+ "kind": "string",
189
+ "list_id": list_id1,
190
+ "title": "phone",
191
+ "value": phone
192
+ },
193
+ {
194
+ "parameter_id": 393119,
195
+ "kind": "string",
196
+ "list_id": list_id1,
197
+ "title": "name",
198
+ "value": name
199
+ }
200
+ ]
201
+ })
202
+ ]
203
+ else: # Вечер после 19:00
204
+ urls = [
205
+ (f"{base_url}/email/lists/{list_id1}/recipients_tue_evening", {
206
+ "email": email,
207
+ "unconfirmed": False,
208
+ "values": [
209
+ {
210
+ "parameter_id": 393120,
211
+ "kind": "string",
212
+ "list_id": list_id1,
213
+ "title": "phone",
214
+ "value": phone
215
+ },
216
+ {
217
+ "parameter_id": 393119,
218
+ "kind": "string",
219
+ "list_id": list_id1,
220
+ "title": "name",
221
+ "value": name
222
+ }
223
+ ]
224
+ })
225
+ ]
226
+ # Среда
227
+ elif current_day == 2:
228
+ current_time = datetime.datetime.now().time()
229
+ if current_time < datetime.time(19, 0): # Утро до 19:00
230
+ urls = [
231
+ (f"{base_url}/email/lists/{list_id1}/recipients_wed_morning", {
232
+ "email": email,
233
+ "unconfirmed": True,
234
+ "values": [
235
+ {
236
+ "parameter_id": 393120,
237
+ "kind": "string",
238
+ "list_id": list_id1,
239
+ "title": "phone",
240
+ "value": phone
241
+ },
242
+ {
243
+ "parameter_id": 393119,
244
+ "kind": "string",
245
+ "list_id": list_id1,
246
+ "title": "name",
247
+ "value": name
248
+ }
249
+ ]
250
+ })
251
+ ]
252
+ else: # Вечер после 19:00
253
+ urls = [
254
+ (f"{base_url}/email/lists/{list_id1}/recipients_wed_evening", {
255
+ "email": email,
256
+ "unconfirmed": False,
257
+ "values": [
258
+ {
259
+ "parameter_id": 393120,
260
+ "kind": "string",
261
+ "list_id": list_id1,
262
+ "title": "phone",
263
+ "value": phone
264
+ },
265
+ {
266
+ "parameter_id": 393119,
267
+ "kind": "string",
268
+ "list_id": list_id1,
269
+ "title": "name",
270
+ "value": name
271
+ }
272
+ ]
273
+ })
274
+ ]
275
+ # Четверг
276
+ elif current_day == 3:
277
+ current_time = datetime.datetime.now().time()
278
+ if current_time < datetime.time(19, 0): # Утро до 19:00
279
+ urls = [
280
+ (f"{base_url}/email/lists/{list_id1}/recipients_thu_morning", {
281
+ "email": email,
282
+ "unconfirmed": True,
283
+ "values": [
284
+ {
285
+ "parameter_id": 393120,
286
+ "kind": "string",
287
+ "list_id": list_id1,
288
+ "title": "phone",
289
+ "value": phone
290
+ },
291
+ {
292
+ "parameter_id": 393119,
293
+ "kind": "string",
294
+ "list_id": list_id1,
295
+ "title": "name",
296
+ "value": name
297
+ }
298
+ ]
299
+ })
300
+ ]
301
+ else: # Вечер после 19:00
302
+ urls = [
303
+ (f"{base_url}/email/lists/{list_id1}/recipients_thu_evening", {
304
+ "email": email,
305
+ "unconfirmed": False,
306
+ "values": [
307
+ {
308
+ "parameter_id": 393120,
309
+ "kind": "string",
310
+ "list_id": list_id1,
311
+ "title": "phone",
312
+ "value": phone
313
+ },
314
+ {
315
+ "parameter_id": 393119,
316
+ "kind": "string",
317
+ "list_id": list_id1,
318
+ "title": "name",
319
+ "value": name
320
+ }
321
+ ]
322
+ })
323
+ ]
324
+
325
+ # Отправляем запросы
326
+ responses = []
327
+ for url, json_data in urls:
328
+ headers = {
329
+ 'Authorization': f'Bearer {token}',
330
+ 'Content-Type': 'application/json'
331
  }
332
+ response = requests.post(url, json=json_data, headers=headers)
333
+ responses.append({
334
+ 'url': url,
335
+ 'status_code': response.status_code,
336
+ 'response_text': response.text
337
+ })
338
+
339
+ return jsonify({'responses': responses})
 
 
340
 
341
 
342