-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdrive_downloader.py
77 lines (63 loc) · 2.88 KB
/
gdrive_downloader.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
import io
from googleapiclient import discovery
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaIoBaseDownload
from httplib2 import Http
from oauth2client import file, client, tools
# code from google-api tutorial
def authorise():
obj = lambda: None
lmao = {"auth_host_name": 'localhost', 'noauth_local_webserver': 'store_true', 'auth_host_port': [8080, 8090],
'logging_level': 'ERROR'}
for k, v in lmao.items():
setattr(obj, k, v)
# authorization boilerplate code
SCOPES = 'https://www.googleapis.com/auth/drive.readonly'
store = file.Storage('my_personal_authorisation_token.json')
creds = store.get()
# The following will give you a link if token.json does not exist, the link allows the user to give this app permission
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('wa_worker_google_credentials.json', SCOPES)
creds = tools.run_flow(flow, store, obj)
return creds
def try_other_format(func):
def decorated(*args, **kwargs):
credentials = authorise()
kwargs.update({'credentials': credentials})
try:
func(*args, **kwargs)
except HttpError as e:
print(e)
mimeType = input('Please enter mime type of document you want to download.\n '
'Default is application/zip\n'
'Choose from here:\n '
'https://developers.google.com/drive/api/v3/manage-downloads\n')
kwargs.update({
'mimeType': mimeType,
'is_document': True
})
decorated(*args, **kwargs)
return decorated
@try_other_format
def download(credentials, file_id, filename='file_I_just_downloaded', is_document=False, mimeType='application/zip'):
DRIVE = discovery.build('drive', 'v3', http=credentials.authorize(Http()))
# if you get the shareable link, the link contains this id, replace the file_id below
if is_document:
request = DRIVE.files().export_media(fileId=file_id,
mimeType=mimeType)
else:
request = DRIVE.files().get_media(fileId=file_id)
# replace the filename and extension in the first field below
fh = io.FileIO(f'{filename}.zip', mode='w')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print("Download %d%%." % int(status.progress() * 100))
print("Download finished")
if __name__ == '__main__':
file_id = input('Input link to file you want to download(GDrive): ')
assert file_id.startswith('https://drive.google.com/open?id=')
file_id = file_id.replace('https://drive.google.com/open?id=', '').replace(' ', '')
assert file_id, 'Please enter file_id in the script'
download(file_id=file_id)