-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jiooum
committed
Oct 13, 2024
1 parent
b2a8b53
commit 3071ff5
Showing
4 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import os | ||
|
||
import requests | ||
|
||
|
||
def download_file(url: str, save_path: str = None, save_folder: str = '', file_name: str = None): | ||
if save_path is None: | ||
if file_name is None: | ||
file_name = os.path.basename(url) | ||
max_length_filename = os.pathconf('/', 'PC_NAME_MAX') | ||
if len(file_name) > max_length_filename: | ||
split_text = os.path.splitext(file_name) | ||
file_name = split_text[0][: max_length_filename - len(split_text[1])] + split_text[1] | ||
os.makedirs(save_folder, exist_ok=True) | ||
save_path = f'{save_folder}/{file_name}' | ||
|
||
response = requests.get(url, timeout=20) | ||
assert response.status_code == 200, f"Failed to reach {url} with the status code of {response.status_code}" | ||
with open(save_path, 'wb') as file: | ||
file.write(response.content) | ||
|
||
# Download success | ||
assert os.path.isfile(save_path), f"Downloaded file is not found in: {save_path}" | ||
return save_path |