2015-01-27 1 views
1

Так вот код ...Пытаясь получить электронную почту за последние 3 дня от перспективы, она почти работает, но она пропускает электронные письма, любая идея почему? [Код внутри]

import win32com.client, datetime 
from datetime import timedelta 

def checkTime(): 
    date_filter_unformated = datetime.date.today() - timedelta(days=3) 
    date_filter = date_filter_unformated.strftime("%m/%d/%y %I:%M:%S") 
    current_message = all_messages.GetPrevious() 
    message_time = current_message.ReceivedTime 
    df_list = list(date_filter) 
    mt_list = list(str(message_time)) 
    df_month, mt_month = int(''.join([df_list[0],df_list[1]])), int(''.join([mt_list[0],mt_list[1]])) 
    df_day, mt_day = int(''.join([df_list[3],df_list[4]])), int(''.join([mt_list[3],mt_list[4]])) 
    df_year, mt_year = int(''.join([df_list[6],df_list[7]])), int(''.join([mt_list[6],mt_list[7]])) 
    if mt_year < df_year: 
     return "Old" 
    elif mt_year == df_year: 
     if mt_month < df_month: 
      return "Old" 
     elif mt_month == df_month: 
      if mt_day < df_day: 
       return "Old" 
      else: 
       CurrentMessage(current_message) 
       return "Pass" 
     elif mt_month > df_month: 
      CurrentMessage(current_message) 
      return "Pass" 

def CurrentMessage(cm): 
    print cm.Sender, cm.ReceivedTime 
    # 

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") 
inbox = outlook.GetDefaultFolder(6) # "6" refers to the index of a folder - in this case, 
            # the inbox. Can change that number to reference 
            # any other folder 

all_messages = inbox.Items 
current_message = all_messages.GetLast() 

while True: 
    if checkTime() == "Pass": 
     continue 
    if checkTime() == "Old": 
     break 

Так он должен вернуть имя отправителя и даты за полученную из всех электронных писем в течение последних 3-х дней, но вместо этого, он возвращает некоторые из писем от 26-е (сегодня), а затем все электронные письма с 23-го (3 дня назад), без которых между ними нет. Я понятия не имею, что заставляет это делать. Какие-либо предложения?

ответ

3

Вы считаете, что сообщения в коллекции Items отсортированы. Коллекция будет сортироваться в определенном порядке только после вызова Items.Sort (all_messages.Sort("[ReceivedTime]", true) в вашем случае).