-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDriveSave.py
49 lines (40 loc) · 1.49 KB
/
DriveSave.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
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
class DriveSaver:
def __init__(self):
self.drive = None
try:
gauth = GoogleAuth()
# Try to load saved client credentials
gauth.LoadCredentialsFile("mycreds.txt")
if gauth.credentials is None:
# Authenticate if they're not there
gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
# Refresh them if expired
gauth.Refresh()
else:
# Initialize the saved creds
gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile("mycreds.txt")
self.drive = GoogleDrive(gauth)
except:
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
self.drive = GoogleDrive(gauth)
def save(self, filename):
try:
file5 = self.drive.CreateFile()
file5.SetContentFile(filename) # Read file and set it as a content of this instance.
file5.Upload() # Upload it
except:
print "we could not save"
def listFiles(self):
file_list = self.drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
for file1 in file_list:
print 'title: %s, id: %s' % (file1['title'], file1['id'])
def main():
d = DriveSaver()
d.listFiles()
if __name__ == "__main__": main()