我正在尝试使用apiclient
and库获取我的最后一封邮件oauth2client
(从这里快速入门:https ://developers.google.com/gmail/api/quickstart/quickstart-python )。
我目前能够检索我所有的最后一封邮件,包括促销邮件和社交网络邮件(我不在乎)。
脚本如下(如果您发现一些错误或更快的方法,请告诉我):
# ... credentials mechanism identical to the quickstart one
gmail_service = build('gmail', 'v1', http=http
# Get the last mails :
last_mails = gmail_service.users().messages().list(userId="me", labelIds=["INBOX"]).execute()
for mailIds in last_mails["messages"]:
all_infos = gmail_service.users().messages().get(userId="me", id=mailIds["id"]).get(userId="me", id=mailIds["id"]).execute()
abstract = all_infos["snippet"]
# Changing json structure
headers = dict([(x["name"], x["value"]) for x in all_infos["payload"]["headers"]])
dest = headers["Delivered-To"]
source = headers["From"]
title = headers["Subject"]
print "Mail : %s\nFrom : %s\nTo : %s\nAbst : %s\n-------" % (
title, source, dest, abstract
)
所以我的目标是从列表中删除促销和社交网络邮件。有什么办法吗?