-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmail.py
416 lines (370 loc) · 15.5 KB
/
mail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import os
import random
import shutil
import string
import time
import requests
import zmail
import re
from datetime import datetime, timedelta
import zipfile
from conf import configs
# 登录邮箱(邮箱名, 密码)
user_email = configs['email']['user_address']
# 存放文件地址
save_path = os.getcwd()
temp_path = save_path + '/bill_save/temp'
archives_path = save_path + '/bill_save/archives'
interval = configs['email']['server']['interval']
debug = configs['debug']
if type(debug) is not bool:
debug = False
# return -1 未能获取目标邮件
# return -2 连接断开,需要重新登录
# return -3 未能获取解压密码
def server_login():
try:
s = zmail.server(username=configs['email']['server']['address'],
password=configs['email']['server']['password'])
except Exception as e:
print("登录服务端邮箱时出现异常 ")
if debug:
print(e)
print("请检查配置文件是否填写完整")
return -1
if s.smtp_able():
print("SMTP服务器连接成功")
if s.pop_able():
print("POP3服务器连接成功")
return s
else:
print("POP3服务器连接失败")
return -1
else:
print("SMTP服务器连接失败")
return -1
def re_login():
for delay_time in [300, 900, 1800, 2700, 3600]:
print(str(int(delay_time / 60)) + "分钟后重试")
time.sleep(delay_time)
server = server_login()
if server != -1:
break
while server == -1:
print("重新登录失败,等待1小时后重试")
time.sleep(3600)
server = server_login()
# 解压文件
def unzip_with_password(zip_file, password, unzip_dir):
print(f"尝试使用密码 {password}")
try:
with zipfile.ZipFile(zip_file, 'r') as zip_file:
for file in zip_file.namelist():
if file.endswith('.csv'):
zip_file.extract(member=file, pwd=password.encode('utf-8'), path=unzip_dir)
# 首先检查目录下的直接文件
for filename in os.listdir(unzip_dir):
if filename.endswith('.csv'):
return unzip_dir + '/' + filename
# 然后遍历目录及其所有子目录
for dir_path, dir_names, filenames in os.walk(unzip_dir):
for filename in filenames:
if filename.endswith('.csv'):
return dir_path + '/' + filename
except FileNotFoundError:
print(f"文件 {zip_file} 不存在.")
return 1
except zipfile.BadZipFile:
print(f"文件 {zip_file} 不是一个有效的zip文件.")
return 2
except RuntimeError:
print(f"解压文件 {zip_file} 时密码错误.")
return 3
# 下载文件
def download_file(url, download_path):
response = requests.get(url)
with open(download_path, 'wb') as fd:
for chunk in response.iter_content(chunk_size=1024):
fd.write(chunk)
# 发送请求邮件
def send_email(server, subject, content):
mail = {
'subject': subject,
'content_text': content
}
server.send_mail(user_email, mail)
print("请求邮件已发送")
# 归档账单
def archive_bill(platform):
if not os.path.exists(temp_path):
print(f"源文件夹 {temp_path} 不存在")
return
if platform == '支付宝':
platform = 'alipay'
elif platform == '微信':
platform = 'wechat'
elif platform == 'all':
for filename in os.listdir(temp_path):
# 清空文件
file_path = os.path.join(temp_path, filename)
if os.path.isdir(file_path):
shutil.rmtree(file_path)
else:
os.remove(file_path)
return
else:
print("平台名称错误")
return
# 创建目标文件夹
if not os.path.exists(archives_path):
print(f"目标文件夹 {archives_path} 不存在,创建该文件夹")
os.mkdir(archives_path)
# 创建目标分类文件夹
platform_path = os.path.join(archives_path, platform)
if not os.path.exists(platform_path):
print(f"目标文件夹 {platform_path} 不存在,创建该文件夹")
os.mkdir(platform_path)
for filename in os.listdir(temp_path):
# 生成源文件和目标文件目录
src_file_path = os.path.join(temp_path, filename)
dst_file_path = os.path.join(platform_path, filename)
# 移动文件到目标文件夹
shutil.move(src_file_path, dst_file_path)
def init_path():
if not os.path.exists(save_path + '/bill_save'):
print(f"目标文件夹 {save_path} 不存在,创建该文件夹")
os.mkdir(save_path + '/bill_save')
if not os.path.exists(save_path + '/bill_save/temp'):
print(f"目标文件夹 {save_path} 不存在,创建该文件夹")
os.mkdir(save_path + '/bill_save/temp')
# 生产随机码用于标示邮件
def generate_code(length):
# 获取所有字母和数字的数组
code_chars = string.ascii_letters + string.digits
# 随机生成 length 个随机码
return ''.join(random.choice(code_chars) for _ in range(length))
def handle_alipay_mail(server, mail_of_alipay):
init_path()
archive_bill("all")
random_code = generate_code(6)
# 保存附件
zmail.save_attachment(mail_of_alipay, temp_path, overwrite=True)
# 从邮箱获取解压密码
start_time = datetime.now()
black_list = [mail_of_alipay['Id']]
send_email(server=server, subject="<" + random_code + ">支付宝账单密码请求",
content='你正在对支付宝进行记账操作,你需要对这封邮件回复6位数字解压密码,有效时间2小时,发信时间为'
+ datetime.now().strftime("%m-%d %H:%M:%S") + '。')
# 轮询邮箱获取密码
time.sleep(interval)
is_loop = True
try_times = 3
while is_loop and try_times > 0:
# 时限为2小时
if datetime.now() - start_time > timedelta(hours=2):
break
# 接收请求邮箱之后的回复邮箱
try:
mail_for_pwd = server.get_mails(subject='<' + random_code + '>支付宝账单密码请求',
start_time=start_time.strftime("%Y-%m-%d %H:%M:%S"))
except ConnectionResetError:
print("连接重置,即将尝试重连")
try_times -= 1
time.sleep(interval)
continue
except BrokenPipeError:
print("连接关闭,将尝试重新登录")
try_times -= 1
re_login()
continue
except Exception as e:
if debug:
print("在获取支付宝文件密码时异常 " + str(e))
print("收取邮件时遇到错误,即将重试")
try_times -= 1
time.sleep(interval)
continue
if len(mail_for_pwd) != 0:
for i in range(0, len(mail_for_pwd)):
# 跳过黑名单的邮件
if mail_for_pwd[i]['Id'] in black_list:
continue
# 尝试获取密码
zip_password = re.search(r'\d{6}', mail_for_pwd[i]['Content_text'][0]).group()
state = unzip_with_password(temp_path + '/' + mail_of_alipay['attachments'][0][0], zip_password,
temp_path)
if state == 1:
is_loop = False
break
elif state == 2:
print('文件失效,该链接可能已过期')
os.remove(temp_path + '/' + mail_of_alipay['attachments'][0][0])
break
elif state == 3:
black_list.append(mail_for_pwd[i]['id'])
send_email(server=server, subject='<' + random_code + '>支付宝账单密码请求',
content='密码 ' + zip_password + '错误' + '\n你正在对支付宝进行记账操作,你需要对这封邮件回复6位数字解压密码,发信时间为'
+ datetime.now().strftime("%m-%d %H:%M:%S") + '。')
else:
os.remove(temp_path + '/' + mail_of_alipay['attachments'][0][0])
if debug:
print(f'解压成功,密码为{zip_password}')
else:
print('解压成功')
if configs['email']['delete_after_used']:
if debug:
print(mail_for_pwd[i]['Id'])
server.delete(mail_of_alipay['Id'])
print('邮件已删除')
return state
print("未获取支付宝账单解压密码,即将重试")
time.sleep(interval)
if try_times == 0:
return -4
print("2小时内未能获取支付宝账单解压密码")
send_email(server=server, subject="同步失败", content="未能获取解压密码")
return -3
def handle_wechat_mail(server, mail_of_wechat):
# 初始化环境
init_path()
archive_bill("all")
random_code = generate_code(6)
# 获取下载地址
content = str(mail_of_wechat['content_html'])
url = re.findall(r'https://download\.bill\.weixin\.qq\.com/[^"]*', content)
download_path = temp_path + '/wechat_bill' + datetime.now().strftime("%Y%m%d%H%M%S") + '.zip'
download_file(url[0], download_path)
# 从邮箱获取解压密码
start_time = datetime.now()
black_list = [mail_of_wechat['Id']]
send_email(server=server, subject="<" + random_code + ">微信账单密码请求",
content='你正在对微信进行记账操作,你需要对这封邮件回复6位数字解压密码,有效时间2小时,发信时间为'
+ datetime.now().strftime("%m-%d %H:%M:%S") + '。')
# 轮询邮箱获取密码
time.sleep(interval)
is_loop = True
try_times = 3
while is_loop and try_times > 0:
# 时限为2小时
if datetime.now() - start_time > timedelta(hours=2):
break
try:
mail_for_pwd = server.get_mails(subject='<' + random_code + '>微信账单密码请求',
start_time=start_time.strftime("%Y-%m-%d %H:%M:%S"))
except ConnectionResetError:
print("连接重置,即将重试")
try_times -= 1
time.sleep(interval)
continue
except BrokenPipeError:
print("连接关闭,将尝试重新登录")
try_times -= 1
re_login()
continue
except Exception as e:
if debug:
print("在获取微信文件密码时异常 " + str(e))
print("收取邮件时遇到错误,即将重试")
try_times -= 1
time.sleep(interval)
continue
if len(mail_for_pwd) != 0:
for i in range(0, len(mail_for_pwd)):
# 跳过黑名单的邮件
if mail_for_pwd[i]['Id'] in black_list:
continue
# 尝试获取密码
zip_password = re.search(r'\d{6}', mail_for_pwd[i]['Content_text'][0]).group()
state = unzip_with_password(zip_file=download_path, password=zip_password, unzip_dir=temp_path)
if state == 1:
is_loop = False
break
elif state == 2:
print('文件失效,该链接可能已过期')
os.remove(download_path)
is_loop = False
break
elif state == 3:
black_list.append(mail_for_pwd[i]['id'])
send_email(server=server, subject="<" + random_code + ">微信账单密码请求",
content='密码 ' + zip_password + '错误' + '\n你正在对微信进行记账操作,你需要对这封邮件回复6位数字解压密码,发信时间为'
+ datetime.now().strftime("%m-%d %H:%M:%S") + '。')
else:
os.remove(download_path)
print(f'解压成功,密码为{zip_password}')
if configs['email']['delete_after_used']:
server.delete(mail_of_wechat['Id'])
print('邮件已删除')
return state
print("未获取微信账单解压密码,即将重试")
time.sleep(interval)
if try_times == 0:
return -4
print("2小时内未能获取微信账单解压密码")
send_email(server=server, subject="同步失败", content="未能获取解压密码")
return -3
def get_mail(receive_time, server):
try_times = 3
result = []
state = None
while try_times > 0:
try:
# 补偿多轮次下接收支付宝和微信邮件的时间差
mails_of_alipay = server.get_mails(subject='支付宝交易流水明细',
start_time=(receive_time - timedelta(seconds=interval)).strftime(
'%Y-%m-%d %H:%M:%S'),
sender='[email protected]')
except ConnectionResetError:
print("连接重置,即将重试")
time.sleep(interval)
try_times -= 1
continue
except BrokenPipeError:
print("连接关闭,将尝试重新登录")
re_login()
continue
except Exception as e:
if debug:
print("在获取支付宝邮件时异常 " + str(e))
print("收取邮件时遇到错误,即将重试")
try_times -= 1
time.sleep(interval)
continue
if len(mails_of_alipay) != 0:
state = handle_alipay_mail(server=server, mail_of_alipay=mails_of_alipay[0])
result = "支付宝", state
else:
time.sleep(interval)
try:
mails_of_wechat = server.get_mails(subject='微信支付',
start_time=receive_time.strftime(
'%Y-%m-%d %H:%M:%S'),
sender='[email protected]')
except ConnectionResetError:
print("连接重置,即将重试")
time.sleep(interval)
try_times -= 1
continue
except BrokenPipeError:
print("连接关闭,将尝试重新登录")
re_login()
continue
except Exception as e:
if debug:
print("在获取微信支付邮件时异常 " + str(e))
print("收取邮件时遇到错误,即将重试")
try_times -= 1
time.sleep(interval)
continue
if len(mails_of_wechat) != 0:
state = handle_wechat_mail(server=server, mail_of_wechat=mails_of_wechat[0])
result = "微信", state
if state == -3:
send_email(server=server, subject="同步失败", content="未能获取解压密码")
return -3
elif state:
return result
else:
return -1
print("获取邮件失败次数达到上限")
return -2