Refine track number filename extraction — separator required
_extract_track_number_from_filename now requires a separator after digits (dash, dot, bracket) to prevent parsing artist names like "50 Cent" as track number 50. Also handles disc-track format "1-03". This is a last-resort fallback only — standard downloads get track numbers from Spotify/iTunes/Deezer metadata. Only affects files with no metadata where the filename starts with digits.
This commit is contained in:
parent
04bf2c5c4f
commit
b8006d93c3
1 changed files with 10 additions and 5 deletions
|
|
@ -14706,17 +14706,22 @@ def _clean_track_title(track_title: str, artist_name: str) -> str:
|
|||
|
||||
def _extract_track_number_from_filename(filename: str, title: str = None) -> int:
|
||||
"""Extract track number from filename, returns 1 if not found.
|
||||
Only matches numbers followed by a separator (dash, dot, space-dash) to avoid
|
||||
picking up numbers that are part of artist/track names (e.g. '50 Cent')."""
|
||||
Requires a separator after digits to avoid matching artist names like '50 Cent'."""
|
||||
import re
|
||||
import os
|
||||
basename = os.path.splitext(os.path.basename(filename))[0]
|
||||
# Match patterns like: "01 - Song", "01. Song", "01-Song", "1 Song"
|
||||
match = re.match(r'^(\d{1,3})\s*[\-\.)\]]\s*', basename.strip())
|
||||
basename = os.path.splitext(os.path.basename(filename))[0].strip()
|
||||
# Match: "01 - Song", "01. Song", "01-Song", "1.Song", "(01) Song", "[01] Song"
|
||||
match = re.match(r'^\(?(\d{1,3})\)?\s*[\-\.)\]]\s*', basename)
|
||||
if match:
|
||||
num = int(match.group(1))
|
||||
if 1 <= num <= 999:
|
||||
return num
|
||||
# Match: "1-03 Song" (disc-track format)
|
||||
match = re.match(r'^\d[\-\.](\d{1,2})\s*[\-\.]\s*', basename)
|
||||
if match:
|
||||
num = int(match.group(1))
|
||||
if 1 <= num <= 99:
|
||||
return num
|
||||
return 1
|
||||
|
||||
def _search_track_in_album_context(original_search: dict, artist: dict) -> dict:
|
||||
|
|
|
|||
Loading…
Reference in a new issue