Improvements to dynamic colors and YT-DL imports.
This commit is contained in:
1 parent
ad68f9f5e9
commit
ed72ded98f
8 files changed
+861
-62
No files matched your search
@@ -1,8 +1,11 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
from pathlib import Path
|
||||
from urllib import error as urlerror
|
||||
from urllib import request as urlrequest
|
||||
|
||||
from ..config import UPLOAD_DIR
|
||||
from ..repositories import jobs as jobs_repo
|
||||
@@ -11,6 +14,37 @@ from .metadata_util import _read_cover_art, read_metadata
|
||||
from .project_service_common import ProjectOperationError, _infer_stem_name, project_output_dir
|
||||
|
||||
|
||||
def _sidecar_path(media_path: Path) -> Path:
|
||||
return media_path.with_suffix(media_path.suffix + ".json")
|
||||
|
||||
|
||||
def _read_import_sidecar(media_path: Path) -> dict:
|
||||
sidecar = _sidecar_path(media_path)
|
||||
if not sidecar.exists():
|
||||
return {}
|
||||
try:
|
||||
raw = json.loads(sidecar.read_text(encoding="utf-8"))
|
||||
return raw if isinstance(raw, dict) else {}
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
|
||||
def _read_source_thumbnail_url(source_path: Path) -> str:
|
||||
sidecar = _read_import_sidecar(source_path)
|
||||
value = str(sidecar.get("thumbnail") or "").strip()
|
||||
if value.startswith(("http://", "https://")):
|
||||
return value
|
||||
return ""
|
||||
|
||||
|
||||
def _fetch_remote_image(url: str) -> tuple[bytes, str | None]:
|
||||
req = urlrequest.Request(url, headers={"User-Agent": "StemEngine/cover-art-fallback"})
|
||||
with urlrequest.urlopen(req, timeout=8) as resp:
|
||||
data = resp.read()
|
||||
mime = resp.headers.get_content_type() if resp.headers else None
|
||||
return data, mime
|
||||
|
||||
|
||||
def list_projects() -> list[dict]:
|
||||
return projects_repo.list_all()
|
||||
|
||||
@@ -73,6 +107,7 @@ def build_project_detail(project: dict) -> dict:
|
||||
|
||||
source_artist = ""
|
||||
source_title = ""
|
||||
source_thumbnail_url = ""
|
||||
source_file = next(
|
||||
(file_entry for file_entry in (sep_job or {}).get("files", []) if file_entry.get("type") == "source"),
|
||||
None,
|
||||
@@ -80,6 +115,7 @@ def build_project_detail(project: dict) -> dict:
|
||||
if source_file:
|
||||
src_path = source_file.get("path", "")
|
||||
if src_path and Path(src_path).exists():
|
||||
source_thumbnail_url = _read_source_thumbnail_url(Path(src_path))
|
||||
try:
|
||||
meta = read_metadata(src_path)
|
||||
source_artist = meta.get("artist") or meta.get("albumartist") or ""
|
||||
@@ -96,6 +132,7 @@ def build_project_detail(project: dict) -> dict:
|
||||
"mix_job": mix_job,
|
||||
"source_artist": source_artist,
|
||||
"source_title": source_title,
|
||||
"source_thumbnail_url": source_thumbnail_url,
|
||||
}
|
||||
|
||||
|
||||
@@ -206,6 +243,18 @@ def get_project_cover_art(project: dict) -> tuple[bytes, str]:
|
||||
raise ProjectOperationError(404, "Source file not found")
|
||||
|
||||
data, mime = _read_cover_art(source_path)
|
||||
if not data:
|
||||
raise ProjectOperationError(404, "No cover art")
|
||||
return data, mime or "image/jpeg"
|
||||
if data:
|
||||
return data, mime or "image/jpeg"
|
||||
|
||||
thumbnail_url = _read_source_thumbnail_url(Path(source_path))
|
||||
if thumbnail_url:
|
||||
try:
|
||||
remote_data, remote_mime = _fetch_remote_image(thumbnail_url)
|
||||
if remote_data:
|
||||
return remote_data, remote_mime or "image/jpeg"
|
||||
except (urlerror.URLError, TimeoutError, ValueError, OSError):
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
raise ProjectOperationError(404, "No cover art")
|
||||
@@ -29,4 +29,5 @@ class ProjectDetailTests(BackendTestCase):
|
||||
self.assertEqual(payload["lrc_jobs"]["vocals"]["id"], lrc_job_id)
|
||||
self.assertEqual(payload["mix_job"]["id"], mix_job_id)
|
||||
self.assertEqual(payload["source_artist"], "")
|
||||
self.assertEqual(payload["source_title"], "")
|
||||
self.assertEqual(payload["source_title"], "")
|
||||
self.assertEqual(payload["source_thumbnail_url"], "")
|
||||
Reference in new issue
Block a user