Download plugins/applemusic.py from ElJoker63/THOR: direct link, hf CLI and curl.
- Browser
- Download file 1.93 kB
-
https://huggingface.co/spaces/ElJoker63/THOR/resolve/main/plugins/applemusic.py
- Command line
-
hf download hf://spaces/ElJoker63/THOR/plugins/applemusic.py
-
curl -L -o applemusic.py https://huggingface.co/spaces/ElJoker63/THOR/resolve/main/plugins/applemusic.py
1.93 kB
| import logging | |
| import os | |
| import re | |
| from urllib.parse import quote_plus | |
| from typing import Optional | |
| import requests | |
| from plugins.common import DownloadCancelled | |
| from plugins.stream import download_stream_url | |
| logger = logging.getLogger("worker.plugins.applemusic") | |
| def download_applemusic( | |
| url: str, | |
| output_dir: str, | |
| progress_fn=None, | |
| cancel_event=None | |
| ) -> str: | |
| """ | |
| Descarga canciones de Apple Music con fallback automático. | |
| """ | |
| if cancel_event and cancel_event.is_set(): | |
| raise DownloadCancelled() | |
| os.makedirs(output_dir, exist_ok=True) | |
| delirius_key = os.getenv("DELIRIUS_API_KEY", "") | |
| try: | |
| enc_url = quote_plus(url.strip()) | |
| api_url = f"https://s.delirius.site/download/applemusic?url={enc_url}" | |
| if delirius_key: | |
| api_url += f"&apikey={delirius_key}" | |
| resp = requests.get(api_url, timeout=25) | |
| if resp.status_code == 200: | |
| json_data = resp.json() | |
| if json_data.get("status") and "data" in json_data: | |
| data = json_data["data"] | |
| dl_url = data.get("download") or data.get("url") or data.get("music") | |
| if dl_url: | |
| title = data.get("title", "track") | |
| artist = data.get("author", data.get("artist", "")) | |
| clean_name = f"{artist} - {title}.mp3".strip(" -") | |
| clean_name = re.sub(r'[\/\\:\*\?"<>\|\x00-\x1f]', '_', clean_name) | |
| target = os.path.join(output_dir, clean_name) | |
| out_path, _ = download_stream_url(dl_url, target, progress_fn=progress_fn, cancel_event=cancel_event) | |
| if os.path.exists(out_path): | |
| return out_path | |
| except Exception as e: | |
| logger.warning(f"Descarga falló para Apple Music: {e}") | |
| raise RuntimeError("No se pudo obtener el audio de Apple Music para este enlace.") | |