-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmp3_tier_scoring.py
More file actions
596 lines (441 loc) · 16.6 KB
/
Copy pathmp3_tier_scoring.py
File metadata and controls
596 lines (441 loc) · 16.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
import sys
import re
import shutil
from pathlib import Path
from typing import Optional, Dict, List, Tuple
from concurrent.futures import ThreadPoolExecutor, as_completed
from mutagen import File
from rapidfuzz import fuzz
# =========================================================
# PATH VALIDATION
# =========================================================
def validate_path(path: str) -> Path:
"""
Validate and normalize input directory path.
"""
# enforce correct input type early
if not isinstance(path, str):
raise ValueError("Path must be a string")
if not path.strip():
raise ValueError("Empty path provided")
p = Path(path)
if not p.exists():
raise ValueError(f"Path does not exist: {path}")
if not p.is_dir():
raise ValueError(f"Path is not a directory: {path}")
return p
def is_mp3(file: Path) -> bool:
"""Check if file is MP3 based on extension."""
return isinstance(file, Path) and file.suffix.lower() == ".mp3"
# =========================================================
# FILENAME PARSING
# =========================================================
FILENAME_PATTERNS = [
re.compile(r"^(?P<track>\d+)\s*-\s*(?P<title>.+)$"),
re.compile(r"^(?P<artist>.+)\s*-\s*(?P<title>.+)$"),
]
def parse_filename(filename: str) -> Dict[str, Optional[str]]:
"""Extract metadata hints from filename."""
if not isinstance(filename, str):
return {"artist": None, "title": None, "track": None, "unparseable": True}
stem = Path(filename).stem.strip()
for pattern in FILENAME_PATTERNS:
m = pattern.match(stem)
if m:
return {
"artist": m.groupdict().get("artist"),
"title": m.groupdict().get("title"),
"track": m.groupdict().get("track"),
"unparseable": False,
}
return {"artist": None, "title": None, "track": None, "unparseable": True}
# =========================================================
# NORMALIZATION
# =========================================================
def normalize(text: Optional[str]) -> str:
"""Normalize text for comparison."""
if not text:
return ""
text = text.lower().strip()
text = re.sub(r"[^\w\s]", "", text)
return re.sub(r"\s+", " ", text)
# =========================================================
# TRACK MODEL
# =========================================================
class TrackFeatures:
"""Container for MP3 metadata + derived analysis state."""
def __init__(
self,
path: str,
artist: Optional[str],
title: Optional[str],
album: Optional[str],
tracknumber: Optional[int],
bitrate: Optional[int],
filename_title: Optional[str],
corrupt: bool,
match_state: str,
failure_type: Optional[str] = None, # NEW: Tier F subcategory
):
self.path = path
self.artist = artist
self.title = title
self.album = album
self.tracknumber = tracknumber
self.bitrate = bitrate
self.filename_title = filename_title
self.corrupt = corrupt
self.match_state = match_state
self.failure_type = failure_type # NEW
# =========================================================
# FAILURE CLASSIFICATION (NEW CORE ADDITION)
# =========================================================
def classify_failure(tags: Optional[dict], audio: Optional[object]) -> str:
"""
Determine Tier F failure category.
This is what enables:
- filtering
- move operations
- subcategory inspection
"""
if audio is None:
return "corrupt"
if not tags:
return "no_tags"
return "parse_failure"
# =========================================================
# MATCH STATE
# =========================================================
def get_match_state(fn_title: Optional[str], tag_title: Optional[str]) -> str:
"""Compare filename vs metadata title."""
fn = normalize(fn_title)
tag = normalize(tag_title)
if not fn or not tag:
return "mismatch"
if fn in tag or tag in fn:
return "clean_match"
score = fuzz.ratio(fn, tag)
if score >= 90:
return "clean_match"
elif score >= 75:
return "partial_match"
return "mismatch"
# =========================================================
# FEATURE EXTRACTION
# =========================================================
def extract_features(file_path: Path) -> TrackFeatures:
"""Extract metadata from single file."""
try:
audio = File(file_path)
if audio is None:
raise ValueError("Unreadable file")
tags = audio.tags or {}
def safe(tag):
try:
return str(tag.text[0]) if tag and hasattr(tag, "text") else None
except Exception:
return None
artist = safe(tags.get("TPE1"))
title = safe(tags.get("TIT2"))
album = safe(tags.get("TALB"))
tracknumber = None
try:
tr = tags.get("TRCK")
if tr:
tracknumber = int(str(tr.text[0]).split("/")[0])
except Exception:
tracknumber = None
fname = parse_filename(file_path.name)
match_state = get_match_state(fname.get("title"), title)
return TrackFeatures(
path=str(file_path),
artist=artist,
title=title,
album=album,
tracknumber=tracknumber,
bitrate=getattr(audio.info, "bitrate", None),
filename_title=fname.get("title"),
corrupt=False,
match_state=match_state,
failure_type=None,
)
except Exception:
# classify failure explicitly for Tier F routing
return TrackFeatures(
path=str(file_path),
artist=None,
title=None,
album=None,
tracknumber=None,
bitrate=None,
filename_title=None,
corrupt=True,
match_state="mismatch",
failure_type="corrupt",
)
# =========================================================
# SCORING
# =========================================================
def score_track(t: TrackFeatures) -> Tuple[int, Dict[str, int]]:
"""Single-pass scoring (no recomputation elsewhere)."""
score = 100
defects = {
"missing_artist": 0,
"missing_title": 0,
"missing_track": 0,
"filename_mismatch": 0,
"low_bitrate": 0,
"corrupt": 0,
}
if t.corrupt:
defects["corrupt"] = 1
return 0, defects
if not t.artist:
score -= 25
defects["missing_artist"] = 1
if not t.title:
score -= 25
defects["missing_title"] = 1
if not t.tracknumber:
score -= 15
defects["missing_track"] = 1
if t.match_state != "clean_match":
defects["filename_mismatch"] = 1
if t.bitrate and t.bitrate < 128000:
score -= 3
defects["low_bitrate"] = 1
return max(score, 0), defects
# =========================================================
# TIERING
# =========================================================
def tier(score: int) -> str:
if score >= 90: return "A"
if score >= 75: return "B"
if score >= 60: return "C"
if score >= 45: return "D"
if score >= 30: return "E"
return "F"
def apply_gate(tier_label: str, match_state: str) -> str:
if tier_label == "A" and match_state != "clean_match":
return "B"
if match_state == "mismatch" and tier_label in ("A", "B"):
return "C"
return tier_label
# =========================================================
# FILE COLLECTION
# =========================================================
def collect_files(root: Path) -> List[Path]:
return [p for p in root.rglob("*") if p.is_file() and is_mp3(p)]
# =========================================================
# ANALYSIS (THREADING)
# =========================================================
def analyze(files: List[Path], workers: int = 8):
results = {t: [] for t in "ABCDE"}
results["F"] = {} # NEW: structured failure buckets
defects = {t: {} for t in "ABCDEF"}
with ThreadPoolExecutor(max_workers=workers) as executor:
futures = [executor.submit(extract_features, f) for f in files]
for future in as_completed(futures):
feat = future.result()
score, d = score_track(feat)
base = tier(score)
final = apply_gate(base, feat.match_state)
if final == "F":
# ensure structured grouping
bucket = feat.failure_type or "parse_failure"
results["F"].setdefault(bucket, []).append(feat)
else:
results[final].append((feat, score))
for k, v in d.items():
defects[final][k] = defects[final].get(k, 0) + v
return results, defects
# =========================================================
# SAFE MOVE SYSTEM (NEW CORE FUNCTIONALITY)
# =========================================================
def validate_move_paths(source_root: Path, dest_root: Path):
"""
Ensure destination is safe and not inside scan tree.
"""
source_root = source_root.resolve()
dest_root = dest_root.resolve()
# CRITICAL: prevent recursive ingestion loops
if dest_root == source_root or source_root in dest_root.parents:
raise ValueError("Destination cannot be inside scan root")
# ensure writable target directory exists
dest_root.mkdir(parents=True, exist_ok=True)
return dest_root
def move_files(files: List[TrackFeatures], dest_root: Path, source_root: Path):
"""
Move files safely across OS boundaries.
"""
dest_root = validate_move_paths(source_root, dest_root)
for f in files:
src = Path(f.path)
# preserve relative structure
rel = src.relative_to(source_root)
target = dest_root / rel
target.parent.mkdir(parents=True, exist_ok=True)
# collision handling
if target.exists():
target = target.with_stem(target.stem + "_dup")
shutil.move(str(src), str(target))
# =========================================================
# REPORT
# =========================================================
def report(results, defects, tier_filter=None, list_mode=False):
"""
Structured tier report renderer.
ONLY CHANGE IN THIS VERSION:
- Core health is omitted if empty
- Main issues is omitted if empty
No other behavior modified.
"""
total = sum(len(v) for k, v in results.items() if k != "F")
def safe_pct(n):
return (n / total * 100) if total else 0
def print_section(title: str, data: Dict[str, int]):
"""
Print non-zero defect groups in sorted order.
"""
items = sorted(data.items(), key=lambda x: -x[1])
printed = False
for k, v in items:
if v > 0:
if not printed:
print(f"\n{title}")
printed = True
print(f"- {k}: {v}")
for t in (tier_filter if tier_filter else "ABCDE"):
items = results[t]
if not items:
continue
count = len(items)
avg = sum(score for _, score in items) / count
print(f"\nTier {t} | count: {count} ({safe_pct(count):.1f}%) | avg: {avg:.1f}")
if list_mode:
for feat, _ in items:
print("-", feat.path)
continue
d = defects[t]
core = {
"missing_artist": d.get("missing_artist", 0),
"missing_title": d.get("missing_title", 0),
}
main = {
"missing_track": d.get("missing_track", 0),
"filename_mismatch": d.get("filename_mismatch", 0),
}
secondary = {
"low_bitrate": d.get("low_bitrate", 0),
}
# Core health (ONLY print if non-empty after filtering)
if any(v > 0 for v in core.values()):
print_section("Core health", core)
# Main issues (ONLY print if non-empty after filtering)
if any(v > 0 for v in main.values()):
print_section("Main issues", main)
print_section("Secondary", secondary)
print_section("Top fixes", d)
if "F" in results and results["F"]:
f = results["F"]
# check if there is any actual data in any bucket
has_data = any(len(v) > 0 for v in f.values())
if not has_data:
return # or just skip Tier F section entirely
print("\nTier F")
for subtype, files in f.items():
if not files:
continue
print(f"\n{subtype} | count: {len(files)}")
if list_mode:
for ft in files:
print("-", ft.path)
print("\nTop fixes")
agg = {k: len(v) for k, v in f.items()}
for k, v in sorted(agg.items(), key=lambda x: -x[1]):
print(f"- {k}: {v}")
# =========================================================
# MAIN (CLI EXTENSION HOOK READY)
# =========================================================
def main():
"""
CLI entry point.
USAGE (updated):
python script.py <folder>
python script.py <folder> --list
python script.py <folder> --list F
python script.py <folder> --list F corrupt
BEHAVIOR:
- No args: full report
- --list: lists all files
- --list F: lists all Tier F files grouped by subtype
- --list F <subtype>: lists only that failure subtype (e.g. corrupt)
"""
if len(sys.argv) < 2:
print("Usage:")
print(" python script.py <folder>")
print(" python script.py <folder> --list")
print(" python script.py <folder> --list F")
print(" python script.py <folder> --list F corrupt")
sys.exit(1)
folder = validate_path(sys.argv[1])
# -----------------------------
# CLI ARG PARSING
# -----------------------------
list_mode = "--list" in sys.argv
tier_filter = None
f_subtype_filter = None
# capture positional CLI args after folder
args = [a for a in sys.argv[2:] if a != "--list"]
# detect Tier filter (A–F)
for a in args:
if a.upper() in ("A", "B", "C", "D", "E", "F"):
tier_filter = a.upper()
# detect Tier F subtype filter (corrupt / no_tags / parse_failure)
for a in args:
if a.lower() in ("corrupt", "no_tags", "parse_failure"):
f_subtype_filter = a.lower()
# -----------------------------
# VALIDATION RULES
# -----------------------------
# list mode with no tier is allowed (lists everything)
# but subtype only makes sense with F
if f_subtype_filter and tier_filter != "F":
print("Error: subtype filter only valid with Tier F")
sys.exit(1)
# -----------------------------
# RUN PIPELINE
# -----------------------------
files = collect_files(folder)
if not files:
print("Error: no mp3 files found")
sys.exit(1)
results, defects = analyze(files)
# -----------------------------
# LIST MODE OVERRIDE
# -----------------------------
if list_mode:
# CASE 1: Tier F subtype listing
if tier_filter == "F" and f_subtype_filter:
items = results["F"].get(f_subtype_filter, [])
for ft in items:
print("-", ft.path)
return
# CASE 2: Tier F full listing
if tier_filter == "F":
for subtype, items in results["F"].items():
print(f"\n{subtype}")
for ft in items:
print("-", ft.path)
return
# CASE 3: normal tier listing
for t in (tier_filter if tier_filter else "ABCDE"):
for feat, _ in results.get(t, []):
print("-", feat.path)
return
# -----------------------------
# NORMAL REPORT MODE
# -----------------------------
report(results, defects, tier_filter=tier_filter)
if __name__ == "__main__":
main()