_download_mp3() returns (output_dir, tracks) tuple so manifest track data flows to callers. download_book() accepts rename_chapters kwarg; after MP3 extraction succeeds, calls rename_chapters() via _rename_and_log() helper which also reports to user via reporter. Both PlainTextReporter and RichProgressReporter show visible feedback: 'Renamed N chapter(s) for Book → example.mp3'. sync_run.py closure threads resolved_rename_chapters flag through to download_book(). 6 new integration tests covering all format strategies and edge cases. ~229 total tests. Closes #34.
5.5 KiB
5.5 KiB
Wire rename_chapters() into Download Pipeline
Date: 2026-06-05 Type: issue Reference: Issue #34
Goal
Wire the rename_chapters() function (built in Issue #32) into the download pipeline so it runs automatically after MP3 extraction when config.rename_chapters is enabled (Issue #33). End-to-end: MP3 downloads → chapters renamed to {num} - {book} - {chapter}.mp3 with visible user feedback.
What Was Done
_download_mp3()return type changed fromPath | Nonetotuple[Path | None, list[dict]]— now returns(output_dir, tracks)on success,(None, [])on failure. Themanifest["tracks"]data was already fetched but previously discarded.download_book()gainedrename_chapters: bool = Falsekwarg — optional, keyword-only, defaults toFalsefor backward compatibility.- New
_rename_and_log()helper indownloader.py— callsrename_chapters(), checks count mismatch before renaming, logs vialogger.info/warning, and callsreporter.chapter_renamed()with an example filename for user-visible output. - Both MP3 success paths wired —
m4b_mp3_fallbackfallback path (line ~282) andmp3_onlypath (line ~293) both unpack the tuple and call_rename_and_log(result_path, mp3_tracks, book.title, reporter). sync_run.pyclosure updated —_make_download_fn()now capturesresolved_rename_chaptersas_rcand passes it todownload_book(..., rename_chapters=_rc). This completes the wiring chain: CLI flag →sync_run()resolution → closure →download_book()→_rename_and_log()→rename_chapters().- User-visible rename feedback added — new
chapter_renamed(count, book_title, example_name)method on bothPlainTextReporterandProgressReporterinprogress.py. Shows output like:Renamed 14 chapter(s) for 'The River Has Roots' → '01 - The River Has Roots - Chapter 1.mp3' - 6 TDD integration tests added in
TestRenameChaptersWiringclass covering all acceptance criteria.
Decisions & Rationale
| Decision | Rationale |
|---|---|
Return tracks tuple from _download_mp3() rather than re-fetching manifest |
Avoids a second API call; manifest is already in memory. Tuple is simple, no new dataclass needed for internal use |
Call rename in download_book() not inside _download_mp3() |
Separation of concerns: _download_mp3 downloads, download_book orchestrates (including post-processing like renaming). Keeps _download_mp3 testable in isolation |
Add reporter param to _rename_and_log() instead of importing console/rich |
Keeps downloader module decoupled from output mechanism; reporter is injected by caller, testable with mock reporters |
Check count mismatch BEFORE calling rename_chapters() |
After zip() truncation, a 3-file/2-track scenario renames 2 files and returns 2 — looks like success. Pre-check catches the mismatch regardless |
Default rename_chapters=False on download_book() |
Backward compatible; existing callers don't need changes. Only the sync_run closure passes the resolved value |
Gotchas & Fixes
| Problem | Root Cause | Fix |
|---|---|---|
Test expected 01 - ... zero-padding but got 1 - ... |
Zero-padding width = len(str(track_count)). With 2 tracks, width=1 (single digit). Test assumed 2-digit padding |
Fixed test expectations to match actual zero-padding logic |
| Count mismatch warning never fired for 3 MP3s + 2 tracks | Original code only warned when rename_chapters() returned 0 AND tracks existed. But zip(3_files, 2_tracks) renames 2 files → returns 2 → info log fires, no warning |
Moved mismatch check BEFORE calling rename_chapters(), comparing len(mp3_files) != len(tracks) directly |
| User couldn't tell if renaming ran — files looked same as unzipped | _rename_and_log() only called logger.info() which goes to logging ether, not terminal. No user-facing output at all |
Added chapter_renamed() method to both reporter classes; _rename_and_log() calls it with first renamed filename as example |
Libro.fm manifest returned null/empty chapter_title values |
API data quality issue — some books have no chapter title metadata | rename_chapters() falls back to "Chapter {num}" pattern (built in Issue #32). Not a bug, just data-dependent behavior |
Files Changed
| File | Change Summary |
|---|---|
librofm_downloader/downloader.py |
_download_mp3() returns tuple; download_book() accepts rename_chapters kwarg; new _rename_and_log() helper with reporter feedback; both MP3 paths wired |
librofm_downloader/sync_run.py |
Closure captures resolved_rename_chapters, passes to download_book(rename_chapters=_rc) |
librofm_downloader/progress.py |
chapter_renamed(count, title, example) on PlainTextReporter + ProgressReporter |
tests/test_downloader.py |
6 new tests in TestRenameChaptersWiring class (mp3_only rename, m4b-only skip, fallback rename, false-skip, count-mismatch warning, verbose logging) |
Open Items & Next Steps
- Consider adding per-file rename logging in verbose mode (
-v) — currently only shows summary line with one example filename - Libro.fm chapter title quality varies widely; some books get generic "Chapter N" names. Future enhancement could scrape chapter titles from alternate sources (Audible API, etc.)
- ID3 tag writing not in scope (issue mentions this explicitly as out of scope)
Log written by write-log skill