forked from GleamingGlaceon94/folderclone
-
Notifications
You must be signed in to change notification settings - Fork 1
/
counter.py
59 lines (47 loc) · 1.79 KB
/
counter.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
from google.oauth2.service_account import Credentials
import googleapiclient.discovery, json, progress.bar, socket, time, sys, glob
stt = time.time()
fct = 0
dct = 0
try:
sas = glob.glob('key.json')
sas.extend(glob.glob('controller/*.json'))
sas.extend(glob.glob('accounts/*.json'))
filename = sas[0]
except IndexError:
print('No Service Account Found.')
sys.exit(0)
credentials = Credentials.from_service_account_file(filename, scopes=[
"https://www.googleapis.com/auth/drive"
])
drive = googleapiclient.discovery.build("drive", "v3", credentials=credentials)
def ls(parent, searchTerms=""):
files = []
resp = drive.files().list(q=f"'{parent}' in parents" + searchTerms, pageSize=1000, supportsAllDrives=True, includeItemsFromAllDrives=True).execute()
files += resp["files"]
while "nextPageToken" in resp:
resp = drive.files().list(q=f"'{parent}' in parents" + searchTerms, pageSize=1000, supportsAllDrives=True, includeItemsFromAllDrives=True, pageToken=resp["nextPageToken"]).execute()
files += resp["files"]
return files
def lsd(parent):
return ls(parent, searchTerms=" and mimeType contains 'application/vnd.google-apps.folder'")
def lsf(parent):
return ls(parent, searchTerms=" and not mimeType contains 'application/vnd.google-apps.folder'")
def rs(source):
global fct, dct
fs = lsf(source)
fct += len(fs)
fd = lsd(source)
dct += len(fd)
for i in fd:
rs(i['id'])
try:
sp = sys.argv[1]
except IndexError:
sp = input('Folder ID to scan? ').strip()
print('Counting objects in %s' % sp)
rs(sp)
print('Objects: %d\nFolders: %d' % (fct,dct))
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))