-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgdrive.py
More file actions
162 lines (132 loc) · 4.45 KB
/
gdrive.py
File metadata and controls
162 lines (132 loc) · 4.45 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from __future__ import print_function
import httplib2
import os
from apiclient import discovery
import oauth2client
from oauth2client import client
from oauth2client import tools
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
SCOPES = 'https://www.googleapis.com/auth/drive'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Drive API Quickstart'
def get_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'drive-quickstart.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatability with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def get_service():
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v2', http=http)
return service
def files():
"""Get all files in My Drive
Creates a Google Drive API service object and returns file
dictionaries for all files.
"""
service = get_service()
results = service.files().list(maxResults=1000).execute()
return results
class Node:
def __init__(self, info):
self.info = info
self.parents = []
self.children = []
def __str__(self):
return '{}: {}'.format(self.type, self.title)
def __repr__(self):
return str(self)
@property
def type(self):
return 'Folder' if self.is_folder else 'File'
@property
def id(self):
return self.info['id']
@property
def title(self):
return self.info['title']
folder_mimetype = 'application/vnd.google-apps.folder'
@property
def is_folder(self):
return self.info['mimeType'] == Node.folder_mimetype
def has_parent(self, parent):
if self.parents:
if any(p == parent for p in self.parents):
return True
if any(p.has_parent(parent) for p in self.parents):
return True
return False
def get_file_graph():
R = files()
nodes = {'root': Node({'id': '0','title': 'root',
'mimeType': Node.folder_mimetype})}
root = nodes['root']
for r in R['items']:
n = Node(r)
nodes[r['id']] = n
for r in R['items']:
n = nodes[r['id']]
if 'parents' in r:
for p in r['parents']:
if p['isRoot']:
n.parents.append(root)
root.children.append(n)
else:
# if p['id'] not in nodes, then it is a folder not
# owned by this user (and probably not relevant)
if p['id'] in nodes:
parent = nodes[p['id']]
n.parents.append(parent)
parent.children.append(n)
return root
def get_node(root, path):
if path == '/':
parts = []
else:
if path[0] == '/':
path = path[1:]
parts = path.split('/')
node = root
for p in parts:
try:
node = [c for c in node.children if c.title == p][0]
except IndexError:
raise IndexError("No node with that path")
return node
def walk(root, path):
node = get_node(root, path)
if not node.is_folder:
yield node.parents[0],[],[node]
raise StopIteration
stack = [node]
while stack:
node = stack.pop()
files = [c for c in node.children if not c.is_folder]
dirs = [c for c in node.children if c.is_folder]
yield node, dirs, files
stack.extend(dirs)
if __name__ == '__main__':
main()