Standalone rename_chapters(output_dir, tracks, book_title) -> int function in downloader.py that renames extracted MP3 files to include chapter titles from the Libro.fm download manifest.
End-to-end behavior: Given a directory of extracted .mp3 files and a tracks list from the manifest, renames each file to {zero-padded-number} - {sanitized_book_title} - {sanitized_chapter_title}.mp3. Returns count of files actually renamed.
Key behaviors:
Natural sort file-to-track pairing: Extract leading numeric prefix from each filename stem, sort numerically. Pair files[i] ↔ tracks[i] by index after sorting both lists.
Null/blank fallback: Tracks with chapter_title = None/empty/whitespace use "Chapter {n}" where n is the track's number field. Per-track — one missing title doesn't skip the batch.
Sanitization: Both book title and chapter title pass through existing sanitize() from path.py.
Count mismatch: Process min(len(files), len(tracks)) pairs, log warning about the gap.
Empty/edge cases: Empty directory, no .mp3 files, empty tracks list → all no-op, no error.
Idempotent: Running twice on same dir doesn't break (files already renamed just get matched again).
Signature:
defrename_chapters(output_dir:Path|str,tracks:list[dict],# each has "number" (int) and "chapter_title" (str|None)book_title:str,)->int:
Acceptance criteria
Basic rename: N files + N tracks with titles → all renamed to {num} - {book} - {chapter}.mp3
Null/blank chapter_title fallback: missing titles → "Chapter N" used per-track
All titles null/blank → all get "Chapter N" fallback
Natural sort: Track - 1.mp3 through Track - 10.mp3 sorted numerically, not alphabetically
Count mismatch (fewer files than tracks): rename what we can, log warning
Extra non-MP3 files present: filtered out, MP3s renamed correctly
Sanitization: colons, slashes, quotes in chapter titles cleaned via existing sanitize()
Empty directory / no MP3 files → no-op, returns 0, no error
Empty tracks list → no-op, returns 0, no error
Idempotency: running twice on same dir doesn't break or double-rename
~12 unit tests covering above scenarios, all passing
Blocked by
None - can start immediately
## Parent
#19
## What to build
Standalone `rename_chapters(output_dir, tracks, book_title) -> int` function in `downloader.py` that renames extracted MP3 files to include chapter titles from the Libro.fm download manifest.
**End-to-end behavior:** Given a directory of extracted `.mp3` files and a tracks list from the manifest, renames each file to `{zero-padded-number} - {sanitized_book_title} - {sanitized_chapter_title}.mp3`. Returns count of files actually renamed.
**Key behaviors:**
- **Natural sort file-to-track pairing:** Extract leading numeric prefix from each filename stem, sort numerically. Pair `files[i] ↔ tracks[i]` by index after sorting both lists.
- **Zero-padding:** Width = digit count of track count (9 tracks → width 1, 10 → width 2, 100+ → width 3).
- **Null/blank fallback:** Tracks with `chapter_title` = None/empty/whitespace use `"Chapter {n}"` where n is the track's `number` field. Per-track — one missing title doesn't skip the batch.
- **Sanitization:** Both book title and chapter title pass through existing `sanitize()` from `path.py`.
- **Count mismatch:** Process `min(len(files), len(tracks))` pairs, log warning about the gap.
- **Empty/edge cases:** Empty directory, no .mp3 files, empty tracks list → all no-op, no error.
- **Idempotent:** Running twice on same dir doesn't break (files already renamed just get matched again).
**Signature:**
```python
def rename_chapters(
output_dir: Path | str,
tracks: list[dict], # each has "number" (int) and "chapter_title" (str|None)
book_title: str,
) -> int:
```
## Acceptance criteria
- [ ] Basic rename: N files + N tracks with titles → all renamed to `{num} - {book} - {chapter}.mp3`
- [ ] Zero-padding: 5 tracks → no padding (`1`–`5`); 12 tracks → 2-digit padding (`01`–`12`)
- [ ] Null/blank chapter_title fallback: missing titles → `"Chapter N"` used per-track
- [ ] All titles null/blank → all get `"Chapter N"` fallback
- [ ] Natural sort: `Track - 1.mp3` through `Track - 10.mp3` sorted numerically, not alphabetically
- [ ] Count mismatch (fewer files than tracks): rename what we can, log warning
- [ ] Extra non-MP3 files present: filtered out, MP3s renamed correctly
- [ ] Sanitization: colons, slashes, quotes in chapter titles cleaned via existing `sanitize()`
- [ ] Empty directory / no MP3 files → no-op, returns 0, no error
- [ ] Empty tracks list → no-op, returns 0, no error
- [ ] Idempotency: running twice on same dir doesn't break or double-rename
- [ ] ~12 unit tests covering above scenarios, all passing
## Blocked by
None - can start immediately
✅Issue #32 Complete — committed as b6df4e3 on feature/rename-mp3.
What was built:
rename_chapters(output_dir, tracks, book_title) -> int in downloader.py
Natural sort by numeric prefix, auto zero-padding, null/blank fallback, sanitization via existing sanitize()
11 tests in TestRenameChapters class — all 12/12 acceptance criteria met
106 tests in test_downloader.py, 275 overall (excl. 3 pre-existing hangs)
Next steps: Wire into _download_mp3() after ZIP extraction (future slice).
✅ **Issue #32 Complete** — committed as `b6df4e3` on `feature/rename-mp3`.
**What was built:**
- `rename_chapters(output_dir, tracks, book_title) -> int` in `downloader.py`
- Natural sort by numeric prefix, auto zero-padding, null/blank fallback, sanitization via existing `sanitize()`
- **11 tests** in `TestRenameChapters` class — all 12/12 acceptance criteria met
- **106 tests** in test_downloader.py, **275 overall** (excl. 3 pre-existing hangs)
**Next steps:** Wire into `_download_mp3()` after ZIP extraction (future slice).
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
Parent
#19
What to build
Standalone
rename_chapters(output_dir, tracks, book_title) -> intfunction indownloader.pythat renames extracted MP3 files to include chapter titles from the Libro.fm download manifest.End-to-end behavior: Given a directory of extracted
.mp3files and a tracks list from the manifest, renames each file to{zero-padded-number} - {sanitized_book_title} - {sanitized_chapter_title}.mp3. Returns count of files actually renamed.Key behaviors:
files[i] ↔ tracks[i]by index after sorting both lists.chapter_title= None/empty/whitespace use"Chapter {n}"where n is the track'snumberfield. Per-track — one missing title doesn't skip the batch.sanitize()frompath.py.min(len(files), len(tracks))pairs, log warning about the gap.Signature:
Acceptance criteria
{num} - {book} - {chapter}.mp31–5); 12 tracks → 2-digit padding (01–12)"Chapter N"used per-track"Chapter N"fallbackTrack - 1.mp3throughTrack - 10.mp3sorted numerically, not alphabeticallysanitize()Blocked by
None - can start immediately
✅ Issue #32 Complete — committed as
b6df4e3onfeature/rename-mp3.What was built:
rename_chapters(output_dir, tracks, book_title) -> intindownloader.pysanitize()TestRenameChaptersclass — all 12/12 acceptance criteria metNext steps: Wire into
_download_mp3()after ZIP extraction (future slice).