-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdownload.py
123 lines (102 loc) · 3.96 KB
/
gdownload.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
from __future__ import print_function
import httplib2
import os
from apiclient import discovery
from apiclient import http
from apiclient import errors
import oauth2client
from oauth2client import client
from oauth2client import tools
import io
import sys
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/drive-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive.metadata'
CLIENT_SECRET_FILE = 'client_secret2.json'
APPLICATION_NAME = 'Drive API Python Quickstart'
DIR_FOR_THIS = '/Users/seokbongchoi/Documents/folder2/'
def printProgress (iteration, total, prefix = '', suffix = '', decimals = 2, barLength = 100):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : number of decimals in percent complete (Int)
barLength - Optional : character length of bar (Int)
"""
filledLength = int(round(barLength * iteration / float(total)))
percents = round(100.00 * (iteration / float(total)), decimals)
bar = '#' * filledLength + '-' * (barLength - filledLength)
sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percents, '%', suffix)),
sys.stdout.flush()
if iteration == total:
sys.stdout.write('\n')
sys.stdout.flush()
def get_credentials():
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'drive-python-quickstart2.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def get_file_list(service):
query = "(mimeType='{0}' or mimeType='{1}') and (name contains '{2}' or name contains '{3}')".format(
'video/mp4',
'video/x-msvideo',
'carib',
'heyzo'
)
print('Query: ' + query)
results = service.files().list(
pageSize=2,
q=query,
fields="nextPageToken, files(id, name, parents)"
).execute()
items = results.get('files', [])
if not items:
print('nothing')
return []
else:
return items
def delete_file(service, file_id):
service.files().delete(fileId=file_id).execute()
def download_file(service, file_id, file_name):
request = service.files().get_media(fileId=file_id)
real_name = DIR_FOR_THIS + file_name
fh = open(real_name, 'wb')
#fh = io.BytesIO()
downloader = http.MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
printProgress(iteration=int(status.progress() * 100), total=100)
fh.close()
def main():
credentials = get_credentials()
https = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v3', http=https)
items = get_file_list(service)
for item in items:
file_name = item['name'].encode('utf-8')
download_file(service,item['id'], file_name)
delete_file(service, item['id'])
if __name__ == '__main__':
main()