-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_question_yash.py
61 lines (54 loc) · 2.17 KB
/
code_question_yash.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from __future__ import print_function
import pickle
import os.path
import io
from googleapiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.http import MediaIoBaseDownload
SCOPES = [
'https://www.googleapis.com/auth/drive.metadata.readonly',
'https://www.googleapis.com/auth/drive.file'
]
def authorize_login_oauth():
store = file.Storage('storage.json')
creds = store.get()
# Add your conditional here. I removed it in order to test different scopes
# You can change 'credentials.json' to 'client_id.json'
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
drive_service = discovery.build('drive', 'v3', http=creds.authorize(Http()))
return drive_service
def get_file_ids(drive_service):
# You can take this out of the loop. I just used it to pull the first file
# I could find on my drive.
results = drive_service.files().list(
pageSize=10,
fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
files_to_download = []
if not items:
print('Nothing here!')
else:
print('Files:')
for item in items:
files_to_download.append((item['name'], item['id']))
print(u'{0} ({1})'.format(item['name'], item['id']))
return files_to_download
def download_file(drive_service, file_id, file_name):
# Using files().get per SO
# --> https://stackoverflow.com/questions/46302540/google-drive-export-non-google-doc-file
request = drive_service.files().get(fileId=file_id)
fh = io.FileIO('downloads/' + file_name, 'wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download %d%%." % int(status.progress() * 100))
if __name__ == '__main__':
drive_service = authorize_login_oauth()
target_files = get_file_ids(drive_service)
for file in target_files:
download_file(drive_service, file[1], file[0])