-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpygdrive
More file actions
221 lines (221 loc) · 6.06 KB
/
pygdrive
File metadata and controls
221 lines (221 loc) · 6.06 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
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python
###################################################
# #
# ##### # # ##### ## ##### # # # ##### #
# # # # # # # # # # # # # # #
# ##### # # ### # # ##### # # # ### #
# # # # # # # # # # # # # #
# # # ##### ##### # # # # ##### #
# #
###################################################
# v0.0.4 #
###################################################
import os,sys
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
dfiles=""
dfilesid=""
tmpfl=""
tmpfli=""
rows, columns = os.popen('stty size', 'r').read().split()
#file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
#for file1 in file_list:
# if dfiles=="":
# dfiles=file1["title"]
# dfilesid=file1["id"]
# else:
# dfiles=dfiles+"\n"+file1["title"]
# dfilesid=dfilesid+"\n"+file1["id"]
def hr():
print "-"*int(columns)
def fld(pre):
global dfiles
global dfilesid
global file_list
for f in file_list:
if f['mimeType']=='application/vnd.google-apps.folder': # if folder
dfiles=dfiles+"\n"+pre+"/"+str(f['title'])+"/"#,"list":ListFolder(f['id'])})
dfilesid=dfilesid+"\n"+str(f['id'])
file_list=drive.ListFile({'q': "'%s' in parents and trashed=false" % f['id']}).GetList()
fld(pre+"/"+str(f['title']))
else:
dfiles=dfiles+"\n"+pre+"/"+str(f['title'])
dfilesid=dfilesid+"\n"+str(f['id'])
def list():
global dfiles
global dfilesid
global file_list
dfiles=""
file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
fld("")
tmp=""
for line in dfiles.splitlines():
if line!="":
if tmp=="":
tmp=line
else:
tmp=tmp+"\n"+line
dfiles=tmp
tmp=""
for line in dfilesid.splitlines():
if line!="":
if tmp=="":
tmp=line
else:
tmp=tmp+"\n"+line
dfilesid=tmp
dfilesarray=dfiles.split("\n")
dfilesidarray=dfilesid.split("\n")
tmp=0
while tmp<len(dfilesarray):
print dfilesarray[tmp],"---",dfilesidarray[tmp]
tmp+=1
return dfiles
if len(sys.argv)>2:
try:
if help in sys.argv[1]:
print "pygdrive help - Display this"
print "pygdrive list - List all file in GDrive"
#print "pygdrive del - Delete all files in GDrive"
print "pygdrive - Upload all file in this direcoty"
sys.exit()
elif sys.argv[1]:
hr()
list()
sys.exit()
else:
print "'"+sys.argv[1]+"' not found\nTry:\npygdrive help"
except:
print "Error"
sys.exit()
hr()
list()
#files=os.listdir(os.getcwd())
arfiles=[]
files=""
filesnum=0
fileproc=0
num=0
pre=os.getcwd()+"/"
for path, subdirs, files in os.walk(os.getcwd()):
for name in files:
arfiles.append(os.path.join(path, name).replace(pre,""))
files='\n'.join(arfiles)
for line in files.splitlines():
filesnum+=1
hr()
for path, subdirs, files in os.walk(os.getcwd()):
for name in files:
fileproc+=1
#fileproc:filesnum=X:100
filesperc=(fileproc*100)/filesnum
ofile=os.path.join(path, name).replace(pre,"")
if "/" in ofile:
strfilepr="Processing: "+ofile+" "+str(filesperc)+"%"
print strfilepr+(" "*(int(columns)-len(strfilepr)))+"\r",
sys.stdout.flush()
if not ofile in dfiles and not ofile in tmpfl:
dd=ofile.split("/")
num=0
f=1
while num<(len(dd)-1):
cur=dd[num]
if not cur in dfiles and not cur in tmpfl:
if f==1:
folder_metadata = {'title' : cur, 'mimeType' : 'application/vnd.google-apps.folder'}
f=0
else:
folder_metadata = {'title' : cur, 'mimeType' : 'application/vnd.google-apps.folder', "parents": [{"kind": "drive#fileLink", "id": folderid}]}
folder = drive.CreateFile(folder_metadata)
folder.Upload()
folderid=folder["id"]
num+=1
folderid=folder["id"]
tmpfl=tmpfl+cur+"\n"
tmpfli=tmpfli+folderid+"\n"
else:
f=0
num2=1
num3=1
if cur in dfiles:
for line in dfiles.splitlines():
if cur==line:
lnum=num2
num2+=1
for line in dfilesid.splitlines():
if lnum==num3:
folderid=line
num3+=1
else:
for line in tmpfl.splitlines():
if cur==line:
lnum=num2
num2+=1
for line in tmpfli.splitlines():
if lnum==num3:
folderid=line
num3+=1
f=0
num+=1
file1 = drive.CreateFile({"title": dd[num], "parents": [{"kind": "drive#fileLink", "id": folderid}]})
file1.SetContentFile(ofile)
file1.Upload()
else:
num2=1
num3=1
for line in dfiles.splitlines():
if "/"+ofile==line:
lnum=num2
num2+=1
for line in dfilesid.splitlines():
if lnum==num3:
fileid=line
num3+=1
fileIN = drive.CreateFile({'id': fileid})
incont=fileIN.GetContentString()
ffile=open(ofile,"r")
cont=ffile.read()
ffile.close()
if cont!=incont:
file1 = drive.CreateFile({'title': ofile, "id": fileid})
file1.SetContentString(cont)
file1.Upload()
else:
strfilepr="Processing: "+ofile+" "+str(filesperc)+"%"
print strfilepr+(" "*(int(columns)-len(strfilepr)))+"\r",
sys.stdout.flush()
if not ofile in dfiles:
ffile=open(ofile,"r")
cont=ffile.read()
ffile.close()
file1 = drive.CreateFile({'title': ofile})
file1.SetContentString(cont)
file1.Upload()
else:
num2=1
num3=1
for line in dfiles.splitlines():
if "/"+ofile==line:
lnum=num2
num2+=1
for line in dfilesid.splitlines():
if lnum==num3:
fileid=line
num3+=1
fileIN = drive.CreateFile({'id': fileid})
incont=fileIN.GetContentString()
ffile=open(ofile,"r")
cont=ffile.read()
ffile.close()
if cont!=incont:
file1 = drive.CreateFile({'title': ofile, "id": fileid})
file1.SetContentString(cont)
file1.Upload()
#file_list = drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
#for file1 in file_list:
# print('title: %s, id: %s' % (file1['title'], file1['id']))
list()
sys.exit()