-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
200 lines (153 loc) · 5.48 KB
/
main.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
from __future__ import print_function
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
from apiclient.http import MediaFileUpload
from cmd import Cmd
import sys
import os
# If modifying these scopes, delete the file token.json.
# Get yout
# https://developers.google.com/sheets/api/quickstart/python
# https://developers.google.com/drive/api/v3/quickstart/python
SCOPES = [ 'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive.file']
class MyPrompt(Cmd):
def do_list(self, args):
if len(args) == 0:
size = 20
else:
size = args.split()[-1]
results = drive_service.files().list(
pageSize=size, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
if not items:
print('No files found.')
else:
print('Files:')
for item in items:
print('{0} ({1})'.format(item['name'], item['id']))
def do_empty_trash(self, args):
file_deleted = drive_service.files().emptyTrash().execute()
print('Trash Empty')
def do_get(self, args):
if len(args) == 0:
print('Please enter the file ID')
return
else:
id = args.split()[0]
file = drive_service.files().get(fileId=id).execute()
print(file)
def do_create_folder(self, args):
if len(args) == 0:
name = 'none'
else:
name = args.split()[-1]
file_metadata = {
'name': name,
'mimeType': 'application/vnd.google-apps.folder'
}
folder = drive_service.files().create(body=file_metadata,
fields='id').execute()
print(folder)
def do_create_spreadsheet(self, args):
if len(args) == 0:
name = 'none'
else:
name = args.split()[-1]
file_metadata = {
'name': name,
'mimeType': 'application/vnd.google-apps.spreadsheet'
}
spreadsheet = drive_service.files().create(body=file_metadata,
fields='id').execute()
print(spreadsheet)
def do_create_document(self, args):
if len(args) == 0:
name = 'none'
else:
name = args.split()[-1]
file_metadata = {
'name': name,
'mimeType': 'application/vnd.google-apps.document'
}
document = drive_service.files().create(body=file_metadata,
fields='id').execute()
print(document)
def do_create_drawing(self, args):
if len(args) == 0:
name = 'none'
else:
name = args.split()[-1]
file_metadata = {
'name': name,
'mimeType': 'application/vnd.google-apps.drawing'
}
drawing = drive_service.files().create(body=file_metadata,
fields='id').execute()
print(drawing)
def do_create_form(self, args):
if len(args) == 0:
name = 'none'
else:
name = args.split()[-1]
file_metadata = {
'name': name,
'mimeType': 'application/vnd.google-apps.form'
}
form = drive_service.files().create(body=file_metadata,
fields='id').execute()
print(form)
def do_create_slide(self, args):
if len(args) == 0:
name = 'none'
else:
name = args.split()[-1]
file_metadata = {
'name': name,
'mimeType': 'application/vnd.google-apps.presentation'
}
slide = drive_service.files().create(body=file_metadata,
fields='id').execute()
print(slide)
def do_create_file(self, args):
name = args.split()[0]
folder_id = args.split()[1]
path = args.split()[2]
type = 'image/jpeg'
print(name + ' ' + folder_id + ' ' + path + ' ' + type)
file_metadata = {
'name': name,
'parents': [folder_id]
}
media = MediaFileUpload(path,
mimetype=type,
resumable=True)
file_created = drive_service.files().create(body=file_metadata,
media_body=media,
fields='id').execute()
print(file_created)
def do_delete(self, args):
id = args.split()[0]
file_deleted = drive_service.files().delete(fileId=id).execute()
print('File deleted')
def do_logout(self, args):
os.remove("token.json")
def do_quit(self, args):
"""Quits the program."""
print ("Quitting.")
raise SystemExit
if __name__ == '__main__':
global drive_service
global sheets_service
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
drive_service = build('drive', 'v3', http=creds.authorize(Http()))
sheets_service = build('sheets', 'v4', http=creds.authorize(Http()))
prompt = MyPrompt()
prompt.prompt = '> '
prompt.cmdloop('Starting prompt...')