| | |
| | """ |
| | 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 |
| |
|
| | |
| | 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: |
| | |
| | if "civitai.com" in url: |
| | if not token: |
| | display(HTML("<h4 style='color:red;'>⚠️ Token de CivitAI no encontrado.</h4>")) |
| | 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"<h3 style='color:yellow;'>📥 Descargando: <code>{pretty}</code></h3>" |
| | f"<h4 style='color:cyan;'>📁 Destino: <code>{dest}</code></h4>")) |
| |
|
| | subprocess.run([ |
| | "aria2c", "--console-log-level=error", "--content-disposition", |
| | "-c", "-x", "6", "-s", "6", "-k", "1M", |
| | "-d", dest, url_token |
| | ]) |
| |
|
| | |
| | else: |
| | pretty = url.split('/')[-1].split('?')[0] |
| | display(HTML(f"<h3 style='color:yellow;'>📥 Descargando: <code>{pretty}</code></h3>" |
| | f"<h4 style='color:cyan;'>📁 Destino: <code>{dest}</code></h4>")) |
| | 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("<h3 style='color:lightgreen;'>✅ Descarga finalizada</h3>")) |
| |
|
| | |
| | get_ipython().register_magic_function(download, magic_kind='line') |