Python reads emails of a specified date
Background: Some data is missing in February September. This data is stored in the mailbox and needs to be pulled again. However, there are hundreds of emails each day, 6 or 70 thousand in total, using stat () combined With retr (which ),
Time consumed
Based on the above background, the general idea is:
Obtain a value in the middle of the time range for data recovery using the bipartite method, that is, the number of mails, and traverse the mails at two ends,
Read all required emails
Algorithm functions:
1. the email retrieval time may be sent to Greenwich Mean Time emails, so the corresponding processing is performed here.
[Python] view plaincopy
- DefgetTimeStamp (cn ):
- Messages = mailServer. retr (cn) [1]
- Mail = email. message_from_bytes ('\ n'. encode ('utf-8'). join (messages ))
- Date = email. header. decode_header (mail. get ('date '))
- Utcstr = date [0] [0]. replace ('+ 00:00 ','')
- Globalutcdatetime
- Try:
- Utcdatetime = datetime. datetime. strptime (utcstr, '% a, % d % B % Y % H: % M: % S + 0000 (GMT )')
- Localdatetime = utcdatetime + datetime. timedelta (hours = + 8)
- Localtimestamp = localdatetime. timestamp ()
- Except t:
- Utcdatetime = datetime. datetime. strptime (utcstr, '% a, % d % B % Y % H: % M: % S + 0800 ')
- Localtimestamp = utcdatetime. timestamp ()
- Returnlocaltimestamp
2. bipartite [python] view plaincopy
- Bu_daytimestampyesterdaytimestamp is the timestamp for data population.
[Python] view plaincopy
- Key_nums = 0
- Mid2 = mailCount
- While1 = 1:
- Mid = getTimeStamp (round (mailCount ))
- Mid2 = round (mid2/2)
- Ifmid> bu_daytimestamp:
- MailCount = mailCount-mid2
- Elifmid <yesterdaytimestamp:
- MailCount = mailCount + mid2
- Elifmid <= bu_daytimestampandmid> = yesterdaytimestamp:
- Key_nums = mailCount
- Break
- Print (mailCount)
- Print (key_nums)
Here we only record the algorithm. The subsequent traversal is relatively simple and will not be shown.
Explain the following parameters:
[Python] view plaincopy
- MailServer: logon email address
[Python] view plaincopy
- <Prename = "code" class = "python"> mailCount: Total number of emails, from mailServer. stat () second parameter </pre> <prename = "code" class = "python"> </pre>