feat(downloader): add M4B download pipeline with resume (closes #5)

Add streaming chunked M4B download via .partial files and atomic
rename, resume support through HTTP Range headers, full CLI
orchestration loop, --verbose flag, and config/secrets templates.

Fix 4 API-shape bugs found during live testing: library endpoint
returns audiobooks not books, M4B endpoint returns m4b_url not url,
narrators nested under audiobook_info, and ISBN int/str type mismatch
in history lookup causing re-downloads.

12 new tests (109 total). 6 books downloaded live (~2.9 GB).
This commit is contained in:
AlexKucera committed 2026-06-03 12:56:41 +02:00
1 parent 25ee66b95d
commit 33aa80b7aa
11 files changed
+989 -15

No files matched your search

+41 -1
View File
@@ -7,6 +7,10 @@ class AuthError(Exception):
"""Authentication failed (bad credentials, network error, etc.)."""
class M4BUnavailableError(Exception):
"""M4B format is not available for this book (404 from API)."""
class LibroFmClient:
"""Libro.fm API client with OAuth2 password grant and library fetching."""
@@ -93,7 +97,43 @@ class LibroFmClient:
resp = client.get(next_url)
resp.raise_for_status()
data = resp.json()
all_books.extend(data.get("books", []))
all_books.extend(data.get("audiobooks", []))
next_url = data.get("next_page") or None
return all_books
def fetch_m4b_url(self, isbn: str, transport: httpx.BaseTransport | None = None) -> str:
"""Fetch the M4B download URL for a given ISBN.
Args:
isbn: The ISBN of the audiobook.
transport: Optional httpx transport override for testing.
Returns:
The CDN URL string for the M4B file.
Raises:
AuthError: If not authenticated.
M4BUnavailableError: If the book has no M4B format available (404).
"""
if not self._access_token:
raise AuthError("Not authenticated. Call authenticate() first.")
headers = {**self.DEFAULT_HEADERS, "Authorization": f"Bearer {self._access_token}"}
client = httpx.Client(
base_url=self._base_url,
headers=headers,
timeout=self._timeout,
transport=transport,
)
resp = client.get(f"/api/v10/audiobooks/{isbn}/packaged_m4b")
if resp.status_code == 404:
raise M4BUnavailableError(f"M4B not available for ISBN {isbn}")
resp.raise_for_status()
data = resp.json()
return data["m4b_url"]