File size: 2,068 Bytes
b563a70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
import os
import importlib.util

def is_package_installed(package_name):
    return importlib.util.find_spec(package_name) is not None

if not is_package_installed("requests"):
    print("The 'requests' library is not installed. Please install it to use this node.")

if not is_package_installed("tqdm"):
    print("The 'tqdm' library is not installed. Please install it to use this node.")

if is_package_installed("requests") and is_package_installed("tqdm"):
    import requests
    from tqdm import tqdm

    class DownloadFileNode:
        @classmethod
        def INPUT_TYPES(s):
            return {
                "required": {
                    "url": ("STRING", {"default": ""}),
                    "save_path": ("STRING", {"default": ""})
                }
            }

        RETURN_TYPES = ("STRING",)
        FUNCTION = "download_file"
        CATEGORY = "utils"

        def download_file(self, url, save_path):
            try:
                response = requests.get(url, stream=True)
                response.raise_for_status()
                
                os.makedirs(os.path.dirname(save_path), exist_ok=True)
                
                total_size = int(response.headers.get('content-length', 0))
                block_size = 8192  # 8KB
                
                with open(save_path, 'wb') as file, tqdm(
                    desc=save_path,
                    total=total_size,
                    unit='iB',
                    unit_scale=True,
                    unit_divisor=1024,
                ) as progress_bar:
                    for data in response.iter_content(block_size):
                        size = file.write(data)
                        progress_bar.update(size)
                
                return (f"File downloaded successfully to {save_path}",)
            except requests.exceptions.RequestException as e:
                return (f"Error downloading file: {str(e)}",)
NODE_CLASS_MAPPINGS = {
    "DownloadFileNode": DownloadFileNode,
}