# download_magic.py """ Mágica %download para IPython / Jupyter / Colab. Lee el token de CivitAI desde ~/.civitai_token.pkl (pickle) Uso: import download_magic %download https://civitai.com/api/download/models/..., https://huggingface.co/... """ from IPython import get_ipython from IPython.core.magic import register_line_magic from IPython.display import HTML, display import os import subprocess import re import requests import pickle from pathlib import Path # ---------- leer token desde pickle ---------- TOKEN_FILE = Path.home() / ".civitai_token.pkl" token = None if TOKEN_FILE.exists(): try: token = pickle.loads(TOKEN_FILE.read_bytes()) except Exception: pass # --------------------------------------------- @register_line_magic def download(line): """ %download url1, url2, ... Muestra solo el nombre real y respeta la carpeta actual (%cd). """ dest = os.getcwd() urls = [u.strip() for u in line.split(",") if u.strip()] for url in urls: # ---------- CivitAI ---------- if "civitai.com" in url: if not token: display(HTML("

⚠️ Token de CivitAI no encontrado.

")) continue url_token = f"{url}{'&' if '?' in url else '?'}token={token}" try: with requests.get(url_token, stream=True, timeout=5) as r: cd = r.headers.get('Content-Disposition', '') match = re.findall(r'filename[*]?=(?:UTF-8\'\')?["\']?([^"\';]+)["\']?', cd) pretty = match[0] if match else url.split('/')[-1].split('?')[0] except Exception: pretty = url.split('/')[-1].split('?')[0] display(HTML(f"

📥 Descargando: {pretty}

" f"

📁 Destino: {dest}

")) subprocess.run([ "aria2c", "--console-log-level=error", "--content-disposition", "-c", "-x", "6", "-s", "6", "-k", "1M", "-d", dest, url_token ]) # ---------- HuggingFace ---------- else: pretty = url.split('/')[-1].split('?')[0] display(HTML(f"

📥 Descargando: {pretty}

" f"

📁 Destino: {dest}

")) subprocess.run([ "aria2c", "--console-log-level=error", "--content-disposition", "-c", "-x", "8", "-s", "8", "-k", "1M", "-d", dest, "-o", pretty, url ]) for f in os.listdir(dest): if re.fullmatch(r'[0-9a-f]{64}', f): os.rename(os.path.join(dest, f), os.path.join(dest, pretty)) break display(HTML("

✅ Descarga finalizada

")) # Registramos la línea mágica al importar el módulo get_ipython().register_magic_function(download, magic_kind='line')