-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgoogle-drive-list-shared.py
65 lines (53 loc) · 1.99 KB
/
google-drive-list-shared.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
62
63
64
65
#!/usr/bin/env python
from __future__ import print_function
import time
import ast
from pprint import pprint
from apiclient import discovery
from httplib2 import Http
from oauth2client import file, client, tools
class DictQuery(dict):
def get(self, path, default = None):
keys = path.split("/")
val = None
for key in keys:
if val:
if isinstance(val, list):
val = [ v.get(key, default) if v else None for v in val]
else:
val = val.get(key, default)
else:
val = dict.get(self, key, default)
if not val:
break;
return val
SCOPES = 'https://www.googleapis.com/auth/drive.readonly.metadata'
store = file.Storage('storage.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('client_id.json', SCOPES)
creds = tools.run_flow(flow, store)
service = discovery.build('drive', 'v3', http=creds.authorize(Http()))
results = service.files().list(
pageSize=1000,
fields="nextPageToken, files(name, permissions/emailAddress, owners/emailAddress)").execute()
token = results.get('nextPageToken', None)
items = results.get('files', [])
while token is not None:
results = service.files().list(
pageSize=1000,
pageToken=token,
fields="nextPageToken, files(name, permissions/emailAddress, owners/emailAddress)").execute()
# Store the new nextPageToken on each loop iteration
token = results.get('nextPageToken', None)
# Append the next set of results to the items variable
items.extend(results.get('files', []))
# The Google Drive does not return valid JSON because the property
# names are not enclosed in double quotes, they are enclosed in
# single quotes. So, use Python AST to convert the string to an
# iterable list.
items_dict = ast.literal_eval(str(items))
# Iterate through and pretty print as JSON all the selected fields
# (name, owner email, and email addresses that files have been shared with).
for i in range(len(items_dict)):
pprint(items_dict[i])