-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasset_downloader.py
More file actions
47 lines (39 loc) · 1.43 KB
/
asset_downloader.py
File metadata and controls
47 lines (39 loc) · 1.43 KB
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
import requests
import os
import sys
from tqdm import tqdm
import zipfile
ASSETZIP = "https://github.com/jake-drysdale/crate-dig/releases/download/assets/assets.zip"
ASSETZIP_NAME = "assets.zip"
DESTINATION = "assets"
def download_file(url, destination_name):
try:
if os.path.exists(destination_name):
tqdm.write(f"File {destination_name} already exists on disk.")
return
response = requests.get(url, stream=True, timeout=1200)
total_size = int(response.headers.get("content-length", 0))
# print(total_size)
chunk_size = 1024 * 1024 # 1 MB chunks
# Initialize progress bar with content-length
progress_bar = tqdm(
total=total_size,
unit="B",
unit_scale=True,
dynamic_ncols=True,
desc=f"Downloading {destination_name}",
)
with open(destination_name, "wb") as f:
for data in response.iter_content(chunk_size=chunk_size):
f.write(data)
progress_bar.update(len(data))
progress_bar.close()
except Exception as e:
tqdm.write(f"Error downloading/uploading {destination_name}: {str(e)}")
raise e
def download_assets(destination):
download_file(ASSETZIP, ASSETZIP_NAME)
print("Extracting assets...")
with zipfile.ZipFile(ASSETZIP_NAME, 'r') as zip_ref:
zip_ref.extractall(destination)
os.remove(ASSETZIP_NAME)