-
Notifications
You must be signed in to change notification settings - Fork 2
/
GoogleDriveWrapper.py
112 lines (91 loc) · 3.52 KB
/
GoogleDriveWrapper.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
from pydrive2.auth import GoogleAuth
from pydrive2.auth import RefreshError
from pydrive2.drive import GoogleDrive
import os
from pathlib import PurePath
class Drive:
def __init__(self):
gauth = GoogleAuth()
gauth.CommandLineAuth()
self.drive = GoogleDrive(gauth)
def fileListFrom(self,root):
q = "'" + root + "' in parents"
fileList = self.drive.ListFile({'q': q}).GetList()
return fileList
def printFileList(self,fileList):
for file in fileList:
print("title:", file['title'], "id:", file['id'])
class File:
def __init__(self,file):
self.file = file
@property
def fileSize(self):
return int(self.file['fileSize'])
@property
def title(self):
return self.file['title']
@property
def id(self):
return self.file['id']
def delete(self):
self.file.Delete()
class Folder:
def __init__(self, path: PurePath, createIfNotExists=False):
self.driveWrapper = Drive()
self.path = path
self.folder = self.folder(createIfNotExists=createIfNotExists)
self.files = self.files()
def folder(self, createIfNotExists=False):
parent_id = 'root'
folder = {'id': parent_id} #by default we are root folder
for component in self.path.parts:
fileList = self.driveWrapper.fileListFrom(parent_id)
filtered = list(filter(lambda f: f['title']==component, fileList))
if len(filtered)>0:
folder = filtered[0]
elif createIfNotExists:
print(f"creating {component} folder")
folder = self.createFolder(parent_id, component)
parent_id = folder['id'] #for intermediate folders
return folder
def createFolder(self, parent_id, name):
metadata = { 'parents': [ { "kind": "drive#fileLink", "id": parent_id } ], 'title': name, 'mimeType': 'application/vnd.google-apps.folder' }
file = self.driveWrapper.drive.CreateFile(metadata=metadata)
file.Upload()
return file
def files(self):
fileList = self.driveWrapper.fileListFrom(self.folder['id'])
return fileList
def downloadAll(self,path):
for f in self.files:
filePath = os.path.join(path,f['title'])
needsDownload = True
if os.path.exists(filePath):
upSize = int(f['fileSize'])
downSize = os.stat(filePath).st_size
if upSize == downSize:
print(f['title'] + " already downloaded")
needsDownload = False
if needsDownload:
print("Downloading "+f['title'])
f.GetContentFile(filePath)
def fileForName(self,name):
for f in self.files:
if f['title']==name:
return File(f)
return None
def upload(self,path):
fileName = os.path.basename(path)
serverFile = self.fileForName(fileName)
if serverFile is not None:
if serverFile.fileSize == os.stat(path).st_size:
print(serverFile.title + " already uploaded")
return serverFile.file
else:
serverFile.delete() #we are going to replace this file
metadata = { 'parents': [ { "kind": "drive#fileLink", "id": self.folder['id'] } ], 'title': fileName }
file = self.driveWrapper.drive.CreateFile(metadata=metadata)
file.Upload()
file.SetContentFile(path)
file.Upload()
return file