ezKit 1.7.8__py3-none-any.whl → 1.8.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ezKit/bottle.py +7 -2
- ezKit/bottle_extensions.py +14 -18
- ezKit/database.py +104 -71
- ezKit/http.py +21 -22
- ezKit/mongo.py +3 -3
- ezKit/qywx.py +48 -34
- ezKit/redis.py +5 -4
- ezKit/sendemail.py +0 -7
- ezKit/utils.py +2 -2
- {ezKit-1.7.8.dist-info → ezKit-1.8.1.dist-info}/METADATA +2 -2
- ezKit-1.8.1.dist-info/RECORD +18 -0
- ezKit/files.py +0 -348
- ezKit/plots.py +0 -155
- ezKit/reports.py +0 -274
- ezKit/zabbix.py +0 -737
- ezKit-1.7.8.dist-info/RECORD +0 -22
- {ezKit-1.7.8.dist-info → ezKit-1.8.1.dist-info}/LICENSE +0 -0
- {ezKit-1.7.8.dist-info → ezKit-1.8.1.dist-info}/WHEEL +0 -0
- {ezKit-1.7.8.dist-info → ezKit-1.8.1.dist-info}/top_level.txt +0 -0
ezKit/bottle.py
CHANGED
@@ -17,7 +17,7 @@ from __future__ import print_function
|
|
17
17
|
import sys
|
18
18
|
|
19
19
|
__author__ = 'Marcel Hellkamp'
|
20
|
-
__version__ = '0.13.
|
20
|
+
__version__ = '0.13.2'
|
21
21
|
__license__ = 'MIT'
|
22
22
|
|
23
23
|
###############################################################################
|
@@ -185,6 +185,7 @@ def depr(major, minor, cause, fix, stacklevel=3):
|
|
185
185
|
if DEBUG == 'strict':
|
186
186
|
raise DeprecationWarning(text)
|
187
187
|
warnings.warn(text, DeprecationWarning, stacklevel=stacklevel)
|
188
|
+
return DeprecationWarning(text)
|
188
189
|
|
189
190
|
|
190
191
|
def makelist(data): # This is just too handy
|
@@ -4671,5 +4672,9 @@ def _main(argv): # pragma: no coverage
|
|
4671
4672
|
config=config)
|
4672
4673
|
|
4673
4674
|
|
4674
|
-
|
4675
|
+
def main():
|
4675
4676
|
_main(sys.argv)
|
4677
|
+
|
4678
|
+
|
4679
|
+
if __name__ == '__main__': # pragma: no coverage
|
4680
|
+
main()
|
ezKit/bottle_extensions.py
CHANGED
@@ -1,36 +1,32 @@
|
|
1
|
+
"""Bottle Extensions"""
|
1
2
|
from typing import Callable
|
2
3
|
|
3
4
|
from . import bottle, utils
|
4
5
|
|
5
6
|
|
6
7
|
def enable_cors(fn: Callable) -> Callable:
|
7
|
-
"""
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
- https://
|
13
|
-
- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers
|
14
|
-
- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods
|
15
|
-
- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
|
16
|
-
"""
|
8
|
+
"""Bottle CORS"""
|
9
|
+
# 参考文档:
|
10
|
+
# - https://stackoverflow.com/a/17262900
|
11
|
+
# - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Headers
|
12
|
+
# - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Methods
|
13
|
+
# - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
|
17
14
|
def cors(*args, **kwargs):
|
18
15
|
bottle.response.headers['Access-Control-Allow-Headers'] = '*'
|
19
16
|
bottle.response.headers['Access-Control-Allow-Methods'] = '*'
|
20
17
|
bottle.response.headers['Access-Control-Allow-Origin'] = '*'
|
21
18
|
if bottle.request.method != 'OPTIONS':
|
22
19
|
return fn(*args, **kwargs)
|
20
|
+
return None
|
23
21
|
return cors
|
24
22
|
|
25
|
-
def request_json() ->
|
26
|
-
"""
|
27
|
-
Bottle Request JSON
|
28
|
-
"""
|
23
|
+
def request_json() -> bottle.DictProperty | None:
|
24
|
+
"""Bottle Request JSON"""
|
29
25
|
try:
|
30
|
-
data
|
26
|
+
data = bottle.request.json
|
31
27
|
if utils.v_true(data, dict):
|
32
28
|
return data
|
33
|
-
|
34
|
-
|
35
|
-
|
29
|
+
return None
|
30
|
+
except Exception as e:
|
31
|
+
print(e)
|
36
32
|
return None
|
ezKit/database.py
CHANGED
@@ -1,15 +1,16 @@
|
|
1
|
-
|
1
|
+
"""
|
2
2
|
Column, Table, MetaData API
|
3
3
|
https://docs.sqlalchemy.org/en/14/core/metadata.html#column-table-metadata-api
|
4
4
|
CursorResult
|
5
5
|
https://docs.sqlalchemy.org/en/20/core/connections.html#sqlalchemy.engine.CursorResult
|
6
6
|
PostgreSQL 14 Data Types
|
7
7
|
https://www.postgresql.org/docs/14/datatype.html
|
8
|
-
|
8
|
+
"""
|
9
9
|
import csv
|
10
|
+
from typing import Any
|
10
11
|
|
11
12
|
from loguru import logger
|
12
|
-
from sqlalchemy import Index, create_engine, text
|
13
|
+
from sqlalchemy import CursorResult, Index, create_engine, text
|
13
14
|
|
14
15
|
from . import utils
|
15
16
|
|
@@ -20,152 +21,184 @@ class Database():
|
|
20
21
|
engine = create_engine('sqlite://')
|
21
22
|
|
22
23
|
def __init__(self, engine_url, **engine_options):
|
23
|
-
|
24
|
-
if engine_url is not None:
|
24
|
+
"""Initiation"""
|
25
|
+
if engine_url is not None and utils.v_true(engine_url, str):
|
25
26
|
if utils.v_true(engine_options, dict):
|
26
27
|
self.engine = create_engine(engine_url, **engine_options)
|
27
28
|
else:
|
28
29
|
self.engine = create_engine(engine_url)
|
30
|
+
else:
|
31
|
+
pass
|
29
32
|
|
30
33
|
def initializer(self):
|
31
|
-
|
34
|
+
"""ensure the parent proc's database connections are not touched in the new connection pool"""
|
32
35
|
self.engine.dispose(close=False)
|
33
36
|
|
34
|
-
def connect_test(self):
|
35
|
-
info =
|
37
|
+
def connect_test(self) -> bool:
|
38
|
+
info = "Database connect test"
|
36
39
|
try:
|
37
|
-
logger.info(f
|
40
|
+
logger.info(f"{info} ......")
|
38
41
|
self.engine.connect()
|
39
|
-
logger.success(f
|
42
|
+
logger.success(f"{info} [success]")
|
40
43
|
return True
|
41
44
|
except Exception as e:
|
42
|
-
logger.error(f
|
45
|
+
logger.error(f"{info} [failure]")
|
43
46
|
logger.exception(e)
|
44
47
|
return False
|
45
48
|
|
46
|
-
def metadata_init(self, base, **kwargs):
|
49
|
+
def metadata_init(self, base, **kwargs) -> bool:
|
47
50
|
# https://stackoverflow.com/questions/19175311/how-to-create-only-one-table-with-sqlalchemy
|
48
|
-
info =
|
51
|
+
info = "Database init table"
|
49
52
|
try:
|
50
|
-
logger.info(f
|
53
|
+
logger.info(f"{info} ......")
|
51
54
|
base.metadata.drop_all(self.engine, **kwargs)
|
52
55
|
base.metadata.create_all(self.engine, **kwargs)
|
53
|
-
logger.success(f
|
56
|
+
logger.success(f"{info} [success]")
|
54
57
|
return True
|
55
58
|
except Exception as e:
|
56
|
-
logger.error(f
|
59
|
+
logger.error(f"{info} [failure]")
|
57
60
|
logger.exception(e)
|
58
61
|
return False
|
59
62
|
|
60
|
-
def create_index(self, index_name, table_field):
|
63
|
+
def create_index(self, index_name, table_field) -> bool:
|
61
64
|
# 创建索引
|
62
65
|
# https://stackoverflow.com/a/41254430
|
63
66
|
# 示例:
|
64
67
|
# index_name: a_share_list_code_idx1
|
65
68
|
# table_field: Table_a_share_list.code
|
66
|
-
info =
|
69
|
+
info = "Database create index"
|
67
70
|
try:
|
68
|
-
logger.info(f
|
71
|
+
logger.info(f"{info} ......")
|
69
72
|
idx = Index(index_name, table_field)
|
70
73
|
try:
|
71
74
|
idx.drop(bind=self.engine)
|
72
75
|
except Exception as e:
|
73
76
|
logger.exception(e)
|
74
77
|
idx.create(bind=self.engine)
|
75
|
-
logger.success(f'{info}[
|
78
|
+
logger.success(f'{info} [success]')
|
76
79
|
return True
|
77
80
|
except Exception as e:
|
78
|
-
logger.error(f'{info}[
|
81
|
+
logger.error(f'{info} [failure]')
|
79
82
|
logger.error(e)
|
80
83
|
return False
|
81
84
|
|
82
85
|
# 私有函数, 保存 execute 的结果到 CSV 文件
|
83
|
-
def _result_save(self, file, data
|
86
|
+
def _result_save(self, file, data) -> bool:
|
84
87
|
try:
|
85
88
|
outcsv = csv.writer(file)
|
86
89
|
outcsv.writerow(data.keys())
|
87
90
|
outcsv.writerows(data)
|
88
91
|
return True
|
89
92
|
except Exception as e:
|
90
|
-
|
91
|
-
logger.exception(e)
|
93
|
+
logger.exception(e)
|
92
94
|
return False
|
93
95
|
|
94
|
-
def execute(
|
95
|
-
|
96
|
+
def execute(
|
97
|
+
self,
|
98
|
+
sql: str | None = None,
|
99
|
+
sql_file: str | None = None,
|
100
|
+
sql_file_kwargs: dict | None = None,
|
101
|
+
csv_file: str | None = None,
|
102
|
+
csv_file_kwargs: dict | None = None
|
103
|
+
) -> CursorResult[Any] | bool:
|
104
|
+
"""
|
96
105
|
echo 是否打印日志
|
97
106
|
某些情况下只需要结果, 不需要日志, 将 echo 设置为 False 即可
|
98
|
-
|
107
|
+
"""
|
99
108
|
|
100
|
-
info_prefix = '[
|
109
|
+
# info_prefix = '[Execute SQL]'
|
101
110
|
|
102
111
|
# ------------------------------------------------------------
|
103
112
|
|
104
113
|
# 提取 SQL
|
105
114
|
# 如果 sql 和 sql_file 同时存在, 优先执行 sql
|
106
115
|
sql_object = None
|
107
|
-
info =
|
116
|
+
info: str = "Extract SQL"
|
108
117
|
try:
|
109
|
-
|
110
|
-
|
118
|
+
|
119
|
+
logger.info(f"{info} ......")
|
120
|
+
|
111
121
|
if utils.v_true(sql, str):
|
122
|
+
|
112
123
|
sql_object = sql
|
113
|
-
|
124
|
+
|
125
|
+
elif sql_file is not None and utils.v_true(sql_file, str):
|
126
|
+
|
114
127
|
# 判断文件是否存在
|
115
|
-
if utils.check_file_type(sql_file,
|
116
|
-
|
128
|
+
if isinstance(sql_file, str) and utils.check_file_type(sql_file, "file") is False:
|
129
|
+
|
130
|
+
logger.error(f"No such file: {sql_file}")
|
117
131
|
return False
|
118
|
-
|
119
|
-
if utils.v_true(
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
132
|
+
|
133
|
+
if isinstance(sql_file, str) and utils.v_true(sql_file, str):
|
134
|
+
|
135
|
+
# 读取文件内容
|
136
|
+
if sql_file_kwargs is not None and utils.v_true(sql_file_kwargs, dict):
|
137
|
+
with open(sql_file, "r", encoding="utf-8", **sql_file_kwargs) as _file:
|
138
|
+
sql_object = _file.read()
|
139
|
+
else:
|
140
|
+
with open(sql_file, "r", encoding="utf-8") as _file:
|
141
|
+
sql_object = _file.read()
|
142
|
+
|
125
143
|
else:
|
126
|
-
|
127
|
-
logger.error(
|
144
|
+
|
145
|
+
logger.error("SQL or SQL file error")
|
146
|
+
logger.error(f"{info} [failure]")
|
128
147
|
return False
|
129
|
-
|
148
|
+
|
149
|
+
logger.success(f'{info} [success]')
|
150
|
+
|
130
151
|
except Exception as e:
|
131
|
-
|
132
|
-
logger.
|
152
|
+
|
153
|
+
logger.error(f"{info} [failure]")
|
154
|
+
logger.exception(e)
|
133
155
|
return False
|
134
156
|
|
135
157
|
# ------------------------------------------------------------
|
136
158
|
|
137
159
|
# 执行 SQL
|
138
|
-
info =
|
160
|
+
info: str = "Execute SQL"
|
139
161
|
try:
|
140
|
-
|
162
|
+
|
163
|
+
logger.info(f"{info} ......")
|
164
|
+
|
141
165
|
with self.engine.connect() as connect:
|
166
|
+
|
142
167
|
# 执行SQL
|
168
|
+
if sql_object is None:
|
169
|
+
return False
|
170
|
+
|
143
171
|
result = connect.execute(text(sql_object))
|
144
|
-
|
172
|
+
|
173
|
+
if csv_file is None:
|
145
174
|
# 如果 csv_file 没有定义, 则直接返回结果
|
146
|
-
logger.success(f'{info}[
|
175
|
+
logger.success(f'{info} [success]')
|
147
176
|
return result
|
177
|
+
|
178
|
+
# 如果 csv_file 有定义, 则保存结果到 csv_file
|
179
|
+
info_of_save = f"Save result to file: {csv_file}"
|
180
|
+
logger.info(f"{info_of_save} .......")
|
181
|
+
|
182
|
+
# 保存结果
|
183
|
+
if isinstance(csv_file_kwargs, dict) and utils.v_true(csv_file_kwargs, dict):
|
184
|
+
with open(csv_file, "w", encoding="utf-8", **csv_file_kwargs) as _file:
|
185
|
+
result_of_save = self._result_save(_file, result)
|
148
186
|
else:
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
logger.success(f'{info}[成功]') if echo == True else next
|
163
|
-
return True
|
164
|
-
else:
|
165
|
-
logger.error(f'{info_of_save} [失败]') if echo == True else next
|
166
|
-
logger.error(f'{info}[失败]') if echo == True else next
|
167
|
-
return False
|
187
|
+
with open(csv_file, "w", encoding="utf-8") as _file:
|
188
|
+
result_of_save = self._result_save(_file, result)
|
189
|
+
|
190
|
+
# 检查保存结果
|
191
|
+
if result_of_save is True:
|
192
|
+
logger.success(f'{info_of_save} [success]')
|
193
|
+
logger.success(f'{info} [success]')
|
194
|
+
return True
|
195
|
+
|
196
|
+
logger.error(f"{info_of_save} [failure]")
|
197
|
+
logger.error(f"{info} [failure]")
|
198
|
+
return False
|
199
|
+
|
168
200
|
except Exception as e:
|
169
|
-
|
170
|
-
logger.
|
201
|
+
|
202
|
+
logger.error(f'{info} [failure]')
|
203
|
+
logger.exception(e)
|
171
204
|
return False
|
ezKit/http.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
import json
|
2
|
+
from typing import Any
|
2
3
|
|
3
4
|
import requests
|
4
5
|
from loguru import logger
|
@@ -11,61 +12,59 @@ def download(
|
|
11
12
|
file: dict,
|
12
13
|
chunks: bool = False,
|
13
14
|
iter_content: dict | None = None,
|
14
|
-
|
15
|
-
info: str = None,
|
16
|
-
debug: bool = False
|
15
|
+
info: str | None = None
|
17
16
|
) -> bool:
|
18
|
-
"下载文件"
|
17
|
+
"""下载文件"""
|
19
18
|
|
20
19
|
if utils.v_true(request, dict):
|
21
|
-
request_arguments = {
|
20
|
+
request_arguments = {"method": "GET", "stream": True, **request}
|
22
21
|
else:
|
23
22
|
return False
|
24
23
|
|
25
24
|
if utils.v_true(file, dict):
|
26
|
-
file_arguments = {
|
25
|
+
file_arguments = {"mode": "wb", **file}
|
27
26
|
else:
|
28
27
|
return False
|
29
28
|
|
30
|
-
if utils.v_true(iter_content, dict):
|
31
|
-
iter_content_arguments = {
|
29
|
+
if iter_content is not None and utils.v_true(iter_content, dict):
|
30
|
+
iter_content_arguments = {"chunk_size": 1024, **iter_content}
|
32
31
|
else:
|
33
|
-
iter_content_arguments = {
|
32
|
+
iter_content_arguments = {"chunk_size": 1024}
|
34
33
|
|
35
|
-
|
34
|
+
info_prefix: str = "Download"
|
35
|
+
if utils.v_true(info, str):
|
36
|
+
info_prefix = f"Download {info}"
|
36
37
|
|
37
38
|
try:
|
38
39
|
|
39
|
-
logger.info(f'{
|
40
|
+
logger.info(f'{info_prefix} ......')
|
40
41
|
|
41
42
|
response = requests.request(**request_arguments)
|
42
43
|
|
43
|
-
|
44
|
+
# # pylint: disable=W1514
|
45
|
+
with open(**file_arguments) as _file: # type: ignore
|
44
46
|
|
45
47
|
if utils.v_true(chunks, bool):
|
46
|
-
for _chunk in response.iter_content(**iter_content_arguments):
|
48
|
+
for _chunk in response.iter_content(**iter_content_arguments): # type: ignore
|
47
49
|
_file.write(_chunk)
|
48
50
|
else:
|
49
51
|
_file.write(response.content)
|
50
52
|
|
51
|
-
logger.success(f'{
|
53
|
+
logger.success(f'{info_prefix} [success]')
|
52
54
|
|
53
55
|
return True
|
54
56
|
|
55
57
|
except Exception as e:
|
56
|
-
|
57
|
-
logger.error(f'{
|
58
|
+
|
59
|
+
logger.error(f'{info_prefix} [failure]')
|
60
|
+
logger.exception(e)
|
58
61
|
return False
|
59
62
|
|
60
63
|
|
61
|
-
def response_json(
|
62
|
-
data: any = None,
|
63
|
-
debug: bool = False,
|
64
|
-
**kwargs
|
65
|
-
) -> str:
|
64
|
+
def response_json(data: Any = None, **kwargs) -> str | None:
|
66
65
|
"""解决字符编码问题: ensure_ascii=False"""
|
67
66
|
try:
|
68
67
|
return json.dumps(data, default=str, ensure_ascii=False, sort_keys=True, **kwargs)
|
69
68
|
except Exception as e:
|
70
|
-
logger.exception(e)
|
69
|
+
logger.exception(e)
|
71
70
|
return None
|
ezKit/mongo.py
CHANGED
@@ -28,7 +28,7 @@ class Mongo():
|
|
28
28
|
logger.success(f"{info} [success]")
|
29
29
|
return True
|
30
30
|
except Exception as e:
|
31
|
-
logger.error(f"{info} [
|
31
|
+
logger.error(f"{info} [failure]")
|
32
32
|
logger.exception(e)
|
33
33
|
return False
|
34
34
|
|
@@ -56,12 +56,12 @@ class Mongo():
|
|
56
56
|
# 插入多条数据
|
57
57
|
result = db_collection.insert_many(data)
|
58
58
|
else:
|
59
|
-
logger.error(f"{info} [
|
59
|
+
logger.error(f"{info} [failure]")
|
60
60
|
logger.error("Data type error")
|
61
61
|
return False
|
62
62
|
logger.success(f"{info} [success]")
|
63
63
|
return result
|
64
64
|
except Exception as e:
|
65
|
-
logger.error(f"{info} [
|
65
|
+
logger.error(f"{info} [failure]")
|
66
66
|
logger.exception(e)
|
67
67
|
return False
|
ezKit/qywx.py
CHANGED
@@ -21,7 +21,7 @@ from . import utils
|
|
21
21
|
class QYWX:
|
22
22
|
"""企业微信"""
|
23
23
|
|
24
|
-
url_prefix =
|
24
|
+
url_prefix = "https://qyapi.weixin.qq.com"
|
25
25
|
work_id: str | None = None
|
26
26
|
agent_id: str | None = None
|
27
27
|
agent_secret: str | None = None
|
@@ -38,20 +38,22 @@ class QYWX:
|
|
38
38
|
|
39
39
|
def getaccess_token(self) -> str | None:
|
40
40
|
try:
|
41
|
-
response = requests.get(f
|
41
|
+
response = requests.get(f"{self.url_prefix}/cgi-bin/gettoken?corpid={self.work_id}&corpsecret={self.agent_secret}", timeout=10)
|
42
42
|
if response.status_code == 200:
|
43
43
|
result: dict = response.json()
|
44
44
|
self.access_token = result.get('access_token')
|
45
45
|
else:
|
46
46
|
self.access_token = None
|
47
47
|
return result.get('access_token')
|
48
|
-
except:
|
48
|
+
except Exception as e:
|
49
|
+
logger.exception(e)
|
49
50
|
return None
|
50
51
|
|
51
52
|
def get_agent_list(self) -> dict | str | None:
|
52
53
|
try:
|
53
|
-
|
54
|
-
|
54
|
+
if self.access_token is None:
|
55
|
+
self.getaccess_token()
|
56
|
+
response = requests.get(f"{self.url_prefix}/cgi-bin/agent/list?access_token={self.access_token}", timeout=10)
|
55
57
|
if response.status_code == 200:
|
56
58
|
response_data: dict = response.json()
|
57
59
|
if response_data.get('errcode') == 42001:
|
@@ -60,79 +62,91 @@ class QYWX:
|
|
60
62
|
self.get_agent_list()
|
61
63
|
return response_data
|
62
64
|
return response.text
|
63
|
-
except:
|
65
|
+
except Exception as e:
|
66
|
+
logger.exception(e)
|
64
67
|
return None
|
65
68
|
|
66
|
-
def get_department_list(self,
|
69
|
+
def get_department_list(self, eid: str | None = None) -> dict | str | None:
|
70
|
+
"""eid: Enterprise ID"""
|
67
71
|
try:
|
68
|
-
|
69
|
-
|
72
|
+
if self.access_token is None:
|
73
|
+
self.getaccess_token()
|
74
|
+
response = requests.get(f"{self.url_prefix}/cgi-bin/department/list?access_token={self.access_token}&id={eid}", timeout=10)
|
70
75
|
if response.status_code == 200:
|
71
76
|
response_data: dict = response.json()
|
72
77
|
if response_data.get('errcode') == 42001:
|
73
78
|
self.getaccess_token()
|
74
79
|
time.sleep(1)
|
75
|
-
self.get_department_list(
|
80
|
+
self.get_department_list(eid)
|
76
81
|
return response_data
|
77
82
|
return response.text
|
78
|
-
except:
|
83
|
+
except Exception as e:
|
84
|
+
logger.exception(e)
|
79
85
|
return None
|
80
86
|
|
81
|
-
def get_user_list(self,
|
87
|
+
def get_user_list(self, eid: str | None = None) -> dict | str | None:
|
88
|
+
"""eid: Enterprise ID"""
|
82
89
|
try:
|
83
|
-
|
84
|
-
|
90
|
+
if self.access_token is None:
|
91
|
+
self.getaccess_token()
|
92
|
+
response = requests.get(f"{self.url_prefix}/cgi-bin/user/list?access_token={self.access_token}&department_id={eid}", timeout=10)
|
85
93
|
if response.status_code == 200:
|
86
94
|
response_data: dict = response.json()
|
87
95
|
if response_data.get('errcode') == 42001:
|
88
96
|
self.getaccess_token()
|
89
97
|
time.sleep(1)
|
90
|
-
self.get_user_list(
|
98
|
+
self.get_user_list(eid)
|
91
99
|
return response_data
|
92
100
|
return response.text
|
93
|
-
except:
|
101
|
+
except Exception as e:
|
102
|
+
logger.exception(e)
|
94
103
|
return None
|
95
104
|
|
96
|
-
def get_user_id_by_mobile(self, mobile) -> dict | str | None:
|
105
|
+
def get_user_id_by_mobile(self, mobile: str | None = None) -> dict | str | None:
|
97
106
|
try:
|
98
|
-
|
107
|
+
if self.access_token is None:
|
108
|
+
self.getaccess_token()
|
99
109
|
json_string = json.dumps({'mobile': mobile})
|
100
|
-
response = requests.post(f
|
110
|
+
response = requests.post(f"{self.url_prefix}/cgi-bin/user/getuserid?access_token={self.access_token}", data=json_string, timeout=10)
|
101
111
|
if response.status_code == 200:
|
102
112
|
response_data: dict = response.json()
|
103
113
|
if response_data.get('errcode') == 42001:
|
104
114
|
self.getaccess_token()
|
105
115
|
time.sleep(1)
|
106
|
-
self.get_user_id_by_mobile(
|
116
|
+
self.get_user_id_by_mobile(mobile)
|
107
117
|
return response_data
|
108
118
|
return response.text
|
109
|
-
except:
|
119
|
+
except Exception as e:
|
120
|
+
logger.exception(e)
|
110
121
|
return None
|
111
122
|
|
112
|
-
def get_user_info(self,
|
123
|
+
def get_user_info(self, eid: str | None = None) -> dict | str | None:
|
124
|
+
"""eid: Enterprise ID"""
|
113
125
|
try:
|
114
|
-
|
115
|
-
|
126
|
+
if self.access_token is None:
|
127
|
+
self.getaccess_token()
|
128
|
+
response = requests.get(f"{self.url_prefix}/cgi-bin/user/get?access_token={self.access_token}&userid={eid}", timeout=10)
|
116
129
|
if response.status_code == 200:
|
117
130
|
response_data: dict = response.json()
|
118
131
|
if response_data.get('errcode') == 42001:
|
119
132
|
self.getaccess_token()
|
120
133
|
time.sleep(1)
|
121
|
-
self.get_user_info(
|
134
|
+
self.get_user_info(eid)
|
122
135
|
return response_data
|
123
136
|
return response.text
|
124
|
-
except:
|
137
|
+
except Exception as e:
|
138
|
+
logger.exception(e)
|
125
139
|
return None
|
126
140
|
|
127
|
-
def send_message_by_mobile(self, mobile: str | list, message: str
|
141
|
+
def send_message_by_mobile(self, mobile: str | list, message: str) -> bool:
|
128
142
|
"""发送消息"""
|
129
|
-
"""
|
130
|
-
参考文档:
|
131
143
|
|
132
|
-
|
133
|
-
|
144
|
+
# 参考文档:
|
145
|
+
# https://developer.work.weixin.qq.com/document/path/90235
|
146
|
+
|
134
147
|
try:
|
135
|
-
|
148
|
+
if self.access_token is None:
|
149
|
+
self.getaccess_token()
|
136
150
|
|
137
151
|
users: list = []
|
138
152
|
|
@@ -157,7 +171,7 @@ class QYWX:
|
|
157
171
|
'duplicate_check_interval': 1800
|
158
172
|
}
|
159
173
|
json_string = json.dumps(json_dict)
|
160
|
-
response = requests.post(f
|
174
|
+
response = requests.post(f"{self.url_prefix}/cgi-bin/message/send?access_token={self.access_token}", data=json_string, timeout=10)
|
161
175
|
if response.status_code == 200:
|
162
176
|
response_data: dict = response.json()
|
163
177
|
if response_data.get('errcode') == 42001:
|
@@ -168,5 +182,5 @@ class QYWX:
|
|
168
182
|
return True
|
169
183
|
|
170
184
|
except Exception as e:
|
171
|
-
logger.exception(e)
|
185
|
+
logger.exception(e)
|
172
186
|
return False
|