If you use the Python Quickstart Tutorial for Google Drive API which you can find here, you'll be able to set up your authentication with the API. You can then loop through your Drive and only download jpg
files by specifying the MIMEType of your search to be image/jpeg
.
First, you want to loop through the files on your drive using files: list
:
# Note: folder_id can be parsed from the shared link
def listFiles(service, folder_id):
listOfFiles = []
query = f"'{folder_id}' in parents and mimeType='image/jpeg'"
# Get list of jpg files in shared folder
page_token = None
while True:
response = service.files().list(
q=query,
fields="nextPageToken, files(id, name)",
pageToken=page_token,
includeItemsFromAllDrives=True,
supportsAllDrives=True
).execute()
for file in response.get('files', []):
listOfFiles.append(file)
page_token = response.get('nextPageToken', None)
if page_token is None:
break
return listOfFiles
Then you can download them by using the files: get_media()
method:
import io
from googleapiclient.http import MediaIoBaseDownload
def downloadFiles(service, listOfFiles):
# Download all jpegs
for fileID in listOfFiles:
request = service.files().get_media(fileId=fileID['id'])
fh = io.FileIO(fileID['name'], 'wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Downloading..." + str(fileID['name']))
You can refine your search further by using the q
parameter in listFiles()
as specified here.