Download plugins/common.py from ElJoker63/THOR: direct link, hf CLI and curl.
- Browser
- Download file 2.57 kB
-
https://huggingface.co/spaces/ElJoker63/THOR/resolve/main/plugins/common.py
- Command line
-
hf download hf://spaces/ElJoker63/THOR/plugins/common.py
-
curl -L -o common.py https://huggingface.co/spaces/ElJoker63/THOR/resolve/main/plugins/common.py
2.57 kB
| class DownloadCancelled(Exception): | |
| """Excepción para cancelar descargas en vuelo.""" | |
| def __init__(self, message: str = "Descarga cancelada por solicitud del usuario"): | |
| super().__init__(message) | |
| def unwrap_redirect_url(url: str, timeout: int = 6) -> str: | |
| """ | |
| Desempaqueta URLs envueltas o enlaces cortos de redirección (ej. getsnap.link, thedylink.com) | |
| utilizando únicamente cabeceras HTTP nativas y análisis de parámetros estándar. | |
| NO utiliza ninguna API externa ni servicios de terceros. | |
| """ | |
| if not url or not isinstance(url, str): | |
| return url | |
| curr = url.strip() | |
| if not curr.startswith(("http://", "https://")): | |
| return curr | |
| from urllib.parse import parse_qs, urljoin, urlparse | |
| try: | |
| parsed = urlparse(curr) | |
| host = (parsed.hostname or "").lower() | |
| except Exception: | |
| return curr | |
| wrapper_domains = ("getsnap.link", "thedylink.com", "snaptube", "bit.ly", "tinyurl.com", "t.co") | |
| if not any(d in host for d in wrapper_domains): | |
| return curr | |
| import requests | |
| session = requests.Session() | |
| session.headers.update({ | |
| "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" | |
| }) | |
| try: | |
| for _ in range(5): | |
| parsed = urlparse(curr) | |
| host = (parsed.hostname or "").lower() | |
| qs = parse_qs(parsed.query) | |
| for key in ("url", "link", "target", "dest"): | |
| val = qs.get(key) | |
| if val and val[0].startswith(("http://", "https://")): | |
| curr = val[0] | |
| break | |
| if not any(d in host for d in wrapper_domains): | |
| break | |
| try: | |
| r = session.get(curr, allow_redirects=False, timeout=timeout) | |
| loc = r.headers.get("Location") | |
| if not loc: | |
| break | |
| if loc.startswith("intent://"): | |
| intent_p = urlparse(loc.replace("intent://", "http://", 1)) | |
| intent_qs = parse_qs(intent_p.query) | |
| if intent_qs.get("url"): | |
| curr = intent_qs["url"][0] | |
| break | |
| elif loc.startswith(("http://", "https://")): | |
| curr = loc | |
| else: | |
| curr = urljoin(curr, loc) | |
| except Exception: | |
| break | |
| except Exception: | |
| pass | |
| finally: | |
| session.close() | |
| return curr | |