From 38acb8bccb6c0719ce418e8acacd759245113dd1 Mon Sep 17 00:00:00 2001 From: ltadeu6 Date: Sun, 7 Jun 2026 10:35:23 -0300 Subject: [PATCH] feat: add /art proxy endpoint and fix /current to expose rgb+art_url Co-Authored-By: Claude Sonnet 4.6 --- luz-musica.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/luz-musica.py b/luz-musica.py index 2ac8aef..51f6bb5 100644 --- a/luz-musica.py +++ b/luz-musica.py @@ -86,6 +86,18 @@ class ControlState: "running": True, } + def set_current(self, art_url: str, rgb: tuple[int, int, int]) -> None: + with self._lock: + self._art_url = art_url + self._rgb = rgb + + def current(self) -> dict[str, object]: + with self._lock: + return { + "art_url": getattr(self, "_art_url", None), + "rgb": list(getattr(self, "_rgb", [0, 200, 255])), + } + CONTROL_STATE = ControlState() @@ -98,6 +110,27 @@ class ControlHandler(BaseHTTPRequestHandler): if self.path == "/status": self.write_json(200, CONTROL_STATE.snapshot()) return + if self.path == "/current": + self.write_json(200, CONTROL_STATE.current()) + return + if self.path.startswith("/art"): + art_url = CONTROL_STATE.current().get("art_url") + if not art_url: + self.write_json(404, {"error": "no art"}) + return + try: + resp = requests.get(art_url, timeout=5) + body = resp.content + ctype = resp.headers.get("Content-Type", "image/jpeg") + self.send_response(200) + self.send_header("Content-Type", ctype) + self.send_header("Content-Length", str(len(body))) + self.send_header("Access-Control-Allow-Origin", "*") + self.end_headers() + self.wfile.write(body) + except Exception: + self.write_json(502, {"error": "fetch failed"}) + return self.write_json(404, {"error": "not found"}) def do_POST(self) -> None: @@ -537,6 +570,7 @@ def main() -> None: print(f"Cor base: {raw_rgb} -> paleta: {palette_rgb}") print(f"Aplicando cor: {rgb}") set_light_color(rgb) + CONTROL_STATE.set_current(art_url, rgb) last_track = track_id last_rgb = rgb