Upload downloadfile.py
Browse files- downloadfile.py +57 -0
downloadfile.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import importlib.util
|
3 |
+
|
4 |
+
def is_package_installed(package_name):
|
5 |
+
return importlib.util.find_spec(package_name) is not None
|
6 |
+
|
7 |
+
if not is_package_installed("requests"):
|
8 |
+
print("The 'requests' library is not installed. Please install it to use this node.")
|
9 |
+
|
10 |
+
if not is_package_installed("tqdm"):
|
11 |
+
print("The 'tqdm' library is not installed. Please install it to use this node.")
|
12 |
+
|
13 |
+
if is_package_installed("requests") and is_package_installed("tqdm"):
|
14 |
+
import requests
|
15 |
+
from tqdm import tqdm
|
16 |
+
|
17 |
+
class DownloadFileNode:
|
18 |
+
@classmethod
|
19 |
+
def INPUT_TYPES(s):
|
20 |
+
return {
|
21 |
+
"required": {
|
22 |
+
"url": ("STRING", {"default": ""}),
|
23 |
+
"save_path": ("STRING", {"default": ""})
|
24 |
+
}
|
25 |
+
}
|
26 |
+
|
27 |
+
RETURN_TYPES = ("STRING",)
|
28 |
+
FUNCTION = "download_file"
|
29 |
+
CATEGORY = "utils"
|
30 |
+
|
31 |
+
def download_file(self, url, save_path):
|
32 |
+
try:
|
33 |
+
response = requests.get(url, stream=True)
|
34 |
+
response.raise_for_status()
|
35 |
+
|
36 |
+
os.makedirs(os.path.dirname(save_path), exist_ok=True)
|
37 |
+
|
38 |
+
total_size = int(response.headers.get('content-length', 0))
|
39 |
+
block_size = 8192 # 8KB
|
40 |
+
|
41 |
+
with open(save_path, 'wb') as file, tqdm(
|
42 |
+
desc=save_path,
|
43 |
+
total=total_size,
|
44 |
+
unit='iB',
|
45 |
+
unit_scale=True,
|
46 |
+
unit_divisor=1024,
|
47 |
+
) as progress_bar:
|
48 |
+
for data in response.iter_content(block_size):
|
49 |
+
size = file.write(data)
|
50 |
+
progress_bar.update(size)
|
51 |
+
|
52 |
+
return (f"File downloaded successfully to {save_path}",)
|
53 |
+
except requests.exceptions.RequestException as e:
|
54 |
+
return (f"Error downloading file: {str(e)}",)
|
55 |
+
NODE_CLASS_MAPPINGS = {
|
56 |
+
"DownloadFileNode": DownloadFileNode,
|
57 |
+
}
|