如何使用gmail api保存按月发送的邮件并按时间划分邮件,并将输出保存到csv或将其转换为df?

时间:2019-01-22 15:51:24

标签: python python-3.x pandas gmail gmail-api

下面的代码使我们从发送消息的时间段起每30天计算一次消息。

此代码为我们提供:(详细信息)

1.Amazon首先以特定阶段(这里是第一顺序)将邮件发送到我的邮件。

2。将纪元格式转换为时间日期并使用timedelta并获取30天间隔内发送的邮件数。

此代码的输出将如下所示:

Amazon first order:

1534476682000

Amazon total orders between 2018-08-01 and 2018-09-01: 20

Amazon total orders between 2018-09-01 and 2018-10-01: 11

Amazon total orders between 2018-10-01 and 2018-11-01: 15

Amazon total orders between 2018-11-01 and 2018-12-01: 7

Amazon total orders between 2018-12-01 and 2019-01-01: 19

Amazon total orders between 2019-01-01 and 2019-02-01: 23

Amazon total orders between 2019-02-01 and 2019-03-01: 12

代码:

#amazonfirstorder
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
from dateutil.relativedelta import relativedelta
from datetime import datetime


SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'

def main():

    store = file.Storage('token.json')
    creds = store.get()
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
    creds = tools.run_flow(flow, store)
service = build('gmail', 'v1', http=creds.authorize(Http()))

results = service.users().messages().list(userId='me', q='from:auto-confirm@amazon.in subject:(your amazon.in order of )',labelIds = ['INBOX']).execute()

messages = results.get('messages', [])


print('\nFilpkart first order:')
if not messages:
    print (" ")
else:
    print (" ")

    msg = service.users().messages().get(userId='me', id=messages[-1]['id']).execute()
    #print(msg['snippet'])
    a=(msg['internalDate'])
    ts = int(a)
    ts /= 1000
    year=int(datetime.utcfromtimestamp(ts).strftime('%Y'))
    month=int(datetime.utcfromtimestamp(ts).strftime('%m'))
    #print(year)
    #print(month)
    print(msg['internalDate'])

    log_results = []
    start_date = datetime(year,month,1)
#start_date = datetime(2016,1,1)
    end_date = datetime.today()
    increment = relativedelta(months=1)
    target_date = start_date + increment

    while target_date <= end_date:

        timestamp_after = int(start_date.timestamp())  # timestamp of start day
        timestamp_before = int(target_date.timestamp())  # timestamp of start day + 30 days

        query = f'from:(auto-confirm@amazon.in) subject:(your amazon.in order of ) after:{timestamp_after} before:{timestamp_before}'
        results = service.users().messages().list(userId='me', q=query, labelIds=['INBOX']).execute()

        messages = results.get('messages', [])
        orders = len(messages)
        start_date_str = start_date.strftime('%Y-%m-%d')
        target_date_str = target_date.strftime('%Y-%m-%d')
        print(f"\nFlipkart total orders between {start_date.strftime('%Y-%m-%d')} and {target_date.strftime('%Y-%m-%d')}: {orders}")

        log_results.append(dict(start=start_date_str, end=target_date_str, orders=orders))

    # update interval
        start_date += increment
        target_date += increment

    return log_results



if __name__ == '__main__':
    log_results = main()    

现在我有两个问题:

第一

如何将该代码的输出保存到csv文件中。

第二

以上代码为我们提供了30天的邮件计数,我需要的是我需要按月将中午12点之前和按月中午12点之后收到的邮件计数保存在csv中。

我需要第二个问题的输出

Amazon total orders between 2018-09-01 and 2018-10-01 before 12:00 PM : 11

Amazon total orders between 2018-10-01 and 2018-11-01 before 12:00 PM : 15

Amazon total orders between 2018-11-01 and 2018-12-01 before 12:00 PM : 7

Amazon total orders between 2018-12-01 and 2019-01-01 before 12:00 PM : 19

Amazon total orders between 2018-09-01 and 2018-10-01 after 12:00 PM : 3

Amazon total orders between 2018-10-01 and 2018-11-01 after 12:00 PM : 6

Amazon total orders between 2018-11-01 and 2018-12-01 after 12:00 PM : 88

Amazon total orders between 2018-12-01 and 2019-01-01 after 12:00 PM : 26

2 个答案:

答案 0 :(得分:2)

您只需要按所需的时间间隔遍历日期即可。

下面的代码从特定时间段检索用户的消息,例如月份消息计数。

您将需要帮助使其自动化以每30天检索一次邮件计数。

例如,此代码获取2016年1月1日至2016年1月30日之间的消息。

因此,从2016年1月1日到2019年1月1日,您将需要以30天的定期间隔对其进行自动化。

from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import time
from dateutil.relativedelta import relativedelta
from datetime import datetime

SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'    
def main():
    store = file.Storage('token.json')
    creds = store.get() 
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
        creds = tools.run_flow(flow, store)
    service = build('gmail', 'v1', http=creds.authorize(Http()))
    end_date = datetime(2019, 1, 1)
    interval = relativedelta(months=1)
    current = datetime(2016, 1, 1)              # init to the start date 
    while current < end_date + interval:
         after = current.timestamp()
         before = (current + interval).timestamp()

         query = 'from:(auto-confirm@amazon.in) subject:(your amazon.in order of ) after:{} before:{}'.format(after, before)
         results = service.users().messages().list(userId='me', q=query, labelIds = ['INBOX']).execute()

         messages = results.get('messages', [])
         print("\namazon total orders in {}: {}".format(current.strftime('%B %Y'), len(messages)))
         current += interval    

if __name__ == '__main__':
    main()  

答案 1 :(得分:2)

类似于已经提出的建议,但是在这种情况下,您将计算增量为恰好一个月而不是30天(请参见使用relativedelta而不是timedelta):

from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
from dateutil.relativedelta import relativedelta
from datetime import datetime

SCOPES = 'https://www.googleapis.com/auth/gmail.readonly'

def main():

    store = file.Storage('token.json')
    creds = store.get()
    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
        creds = tools.run_flow(flow, store)
    service = build('gmail', 'v1', http=creds.authorize(Http()))

    log_results = []

    start_date = datetime(2016, 1, 1)
    end_date = datetime.today()
    increment = relativedelta(months=1)
    target_date = start_date + increment

    while target_date <= end_date:

        timestamp_after = int(start_date.timestamp())  # timestamp of start day
        timestamp_before = int(target_date.timestamp())  # timestamp of start day + 30 days

        query = f'from:(auto-confirm@amazon.in) subject:(your amazon.in order of ) after:{timestamp_after} before:{timestamp_before}'
        results = service.users().messages().list(userId='me', q=query, labelIds=['INBOX']).execute()

        messages = results.get('messages', [])
        orders = len(messages)
        start_date_str = start_date.strftime('%Y-%m-%d')
        target_date_str = target_date.strftime('%Y-%m-%d')
        print(f"\nAmazon total orders between {start_date.strftime('%Y-%m-%d')} and {target_date.strftime('%Y-%m-%d')}: {orders}")

        log_results.append(dict(start=start_date_str, end=target_date_str, orders=orders))

        # update interval
        start_date += increment
        target_date += increment

    return log_results



if __name__ == '__main__':
    log_results = main()
    # Write to csv
    import pandas as pd
    df = pd.DataFrame(log_results)
    df.to_csv('orders.csv')