Spaces:
Sleeping
Sleeping
DmitrMakeev
commited on
Commit
•
54b5657
1
Parent(s):
bdbb93a
Update google_db.py
Browse files- google_db.py +45 -15
google_db.py
CHANGED
@@ -1,23 +1,29 @@
|
|
1 |
from flask import request, jsonify
|
2 |
-
import globs
|
3 |
import os
|
4 |
import asyncio
|
5 |
import aiohttp
|
6 |
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
9 |
|
10 |
-
async def
|
|
|
|
|
|
|
|
|
11 |
async with aiohttp.ClientSession() as session:
|
12 |
-
async with session.post(
|
13 |
-
|
14 |
-
return response.status == 200
|
15 |
|
16 |
-
def
|
17 |
err = 0
|
18 |
|
19 |
-
if
|
20 |
-
|
|
|
21 |
emails = globs.email
|
22 |
phones = globs.phone
|
23 |
pr1s = globs.pr1
|
@@ -25,19 +31,43 @@ def save_db():
|
|
25 |
pr3s = globs.pr3
|
26 |
grups_id = globs.grup_id
|
27 |
|
28 |
-
|
29 |
-
data =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
# Запускаем асинхронный запрос в отдельном потоке
|
32 |
loop = asyncio.new_event_loop()
|
33 |
asyncio.set_event_loop(loop)
|
34 |
-
result = loop.run_until_complete(
|
35 |
loop.close()
|
36 |
|
37 |
-
err = 1 if result else 0
|
38 |
|
39 |
response_data = {
|
40 |
-
"
|
41 |
"err": err
|
42 |
}
|
43 |
|
|
|
1 |
from flask import request, jsonify
|
|
|
2 |
import os
|
3 |
import asyncio
|
4 |
import aiohttp
|
5 |
|
6 |
+
api_key = os.getenv('api_key')
|
7 |
+
base_url = os.getenv('base_url')
|
8 |
+
token = os.getenv('token')
|
9 |
+
list_id = os.getenv('list_id')
|
10 |
+
parameter_id = os.getenv('parameter_id')
|
11 |
|
12 |
+
async def async_add_user(data):
|
13 |
+
headers = {
|
14 |
+
'Authorization': f'Bearer {token}',
|
15 |
+
'Content-Type': 'application/json'
|
16 |
+
}
|
17 |
async with aiohttp.ClientSession() as session:
|
18 |
+
async with session.post(f"{base_url}lists/{list_id}/recipients", json=data, headers=headers) as response:
|
19 |
+
return response.status
|
|
|
20 |
|
21 |
+
def add_user():
|
22 |
err = 0
|
23 |
|
24 |
+
if api_key == request.args.get('api'):
|
25 |
+
# Получаем данные из глобальных переменных
|
26 |
+
names = globs.name
|
27 |
emails = globs.email
|
28 |
phones = globs.phone
|
29 |
pr1s = globs.pr1
|
|
|
31 |
pr3s = globs.pr3
|
32 |
grups_id = globs.grup_id
|
33 |
|
34 |
+
# Создаем данные для отправки
|
35 |
+
data = {
|
36 |
+
'email': emails,
|
37 |
+
'values': [
|
38 |
+
{
|
39 |
+
'value': names,
|
40 |
+
'parameter_id': parameter_id
|
41 |
+
},
|
42 |
+
{
|
43 |
+
'value': phones,
|
44 |
+
'parameter_id': parameter_id + 1 # Предполагаем, что есть уникальный ID для каждого параметра
|
45 |
+
},
|
46 |
+
{
|
47 |
+
'value': pr1s,
|
48 |
+
'parameter_id': parameter_id + 2
|
49 |
+
},
|
50 |
+
{
|
51 |
+
'value': pr2s,
|
52 |
+
'parameter_id': parameter_id + 3
|
53 |
+
},
|
54 |
+
{
|
55 |
+
'value': pr3s,
|
56 |
+
'parameter_id': parameter_id + 4
|
57 |
+
}
|
58 |
+
]
|
59 |
+
}
|
60 |
|
61 |
# Запускаем асинхронный запрос в отдельном потоке
|
62 |
loop = asyncio.new_event_loop()
|
63 |
asyncio.set_event_loop(loop)
|
64 |
+
result = loop.run_until_complete(async_add_user(data))
|
65 |
loop.close()
|
66 |
|
67 |
+
err = 1 if result == 200 else 0
|
68 |
|
69 |
response_data = {
|
70 |
+
"status": "success" if err == 1 else "error",
|
71 |
"err": err
|
72 |
}
|
73 |
|