7.0 KiB
7.0 KiB
Slice 3b: M4B Download Pipeline (Issue #5)
Date: 2026-06-03 Type: slice Reference: Issue #5
Goal
Build the core M4B download pipeline — first working end-to-end audiobook download. Includes client M4B endpoint, streaming chunked downloader with resume support, path integration with Slice 3a logic, history post-download recording, and CLI orchestration loop.
What Was Done
Client module (librofm_downloader/client.py)
- Added
M4BUnavailableErrorexception for 404 responses - Added
fetch_m4b_url(isbn, transport)method — GET/api/v10/audiobooks/{isbn}/packaged_m4b- Returns CDN URL string on 200
- Raises
M4BUnavailableErroron 404 (book has no M4B) - Raises
AuthErrorif not authenticated
Downloader module (librofm_downloader/downloader.py)
- Added
download_m4b(url, output_path, transport)function:- Streaming chunked download via
httpx.Client.stream()with 8MB chunks - Writes to
{filename}.m4b.partialduring transfer - Atomic rename from
.partialto final.m4bon completion - Resume support: detects existing
.partial, reads size, sendsRange: bytes={size}-header - Opens file in append mode (
"ab") when resuming vs write mode ("wb") for fresh downloads
- Streaming chunked download via
- Added
download_book(book, client, output_base, history, transport)orchestration function:- Resolves output path using Slice 3a's
resolve_path()+sanitize() - Applies subdirectory decision via
needs_subdirectory()(extras/cover → subdirectory; else flat) - Queries M4B URL via client, catches
M4BUnavailableError→ returns None (skipped) - Calls
download_m4b()for actual I/O - Writes
HistoryEntryonly after successful complete download (atomic rename done) - Returns
Pathon success,Noneif skipped, propagates exceptions on failure
- Resolves output path using Slice 3a's
CLI module (librofm_downloader/cli.py)
- Wired full download orchestration loop in
run():- Converts raw API book dicts →
Bookdataclass instances - Filters already-downloaded books via
DownloadHistory.is_downloaded() - Loops over new books calling
download_book()for each - Tracks downloaded / skipped / failed counts
- Prints per-book status (✓ downloaded, ⏭ skipped, ✗ failed)
- Prints summary line at end
- Returns exit code 0 always (individual failures don't crash the run)
- Converts raw API book dicts →
Decisions & Rationale
| Decision | Rationale |
|---|---|
M4BUnavailableError as specific exception (not generic) |
Lets caller distinguish "book has no M4B" from network errors — critical for skip-vs-fail logic |
.partial suffix pattern (not tempdir) |
Partial files live next to final destination so they survive process restarts for resume |
download_book() as separate function from download_m4b() |
Separation of concerns: download_m4b is pure I/O, download_book is orchestration with domain logic |
History write inside download_book, not CLI |
Keeps history coupling close to the download action; ensures entry is written iff atomic rename succeeds |
| CLI returns 0 even with failures | One bad book shouldn't stop the rest of the batch downloading |
String type hints for LibroFmClient / DownloadHistory in download_book() |
Avoids circular imports between modules |
Gotchas & Fixes
| Problem | Root Cause | Fix |
|---|---|---|
Duplicate import M4BUnavailableError, M4BUnavailableError |
Edit tool applied both the import replace and insert_after which both added the import | Fixed by re-reading file and using single replace edit |
| Resume test passed without actually resuming | Mock handler fell back to returning all data on any request, masking missing Range header | Made mock strict: only returns 206 when correct Range: bytes=30- header present, rejects others with 400 |
Flat path test assertion wrong (O Brien vs O'Brien) |
Misremembered sanitize behavior — apostrophes are preserved, only colons are replaced | Fixed assertion to check for O'Brien (apostrophe preserved) |
| Flat path parent assertion wrong | Assumed title would be immediate parent dir, but flat path is Author/Title.m4b so parent is author dir |
Changed to assert result.stem == title and result.parent.name == author |
import logging placed mid-file after insertion point |
Inserted logging import near download functions rather than at top | Moved to top of file alongside other imports during refactor step |
Files Changed
| File | Change Summary |
|---|---|
librofm_downloader/client.py |
+M4BUnavailableError, +fetch_m4b_url() method (~40 lines) |
librofm_downloader/downloader.py |
+download_m4b() streaming function, +download_book() orchestration, +imports (~100 lines net) |
librofm_downloader/cli.py |
Full download loop: Book conversion, per-book download, summary reporting (~50 lines changed) |
tests/test_client.py |
+TestFetchM4BUrl class (2 tests: success + 404) |
tests/test_downloader.py |
+TestDownloadM4B (4 tests), +TestDownloadBook (5 tests) |
tests/test_cli.py |
+TestCLIDownloadOrchestration (1 test: happy-path mock) |
Test Summary
11 new tests added (97 → 108 total, all passing):
| Class | Tests | Covers AC |
|---|---|---|
TestFetchM4BUrl |
2 | AC#1, AC#2 |
TestDownloadM4B |
4 | AC#3–AC#8 |
TestDownloadBook |
5 | AC#9, AC#10 |
TestCLIDownloadOrchestration |
1 | AC#11 (CLI wiring) |
Acceptance Criteria Pass/Fail
- M4B metadata query returns download URL when available
- M4B metadata query returns None/specific error on 404 (book has no M4B)
- Download writes to
.partialfile during transfer - On completion, file atomically renamed to final
.m4b - Existing
.partialfile detected and resume via Range header - Resume starts from correct byte offset (partial file size)
- Output path follows pattern from Slice 3a with sanitization applied
- Subdirectory created when accompanying files present; flat otherwise
- History entry written only after successful complete download
- Book without M4B logged and skipped (not treated as error)
- ~25 tests: M4B query success/404, streaming to temp dir, resume from partial, atomic rename, path resolution integration, happy-path orchestration with mocks (11 tests, all AC covered)
- All tests pass (108/108)
Open Items & Next Steps
- MP3 fallback (next slice): Issue #5 mentions MP3 ZIP as fallback when M4B unavailable. Currently books without M4B are just skipped. Next slice should implement MP3 download manifest parsing + ZIP part extraction.
- Real network testing: All tests use
httpx.MockTransport. Consider an integration test against a local mock server or recorded HTTP fixtures. - Progress reporting: No progress bar or byte-count reporting during download yet. Could add Rich progress display for large files.
- Concurrency: Sequential download loop. Could parallelize for users with many books.
Log written by write-log skill