Hermes Agent commited on
Commit
3056d2b
·
1 Parent(s): edd1509

fix(repair): merge legacy duplicate directories and normalize multi-year folders

Browse files
apex/repair_jellyfin_libraries.py CHANGED
@@ -418,9 +418,9 @@ def _sanitize_media_title(text: str) -> str:
418
  s = s[m.end():].strip()
419
  s = re.sub(r"(?i)\b(?:wmr|wm|mm\s*linkz|tamilmv|tamilblasters|1tamilmv)\b[._\s-]*", "", s).strip()
420
  s = re.sub(r"^[^\w]+", "", s).strip()
421
- # Normalize duplicate years like "Title (2008) (2008)" -> "Title (2008)"
422
  years = re.findall(r"\((\d{4})\)", s)
423
- if len(years) >= 2 and len(set(years)) == 1:
424
  y = years[0]
425
  s = re.sub(r"\s*\(\d{4}\)", "", s).strip()
426
  s = f"{s} ({y})"
@@ -514,6 +514,35 @@ def heal_mismatched_media(roots=None, databases=None):
514
  pass
515
  except Exception as ren_err:
516
  print(f"[REPAIR] Note renaming folder {dirpath}: {ren_err}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
517
 
518
  # 2. Rename .strm files inside directory and heal NFOs
519
  for fn in list(filenames):
 
418
  s = s[m.end():].strip()
419
  s = re.sub(r"(?i)\b(?:wmr|wm|mm\s*linkz|tamilmv|tamilblasters|1tamilmv)\b[._\s-]*", "", s).strip()
420
  s = re.sub(r"^[^\w]+", "", s).strip()
421
+ # Normalize multiple years like "Title (2008) (2016)" -> "Title (2008)"
422
  years = re.findall(r"\((\d{4})\)", s)
423
+ if len(years) >= 2:
424
  y = years[0]
425
  s = re.sub(r"\s*\(\d{4}\)", "", s).strip()
426
  s = f"{s} ({y})"
 
514
  pass
515
  except Exception as ren_err:
516
  print(f"[REPAIR] Note renaming folder {dirpath}: {ren_err}")
517
+ else:
518
+ # Destination folder already exists; merge non-conflicting files and clean empty legacy folder
519
+ try:
520
+ for f in os.listdir(dirpath):
521
+ src_f = os.path.join(dirpath, f)
522
+ dst_f = os.path.join(new_dirpath, f)
523
+ if not os.path.exists(dst_f):
524
+ shutil.move(src_f, dst_f)
525
+ for db_p in db_list:
526
+ if os.path.exists(db_p):
527
+ try:
528
+ conn = sqlite3.connect(db_p, timeout=5)
529
+ conn.execute("UPDATE BaseItems SET Path = ? WHERE Path = ?", (dst_f, src_f))
530
+ conn.commit()
531
+ conn.close()
532
+ except Exception:
533
+ pass
534
+ elif f.endswith(".strm") or f.lower() in ("movie.nfo", "tvshow.nfo"):
535
+ try:
536
+ os.remove(src_f)
537
+ except Exception:
538
+ pass
539
+ try:
540
+ os.rmdir(dirpath)
541
+ print(f"[REPAIR] Merged legacy duplicate folder: '{dirpath}' -> '{new_dirpath}'")
542
+ except OSError:
543
+ pass
544
+ except Exception as merge_err:
545
+ print(f"[REPAIR] Note merging duplicate folder {dirpath}: {merge_err}")
546
 
547
  # 2. Rename .strm files inside directory and heal NFOs
548
  for fn in list(filenames):
apex/tests/test_repair_jellyfin_libraries.py CHANGED
@@ -672,6 +672,32 @@ def test_heal_channel_names_and_database(tmp_path):
672
  assert "WMR" not in row[0]
673
 
674
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
675
 
676
 
677
 
 
672
  assert "WMR" not in row[0]
673
 
674
 
675
+ def test_heal_mismatched_media_merges_into_existing_destination(tmp_path):
676
+ movies_root = tmp_path / "Movies"
677
+ movies_root.mkdir()
678
+
679
+ # Existing clean folder
680
+ clean_folder = movies_root / "Iron Man (2008)"
681
+ clean_folder.mkdir()
682
+ clean_strm = clean_folder / "Iron Man (2008).strm"
683
+ clean_strm.write_text("http://stream/clean")
684
+
685
+ # Legacy folder with different year drift: "Iron Man (2008) (2016)"
686
+ drift_folder = movies_root / "Iron Man (2008) (2016)"
687
+ drift_folder.mkdir()
688
+ drift_sub = drift_folder / "Iron Man (2008).eng.srt"
689
+ drift_sub.write_text("1\n00:00:01,000 --> 00:00:02,000\nSubtitle\n")
690
+
691
+ heal_mismatched_media(roots=[str(movies_root)], databases=[])
692
+
693
+ # Drift folder must be merged into clean_folder and rmdir'd
694
+ assert clean_folder.exists()
695
+ assert clean_strm.exists()
696
+ assert (clean_folder / "Iron Man (2008).eng.srt").exists()
697
+ assert not drift_folder.exists()
698
+
699
+
700
+
701
 
702
 
703