pytbox 0.1.4__py3-none-any.whl → 0.1.5__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.
Potentially problematic release.
This version of pytbox might be problematic. Click here for more details.
- pytbox/base.py +3 -1
- pytbox/mail/alimail.py +55 -42
- pytbox/utils/timeutils.py +5 -2
- {pytbox-0.1.4.dist-info → pytbox-0.1.5.dist-info}/METADATA +1 -1
- {pytbox-0.1.4.dist-info → pytbox-0.1.5.dist-info}/RECORD +8 -8
- {pytbox-0.1.4.dist-info → pytbox-0.1.5.dist-info}/WHEEL +0 -0
- {pytbox-0.1.4.dist-info → pytbox-0.1.5.dist-info}/entry_points.txt +0 -0
- {pytbox-0.1.4.dist-info → pytbox-0.1.5.dist-info}/top_level.txt +0 -0
pytbox/base.py
CHANGED
|
@@ -14,6 +14,7 @@ from pytbox.utils.env import get_env_by_os_environment
|
|
|
14
14
|
from pytbox.vmware import VMwareClient
|
|
15
15
|
from pytbox.pyjira import PyJira
|
|
16
16
|
from pytbox.mail.client import MailClient
|
|
17
|
+
from pytbox.mail.alimail import AliMail
|
|
17
18
|
|
|
18
19
|
config = load_config_by_file(path='/workspaces/pytbox/tests/alert/config_dev.toml', oc_vault_id=os.environ.get('oc_vault_id'))
|
|
19
20
|
|
|
@@ -83,4 +84,5 @@ pyjira = PyJira(
|
|
|
83
84
|
)
|
|
84
85
|
|
|
85
86
|
mail_163 = MailClient(mail_address=config['mail']['163']['mail_address'], password=config['mail']['163']['password'])
|
|
86
|
-
mail_qq = MailClient(mail_address=config['mail']['qq']['mail_address'], password=config['mail']['qq']['password'])
|
|
87
|
+
mail_qq = MailClient(mail_address=config['mail']['qq']['mail_address'], password=config['mail']['qq']['password'])
|
|
88
|
+
ali_mail = AliMail(mail_address=config['mail']['aliyun']['mail_address'], client_id=config['mail']['aliyun']['client_id'], client_secret=config['mail']['aliyun']['client_secret'])
|
pytbox/mail/alimail.py
CHANGED
|
@@ -4,32 +4,29 @@ from typing import Literal
|
|
|
4
4
|
from datetime import datetime, timedelta
|
|
5
5
|
|
|
6
6
|
import requests
|
|
7
|
+
from ..utils.response import ReturnResponse
|
|
8
|
+
from .mail_detail import MailDetail
|
|
9
|
+
from ..utils.timeutils import TimeUtils
|
|
7
10
|
|
|
8
11
|
|
|
9
12
|
|
|
10
13
|
class AliMail:
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
self.
|
|
14
|
+
'''
|
|
15
|
+
_summary_
|
|
16
|
+
'''
|
|
17
|
+
def __init__(self, mail_address: str=None, client_id: str=None, client_secret: str=None, timeout: int=3):
|
|
18
|
+
self.email_address = mail_address
|
|
19
|
+
self.client_id = client_id
|
|
20
|
+
self.client_secret = client_secret
|
|
16
21
|
self.base_url = "https://alimail-cn.aliyuncs.com/v2"
|
|
22
|
+
self.timeout = timeout
|
|
17
23
|
self.headers = {
|
|
18
24
|
"Content-Type": "application/json",
|
|
19
|
-
"Authorization": f"bearer {self.
|
|
25
|
+
"Authorization": f"bearer {self._get_access_token_by_request()}"
|
|
20
26
|
}
|
|
27
|
+
self.session = requests.Session()
|
|
28
|
+
self.session.headers.update(self.headers)
|
|
21
29
|
|
|
22
|
-
def _get_access_token(self):
|
|
23
|
-
current_time = datetime.now()
|
|
24
|
-
# stored_token = mongo_alimail_token.find_one({"token_type": "bearer"})
|
|
25
|
-
if stored_token and stored_token.get('expiration_time'):
|
|
26
|
-
expiration_time = stored_token['expiration_time']
|
|
27
|
-
if current_time < expiration_time:
|
|
28
|
-
return stored_token['access_token']
|
|
29
|
-
else:
|
|
30
|
-
return self._get_access_token_by_request()
|
|
31
|
-
else:
|
|
32
|
-
return self._get_access_token_by_request()
|
|
33
30
|
|
|
34
31
|
def _get_access_token_by_request(self):
|
|
35
32
|
'''
|
|
@@ -52,7 +49,7 @@ class AliMail:
|
|
|
52
49
|
"client_secret": self.client_secret
|
|
53
50
|
}
|
|
54
51
|
try:
|
|
55
|
-
response = requests.post(interface_url, headers=headers, data=data, timeout=
|
|
52
|
+
response = requests.post(interface_url, headers=headers, data=data, timeout=self.timeout)
|
|
56
53
|
response_json = response.json()
|
|
57
54
|
current_time = datetime.now()
|
|
58
55
|
data = {
|
|
@@ -61,28 +58,23 @@ class AliMail:
|
|
|
61
58
|
'expires_in': response_json["expires_in"],
|
|
62
59
|
'expiration_time': current_time + timedelta(seconds=response_json["expires_in"])
|
|
63
60
|
}
|
|
64
|
-
mongo_alimail_token.update_one(
|
|
65
|
-
{"token_type": "bearer"},
|
|
66
|
-
{"$set": data},
|
|
67
|
-
upsert=True
|
|
68
|
-
)
|
|
69
61
|
return data.get("access_token")
|
|
70
62
|
except requests.RequestException as e:
|
|
71
63
|
# 处理请求失败异常
|
|
72
|
-
|
|
64
|
+
raise e
|
|
73
65
|
except (KeyError, ValueError) as e:
|
|
74
66
|
# 处理解析响应失败异常
|
|
75
|
-
|
|
67
|
+
raise e
|
|
76
68
|
|
|
77
|
-
def
|
|
78
|
-
response =
|
|
69
|
+
def get_mail_folders(self):
|
|
70
|
+
response = self.session.get(
|
|
79
71
|
url=f"{self.base_url}/users/{self.email_address}/mailFolders",
|
|
80
72
|
headers=self.headers
|
|
81
73
|
)
|
|
82
74
|
return response.json().get('folders')
|
|
83
75
|
|
|
84
|
-
def
|
|
85
|
-
folders = self.
|
|
76
|
+
def get_folder_id(self, folder_name: Literal['inbox']='inbox'):
|
|
77
|
+
folders = self.get_mail_folders()
|
|
86
78
|
for folder in folders:
|
|
87
79
|
if folder.get('displayName') == folder_name:
|
|
88
80
|
return folder.get('id')
|
|
@@ -92,7 +84,7 @@ class AliMail:
|
|
|
92
84
|
params = {
|
|
93
85
|
"$select": "body,toRecipients,internetMessageId,internetMessageHeaders"
|
|
94
86
|
}
|
|
95
|
-
response =
|
|
87
|
+
response = self.session.get(
|
|
96
88
|
url=f"{self.base_url}/users/{self.email_address}/messages/{mail_id}",
|
|
97
89
|
headers=self.headers,
|
|
98
90
|
params=params,
|
|
@@ -100,30 +92,51 @@ class AliMail:
|
|
|
100
92
|
)
|
|
101
93
|
return response.json().get('message')
|
|
102
94
|
|
|
103
|
-
def
|
|
104
|
-
folder_id = self.
|
|
95
|
+
def get_mail_list(self, folder_name: str='inbox', size: int=100):
|
|
96
|
+
folder_id = self.get_folder_id(folder_name=folder_name)
|
|
105
97
|
params = {
|
|
106
98
|
"size": size,
|
|
107
99
|
# "$select": "toRecipients"
|
|
108
100
|
}
|
|
109
|
-
response =
|
|
101
|
+
response = self.session.get(
|
|
110
102
|
url=f"{self.base_url}/users/{self.email_address}/mailFolders/{folder_id}/messages",
|
|
111
103
|
headers=self.headers,
|
|
112
104
|
params=params,
|
|
113
105
|
timeout=3
|
|
114
106
|
)
|
|
115
107
|
messages = response.json().get("messages")
|
|
108
|
+
sent_to_list = []
|
|
116
109
|
for message in messages:
|
|
117
110
|
mail_id = message.get("id")
|
|
118
111
|
detail = self.get_mail_detail(mail_id=mail_id)
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
112
|
+
|
|
113
|
+
for to_recipient in detail.get('toRecipients'):
|
|
114
|
+
sent_to_list.append(to_recipient.get('email'))
|
|
115
|
+
|
|
116
|
+
yield MailDetail(
|
|
117
|
+
uid=message.get('id'),
|
|
118
|
+
sent_from=message.get('from').get('email'),
|
|
119
|
+
sent_to=sent_to_list,
|
|
120
|
+
date=TimeUtils.convert_str_to_datetime(time_str=message.get('sentDateTime'), app='alimail'),
|
|
121
|
+
cc="",
|
|
122
|
+
subject=message.get('subject'),
|
|
123
|
+
body_plain=message.get('summary'),
|
|
124
|
+
body_html=""
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
def move(self, uid: str, destination_folder: str) -> ReturnResponse:
|
|
128
|
+
params = {
|
|
129
|
+
"ids": [uid],
|
|
130
|
+
"destinationFolderId": self.get_folder_id(destination_folder)
|
|
131
|
+
}
|
|
132
|
+
r = self.session.post(
|
|
133
|
+
url=f"{self.base_url}/users/{self.email_address}/messages/move",
|
|
134
|
+
params=params
|
|
135
|
+
)
|
|
136
|
+
if r.status_code == 200:
|
|
137
|
+
return ReturnResponse(code=0, msg=f'邮件移动到 {destination_folder} 成功', data=None)
|
|
138
|
+
else:
|
|
139
|
+
return ReturnResponse(code=1, msg=f'邮件移动到 {destination_folder} 失败', data=r.json())
|
|
122
140
|
|
|
123
141
|
if __name__ == '__main__':
|
|
124
|
-
ali_mail = AliMail()
|
|
125
|
-
# print(ali_mail.query_folder_id())
|
|
126
|
-
# for i in ali_mail.list_mail(size=1):
|
|
127
|
-
# print(i)
|
|
128
|
-
# print(ali_mail.access_token)
|
|
129
|
-
print(ali_mail.get_mail_detail(mail_id='DzzzzzzMeuY'))
|
|
142
|
+
ali_mail = AliMail()
|
pytbox/utils/timeutils.py
CHANGED
|
@@ -287,7 +287,7 @@ class TimeUtils:
|
|
|
287
287
|
return int(time.time()) * 1000
|
|
288
288
|
|
|
289
289
|
@staticmethod
|
|
290
|
-
def convert_str_to_datetime(time_str: str, app: Literal['lg_alert_trigger', 'lg_alert_resolved', 'mongo']='lg_alert_trigger') -> datetime.datetime:
|
|
290
|
+
def convert_str_to_datetime(time_str: str, app: Literal['lg_alert_trigger', 'lg_alert_resolved', 'mongo', 'alimail']='lg_alert_trigger') -> datetime.datetime:
|
|
291
291
|
"""
|
|
292
292
|
将字符串转换为datetime对象
|
|
293
293
|
|
|
@@ -305,7 +305,10 @@ class TimeUtils:
|
|
|
305
305
|
elif app == 'mongo':
|
|
306
306
|
time_obj = datetime.datetime.fromisoformat(time_str.replace('Z', '+00:00'))
|
|
307
307
|
return time_obj # 已经包含时区信息,直接返回
|
|
308
|
-
|
|
308
|
+
elif app == 'alimail':
|
|
309
|
+
time_obj = datetime.datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%SZ")
|
|
310
|
+
time_obj = time_obj + datetime.timedelta(hours=8)
|
|
311
|
+
# return time_obj
|
|
309
312
|
# 对于没有时区信息的时间对象,设置为Asia/Shanghai时区
|
|
310
313
|
time_with_tz = time_obj.replace(tzinfo=ZoneInfo("Asia/Shanghai"))
|
|
311
314
|
return time_with_tz
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
pytbox/base.py,sha256=
|
|
1
|
+
pytbox/base.py,sha256=wWjzJ6gcEnUBIG3-U49CKeoqT-1Tk20EJRt2oYDAj0k,3157
|
|
2
2
|
pytbox/cli.py,sha256=N775a0GK80IT2lQC2KRYtkZpIiu9UjavZmaxgNUgJhQ,160
|
|
3
3
|
pytbox/dida365.py,sha256=pUMPB9AyLZpTTbaz2LbtzdEpyjvuGf4YlRrCvM5sbJo,10545
|
|
4
4
|
pytbox/excel.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
@@ -51,7 +51,7 @@ pytbox/feishu/helpers.py,sha256=jhSkHiUw4822QBXx2Jw8AksogZdakZ-3QqvC3lB3qEI,201
|
|
|
51
51
|
pytbox/feishu/typing.py,sha256=3hWkJgOi-v2bt9viMxkyvNHsPgrbAa0aZOxsZYg2vdM,122
|
|
52
52
|
pytbox/log/logger.py,sha256=7ZisXRxLb_MVbIqlYHWoTbj1EA0Z4G5SZvITlt1IKW8,7416
|
|
53
53
|
pytbox/log/victorialog.py,sha256=gffEiq38adv9sC5oZeMcyKghd3SGfRuqtZOFuqHQF6E,4139
|
|
54
|
-
pytbox/mail/alimail.py,sha256=
|
|
54
|
+
pytbox/mail/alimail.py,sha256=njKA3PUbIaiKFaxKvUObmklmEEHg2YA-O5rpgsgT5_w,5147
|
|
55
55
|
pytbox/mail/client.py,sha256=6HeKpChHGjTCYGBgQcfAhWlU_wh9wtO-bjP6TU38pGM,6120
|
|
56
56
|
pytbox/mail/mail_detail.py,sha256=6u8DK-7WzYPSuX6TdicSCh2Os_9Ou6Rn9xc6WRvv85M,699
|
|
57
57
|
pytbox/network/meraki.py,sha256=054E3C5KzAuXs9aPalvdAOUo6Hc5aOKZSWUaVbPquy4,6112
|
|
@@ -61,10 +61,10 @@ pytbox/utils/load_vm_devfile.py,sha256=GVbB-FvGb0l586SDaraz__ZaXyDWd1WxcXVw5xGfX
|
|
|
61
61
|
pytbox/utils/ping_checker.py,sha256=Nqnn8clbgv-5l0PgxcTOldg8mkMKrFn4TvPL-rYUUGg,1
|
|
62
62
|
pytbox/utils/response.py,sha256=kXjlwt0WVmLRam2eu1shzX2cQ7ux4cCQryaPGYwle5g,1247
|
|
63
63
|
pytbox/utils/richutils.py,sha256=OT9_q2Q1bthzB0g1GlhZVvM4ZAepJRKL6a_Vsr6vEqo,487
|
|
64
|
-
pytbox/utils/timeutils.py,sha256=
|
|
64
|
+
pytbox/utils/timeutils.py,sha256=uSKgwt20mVcgIGKLsH2tNum8v3rcpzgmBibPvyPQFgM,20433
|
|
65
65
|
pytbox/win/ad.py,sha256=-3pWfL3dElz-XoO4j4M9lrgu3KJtlhrS9gCWJBpafAU,1147
|
|
66
|
-
pytbox-0.1.
|
|
67
|
-
pytbox-0.1.
|
|
68
|
-
pytbox-0.1.
|
|
69
|
-
pytbox-0.1.
|
|
70
|
-
pytbox-0.1.
|
|
66
|
+
pytbox-0.1.5.dist-info/METADATA,sha256=hIq6VA2Lt7Y0T3nZgFuP1K_ug-pcckGJ3olhE4cc4Bg,6319
|
|
67
|
+
pytbox-0.1.5.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
68
|
+
pytbox-0.1.5.dist-info/entry_points.txt,sha256=YaTOJ2oPjOiv2SZwY0UC-UA9QS2phRH1oMvxGnxO0Js,43
|
|
69
|
+
pytbox-0.1.5.dist-info/top_level.txt,sha256=YADgWue-Oe128ptN3J2hS3GB0Ncc5uZaSUM3e1rwswE,7
|
|
70
|
+
pytbox-0.1.5.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|