下午好。我刚刚创建了一个代码,它接受一个图像id并返回对该图像的评论。我有大约100图像的文件夹,我想创建一个代码,将拿起每一个这些图像一个接一个,并返回评论。我创建的代码包含在下面:
def retrieve_comments(service, file_id):
"""Retrieve a list of comments.
Args:
service: Drive API service instance.
file_id: ID of the file to retrieve comments for.
Returns:
List of comments.'https://www.googleapis.com/auth/drive.metadata.readonly'
"""
try:
comments = service.comments().list(fileId=file_id).execute()
return comments.get('items', [])
except errors.HttpError as error:
print('An error occurred: %s' % error)
return None
SCOPES = ['https://www.googleapis.com/auth/drive', 'https://www.googleapis.com/auth/drive.metadata.readonly']
credentials = Credentials.from_authorized_user_file('token.json', SCOPES)
service = build('drive', 'v2', credentials=credentials)
comment = retrieve_comments(service, '18h-MkLdhqeHZ45pIiDZ3G5RkqJm6Pwh0')[0]['content']
为了检索文件夹内的图像,您必须发出files.list
请求并提供q
查询参数。
q参数看起来像这样:
q = 'IMAGE_FOLDER_ID' in parents and mimeType = 'image/png' # or other mime type
和请求:
GET https://www.googleapis.com/drive/v3/files?q='IMAGE_FOLDER_ID'%20in%20parents%20and%20mimeType%20%3D%20'image%2Fpng'
至于将其翻译成Python:
response = drive_service.files().list(q= "'IMAGE_FOLDER_ID' in parents and mimeType = 'image/png'").execute()
for file in response.get('files', []):
# retrieve the comments for each file
参考Drive API v3 files.list;
驱动API v3搜索文件和文件夹;
驱动API v3搜索查询条件。