Upload download_magic.py
Browse files- scripts/download_magic.py +79 -0
scripts/download_magic.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# download_magic.py
|
| 2 |
+
"""
|
| 3 |
+
Mágica %download para IPython / Jupyter / Colab.
|
| 4 |
+
Al importar este módulo se registra automáticamente la línea mágica %download.
|
| 5 |
+
Uso:
|
| 6 |
+
import download_magic
|
| 7 |
+
%download https://civitai.com/api/download/models/..., https://huggingface.co/...
|
| 8 |
+
"""
|
| 9 |
+
|
| 10 |
+
from IPython import get_ipython
|
| 11 |
+
from IPython.core.magic import register_line_magic
|
| 12 |
+
from IPython.display import HTML, display
|
| 13 |
+
import os
|
| 14 |
+
import subprocess
|
| 15 |
+
import re
|
| 16 |
+
import requests
|
| 17 |
+
|
| 18 |
+
# Recuperamos el token de %store
|
| 19 |
+
ipython = get_ipython()
|
| 20 |
+
token = None
|
| 21 |
+
try:
|
| 22 |
+
ipython.magic("store -r token_civitai")
|
| 23 |
+
token = ipython.user_ns.get("token_civitai")
|
| 24 |
+
except Exception:
|
| 25 |
+
pass
|
| 26 |
+
|
| 27 |
+
@register_line_magic
|
| 28 |
+
def download(line):
|
| 29 |
+
"""
|
| 30 |
+
%download url1, url2, ...
|
| 31 |
+
Muestra solo el nombre real y respeta la carpeta actual (%cd).
|
| 32 |
+
"""
|
| 33 |
+
dest = os.getcwd()
|
| 34 |
+
urls = [u.strip() for u in line.split(",") if u.strip()]
|
| 35 |
+
|
| 36 |
+
for url in urls:
|
| 37 |
+
# ---------- CivitAI ----------
|
| 38 |
+
if "civitai.com" in url:
|
| 39 |
+
if not token:
|
| 40 |
+
display(HTML("<h4 style='color:red;'>⚠️ Token de CivitAI no encontrado.</h4>"))
|
| 41 |
+
continue
|
| 42 |
+
url_token = f"{url}{'&' if '?' in url else '?'}token={token}"
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
with requests.get(url_token, stream=True, timeout=5) as r:
|
| 46 |
+
cd = r.headers.get('Content-Disposition', '')
|
| 47 |
+
match = re.findall(r'filename[*]?=(?:UTF-8\'\')?["\']?([^"\';]+)["\']?', cd)
|
| 48 |
+
pretty = match[0] if match else url.split('/')[-1].split('?')[0]
|
| 49 |
+
except Exception:
|
| 50 |
+
pretty = url.split('/')[-1].split('?')[0]
|
| 51 |
+
|
| 52 |
+
display(HTML(f"<h3 style='color:yellow;'>📥 Descargando: <code>{pretty}</code></h3>"
|
| 53 |
+
f"<h4 style='color:cyan;'>📁 Destino: <code>{dest}</code></h4>"))
|
| 54 |
+
|
| 55 |
+
subprocess.run([
|
| 56 |
+
"aria2c", "--console-log-level=error", "--content-disposition",
|
| 57 |
+
"-c", "-x", "6", "-s", "6", "-k", "1M",
|
| 58 |
+
"-d", dest, url_token
|
| 59 |
+
])
|
| 60 |
+
|
| 61 |
+
# ---------- HuggingFace ----------
|
| 62 |
+
else:
|
| 63 |
+
pretty = url.split('/')[-1].split('?')[0]
|
| 64 |
+
display(HTML(f"<h3 style='color:yellow;'>📥 Descargando: <code>{pretty}</code></h3>"
|
| 65 |
+
f"<h4 style='color:cyan;'>📁 Destino: <code>{dest}</code></h4>"))
|
| 66 |
+
subprocess.run([
|
| 67 |
+
"aria2c", "--console-log-level=error", "--content-disposition",
|
| 68 |
+
"-c", "-x", "8", "-s", "8", "-k", "1M",
|
| 69 |
+
"-d", dest, "-o", pretty, url
|
| 70 |
+
])
|
| 71 |
+
for f in os.listdir(dest):
|
| 72 |
+
if re.fullmatch(r'[0-9a-f]{64}', f):
|
| 73 |
+
os.rename(os.path.join(dest, f), os.path.join(dest, pretty))
|
| 74 |
+
break
|
| 75 |
+
|
| 76 |
+
display(HTML("<h3 style='color:lightgreen;'>✅ Descarga finalizada</h3>"))
|
| 77 |
+
|
| 78 |
+
# Registramos la línea mágica al importar el módulo
|
| 79 |
+
get_ipython().register_magic_function(download, magic_kind='line')
|