def fetch(self):
BATCH_MESSAGES = True
before_gdate = datetime.strftime(self.next_date_dt, "%Y/%m/%d")
after_gdate = datetime.strftime(self.date_dt, "%Y/%m/%d")
self.build_service('gmail', 'v1')
query = 'before:%s after:%s' % (before_gdate, after_gdate)
logging.debug(query)
if BATCH_MESSAGES:
# Fetch message IDs
results = self.service.users().messages().list(
userId='me', maxResults=self.limit, q=query).execute()
if results:
ids = [r.get('id') for r in results.get('messages', [])]
if ids:
batch = self.service.new_batch_http_request(
callback=self._handle_gmail_message)
for id in ids:
batch.add(
self.service.users().messages().get(
id=id, userId="me"), request_id=id)
# Blocks, populates self.items
batch.execute(http=self.http_auth)
else:
# Only threads show snippets in Gmail API?
results = self.service.users().threads().list(
userId='me', maxResults=self.limit, fields='threads', q=query).execute()
if results:
self.items = [
Item(
svc=SERVICE.GMAIL,
title=r.get('snippet'),
id=r.get('id'),
type=SERVICE.EMAIL).json() for r in results.get(
'threads',
[])]
return self.items
评论列表
文章目录