mirror of
https://github.com/flibusta-apps/telegram_files_cache_server.git
synced 2025-12-06 14:45:36 +01:00
Fix get_book
This commit is contained in:
@@ -110,16 +110,14 @@ async def cache_file_by_book_id(
|
||||
) -> Optional[CachedFile]:
|
||||
r_client: aioredis.Redis = ctx["redis"]
|
||||
|
||||
try:
|
||||
book = await get_book(book_id)
|
||||
except httpx.ConnectError:
|
||||
get_book_retry = 3 if by_request else 1
|
||||
book = await get_book(book_id, get_book_retry)
|
||||
|
||||
if book is None:
|
||||
if by_request:
|
||||
return None
|
||||
raise Retry(defer=15)
|
||||
|
||||
if book is None:
|
||||
return None
|
||||
|
||||
if file_type not in book.available_types:
|
||||
return None
|
||||
|
||||
|
||||
@@ -48,7 +48,11 @@ class BookDetail(Book):
|
||||
AUTH_HEADERS = {"Authorization": env_config.LIBRARY_API_KEY}
|
||||
|
||||
|
||||
async def get_book(book_id: int) -> Optional[BookDetail]:
|
||||
async def get_book(book_id: int, retry: int = 3) -> Optional[BookDetail]:
|
||||
if retry == 0:
|
||||
return None
|
||||
|
||||
try:
|
||||
async with httpx.AsyncClient(timeout=2 * 60) as client:
|
||||
response = await client.get(
|
||||
f"{env_config.LIBRARY_URL}/api/v1/books/{book_id}", headers=AUTH_HEADERS
|
||||
@@ -58,6 +62,8 @@ async def get_book(book_id: int) -> Optional[BookDetail]:
|
||||
return None
|
||||
|
||||
return BookDetail.parse_obj(response.json())
|
||||
except (httpx.ConnectError, httpx.ReadError, httpx.ReadTimeout):
|
||||
return await get_book(book_id, retry=retry - 1)
|
||||
|
||||
|
||||
async def get_books(page: int, page_size: int) -> Page[Book]:
|
||||
|
||||
Reference in New Issue
Block a user