-
Notifications
You must be signed in to change notification settings - Fork 0
/
drive.py
131 lines (96 loc) · 4.69 KB
/
drive.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
import os.path
import io
import google.auth
import json
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from googleapiclient.http import MediaIoBaseDownload
from flask import send_file
from zipfile import ZipFile
from credentials import creds
start_folder_id = '1EPZPIV36JUNKf-azQxcBKSOcM2LB56R-' # Got this directly from the URL of my overall beat folder on GDrive
# Creating a service
drive_service = build('drive', 'v3', credentials=creds)
def return_directory(folder, mode=None):
if mode == 'folders-only':
query = f"mimeType = 'application/vnd.google-apps.folder' and parents in '{folder}'"
elif mode == 'files-only':
query = f"mimeType != 'application/vnd.google-apps.folder' and parents in '{folder}'"
else:
query = f"parents in '{folder}'"
current_dir = {}
page_token = None
while True:
response = drive_service.files().list(
q=query,
spaces='drive',
fields='nextPageToken, files(id, name)',
pageToken=page_token
).execute()
for file in response.get('files', []):
current_dir[file.get('name')] = file.get('id')
page_token = response.get('nextPageToken', None)
if page_token is None:
break
return current_dir
def fetch_beat_files(beat_name):
stems, mixdowns = None, None
try:
beat_folder_id = return_directory(start_folder_id)[beat_name]
beat_folder_dir = return_directory(beat_folder_id)
stems_folder_id = beat_folder_dir['Stems']
stems = return_directory(stems_folder_id, mode='files-only')
mixdown_folder_id = beat_folder_dir['Mixdown']
mixdowns = return_directory(mixdown_folder_id, mode='files-only')
except KeyError:
print(f"Either there's no folder called '{beat_name}', or there IS a folder called '{beat_name}' but 'Stems' or 'Mixdown' are missing from that folder.")
return stems, mixdowns
def download_file(file_id, file_path):
try:
request = drive_service.files().get_media(fileId=file_id) # The API call details
file = io.BytesIO() # Don't fully understand what this does
downloader = MediaIoBaseDownload(file, request) # I believe this is where the API request is executed.
# Seems the download is carried out in chunks. The following loops goes through each chunk until the entire file has been downloaded.
done = False
while done is False:
status, done = downloader.next_chunk()
print(f"Downloading '{file_path}' - {int(status.progress() * 100)}%")
file.seek(0)
return send_file(file, as_attachment=True, download_name=file_path)
# with io.BytesIO() as file: THIS MIGHT BE A BETTER WAY TO CLOSE OPEN AND CLOSE THE BUFFER IF I CAN GET IT WORKING.
# downloader = MediaIoBaseDownload(file, request) # I believe this is where the API request is executed.
# # Seems the download is carried out in chunks. The following loops goes through each chunk until the entire file has been downloaded.
# done = False
# while done is False:
# status, done = downloader.next_chunk()
# print(f"Downloading '{file_path}' - {int(status.progress() * 100)}%")
# file.seek(0)
# return send_file(file, as_attachment=True, download_name=file_path)
except HttpError as error:
print(F'An error occurred: {error}')
file = None
return False # Error
def download_all_files(beat_name, download_order, label):
zip_stream = io.BytesIO()
with ZipFile(zip_stream, 'w') as archive:
for file_name in dict.keys(download_order):
with archive.open(file_name, 'w') as f:
try:
request = drive_service.files().get_media(fileId=download_order[file_name]) # The API call details
file_stream = io.BytesIO()
downloader = MediaIoBaseDownload(file_stream, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print(f"Downloading '{file_name}' - {int(status.progress() * 100)}%")
f.write(file_stream.getvalue())
file_stream.close()
except HttpError as error:
print(F'An error occurred: {error}')
file = None
return False # Error
print('Finished Downloading')
zip_stream.seek(0)
return send_file(zip_stream, as_attachment=True, download_name=f"{label} - '{beat_name}'.zip")
if __name__ == '__main__':
fetch_beat_files('The Abyss')