因此,我正在编写一个脚本来查找给定Gmail的所有看不见的电子邮件,并按发件人的地址将它们分组为键,将UID分组为值。
但是对这些电子邮件进行分组需要花费大量时间,有什么方法可以更快地对其进行分组,CPU在这里起着重要的作用吗?因为我只有4GB。
import imapclient, pyzmail,imaplib,pprint,time
imaplib._MAXLINE = 10000000
imapobj = imapclient.IMAPClient('imap.gmail.com',ssl = True)
imapobj.login('MAIL ID','PASSWORD')
print('Log in Successful...')
imapobj.select_folder('INBOX',readonly=False)
print('INBOX checking...')
uids = imapobj.search(['UNSEEN'])
len_uids = len(uids)
from_dict = {}
print('Grouping....')
time1 = time.time()
iteration = 0
for UID in uids:
iteration += 1
if UID == (len_uids//2):
print('Half done..')
time2 = time.time()
print(round(time2 - time1,1))
rawmessages = imapobj.fetch([UID],['BODY[]','FLAGS'])
message = pyzmail.PyzMessage.factory(rawmessages[UID][b'BODY[]'])
try:
from_dict[message.get_addresses('from')[0][1]].append(UID)
except KeyError:
from_dict.setdefault(message.get_addresses('from')[0][1],[UID])
print(str(UID)+' is grouped.')
print('********Total mails to group is:'+str(len_uids -
iteration)+'********')
pprint.pprint(from_dict)
imapobj.logout()