ComprehendController.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. # import imaplib
  2. # import shutil
  3. # import smtplib
  4. # from email.mime.multipart import MIMEMultipart
  5. # from email.mime.text import MIMEText
  6. #
  7. # import boto3
  8. # from boto3 import Session
  9. # from django.views import View
  10. #
  11. # from model.models import EmailSortModel, EmailConfigModel
  12. # from Utils.NeteaseMail import NeteaseMail
  13. # from django.views.generic import TemplateView
  14. #
  15. # from object.ResponseObject import ResponseObject
  16. # import os
  17. # import csv
  18. #
  19. #
  20. # class ComprehendView(TemplateView):
  21. #
  22. # def get(self, request, *args, **kwargs):
  23. # request.encoding = 'utf-8'
  24. # operation = kwargs.get('operation')
  25. # return self.validation(request.GET, request, operation)
  26. #
  27. # def post(self, request, *args, **kwargs):
  28. # request.encoding = 'utf-8'
  29. # operation = kwargs.get('operation')
  30. # return self.validation(request.POST, request, operation)
  31. #
  32. # def validation(self, request_dict, request, operation):
  33. # response = ResponseObject()
  34. # if operation is None:
  35. # return response.json(444, 'error path')
  36. # else:
  37. # if operation == 'createSort':
  38. # return self.createSort(request_dict, response)
  39. # elif operation == 'doStartSortWord':
  40. # return self.doStartSortWord(request_dict, response)
  41. # elif operation == 'write_csv':
  42. # return self.write_csv(request_dict, response)
  43. # elif operation == 'upload_csv':
  44. # return self.upload_csv(request_dict, response)
  45. # elif operation == 'create_document_classifier':
  46. # return self.create_document_classifier(request_dict, response)
  47. # elif operation == 'create_endpoint':
  48. # return self.create_endpoint(request_dict, response)
  49. # elif operation == 'describe_document_classifier':
  50. # return self.describe_document_classifier(request_dict, response)
  51. # elif operation == 'describe_endpoint':
  52. # return self.describe_endpoint(request_dict, response)
  53. # elif operation == 'delete_endpoint':
  54. # return self.delete_endpoint(request_dict, response)
  55. # elif operation == 'delete_document_classifier':
  56. # return self.delete_document_classifier(request_dict, response)
  57. # elif operation == 'getDocumentClassifier':
  58. # return self.get_document_classifier(request_dict, response)
  59. # elif operation == 'email':
  60. # return self.email(request_dict, response)
  61. #
  62. # def createSort(self, request_dict, response):
  63. # client = boto3.client('comprehend', region_name='region')
  64. #
  65. # # Create a document classifier
  66. # create_response = client.create_document_classifier(
  67. # InputDataConfig={
  68. # 'S3Uri': 's3://S3Bucket/docclass/file name'
  69. # },
  70. # DataAccessRoleArn='arn:aws:iam::account number:role/resource name',
  71. # DocumentClassifierName='SampleCodeClassifier1',
  72. # LanguageCode='en'
  73. # )
  74. # print("Create response: %s\n", create_response)
  75. #
  76. # # Check the status of the classifier
  77. # describe_response = client.describe_document_classifier(
  78. # DocumentClassifierArn=create_response['DocumentClassifierArn'])
  79. # print("Describe response: %s\n", describe_response)
  80. #
  81. # # List all classifiers in account
  82. # list_response = client.list_document_classifiers()
  83. # print("List response: %s\n", list_response)
  84. #
  85. # def doStartSortWord(self, request_dict, response):
  86. # client = boto3.client('comprehend', region_name='region')
  87. #
  88. # start_response = client.start_document_classification_job(
  89. # InputDataConfig={
  90. # 'S3Uri': 's3://srikad-us-west-2-input/docclass/file name',
  91. # 'InputFormat': 'ONE_DOC_PER_LINE'
  92. # },
  93. # OutputDataConfig={
  94. # 'S3Uri': 's3://S3Bucket/output'
  95. # },
  96. # DataAccessRoleArn='arn:aws:iam::account number:role/resource name',
  97. # DocumentClassifierArn=
  98. # 'arn:aws:comprehend:region:account number:document-classifier/SampleCodeClassifier1'
  99. # )
  100. #
  101. # print("Start response: %s\n", start_response)
  102. #
  103. # # Check the status of the job
  104. # describe_response = client.describe_document_classification_job(JobId=start_response['JobId'])
  105. # print("Describe response: %s\n", describe_response)
  106. #
  107. # # List all classification jobs in account
  108. # list_response = client.list_document_classification_jobs()
  109. # print("List response: %s\n", list_response)
  110. #
  111. # def write_csv(self, request_dict, response):
  112. # file_path = os.path.join(os.path.dirname(__file__) + '/../emailtrainfile/emailtrain.csv')
  113. # with open(file_path, "w", newline='') as f:
  114. # sort = EmailSortModel.objects.values('sort', 'clientquestion')
  115. # for a in sort:
  116. # text = [a['sort'], a['clientquestion']]
  117. # csv_writer = csv.writer(f)
  118. # for i in range(50):
  119. # csv_writer.writerow(text)
  120. # return response.json(0)
  121. #
  122. # def upload_csv(self, request_dict, response):
  123. # file_path = os.path.join(os.path.dirname(__file__) + '/../emailtrainfile/emailtrain.csv')
  124. # bucket_name = "asj-amazon-comprehend"
  125. # upload_key = "location/emailtrain.csv"
  126. # with open(file_path, "rb") as f:
  127. # NeteaseMail().s3_login().upload_fileobj(f, bucket_name, Key=upload_key)
  128. # return response.json(0)
  129. #
  130. # def create_document_classifier(self, request_dict, response):
  131. # try:
  132. # response1 = NeteaseMail().aws_login().create_document_classifier(
  133. # DocumentClassifierName='lhq',
  134. # DataAccessRoleArn='arn:aws:iam::697864307463:role/service-role/AmazonComprehendServiceRoleS3FullAccess-admin',
  135. # # Tags=[{'Key': 'string','Value': 'string'},],
  136. # InputDataConfig={
  137. # 'DataFormat': 'COMPREHEND_CSV',
  138. # # | 'AUGMENTED_MANIFEST',
  139. # 'S3Uri': 's3://asj-amazon-comprehend/location/emailtrain.csv',
  140. # # 'LabelDelimiter': 'string',
  141. # # 'AugmentedManifests': [
  142. # # {
  143. # # 'S3Uri': 'string',
  144. # # 'AttributeNames': ['string', ]
  145. # # },
  146. # # ]
  147. # },
  148. # OutputDataConfig={
  149. # 'S3Uri': 's3://asj-amazon-comprehend/output/lhq/'},
  150. # # 'KmsKeyId': 'string'},
  151. # # ClientRequestToken='string',
  152. # LanguageCode='en',
  153. # # | 'es' | 'fr' | 'de' | 'it' | 'pt' | 'ar'
  154. # # | 'hi' | 'ja' | 'ko' | 'zh' | 'zh-TW',
  155. # # VolumeKmsKeyId='string',
  156. # # VpcConfig={
  157. # # 'SecurityGroupIds': ['string', ],
  158. # # 'Subnets': ['string', ]
  159. # # },
  160. # Mode='MULTI_CLASS' # 'MULTI_LABEL'
  161. # )
  162. # except Exception as e:
  163. # return response.json(404, repr('模型正在训练或已存在'))
  164. # else:
  165. # print(response1)
  166. # return response.json(0)
  167. #
  168. # def create_endpoint(self, request_dict, response):
  169. # try:
  170. # response1 = NeteaseMail().aws_login().create_endpoint(
  171. # EndpointName='lhq',
  172. # ModelArn='arn:aws:comprehend:us-east-1:697864307463:document-classifier/lhq',
  173. # # Document classifier arn
  174. # DesiredInferenceUnits=1,
  175. # # ClientRequestToken='string',
  176. # # Tags=[{'Key': 'string','Value': 'string'},]
  177. # )
  178. # except Exception as e:
  179. # return response.json(404, repr('模型正在训练或不存在,请等待模型训练完再创建端点'))
  180. # else:
  181. # print(response1)
  182. # return response.json(0)
  183. #
  184. # def describe_document_classifier(self, request_dict, response):
  185. # try:
  186. # # classifier_name = request_dict.get('classifier_name', None)
  187. # a = NeteaseMail().aws_arn()[0]
  188. # # classifier_arn = a + str(classifier_name)
  189. # classifier_arn = a + 'lhq'
  190. # response1 = NeteaseMail().aws_login().describe_document_classifier(
  191. # DocumentClassifierArn=classifier_arn
  192. # )
  193. # a1 = {"Status": response1['DocumentClassifierProperties']['Status']}
  194. # except Exception as e:
  195. # return response.json(404, repr('模型不存在'))
  196. # else:
  197. # return response.json(0, a1)
  198. #
  199. # def describe_endpoint(self, request_dict, response):
  200. # try:
  201. # # endpoint_name = request_dict.get('endpoint_name', None)
  202. # a = NeteaseMail().aws_arn()[1]
  203. # # endpoint_arn = a + str(endpoint_name)
  204. # endpoint_arn = a + 'lhq'
  205. # response1 = NeteaseMail().aws_login().describe_endpoint(
  206. # EndpointArn=endpoint_arn
  207. # )
  208. # a1 = {"Status": response1['EndpointProperties']['Status']}
  209. # except Exception as e:
  210. # return response.json(404, repr('端点不存在'))
  211. # else:
  212. # return response.json(0, a1)
  213. #
  214. # def delete_endpoint(self, request_dict, response):
  215. # try:
  216. # # endpoint_name = request_dict.get('endpoint_name', None)
  217. # a = NeteaseMail().aws_arn()[1]
  218. # # endpoint_arn = a + str(endpoint_name)
  219. # endpoint_arn = a + 'lhq'
  220. # response1 = NeteaseMail().aws_login().delete_endpoint(
  221. # EndpointArn=endpoint_arn)
  222. # except Exception as e:
  223. # return response.json(404, repr('端点不存在或正在删除中'))
  224. # else:
  225. # return response.json(0)
  226. #
  227. # def delete_document_classifier(self, request_dict, response):
  228. # try:
  229. # # classifier_name = request_dict.get('classifier_name', None)
  230. # a = NeteaseMail().aws_arn()[0]
  231. # # classifier_arn = a + str(classifier_name)
  232. # classifier_arn = a + 'lhq'
  233. # response1 = NeteaseMail().aws_login().delete_document_classifier(
  234. # DocumentClassifierArn=classifier_arn)
  235. # except Exception as e:
  236. # return response.json(404, repr('模型不存在或正在删除中'))
  237. # else:
  238. # return response.json(0)
  239. #
  240. # def get_document_classifier(self, request_dict, response):
  241. # ec_qs = EmailConfigModel.objects.filter(emailtag=1) # 获取总邮件
  242. # emailsum = 0
  243. # replyemailsum = 0
  244. # for ec in ec_qs:
  245. # email_server = NeteaseMail().loginEmail(ec.emailserver, ec.fromaddr, ec.password, ec.emailserverport)
  246. # subject_list, from_list, bodydata_list, uid_list, results, email_body, fujian_list, image_list = NeteaseMail().getEmailContext(
  247. # email_server)
  248. # emailsum += len(results)
  249. # print("标题:", len(subject_list), subject_list)
  250. # print("发件人:", len(from_list), from_list)
  251. # print("邮件内容:", len(bodydata_list), bodydata_list)
  252. # print("邮件uid:", len(uid_list), uid_list)
  253. # print("附件判断:", len(fujian_list), fujian_list)
  254. # print('图片数量:', len(image_list), image_list)
  255. # if not bodydata_list:
  256. # print("邮箱内无未读邮件")
  257. # else:
  258. # print('成功拿到邮件数据')
  259. # for i, v in enumerate(subject_list):
  260. # print('-------------------------------------------------------------------------')
  261. # print("邮件标题:", subject_list[i])
  262. # if not bodydata_list[i] or not subject_list[i]:
  263. # print("邮件无正文或无标题,不进行回复")
  264. # continue
  265. #
  266. # if fujian_list[i] == 'have':
  267. # reply_model = bodydata_list[i]
  268. # image = image_list
  269. # shoujianren = 'liehaoquan2021@163.com'
  270. # fajianren = from_list[i].split()[1]
  271. # NeteaseMail().sentemail(None, uid_list[i],reply_model, image, subject_list[i], fajianren, shoujianren,
  272. # ec.fromaddr, ec.password)
  273. # file_path = os.path.join(os.path.dirname(__file__) + '/../emailtrainfile/image/')
  274. # shutil.rmtree(file_path)
  275. # os.mkdir(file_path)
  276. # print('邮件含有图片或附件,已转发到zendesk')
  277. # email_server.set_flags(uid_list[i], b'\\Seen', silent=False)
  278. # continue
  279. #
  280. # response1 = NeteaseMail().aws_login().classify_document(
  281. # Text=bodydata_list[i].split('\n\n\n\n\n')[0],
  282. # EndpointArn='arn:aws:comprehend:us-east-1:697864307463:document-classifier-endpoint/lhq'
  283. # # EndpointArn='arn:aws:comprehend:us-east-1:697864307463:document-classifier-endpoint/test'
  284. # )
  285. # # print(response.index(max(response['Classes']['Score'])))
  286. # nums = []
  287. # print('emailsort:')
  288. # for cls in response1['Classes']:
  289. # print(cls['Name'])
  290. # nums.append(cls['Score'])
  291. # print("识别种类及命中率:", response1['Classes'][nums.index(max(nums))])
  292. # if response1['Classes'][nums.index(max(nums))]['Score'] >= 0.95:
  293. # reply_sort = EmailSortModel.objects.filter(
  294. # sort=response1['Classes'][nums.index(max(nums))]['Name']).values('autoreplymodel')
  295. # if reply_sort.exists():
  296. # reply_model = reply_sort[0]['autoreplymodel'] + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + \
  297. # 'At' + email_body[i]['Date'] + from_list[i].split()[0] + \
  298. # from_list[i].split()[1] + \
  299. # 'wrote:\n ' + bodydata_list[i]
  300. # print(reply_model)
  301. # shoujianren = from_list[i].split()[1].replace('<', '').replace('>', '')
  302. # a = NeteaseMail().sentemail(None, uid_list[i], reply_model, None, subject_list[i], ec.fromaddr, shoujianren,
  303. # ec.fromaddr, ec.password)
  304. # email_server.set_flags(uid_list[i], b'\\Seen', silent=False)
  305. # if a == 'fail':
  306. # print("此邮件被判定为垃圾邮件,不进行回复")
  307. # email_server.remove_flags(uid_list[i], b'\\Seen', silent=False)
  308. # else:
  309. # replyemailsum += a
  310. # else:
  311. # print("无此转发分类,不进行回复")
  312. # else:
  313. # reply_model = bodydata_list[i]
  314. # shoujianren = 'liehaoquan2021@163.com'
  315. # fajianren = from_list[i].split()[1]
  316. # NeteaseMail().sentemail(reply_model, None, subject_list[i], fajianren, shoujianren,
  317. # ec.fromaddr, ec.password)
  318. # email_server.set_flags(uid_list[i], b'\\Seen', silent=False)
  319. # print("命中率低于0.95,已转发到zendesk")
  320. # # es_qs = EmailSortModel.objects.filter(sort=response1['Classes'][nums.index(max(nums))]['Name'])
  321. # # if es_qs.exists():
  322. # # uemail_qs = EmailConfigModel.objects.filter(userid=es_qs[0]['userid'], langconfig__langcode='en')
  323. # # if uemail_qs.exists():
  324. # # for uem in uemail_qs:
  325. # # NeteaseMail().sentemail(bodydata_list[i], subject_list[i], from_list[i], uem.fromaddr, ec.fromaddr, ec.password)
  326. # NeteaseMail().closeEmail(email_server)
  327. # emaildict = {
  328. # "邮箱内获取到未读邮件数": emailsum,
  329. # "已自动回复的邮件数": replyemailsum
  330. # }
  331. # return response.json(0, emaildict)
  332. #
  333. # def email(self, request_dict, response):
  334. # from imapclient import IMAPClient
  335. # import email
  336. # import time
  337. # import email.parser
  338. # from nntplib import decode_header
  339. # from email.header import Header
  340. # import chardet
  341. #
  342. # ec_qs = EmailConfigModel.objects.filter(emailtag=1) # 获取总邮件
  343. # for ec in ec_qs:
  344. # global a, email_server
  345. #
  346. # #邮件登录
  347. # try:
  348. # email_server = IMAPClient(ec.emailserver, ssl=True, port=ec.emailserverport)
  349. # # email_server = imaplib.IMAP4_SSL(IMAP_SERVER, 993) # 网易企业邮箱服务器及SSL端口
  350. # print("imap4 服务器连接成功")
  351. # email_server.login(ec.fromaddr, ec.password)
  352. # email_server.id_({"name": "IMAPClient", "version": "2.1.0"})
  353. # # email_server.login(FROM_ADDR, PASSWORD)
  354. # print("imap4 (%s)账号密码正确,登录成功" % ec.fromaddr)
  355. # except:
  356. # print("imap4 服务器连接失败")
  357. #
  358. # email_server.select_folder('INBOX')
  359. # results = email_server.search('UNSEEN') # 读取未读邮件
  360. # print("邮箱内获取到未读邮件:", len(results))
  361. # subject_list = []
  362. # from_list = []
  363. # uid_list = []
  364. # email_body = []
  365. # fujian_list = []
  366. # image_list = []
  367. # plain_list = []
  368. # html_list = []
  369. # a = 0
  370. #
  371. # # 对每一封邮件进行内容解析
  372. # for uid in results:
  373. # msgdict = email_server.fetch(uid, ['Body[]', 'ENVELOPE'], '(RFC822)')
  374. # mailbody = msgdict[uid][b'BODY[]']
  375. # envelope = msgdict[uid][b'ENVELOPE']
  376. # message = email.message_from_bytes(mailbody)
  377. # text = message.as_string()
  378. # body = email.parser.Parser().parsestr(text)
  379. #
  380. # nowtime = time.asctime(time.localtime(time.time()))
  381. # nowmonth = nowtime.split()[1]
  382. # nowday = nowtime.split()[2]
  383. # emailmonth = body['Date'].split()[2]
  384. # emailday = body['Date'].split()[1]
  385. # print('email',emailmonth,emailday)
  386. # print('now', nowmonth, nowday)
  387. # # if emailmonth != nowmonth or emailday != nowday:
  388. # # email_server.remove_flags(uid, b'\\Seen', silent=False)
  389. # # continue
  390. #
  391. # email_body.append(body)
  392. # subject_list.append(decode_header(body['Subject']))
  393. # from_list.append(decode_header(body['from']))
  394. # uid_list.append(uid)
  395. # print("标题:", subject_list)
  396. #
  397. # attlist = {}
  398. # try:
  399. # for part in body.walk():
  400. # if not part.is_multipart():
  401. # file = part.get_filename() # 附件名
  402. # if file:
  403. # a = 1
  404. # filename = email.header.decode_header(file)[0][0] # 附件名
  405. # charset = email.header.decode_header(file)[0][1] # 编码
  406. # if part.get_all("Content-ID"):
  407. # content_id = part.get_all("Content-ID")[0][1:-1]
  408. # else:
  409. # content_id = "" # 附件ID,也就是邮件源码里面的cid
  410. # ''' 多个附件时将附件名和ID对应保存到dict里面,后面将正文中的cid替换为本地保存路径 '''
  411. # attlist[content_id] = filename
  412. # ''' 附件文件名为中文或有编码的时候要进行转码 '''
  413. # if str(charset) != "None":
  414. # filename = filename.decode(charset)
  415. # filedata = part.get_payload(decode=True) # 附件内容
  416. # ''' 把附件写到文件里面,附件一定要用wb打开,二进制 '''
  417. # file_path = os.path.join(os.path.dirname(__file__) + '/../emailtrainfile/image/')
  418. # image_list.append(file_path + filename)
  419. # with open(file_path + filename, "wb") as fw:
  420. # fw.write(filedata)
  421. # fw.close()
  422. #
  423. # elif part.get_content_type() == 'text/plain':
  424. # con3 = part.get_payload(decode=True).strip()
  425. # print('plain:', chardet.detect(con3))
  426. # if chardet.detect(con3)['encoding'] == 'utf-8':
  427. # plain_list.append(con3.decode('utf-8'))
  428. # continue
  429. # # if chardet.detect(con3)['encoding'] == 'ISO-8859-1':
  430. # # plain_list.append(con3.decode('ISO-8859-1')) # .decode('gbk').encode('utf8')
  431. # # continue
  432. # else:
  433. # plain_list.append(con3.decode('gbk'))
  434. # continue
  435. # elif part.get_content_type() == 'text/html':
  436. # con3 = part.get_payload(decode=True).strip()
  437. # print('html', chardet.detect(con3))
  438. # if chardet.detect(con3)['encoding'] == 'utf-8':
  439. # html_list.append(con3.decode('utf-8'))
  440. # continue
  441. # # if chardet.detect(con3)['encoding'] == 'ISO-8859-1':
  442. # # html_list.append(con3.decode('ISO-8859-1'))
  443. # # continue
  444. # else:
  445. # html_list.append(con3.decode('gbk'))
  446. # continue
  447. # except Exception as e:
  448. # print(e)
  449. # print('获取邮件内容失败')
  450. # email_server.remove_flags(uid, b'\\Seen', silent=False)
  451. # subject_list.clear()
  452. # from_list.clear()
  453. # uid_list.clear()
  454. # email_body.clear()
  455. # fujian_list.clear()
  456. # image_list.clear()
  457. # plain_list.clear()
  458. # html_list.clear()
  459. # print('-------------------------------------------------------------------------')
  460. # continue
  461. #
  462. #
  463. # if a == 1:
  464. # fujian_list.append('have')
  465. # a = 0
  466. # else:
  467. # fujian_list.append('none')
  468. # email_server.remove_flags(uid, b'\\Seen', silent=False)
  469. # print("发件人:", from_list)
  470. # print("邮件内容:")
  471. # print("plain形式:",plain_list)
  472. # print("html形式:",html_list)
  473. # print("邮件uid:", uid_list)
  474. # print("邮件是否含有图片或附件:", fujian_list)
  475. # print('图片或附件名字:', image_list)
  476. # if not html_list and not plain_list:
  477. # print("邮箱内无未读邮件")
  478. # else:
  479. # print('成功拿到邮件数据')
  480. #
  481. # # 对邮件类型进行判断处理
  482. # for i, v in enumerate(uid_list):
  483. # print("邮件标题:", subject_list[i])
  484. # if html_list and not plain_list:
  485. # print('此邮件仅含有html')
  486. # reply_model = html_list[i]
  487. # fajianren = from_list[i]
  488. # shoujianren = 'liehaoquan2021@163.com' # 写zendesk邮箱
  489. # NeteaseMail().sentemail(email_server, uid_list[i],reply_model, None, subject_list[i], fajianren, shoujianren,
  490. # ec.fromaddr, ec.password)
  491. # print('-------------------------------------------------------------------------')
  492. # break
  493. #
  494. # if plain_list[i] == '':
  495. # print('此邮件无正文')
  496. # reply_model = ''
  497. # fajianren = from_list[i]
  498. # shoujianren = 'liehaoquan2021@163.com' # 写zendesk邮箱
  499. # NeteaseMail().sentemail(email_server, uid_list[i],reply_model, None, subject_list[i], fajianren, shoujianren,
  500. # ec.fromaddr, ec.password)
  501. # print('-------------------------------------------------------------------------')
  502. # break
  503. #
  504. # if not subject_list[i]:
  505. # print('此邮件无主题')
  506. # if html_list and not plain_list:
  507. # print('此邮件无主题且仅含有html')
  508. # reply_model = html_list[i]
  509. # fajianren = from_list[i]
  510. # shoujianren = 'liehaoquan2021@163.com' # 写zendesk邮箱
  511. # biaoti = '(无主题)'
  512. # NeteaseMail().sentemail(email_server, uid_list[i],reply_model, None, biaoti, fajianren, shoujianren,
  513. # ec.fromaddr, ec.password)
  514. # print('-------------------------------------------------------------------------')
  515. # break
  516. # elif plain_list:
  517. # print('此邮件无主题且含有正文')
  518. # reply_model = plain_list[i]
  519. # fajianren = from_list[i]
  520. # shoujianren = 'liehaoquan2021@163.com' # 写zendesk邮箱
  521. # biaoti = '(无主题)'
  522. # NeteaseMail().sentemail(email_server, uid_list[i], reply_model, None, biaoti, fajianren, shoujianren,
  523. # ec.fromaddr, ec.password)
  524. # print('-------------------------------------------------------------------------')
  525. # break
  526. #
  527. # # 处理含有图片或附件的邮件
  528. # if fujian_list[i] == 'have':
  529. # print('此邮件含有图片或附件或视频或压缩包')
  530. # reply_model = html_list[i]
  531. # image = image_list
  532. # fajianren = from_list[i]
  533. # shoujianren = 'liehaoquan2021@163.com' # 写zendesk邮箱
  534. # NeteaseMail().sentemail(email_server, uid_list[i], reply_model, image, subject_list[i], fajianren, shoujianren,
  535. # ec.fromaddr, ec.password)
  536. # file_path = os.path.join(os.path.dirname(__file__) + '/../emailtrainfile/image/')
  537. # shutil.rmtree(file_path)
  538. # os.mkdir(file_path)
  539. # print('-------------------------------------------------------------------------')
  540. # break
  541. #
  542. # if fujian_list[i] == 'none':
  543. # # 处理无图片或附件的邮件
  544. # # 进行种类判断
  545. # print("识别内容:", plain_list[i].split('\n\n')[0]) # plain
  546. # if len(plain_list[i].split('\n\n\n')[0]) < 1000:
  547. # response1 = NeteaseMail().aws_login().classify_document(
  548. # Text=plain_list[i].split('\n\n\n')[0],
  549. # EndpointArn='arn:aws:comprehend:us-east-1:697864307463:document-classifier-endpoint/lhq'
  550. # # EndpointArn='arn:aws:comprehend:us-east-1:697864307463:document-classifier-endpoint/test'
  551. # )
  552. # # print(response.index(max(response['Classes']['Score'])))
  553. # nums = []
  554. # print('emailsort:')
  555. # for cls in response1['Classes']:
  556. # print(cls['Name'])
  557. # nums.append(cls['Score'])
  558. # print("识别种类及命中率:", response1['Classes'][nums.index(max(nums))])
  559. # # 命中率高于0.95时
  560. # if response1['Classes'][nums.index(max(nums))]['Score'] >= 0.95:
  561. # reply_sort = EmailSortModel.objects.filter(
  562. # sort=response1['Classes'][nums.index(max(nums))]['Name']).values('autoreplymodel')
  563. # if reply_sort.exists():
  564. # reply_model = reply_sort[0]['autoreplymodel'] + '\n' + '\n' + '\n' + '\n' + '\n' + '\n' + \
  565. # 'At' + email_body[i]['Date'] + str(from_list[i].split()[0:-1]) + \
  566. # from_list[i].split()[-1] + \
  567. # 'wrote:\n ' + plain_list[i]
  568. # print(reply_model)
  569. # shoujianren = from_list[i].split()[1].replace('<', '').replace('>', '')
  570. # NeteaseMail().sentemail(email_server, uid_list[i], reply_model, None, subject_list[i], ec.fromaddr, shoujianren,
  571. # ec.fromaddr, ec.password)
  572. # print('-------------------------------------------------------------------------')
  573. # break
  574. # else:
  575. # print("此邮件命中率低于0.95")
  576. # reply_model = html_list[i] # html
  577. # fajianren = from_list[i]
  578. # print(fujian_list)
  579. # shoujianren = 'liehaoquan2021@163.com' # 写zendesk邮箱
  580. # NeteaseMail().sentemail(email_server, uid_list[i], reply_model, None, subject_list[i],
  581. # fajianren, shoujianren,
  582. # ec.fromaddr, ec.password)
  583. # print('-------------------------------------------------------------------------')
  584. # break
  585. #
  586. # else:
  587. # print("识别内容过长")
  588. # reply_model = html_list[i] # html
  589. # fajianren = from_list[i]
  590. # print(fujian_list)
  591. # shoujianren = 'liehaoquan2021@163.com' # 写zendesk邮箱
  592. # NeteaseMail().sentemail(email_server, uid_list[i], reply_model, None, subject_list[i], fajianren, shoujianren,
  593. # ec.fromaddr, ec.password)
  594. # print('-------------------------------------------------------------------------')
  595. # break
  596. # subject_list.clear()
  597. # from_list.clear()
  598. # uid_list.clear()
  599. # email_body.clear()
  600. # fujian_list.clear()
  601. # image_list.clear()
  602. # plain_list.clear()
  603. # html_list.clear()
  604. # NeteaseMail().closeEmail(email_server)
  605. # return response.json(0)
  606. #
  607. # # if __name__ == '__main__':
  608. # # ComprehendAction().get_document_classifier()