summaryrefslogtreecommitdiffstats
path: root/packages/meshbay-node/src/meshbay_node/indexer/title_parse.py
diff options
context:
space:
mode:
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.