Skip to content

Instantly share code, notes, and snippets.

@atbradley
Created January 9, 2020 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atbradley/ee1e211864460554cbba358b780d9672 to your computer and use it in GitHub Desktop.
Save atbradley/ee1e211864460554cbba358b780d9672 to your computer and use it in GitHub Desktop.
List and download files in Google Drive. Utility class for a data dashboard.
#Standard.
import io
#External (`pip install google-api-python-client google-auth-httplib2` should give you these.)
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build
from apiclient.http import MediaIoBaseDownload
import httplib2
class Tool():
def __init__(self, credfile,
scopes=['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']):
"""
Constructor.
Keyword Arguments:
credfile - Path to the JSON credential file for your Service Account.
Get this from the Google Developers Console: https://console.developers.google.com/
scopes - A list of scopes required for your connection. The default
should be fine.
"""
credentials = ServiceAccountCredentials.from_json_keyfile_name(
credfile, scopes=scopes)
self.service = build('drive', 'v3', credentials=credentials)
#If this is in .service now, do I need it?
self.http = credentials.authorize(httplib2.Http())
def listFiles(self, filetype=None, folder=None, count=100, **kwargs):
"""
List files in a Google drive.
Keyword Arguments:
type -- a MIME type. If you're looking for GSuite files, their
types are listed at https://developers.google.com/drive/api/v3/mime-types
folder -- the parent folder to search. Defaults you your entire drive.
count -- Number of files to return. Can range from 1 to 1000. Overrides
kwargs['pageSize'], if that's provided given.
this function doesn't handle pagination.
**kwargs -- additional parameters passed to the files.list API call. See
https://developers.google.com/drive/api/v3/reference/files/list
Also see https://developers.google.com/drive/api/v3/search-files
Returns: a list of file descriptors. Exact contents may vary based on
kwargs['fields']'s value.
"""
qs = []
if filetype:
qs.append("mimeType = '%s'" % filetype)
if folder:
qs.append("'%s' in parents" % folder)
kwargs['pageSize'] = count
response = self.service.files().list(q=' and '.join(qs),
**kwargs).execute()
return response['files']
def downloadGSuite(self, file_id, ftype):
"""
Download a G Suite document from Google Drive
Keyword Arguments:
file_id: Google's file id for the file. You could get it by querying the API
(maybe using listFiles()), or copy it from the address bar when looking at
the file in your browser.
ftype: The mime type of the export format you want. There's a list of
valid ones for each type of G Suite file at
https://developers.google.com/drive/api/v3/ref-export-formats
Returns: an io.BytesIO object. This is a "file-like object" you can read from.
"""
request = self.service.files().export_media(fileId=file_id,
mimeType=ftype)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
return fh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment