【发布时间】:2021-06-29 01:12:44
【问题描述】:
我正在尝试通过收件箱使用 Gmail API 过滤来阅读用户的所有邮件。但是要阅读 16k+ 封邮件大约需要 2 小时。有什么有效的方法吗?
now = datetime.now()
timestamp = math.floor(datetime.timestamp(now))
count = 0
while True:
results = service.users().messages().list(maxResults=50,userId='me',q='in:inbox before:{}'.format(timestamp)).execute()
messages = results.get('messages')
EmailRecepit=[]
if messages==None:
break
for msg in messages:
print("Count",count)
count+=1
# Get the message from its id
txt = service.users().messages().get(userId='me', id=msg['id']).execute()
try:
# Get value of 'payload' from dictionary 'txt'
payload = txt['payload']
headers = payload['headers']
attachment = payload['parts']
for header in headers: # getting the Sender
if header['name'] == 'From':
msg_from = header['value']
name=sender_name(msg_from)#Sender Name Not email
for a in attachment:
if a.get('filename') != '' and len(a.get('filename')) != 0:
document = a.get('filename')
if count % 50==0:
timestamp = math.floor(datetime.timestamp(parser.parse(headers['Date']))
except socket.error as error:
pass
except:
pass
【问题讨论】:
-
您好 Anurodh,添加您正在尝试的代码的一部分...以便我们知道如何为您提供帮助。
标签: python oauth-2.0 gmail-api