summaryrefslogtreecommitdiffstats
path: root/packages/meshbay-node/src/meshbay_node/indexer/title_parse.py
diff options
context:
space:
mode:
authorChristophe Besson <cbesson@gmail.com>2026-08-24 17:12:36 +0200
committerChristophe Besson <cbesson@gmail.com>2026-08-24 17:12:36 +0200
commit941d1a135dd7b03834576855e8e9fdaa24c4e406 (patch)
tree5d7c27d45a3f1e77320e089f6a4522b8383cbd45 /packages/meshbay-node/src/meshbay_node/indexer/title_parse.py
parent16bc07acf053d7d14f8182f5523da1d179154a15 (diff)
downloadmeshbay-941d1a135dd7b03834576855e8e9fdaa24c4e406.tar.gz
feat(node): Music app node-side — indexing, MusicBrainz enrichment, protocol
Implements the node half of docs/musicbay.md against MNP 0.8: - IndexEntry gains artist/album/track_no (reuses duration/thumb_hash/ display_title, already generic). New musicbrainz_config/_enabled and music_meta_req/_resp message pairs, mirroring the TMDB shape. - title_parse.parse_track_filename: track-number-prefix + title parsing, fallback-only (embedded tags are the primary source, unlike Videos). - indexer.enrich_audio.AudioEnricher: mutagen-based tag/embedded-cover extraction through its own bounded pool (asyncio.to_thread, no subprocess — no ffmpeg-shaped deadlock risk). Gated on "music" in a group's enabled_apps rather than a video_root-style scoped folder. - musicbrainz.py: MusicBrainzClient — no API key (unlike TMDB), just a self-imposed ~1 req/s pace and a configurable, non-default User-Agent contact string; inert (no calls at all) when no contact is configured, never sends an unidentified client. - media_cache.py: file_mbid/mbid_meta tables alongside the existing TMDB ones, cover art reusing the thumbs table via a synthetic musicbrainz:{mbid} id, pruned on file deletion. - roster.py/ops.py/webrtc_server.py: musicbrainz_contact (node-wide) and musicbrainz_enabled (per-group, from the start) as signed operator settings, ALLOWED_APPS gains "music", _do_music_meta_request resolves and caches a release-level MusicBrainz match per (artist, album). - daemon.py: AudioEnricher/MusicBrainzClient wired alongside the video ones; a group's existing library is swept when "music" is newly enabled (no video_root equivalent — see musicbay.md §2.1). 41 new tests (musicbrainz.py against a mocked transport, admin-op policy for both new settings, media_cache round-trip/pruning, enrich_audio end-to-end against real ffmpeg-generated MP3s). Full suite (common + node + hub): 1116 passed, no regressions. Client-side (music-app.js, persistent player bar) not started yet. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KBi7ALLGfwcjBXt57yNMcy
Diffstat (limited to 'packages/meshbay-node/src/meshbay_node/indexer/title_parse.py')
-rw-r--r--packages/meshbay-node/src/meshbay_node/indexer/title_parse.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/packages/meshbay-node/src/meshbay_node/indexer/title_parse.py b/packages/meshbay-node/src/meshbay_node/indexer/title_parse.py
index ef522ad..6676e9b 100644
--- a/packages/meshbay-node/src/meshbay_node/indexer/title_parse.py
+++ b/packages/meshbay-node/src/meshbay_node/indexer/title_parse.py
@@ -146,6 +146,47 @@ def parse_movie_filename(filename: str) -> ParsedName:
)
+
+# ── Music app (docs/musicbay.md §2.1) ────────────────────────────────────────
+#
+# Filename parsing is the *fallback* here, not the primary source (unlike
+# Videos, where guessit does all the work): embedded ID3/Vorbis tags are read
+# first by enrich_audio.py, and this only fills whatever a tag left empty.
+# Scope is narrower than the video parser too — a track number and a title,
+# nothing guessit-shaped is needed since there is no season/episode grammar
+# to parse.
+
+# "01 - Venus As A Boy.mp3", "03. Human Behaviour.mp3", "12_Some_Title.mp3" —
+# a leading track number, optionally disc-prefixed ("1-01 "), then a
+# separator before the title. Capped at 3 digits so a filename that merely
+# starts with a year ("1999 - Some Title.mp3") isn't misread as track 199.
+_TRACK_PREFIX_RE = re.compile(r"^(?:\d+[\s._-]+)?(\d{1,3})[\s._-]+(?=\S)")
+
+
+@dataclass
+class ParsedTrack:
+ title: str | None
+ track_no: int | None
+ naive_title: str = ""
+
+
+def parse_track_filename(filename: str) -> ParsedTrack:
+ """
+ Split a leading track-number prefix from the rest of the filename and
+ clean up the remainder into a title. `track_no` is None when there's no
+ recognizable prefix — the caller (enrich_audio.py) then falls back to
+ the tag or leaves it unset, never guesses a number.
+ """
+ stem = filename.rsplit(".", 1)[0] if "." in filename else filename
+ m = _TRACK_PREFIX_RE.match(stem)
+ track_no = int(m.group(1)) if m else None
+ rest = stem[m.end():] if m else stem
+ rest = re.sub(r"[._]+", " ", rest)
+ rest = re.sub(r"^[\s-]+", "", rest) # a leftover " - " separator
+ title = re.sub(r"\s+", " ", rest).strip() or None
+ return ParsedTrack(title=title, track_no=track_no, naive_title=naive_title(filename))
+
+
def parse_episode_filename(filename: str) -> ParsedName:
"""
Parse an episode filename. `display_title` may come back None (e.g.