Attachment's contentUrl
are not provided in the TimelineItem
metadata, you need to send an authorized request to the mirror.timeline.attachments.get
endpoint to retrieve more information about the attachment:
from apiclient import errors
# ...
def print_attachment_metadata(service, item_id, attachment_id):
"""Print an attachment's metadata
Args:
service: Authorized Mirror service.
item_id: ID of the timeline item the attachment belongs to.
attachment_id: ID of the attachment to print metadata for.
"""
try:
attachment = service.timeline().attachments().get(
itemId=item_id, attachmentId=attachment_id).execute()
print 'Attachment content type: %s' % attachment['contentType']
print 'Attachment content URL: %s' % attachment['contentUrl']
except errors.HttpError, error:
print 'An error occurred: %s' % error
Once you get the attachment's metadata, check for the isProcessingContent
property: it needs to be set to False
in order to retrieve a contentUrl
.
Unfortunately, there are no push notifications for when the property changes value and your service would have to poll using exponential backoff in order to save quota and resources.
From the attachment's metadata, when the contentUrl
is available, you can retrieve the attachment's content like this:
def download_attachment(service, attachment):
"""Download an attachment's content
Args:
service: Authorized Mirror service.
attachment: Attachment's metadata.
Returns:
Attachment's content if successful, None otherwise.
"""
resp, content = service._http.request(attachment['contentUrl'])
if resp.status == 200:
return content
else:
print 'An error occurred: %s' % resp
return None