f
This commit is contained in:
@@ -2753,243 +2753,14 @@ def register_url_with_local_library(
|
||||
return False
|
||||
|
||||
|
||||
def resolve_tidal_manifest_path(item: Any) -> Optional[str]:
|
||||
"""Persist the Tidal manifest from search results and return a local path."""
|
||||
|
||||
metadata = None
|
||||
if isinstance(item, dict):
|
||||
metadata = item.get("full_metadata") or item.get("metadata")
|
||||
else:
|
||||
metadata = getattr(item, "full_metadata", None) or getattr(item, "metadata", None)
|
||||
|
||||
if not isinstance(metadata, dict):
|
||||
try:
|
||||
# Provider-specific implementation lives with the provider code.
|
||||
from Provider.tidal_manifest import resolve_tidal_manifest_path
|
||||
except Exception: # pragma: no cover
|
||||
def resolve_tidal_manifest_path(item: Any) -> Optional[str]:
|
||||
_ = item
|
||||
return None
|
||||
|
||||
existing_path = metadata.get("_tidal_manifest_path")
|
||||
if existing_path:
|
||||
try:
|
||||
resolved = Path(str(existing_path))
|
||||
if resolved.is_file():
|
||||
return str(resolved)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
existing_url = metadata.get("_tidal_manifest_url")
|
||||
if existing_url and isinstance(existing_url, str):
|
||||
candidate = existing_url.strip()
|
||||
if candidate:
|
||||
return candidate
|
||||
|
||||
raw_manifest = metadata.get("manifest")
|
||||
if not raw_manifest:
|
||||
# When piping directly from the Tidal search table, we may only have a track id.
|
||||
# Fetch track details from the proxy so downstream stages can decode the manifest.
|
||||
try:
|
||||
already = bool(metadata.get("_tidal_track_details_fetched"))
|
||||
except Exception:
|
||||
already = False
|
||||
|
||||
track_id = metadata.get("trackId") or metadata.get("id")
|
||||
if track_id is None:
|
||||
try:
|
||||
if isinstance(item, dict):
|
||||
candidate_path = item.get("path") or item.get("url")
|
||||
else:
|
||||
candidate_path = getattr(item, "path", None) or getattr(item, "url", None)
|
||||
except Exception:
|
||||
candidate_path = None
|
||||
|
||||
if candidate_path:
|
||||
m = re.search(
|
||||
r"(tidal|hifi):(?://)?track[\\/](\d+)",
|
||||
str(candidate_path),
|
||||
flags=re.IGNORECASE,
|
||||
)
|
||||
if m:
|
||||
track_id = m.group(2)
|
||||
|
||||
if (not already) and track_id is not None:
|
||||
try:
|
||||
track_int = int(track_id)
|
||||
except Exception:
|
||||
track_int = None
|
||||
|
||||
if track_int and track_int > 0:
|
||||
try:
|
||||
import httpx
|
||||
|
||||
resp = httpx.get(
|
||||
"https://tidal-api.binimum.org/track/",
|
||||
params={"id": str(track_int)},
|
||||
timeout=10.0,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
payload = resp.json()
|
||||
data = payload.get("data") if isinstance(payload, dict) else None
|
||||
if isinstance(data, dict) and data:
|
||||
try:
|
||||
metadata.update(data)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
metadata["_tidal_track_details_fetched"] = True
|
||||
except Exception:
|
||||
pass
|
||||
if not metadata.get("url"):
|
||||
try:
|
||||
resp_info = httpx.get(
|
||||
"https://tidal-api.binimum.org/info/",
|
||||
params={"id": str(track_int)},
|
||||
timeout=10.0,
|
||||
)
|
||||
resp_info.raise_for_status()
|
||||
info_payload = resp_info.json()
|
||||
info_data = info_payload.get("data") if isinstance(info_payload, dict) else None
|
||||
if isinstance(info_data, dict) and info_data:
|
||||
try:
|
||||
for k, v in info_data.items():
|
||||
if k not in metadata:
|
||||
metadata[k] = v
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
if info_data.get("url"):
|
||||
metadata["url"] = info_data.get("url")
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
raw_manifest = metadata.get("manifest")
|
||||
if not raw_manifest:
|
||||
return None
|
||||
|
||||
manifest_str = "".join(str(raw_manifest or "").split())
|
||||
if not manifest_str:
|
||||
return None
|
||||
|
||||
manifest_bytes: bytes
|
||||
try:
|
||||
manifest_bytes = base64.b64decode(manifest_str, validate=True)
|
||||
except Exception:
|
||||
try:
|
||||
manifest_bytes = base64.b64decode(manifest_str, validate=False)
|
||||
except Exception:
|
||||
try:
|
||||
manifest_bytes = manifest_str.encode("utf-8")
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
if not manifest_bytes:
|
||||
return None
|
||||
|
||||
head = (manifest_bytes[:1024] or b"").lstrip()
|
||||
if head.startswith((b"{", b"[")):
|
||||
try:
|
||||
text = manifest_bytes.decode("utf-8", errors="ignore")
|
||||
payload = json.loads(text)
|
||||
urls = payload.get("urls") or []
|
||||
selected_url = None
|
||||
for candidate in urls:
|
||||
if isinstance(candidate, str):
|
||||
candidate = candidate.strip()
|
||||
if candidate:
|
||||
selected_url = candidate
|
||||
break
|
||||
if selected_url:
|
||||
try:
|
||||
metadata["_tidal_manifest_url"] = selected_url
|
||||
except Exception:
|
||||
pass
|
||||
return selected_url
|
||||
try:
|
||||
metadata["_tidal_manifest_error"] = "JSON manifest contained no urls"
|
||||
except Exception:
|
||||
pass
|
||||
log(
|
||||
f"[tidal] JSON manifest for track {metadata.get('trackId') or metadata.get('id')} had no playable urls",
|
||||
file=sys.stderr,
|
||||
)
|
||||
except Exception as exc:
|
||||
try:
|
||||
metadata["_tidal_manifest_error"] = (
|
||||
f"Failed to parse JSON manifest: {exc}"
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
log(
|
||||
f"[tidal] Failed to parse JSON manifest for track {metadata.get('trackId') or metadata.get('id')}: {exc}",
|
||||
file=sys.stderr,
|
||||
)
|
||||
return None
|
||||
|
||||
looks_like_mpd = (
|
||||
head.startswith(b"<?xml")
|
||||
or head.startswith(b"<MPD")
|
||||
or b"<MPD" in head
|
||||
)
|
||||
|
||||
if not looks_like_mpd:
|
||||
manifest_mime = str(metadata.get("manifestMimeType") or "").strip().lower()
|
||||
try:
|
||||
metadata["_tidal_manifest_error"] = (
|
||||
f"Decoded manifest is not an MPD XML (mime: {manifest_mime or 'unknown'})"
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
log(
|
||||
f"[tidal] Decoded manifest is not an MPD XML for track {metadata.get('trackId') or metadata.get('id')} (mime {manifest_mime or 'unknown'})",
|
||||
file=sys.stderr,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
manifest_hash = str(metadata.get("manifestHash") or "").strip()
|
||||
track_id = metadata.get("trackId") or metadata.get("id")
|
||||
identifier = manifest_hash or hashlib.sha256(manifest_bytes).hexdigest()
|
||||
identifier_safe = re.sub(r"[^A-Za-z0-9_-]+", "_", identifier)[:64]
|
||||
if not identifier_safe:
|
||||
identifier_safe = hashlib.sha256(manifest_bytes).hexdigest()[:12]
|
||||
|
||||
track_safe = "tidal"
|
||||
if track_id is not None:
|
||||
track_safe = re.sub(r"[^A-Za-z0-9_-]+", "_", str(track_id))[:32]
|
||||
if not track_safe:
|
||||
track_safe = "tidal"
|
||||
|
||||
# Persist as .mpd for DASH manifests.
|
||||
ext = "mpd"
|
||||
|
||||
manifest_dir = Path(tempfile.gettempdir()) / "medeia" / "tidal"
|
||||
try:
|
||||
manifest_dir.mkdir(parents=True, exist_ok=True)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
filename = f"tidal-{track_safe}-{identifier_safe[:24]}.{ext}"
|
||||
target_path = manifest_dir / filename
|
||||
try:
|
||||
with open(target_path, "wb") as fh:
|
||||
fh.write(manifest_bytes)
|
||||
metadata["_tidal_manifest_path"] = str(target_path)
|
||||
if isinstance(item, dict):
|
||||
if item.get("full_metadata") is metadata:
|
||||
item["full_metadata"] = metadata
|
||||
elif item.get("metadata") is metadata:
|
||||
item["metadata"] = metadata
|
||||
else:
|
||||
extra = getattr(item, "extra", None)
|
||||
if isinstance(extra, dict):
|
||||
extra["_tidal_manifest_path"] = str(target_path)
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
return str(target_path)
|
||||
|
||||
def check_url_exists_in_storage(
|
||||
urls: Sequence[str],
|
||||
storage: Any,
|
||||
|
||||
@@ -568,14 +568,6 @@ class Add_File(Cmdlet):
|
||||
progress.step("ingesting file")
|
||||
|
||||
if provider_name:
|
||||
if str(provider_name).strip().lower() == "matrix":
|
||||
log(
|
||||
"Matrix uploads are handled by .matrix (not add-file).",
|
||||
file=sys.stderr,
|
||||
)
|
||||
failures += 1
|
||||
continue
|
||||
|
||||
code = self._handle_provider_upload(
|
||||
media_path,
|
||||
provider_name,
|
||||
|
||||
@@ -287,48 +287,29 @@ class search_file(Cmdlet):
|
||||
results = provider.search(query, limit=limit, filters=search_filters or None)
|
||||
debug(f"[search-file] {provider_name} -> {len(results or [])} result(s)")
|
||||
|
||||
# Tidal artist UX: if there is exactly one artist match, auto-expand
|
||||
# directly to albums without requiring an explicit @1 selection.
|
||||
if (
|
||||
provider_lower == "tidal"
|
||||
and table_meta.get("view") == "artist"
|
||||
and isinstance(results, list)
|
||||
and len(results) == 1
|
||||
):
|
||||
try:
|
||||
artist_res = results[0]
|
||||
artist_name = str(getattr(artist_res, "title", "") or "").strip()
|
||||
artist_md = getattr(artist_res, "full_metadata", None)
|
||||
artist_id = None
|
||||
if isinstance(artist_md, dict):
|
||||
raw_id = artist_md.get("artistId") or artist_md.get("id")
|
||||
try:
|
||||
artist_id = int(raw_id) if raw_id is not None else None
|
||||
except Exception:
|
||||
artist_id = None
|
||||
|
||||
album_results = []
|
||||
if hasattr(provider, "_albums_for_artist") and callable(getattr(provider, "_albums_for_artist")):
|
||||
try:
|
||||
album_results = provider._albums_for_artist( # type: ignore[attr-defined]
|
||||
artist_id=artist_id,
|
||||
artist_name=artist_name,
|
||||
limit=max(int(limit or 0), 200),
|
||||
)
|
||||
except Exception:
|
||||
album_results = []
|
||||
|
||||
if album_results:
|
||||
results = album_results
|
||||
table_type = "tidal.album"
|
||||
# Allow providers to apply provider-specific UX transforms (e.g. auto-expansion)
|
||||
try:
|
||||
post = getattr(provider, "postprocess_search_results", None)
|
||||
if callable(post) and isinstance(results, list):
|
||||
results, table_type_override, table_meta_override = post(
|
||||
query=query,
|
||||
results=results,
|
||||
filters=search_filters or None,
|
||||
limit=int(limit or 0),
|
||||
table_type=str(table_type or ""),
|
||||
table_meta=dict(table_meta) if isinstance(table_meta, dict) else None,
|
||||
)
|
||||
if table_type_override:
|
||||
table_type = str(table_type_override)
|
||||
table.set_table(table_type)
|
||||
table_meta["view"] = "album"
|
||||
if isinstance(table_meta_override, dict) and table_meta_override:
|
||||
table_meta = dict(table_meta_override)
|
||||
try:
|
||||
table.set_table_metadata(table_meta)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if not results:
|
||||
log(f"No results found for query: {query}", file=sys.stderr)
|
||||
|
||||
Reference in New Issue
Block a user