Files
librofm-downloader/docs/devlog/20260603-slice-3b-m4b-download-pipeline-issue5_log.md

112 lines
7.0 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Slice 3b: M4B Download Pipeline (Issue #5)
> **Date:** 2026-06-03
> **Type:** slice
> **Reference:** [Issue #5](https://github.com/AlexKucera/librofm-downloader/issues/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 `M4BUnavailableError` exception for 404 responses
- Added `fetch_m4b_url(isbn, transport)` method — GET `/api/v10/audiobooks/{isbn}/packaged_m4b`
- Returns CDN URL string on 200
- Raises `M4BUnavailableError` on 404 (book has no M4B)
- Raises `AuthError` if 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.partial` during transfer
- Atomic rename from `.partial` to final `.m4b` on completion
- Resume support: detects existing `.partial`, reads size, sends `Range: bytes={size}-` header
- Opens file in append mode (`"ab"`) when resuming vs write mode (`"wb"`) for fresh downloads
- 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 `HistoryEntry` only after successful complete download (atomic rename done)
- Returns `Path` on success, `None` if skipped, propagates exceptions on failure
### CLI module (`librofm_downloader/cli.py`)
- Wired full download orchestration loop in `run()`:
- Converts raw API book dicts → `Book` dataclass 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)
## 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
- [x] M4B metadata query returns download URL when available
- [x] M4B metadata query returns None/specific error on 404 (book has no M4B)
- [x] Download writes to `.partial` file during transfer
- [x] On completion, file atomically renamed to final `.m4b`
- [x] Existing `.partial` file detected and resume via Range header
- [x] Resume starts from correct byte offset (partial file size)
- [x] Output path follows pattern from Slice 3a with sanitization applied
- [x] Subdirectory created when accompanying files present; flat otherwise
- [x] History entry written only after successful complete download
- [x] Book without M4B logged and skipped (not treated as error)
- [x] ~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)
- [x] 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*