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, }