Python reads emails of a specified date

Source: Internet
Author: User

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
  1. DefgetTimeStamp (cn ):
  2. Messages = mailServer. retr (cn) [1]
  3. Mail = email. message_from_bytes ('\ n'. encode ('utf-8'). join (messages ))
  4. Date = email. header. decode_header (mail. get ('date '))
  5. Utcstr = date [0] [0]. replace ('+ 00:00 ','')
  6. Globalutcdatetime
  7. Try:
  8. Utcdatetime = datetime. datetime. strptime (utcstr, '% a, % d % B % Y % H: % M: % S + 0000 (GMT )')
  9. Localdatetime = utcdatetime + datetime. timedelta (hours = + 8)
  10. Localtimestamp = localdatetime. timestamp ()
  11. Except t:
  12. Utcdatetime = datetime. datetime. strptime (utcstr, '% a, % d % B % Y % H: % M: % S + 0800 ')
  13. Localtimestamp = utcdatetime. timestamp ()
  14. Returnlocaltimestamp

2. bipartite [python] view plaincopy
  1. Bu_daytimestampyesterdaytimestamp is the timestamp for data population.

[Python] view plaincopy
  1. Key_nums = 0
  2. Mid2 = mailCount
  3. While1 = 1:
  4. Mid = getTimeStamp (round (mailCount ))
  5. Mid2 = round (mid2/2)
  6. Ifmid> bu_daytimestamp:
  7. MailCount = mailCount-mid2
  8. Elifmid <yesterdaytimestamp:
  9. MailCount = mailCount + mid2
  10. Elifmid <= bu_daytimestampandmid> = yesterdaytimestamp:
  11. Key_nums = mailCount
  12. Break
  13. Print (mailCount)
  14. 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
  1. MailServer: logon email address
[Python] view plaincopy
  1. <Prename = "code" class = "python"> mailCount: Total number of emails, from mailServer. stat () second parameter </pre> <prename = "code" class = "python"> </pre>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.