-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfolderclone.py
207 lines (181 loc) · 7.08 KB
/
folderclone.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
201
202
203
204
205
206
207
import time
import threading
import glob
import argparse
import json
import random
import socket
from google.oauth2.service_account import Credentials
from googleapiclient.errors import HttpError
from googleapiclient.discovery import build
from CounterProgress import CounterProgress
stt = time.time()
views = ["tree", "basic"]
args = {}
parse = argparse.ArgumentParser(description='A tool intended to copy large files from one folder to another.')
parse.add_argument('--view', default=0, help='Set the view to a different setting (tree[0]|basic[1]).')
parse.add_argument('--thread', '-t', default=50, help='Specify total number of threads to use.')
parse.add_argument('--skip', default=None, help='Folder ID mark')
parsereq = parse.add_argument_group('required arguments')
parsereq.add_argument('--source-id', '-s', help='The source ID of the folder to copy.', required=True)
parsereq.add_argument('--destination-id', '-d', help='The destination ID of the folder to copy to.', required=True)
args = parse.parse_args()
view = int(args.view)
thread_num = int(args.thread)
skip = args.skip
print('Copy from %s to %s.' % (args.source_id, args.destination_id))
print('View set to %s (%s).' % (view, views[view]))
def ls(parent, searchTerms=""):
while True:
random_drive = generaterandomdrive()
try:
files = []
resp = random_drive.files().list(q=f"'{parent}' in parents{searchTerms}", pageSize=1000, supportsAllDrives=True, includeItemsFromAllDrives=True).execute()
files += resp["files"]
while "nextPageToken" in resp:
resp = random_drive.files().list(q=f"'{parent}' in parents" + searchTerms, pageSize=1000, supportsAllDrives=True, includeItemsFromAllDrives=True, pageToken=resp["nextPageToken"]).execute()
files += resp["files"]
return files
except HttpError:
print("#Error Listing")
time.sleep(3)
def list_folder(parent):
return ls(parent, searchTerms=" and mimeType contains 'application/vnd.google-apps.folder'")
def list_file(parent):
return ls(parent, searchTerms=" and not mimeType contains 'application/vnd.google-apps.folder'")
def generaterandomdrive():
global accsf
socket.setdefaulttimeout(600)
acc_thread.acquire()
random_acc = random.choice(accsf)
while True:
try:
credentials = Credentials.from_service_account_file(random_acc, scopes=[
"https://www.googleapis.com/auth/drive"
])
random_drive = build("drive", "v3", credentials=credentials)
except HttpError:
random_acc = random.choice(accsf)
print("#Error SA Error")
else:
break
acc_thread.release()
return random_drive
def copy(source, dest):
while True:
random_drive = generaterandomdrive()
try:
random_drive.files().copy(fileId=source, body={"parents": [dest]}, supportsAllDrives=True).execute()
except HttpError as err:
reason = json.loads(err.content).get('error').get('errors')[0].get('reason')
reason_list = ["dailyLimitExceeded", "userRateLimitExceeded", "sharingRateLimitExceeded"]
if reason not in reason_list:
print(f"#Error {reason}")
time.sleep(3)
else:
break
time.sleep(20)
threads.release()
def rcopy(source, dest, sname, pre):
global skip
pres = pre
if view == 1:
pres = ""
filestocopy = list_file(source)
if skip:
if source != skip:
if filestocopy:
print(f"{pre}{sname} - Skipped")
else:
skip = None
if filestocopy:
if not skip:
fullname = pres + sname
pbar = CounterProgress(f"{fullname[:40]} ({source}) - ", max=len(filestocopy))
pbar.update()
for i in filestocopy:
threads.acquire()
thread = threading.Thread(target=copy, args=(i["id"], dest))
thread.start()
pbar.next()
pbar.finish()
else:
print(f"{pres}{sname} ({source})")
folderstocopy = list_folder(source)
folderlen = len(folderstocopy) - 1
currentlen = 0
for i in folderstocopy:
pre = pre.replace(f"├─ ", f"│ ")
nstu = pre.replace(f"└─ ", f" ")
if currentlen == folderlen:
nstu += f"└─ "
else:
nstu += f"├─ "
if not skip:
while True:
random_drive = generaterandomdrive()
try:
resp = random_drive.files().create(body={
"name": i["name"],
"mimeType": "application/vnd.google-apps.folder",
"parents": [dest]
}, supportsAllDrives=True).execute()
except HttpError:
print("#Error Create Error")
else:
break
else:
while True:
random_drive = generaterandomdrive()
try:
td_id = random_drive.files().get(
fileId=dest,
supportsAllDrives=True
).execute()["driveId"]
except HttpError:
print("#Error Get Error")
else:
break
while True:
random_drive = generaterandomdrive()
name = i['name'].replace("'", "\'")
try:
resp = random_drive.files().list(
corpora="drive",
driveId=td_id,
includeItemsFromAllDrives=True,
q=f"name = '{name}'",
supportsAllDrives=True
).execute()['files'][0]
except HttpError:
print("#Error Find Error")
except IndexError:
random_drive = generaterandomdrive()
try:
resp = random_drive.files().create(body={
"name": i["name"],
"mimeType": "application/vnd.google-apps.folder",
"parents": [dest]
}, supportsAllDrives=True).execute()
except HttpError:
print("#Error Create Error")
else:
break
else:
break
rcopy(i["id"], resp["id"], i["name"].replace('%', "%%"), nstu)
currentlen += 1
accsf = glob.glob('accounts/*.json')
acc_thread = threading.BoundedSemaphore(1)
threads = threading.BoundedSemaphore(thread_num)
print('BoundedSemaphore with %d threads' % thread_num)
try:
rcopy(args.source_id, args.destination_id, "root", "")
except KeyboardInterrupt:
print('Quitting')
except Exception as e:
print(e)
print('Complete.')
hours, rem = divmod((time.time() - stt), 3600)
minutes, sec = divmod(rem, 60)
print("Elapsed Time:\n{:0>2}:{:0>2}:{:05.2f}".format(int(hours), int(minutes), sec))