-
Notifications
You must be signed in to change notification settings - Fork 20
Fix/docs build #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix/docs build #142
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,25 +1,80 @@ | ||
| import os | ||
| import requests | ||
|
|
||
|
|
||
| def download_file(url, local_path): | ||
| def download_file(url, local_path, backup_url=None): | ||
| """ | ||
| Download a file from a URL if it does not exist locally. | ||
|
|
||
| url (str): The URL to download the file from. | ||
| local_path (str): The local path to save the file to. Does nothing if the file already exists. | ||
| Args: | ||
| url (str): The primary URL to download the file from. | ||
| local_path (str): The local path to save the file to. Does nothing if the file already exists. | ||
| backup_url (str, optional): A backup URL to try if the primary URL fails. | ||
| """ | ||
| if not os.path.exists(local_path): | ||
| import requests | ||
| if os.path.exists(local_path): | ||
| return | ||
|
|
||
| response = requests.get(url, timeout=30, headers={"User-Agent": "Mozilla/5.0"}) | ||
| response.raise_for_status() | ||
| urls_to_try = [url] | ||
| if backup_url: | ||
| urls_to_try.append(backup_url) | ||
|
|
||
| # Detect if file is binary (e.g., .zip files) | ||
| is_binary = local_path.endswith((".zip", ".gz", ".tar")) | ||
| mode = "wb" if is_binary else "w" | ||
| last_error = None | ||
| for i, try_url in enumerate(urls_to_try): | ||
| try: | ||
| response = requests.get( | ||
| try_url, timeout=30, headers={"User-Agent": "Mozilla/5.0"} | ||
| ) | ||
| response.raise_for_status() | ||
|
|
||
| with open(local_path, mode) as f: | ||
| f.write(response.content if is_binary else response.text) | ||
| # Detect if file is binary (e.g., .zip files) | ||
| is_binary = local_path.endswith((".zip", ".gz", ".tar")) | ||
| mode = "wb" if is_binary else "w" | ||
|
|
||
| with open(local_path, mode) as f: | ||
| f.write(response.content if is_binary else response.text) | ||
| return # Success, exit the function | ||
|
|
||
| except requests.exceptions.HTTPError as e: | ||
| last_error = e | ||
| error_msg = ( | ||
| f"Failed to download from URL ({i + 1}/{len(urls_to_try)}): {try_url}\n" | ||
| f" HTTP Status: {e.response.status_code}\n" | ||
| f" Reason: {e.response.reason}" | ||
| ) | ||
| # Check for common error codes and provide helpful messages | ||
| if e.response.status_code == 403: | ||
| error_msg += ( | ||
| "\n Note: 403 Forbidden often means the server is blocking automated requests " | ||
| "(rate limiting, bot detection, or access restrictions)." | ||
| ) | ||
| elif e.response.status_code == 404: | ||
| error_msg += ( | ||
| "\n Note: 404 Not Found - the file may have been moved or deleted." | ||
| ) | ||
|
|
||
| if i < len(urls_to_try) - 1: | ||
| print(f"Warning: {error_msg}\n Trying backup URL...") | ||
| else: | ||
| print(f"Error: {error_msg}") | ||
|
Comment on lines
+55
to
+58
|
||
|
|
||
| except requests.exceptions.RequestException as e: | ||
| last_error = e | ||
| error_msg = ( | ||
| f"Failed to download from URL ({i + 1}/{len(urls_to_try)}): {try_url}\n" | ||
| f" Error: {type(e).__name__}: {e}" | ||
| ) | ||
| if i < len(urls_to_try) - 1: | ||
| print(f"Warning: {error_msg}\n Trying backup URL...") | ||
| else: | ||
| print(f"Error: {error_msg}") | ||
|
|
||
| # If we get here, all URLs failed | ||
| raise RuntimeError( | ||
| f"Failed to download '{local_path}' from all provided URLs.\n" | ||
| f" Primary URL: {url}\n" | ||
| + (f" Backup URL: {backup_url}\n" if backup_url else "") | ||
| + f" Last error: {last_error}" | ||
| ) | ||
|
|
||
|
|
||
| def unzip_file(zip_path, extract_to): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When catching requests.exceptions.HTTPError, the code accesses e.response.status_code and e.response.reason without checking if e.response is not None. While HTTPError typically has a response attribute populated, it's better to add a defensive check to prevent potential AttributeError if the response attribute is None in edge cases.