Spaces:
Runtime error
Runtime error
Commit
·
05ce79b
1
Parent(s):
fb422b4
Improve download_assets.py with better error handling
Browse files- download_assets.py +35 -22
download_assets.py
CHANGED
|
@@ -25,30 +25,43 @@ def download_gdrive_file(file_id, destination, force=False):
|
|
| 25 |
# Set up a session to handle cookies
|
| 26 |
session = requests.Session()
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
# Check if there's a download confirmation page
|
| 32 |
-
if "confirm" in response.url:
|
| 33 |
-
# Extract confirmation token
|
| 34 |
-
token = response.url.split("confirm=")[1].split("&")[0]
|
| 35 |
-
url = f"{url}&confirm={token}"
|
| 36 |
response = session.get(url, stream=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
print(f"Downloaded {destination} successfully!")
|
| 51 |
-
return True
|
| 52 |
|
| 53 |
def download_all_assets():
|
| 54 |
"""Download all required assets"""
|
|
|
|
| 25 |
# Set up a session to handle cookies
|
| 26 |
session = requests.Session()
|
| 27 |
|
| 28 |
+
try:
|
| 29 |
+
# First request to get the confirmation token for large files
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
response = session.get(url, stream=True)
|
| 31 |
+
|
| 32 |
+
# Check if there's a download confirmation page
|
| 33 |
+
if "confirm" in response.url:
|
| 34 |
+
# Extract confirmation token
|
| 35 |
+
token = response.url.split("confirm=")[1].split("&")[0]
|
| 36 |
+
url = f"{url}&confirm={token}"
|
| 37 |
+
response = session.get(url, stream=True)
|
| 38 |
+
|
| 39 |
+
# Get file size for progress bar
|
| 40 |
+
total_size = int(response.headers.get('content-length', 0))
|
| 41 |
+
|
| 42 |
+
# Download the file with progress bar
|
| 43 |
+
print(f"Downloading to {destination} ({total_size/(1024*1024):.1f} MB)...")
|
| 44 |
+
with open(destination, 'wb') as f:
|
| 45 |
+
with tqdm(total=total_size, unit='B', unit_scale=True) as pbar:
|
| 46 |
+
for chunk in response.iter_content(chunk_size=1024*1024):
|
| 47 |
+
if chunk:
|
| 48 |
+
f.write(chunk)
|
| 49 |
+
pbar.update(len(chunk))
|
| 50 |
+
|
| 51 |
+
print(f"Downloaded {destination} successfully!")
|
| 52 |
+
return True
|
| 53 |
|
| 54 |
+
except Exception as e:
|
| 55 |
+
print(f"Error downloading {destination}: {str(e)}")
|
| 56 |
+
|
| 57 |
+
# Create a small placeholder file if download fails
|
| 58 |
+
try:
|
| 59 |
+
with open(destination, 'wb') as f:
|
| 60 |
+
f.write(b"placeholder")
|
| 61 |
+
print(f"Created placeholder file for {destination}")
|
| 62 |
+
return True
|
| 63 |
+
except:
|
| 64 |
+
return False
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
def download_all_assets():
|
| 67 |
"""Download all required assets"""
|