Files
matrix/now-playing/lotus_now_playing.py
T
jared 3a0a4e15a8
Lint / Shell (shellcheck) (push) Successful in 10s
Lint / JS (eslint) (push) Successful in 7s
Lint / No Hookshot webhook IDs in README (push) Successful in 6s
Lint / No secrets in webhook configs (push) Successful in 6s
Lint / Landing page is rendered (matrix (push) Successful in 7s
Lint / Python (ruff) (push) Successful in 11s
Lint / Python deps (pip-audit) (push) Successful in 36s
Lint / Secret scan (gitleaks) (push) Successful in 5s
now-playing: Navidrome "Listening to" service for Lotus Chat (cinny #491) (#68)
2026-11-16 23:51:07 -05:00

494 lines
18 KiB
Python

#!/usr/bin/env python3
"""Now playing (Navidrome) -> Lotus Chat "Listening to" cards. [cinny #491]
Navidrome is the scrobbling master: every play, whether from Dropped Needle or
any Subsonic app, shows up in its `getNowPlaying` under the listener's own
username, which is their SSO (LLDAP) username. This service polls that list and
serves it to Lotus Chat for users who opted in, keyed by Matrix user ID.
- Who is asking: Lotus Chat sends a Matrix OpenID token (Authorization: Bearer).
It is checked against Synapse's /_matrix/federation/v1/openid/userinfo, so
only signed-in users of this homeserver can read anything.
- Matrix user -> SSO username: an override file first (accounts whose SSO link
carries no email), then the Synapse admin API (email <name>@<domain>, or a
legacy external ID that is the plain username).
- Opt-in: off by default, stored here in STATE_DIR/state.json.
- Cover art is proxied only for covers that an opted-in user is playing right
now, so the endpoint can't be used to browse the library.
Track titles, artists and tokens are never logged.
"""
import hashlib
import json
import logging
import os
import re
import secrets
import tempfile
import threading
import time
import urllib.error
import urllib.parse
import urllib.request
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
log = logging.getLogger("lotus-now-playing")
PREFIX = "/now-playing/v1"
MAX_IDS = 50
SUBSONIC_CLIENT = "lotus-now-playing"
SUBSONIC_VERSION = "1.16.1"
# Navidrome drops a session a while after the last report, so an entry older
# than this (no position updates, not paused) is treated as stale.
STALE_MINUTES = 30
COVER_SIZES = (64, 128, 256, 512)
UUID_RE = re.compile(r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
# --- pure helpers (unit tested) ----------------------------------------------
def parse_now_playing(payload, ignore_users=()):
"""getNowPlaying JSON -> {username: entry}. Keeps the most recent session per user."""
resp = (payload or {}).get("subsonic-response") or {}
if resp.get("status") != "ok":
raise ValueError((resp.get("error") or {}).get("message") or "getNowPlaying failed")
entries = (resp.get("nowPlaying") or {}).get("entry") or []
if isinstance(entries, dict):
entries = [entries]
out = {}
for e in entries:
user = (e.get("username") or "").strip()
if not user or user in ignore_users:
continue
state = (e.get("state") or "playing").lower()
if state not in ("playing", "paused"):
continue
minutes = int(e.get("minutesAgo") or 0)
if state == "playing" and minutes > STALE_MINUTES and not e.get("positionMs"):
continue
entry = {
"title": e.get("title") or "",
"artist": e.get("displayArtist") or e.get("artist") or "",
"album": e.get("album") or "",
"durationS": int(e.get("duration") or 0),
"positionMs": int(e.get("positionMs") or 0),
"state": state,
"player": e.get("playerName") or "",
"coverArt": e.get("coverArt") or "",
"minutesAgo": minutes,
}
if not entry["title"]:
continue
prev = out.get(user)
if prev is None or minutes < prev["minutesAgo"]:
out[user] = entry
return out
def username_from_admin_user(user, email_domain):
"""Synapse admin v2 user JSON -> SSO username, or None."""
for ext in user.get("external_ids") or []:
provider = ext.get("auth_provider") or ""
ext_id = ext.get("external_id") or ""
# One legacy link stores the plain username instead of Authelia's opaque ID.
if (provider.startswith("http") and re.fullmatch(r"[a-z0-9._-]+", ext_id)
and not UUID_RE.fullmatch(ext_id)):
return ext_id
suffix = "@" + email_domain.lower()
for tp in user.get("threepids") or []:
addr = (tp.get("address") or "").strip().lower()
if tp.get("medium") == "email" and addr.endswith(suffix):
local = addr[: -len(suffix)]
if re.fullmatch(r"[a-z0-9._-]+", local):
return local
return None
def public_entry(entry, now_ms):
"""What Lotus Chat gets for one listener."""
out = {k: entry[k] for k in ("title", "artist", "album", "durationS", "positionMs", "state", "player")}
out["updatedAt"] = now_ms
out["cover"] = f"{PREFIX}/cover/{urllib.parse.quote(entry['coverArt'], safe='')}" if entry.get("coverArt") else None
return out
def parse_ids(raw, server_name):
ids = []
for part in (raw or "").split(","):
part = part.strip()
if part.startswith("@") and part.endswith(":" + server_name) and part not in ids:
ids.append(part)
return ids[:MAX_IDS]
# --- stateful pieces ---------------------------------------------------------
class OptIns:
"""{mxid: True} persisted atomically."""
def __init__(self, path):
self.path = path
self.lock = threading.Lock()
self.data = {}
try:
with open(path) as f:
loaded = json.load(f)
self.data = {k: True for k, v in loaded.items() if v is True}
except FileNotFoundError:
pass
except (OSError, ValueError) as exc:
log.error("could not read %s: %s", path, exc)
def get(self, mxid):
with self.lock:
return self.data.get(mxid, False)
def all(self):
with self.lock:
return list(self.data)
def set(self, mxid, share):
with self.lock:
if share:
self.data[mxid] = True
else:
self.data.pop(mxid, None)
snapshot = dict(self.data)
d = os.path.dirname(self.path) or "."
fd, tmp = tempfile.mkstemp(dir=d, prefix=".state-")
try:
with os.fdopen(fd, "w") as f:
json.dump(snapshot, f, indent=1, sort_keys=True)
os.replace(tmp, self.path)
except BaseException:
try:
os.unlink(tmp)
except OSError:
pass
raise
class TTLCache:
def __init__(self, max_items=2000):
self.lock = threading.Lock()
self.items = {}
self.max_items = max_items
def get(self, key, now=None):
now = time.monotonic() if now is None else now
with self.lock:
hit = self.items.get(key)
if hit and hit[1] > now:
return hit[0], True
self.items.pop(key, None)
return None, False
def put(self, key, value, ttl, now=None):
now = time.monotonic() if now is None else now
with self.lock:
if len(self.items) >= self.max_items:
for k in [k for k, (_, exp) in self.items.items() if exp <= now] or list(self.items)[:100]:
self.items.pop(k, None)
self.items[key] = (value, now + ttl)
class Service:
def __init__(self, cfg, http_get=None):
self.cfg = cfg
self.http_get = http_get or _http_get
self.optins = OptIns(os.path.join(cfg["state_dir"], "state.json"))
self.tokens = TTLCache()
self.users = TTLCache()
self.lock = threading.Lock()
self.playing = {}
self.overrides = {}
self.overrides_mtime = None
self.load_overrides()
# Navidrome
def subsonic_url(self, method, **params):
salt = secrets.token_hex(8)
token = hashlib.md5((self.cfg["nd_password"] + salt).encode()).hexdigest() # noqa: S324 (Subsonic protocol)
q = {"u": self.cfg["nd_user"], "t": token, "s": salt, "v": SUBSONIC_VERSION,
"c": SUBSONIC_CLIENT, "f": "json", **params}
return f"{self.cfg['nd_url']}/rest/{method}?{urllib.parse.urlencode(q)}"
def poll_once(self):
status, body, _ = self.http_get(self.subsonic_url("getNowPlaying"), {}, 10)
if status != 200:
raise ValueError(f"getNowPlaying HTTP {status}")
parsed = parse_now_playing(json.loads(body), self.cfg["ignore_users"])
with self.lock:
self.playing = parsed
return len(parsed)
def poll_forever(self):
failures = 0
while True:
try:
n = self.poll_once()
if failures:
log.info("navidrome reachable again (%d sessions)", n)
failures = 0
except Exception as exc: # keep polling whatever happens
failures += 1
if failures in (1, 10) or failures % 100 == 0:
log.warning("poll failed (%d in a row): %s", failures, exc)
if failures >= 3:
with self.lock:
self.playing = {}
time.sleep(self.cfg["poll_s"])
# identity
def verify_token(self, token):
if not token or len(token) > 512:
return None
cached, hit = self.tokens.get(token)
if hit:
return cached
url = (f"{self.cfg['synapse']}/_matrix/federation/v1/openid/userinfo?"
+ urllib.parse.urlencode({"access_token": token}))
try:
status, body, _ = self.http_get(url, {}, 5)
except Exception as exc:
log.warning("openid check failed: %s", exc)
return None
mxid = None
if status == 200:
sub = (json.loads(body) or {}).get("sub") or ""
if sub.startswith("@") and sub.endswith(":" + self.cfg["server_name"]):
mxid = sub
# Tokens live about an hour; a rejected one is remembered briefly.
self.tokens.put(token, mxid, 600 if mxid else 30)
return mxid
def load_overrides(self):
path = self.cfg["users_file"]
try:
mtime = os.path.getmtime(path)
except OSError:
self.overrides = {}
return
if mtime == self.overrides_mtime:
return
try:
with open(path) as f:
data = json.load(f)
self.overrides = {k: v for k, v in data.items() if isinstance(v, str) and v}
self.overrides_mtime = mtime
except (OSError, ValueError) as exc:
log.error("could not read %s: %s", path, exc)
def resolve(self, mxid):
self.load_overrides()
if mxid in self.overrides:
return self.overrides[mxid]
cached, hit = self.users.get(mxid)
if hit:
return cached
name = None
url = f"{self.cfg['synapse']}/_synapse/admin/v2/users/{urllib.parse.quote(mxid)}"
try:
status, body, _ = self.http_get(url, {"Authorization": f"Bearer {self.cfg['matrix_token']}"}, 5)
if status == 200:
name = username_from_admin_user(json.loads(body), self.cfg["email_domain"])
elif status != 404:
return None # don't cache transient failures
except Exception as exc:
log.warning("user lookup failed: %s", exc)
return None
self.users.put(mxid, name, 3600 if name else 300)
return name
# views
def entry_for(self, mxid):
if not self.optins.get(mxid):
return None
name = self.resolve(mxid)
if not name:
return None
with self.lock:
return self.playing.get(name)
def users_view(self, ids):
now_ms = int(time.time() * 1000)
out = {}
for mxid in ids:
entry = self.entry_for(mxid)
if entry:
out[mxid] = public_entry(entry, now_ms)
return out
def me_view(self, mxid):
name = self.resolve(mxid)
share = self.optins.get(mxid)
entry = None
if name:
with self.lock:
entry = self.playing.get(name)
return {
"share": share,
"linked": bool(name),
"playing": public_entry(entry, int(time.time() * 1000)) if entry else None,
}
def cover_allowed(self, cover_id):
with self.lock:
playing = dict(self.playing)
wanted = {name for name, e in playing.items() if e.get("coverArt") == cover_id}
if not wanted:
return False
return any(self.resolve(m) in wanted for m in self.optins.all())
# --- HTTP --------------------------------------------------------------------
def _http_get(url, headers, timeout):
req = urllib.request.Request(url, headers=headers)
try:
with urllib.request.urlopen(req, timeout=timeout) as resp:
return resp.status, resp.read(), resp.headers.get("Content-Type", "")
except urllib.error.HTTPError as exc:
return exc.code, exc.read(), exc.headers.get("Content-Type", "")
class Handler(BaseHTTPRequestHandler):
server_version = "lotus-now-playing"
service: Service = None # set in main()
def log_message(self, fmt, *args): # no request logs: paths carry user IDs
pass
def _cors(self):
self.send_header("Access-Control-Allow-Origin", "*")
self.send_header("Access-Control-Allow-Headers", "Authorization, Content-Type")
self.send_header("Access-Control-Allow-Methods", "GET, PUT, OPTIONS")
self.send_header("Access-Control-Max-Age", "600")
def _json(self, status, obj):
body = json.dumps(obj).encode()
self.send_response(status)
self._cors()
self.send_header("Content-Type", "application/json")
self.send_header("Cache-Control", "no-store")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def _caller(self):
auth = self.headers.get("Authorization") or ""
token = auth[7:].strip() if auth.lower().startswith("bearer ") else ""
return self.service.verify_token(token)
def do_OPTIONS(self):
self.send_response(204)
self._cors()
self.end_headers()
def do_GET(self):
url = urllib.parse.urlsplit(self.path)
if url.path == "/healthz":
return self._json(200, {"ok": True})
if url.path.startswith(PREFIX + "/cover/"):
return self._cover(urllib.parse.unquote(url.path[len(PREFIX) + 7:]), url.query)
mxid = self._caller()
if not mxid:
return self._json(401, {"errcode": "M_UNKNOWN_TOKEN", "error": "Matrix OpenID token required"})
if url.path == PREFIX + "/me":
return self._json(200, self.service.me_view(mxid))
if url.path == PREFIX + "/users":
raw = urllib.parse.parse_qs(url.query).get("ids", [""])[0]
ids = parse_ids(raw, self.service.cfg["server_name"])
return self._json(200, {"users": self.service.users_view(ids)})
return self._json(404, {"errcode": "M_NOT_FOUND"})
def do_PUT(self):
url = urllib.parse.urlsplit(self.path)
if url.path != PREFIX + "/me":
return self._json(404, {"errcode": "M_NOT_FOUND"})
mxid = self._caller()
if not mxid:
return self._json(401, {"errcode": "M_UNKNOWN_TOKEN", "error": "Matrix OpenID token required"})
try:
length = int(self.headers.get("Content-Length") or 0)
body = json.loads(self.rfile.read(min(length, 4096)) or b"{}")
share = body["share"]
if not isinstance(share, bool):
raise ValueError
except (ValueError, KeyError, TypeError):
return self._json(400, {"errcode": "M_BAD_JSON", "error": "expected {\"share\": true|false}"})
try:
self.service.optins.set(mxid, share)
except OSError as exc:
log.error("could not save opt-in: %s", exc)
return self._json(500, {"errcode": "M_UNKNOWN", "error": "could not save"})
log.info("sharing %s (now %d users)", "on" if share else "off", len(self.service.optins.all()))
return self._json(200, self.service.me_view(mxid))
def _cover(self, cover_id, query):
if not cover_id or len(cover_id) > 128 or not self.service.cover_allowed(cover_id):
return self._json(404, {"errcode": "M_NOT_FOUND"})
try:
size = int(urllib.parse.parse_qs(query).get("size", ["256"])[0])
except ValueError:
size = 256
size = min(COVER_SIZES, key=lambda s: abs(s - size))
try:
status, body, ctype = self.service.http_get(
self.service.subsonic_url("getCoverArt", id=cover_id, size=size), {}, 10)
except Exception:
status, body, ctype = 502, b"", ""
if status != 200 or not ctype.startswith("image/"):
return self._json(502, {"errcode": "M_UNKNOWN", "error": "cover unavailable"})
self.send_response(200)
self._cors()
self.send_header("Content-Type", ctype)
self.send_header("Cache-Control", "public, max-age=300")
self.send_header("Content-Length", str(len(body)))
self.end_headers()
self.wfile.write(body)
def load_config(env=os.environ):
def need(name):
value = env.get(name, "")
if not value:
raise SystemExit(f"missing {name}")
return value
return {
"bind": env.get("BIND_HOST", "0.0.0.0"),
"port": int(env.get("BIND_PORT", "8072")),
"server_name": env.get("SERVER_NAME", "matrix.lotusguild.org"),
"email_domain": env.get("EMAIL_DOMAIN", "lotusguild.org"),
"synapse": env.get("SYNAPSE_API", "http://127.0.0.1:8008").rstrip("/"),
"matrix_token": need("MATRIX_TOKEN"),
"nd_url": need("NAVIDROME_URL").rstrip("/"),
"nd_user": need("NAVIDROME_USER"),
"nd_password": need("NAVIDROME_PASSWORD"),
"ignore_users": tuple(u for u in env.get("IGNORE_USERS", "droppedneedle").split(",") if u),
"state_dir": env.get("STATE_DIR", "/var/lib/lotus-now-playing"),
"users_file": env.get("USERS_FILE", "/etc/lotus-now-playing/users.json"),
"poll_s": max(5, int(env.get("POLL_SECONDS", "10"))),
}
def main():
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
cfg = load_config()
service = Service(cfg)
try:
log.info("navidrome ok, %d sessions", service.poll_once())
except Exception as exc:
log.warning("first poll failed: %s", exc)
threading.Thread(target=service.poll_forever, daemon=True).start()
Handler.service = service
server = ThreadingHTTPServer((cfg["bind"], cfg["port"]), Handler)
server.daemon_threads = True
log.info("listening on %s:%d (%d users sharing)", cfg["bind"], cfg["port"], len(service.optins.all()))
server.serve_forever()
if __name__ == "__main__":
main()