mirror of
https://github.com/flibusta-apps/telegram_files_cache_server.git
synced 2025-12-08 09:30:40 +01:00
Fix downloading
This commit is contained in:
@@ -2,6 +2,7 @@ from base64 import b64decode
|
||||
from typing import Optional
|
||||
|
||||
import httpx
|
||||
from sentry_sdk import capture_exception
|
||||
|
||||
from core.config import env_config
|
||||
|
||||
@@ -37,14 +38,18 @@ async def download(
|
||||
async def get_filename(book_id: int, file_type: str) -> Optional[str]:
|
||||
headers = {"Authorization": env_config.DOWNLOADER_API_KEY}
|
||||
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.get(
|
||||
f"{env_config.DOWNLOADER_URL}/filename/{book_id}/{file_type}",
|
||||
headers=headers,
|
||||
timeout=5 * 60,
|
||||
)
|
||||
try:
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.get(
|
||||
f"{env_config.DOWNLOADER_URL}/filename/{book_id}/{file_type}",
|
||||
headers=headers,
|
||||
timeout=5 * 60,
|
||||
)
|
||||
|
||||
if response.status_code != 200:
|
||||
return None
|
||||
if response.status_code != 200:
|
||||
return None
|
||||
|
||||
return response.text
|
||||
return response.text
|
||||
except httpx.HTTPError as e:
|
||||
capture_exception(e)
|
||||
return None
|
||||
|
||||
@@ -3,6 +3,7 @@ from typing import Generic, Optional, TypeVar
|
||||
|
||||
import httpx
|
||||
from pydantic import BaseModel
|
||||
from sentry_sdk import capture_exception
|
||||
|
||||
from core.config import env_config
|
||||
|
||||
@@ -47,8 +48,13 @@ class BookDetail(Book):
|
||||
AUTH_HEADERS = {"Authorization": env_config.LIBRARY_API_KEY}
|
||||
|
||||
|
||||
async def get_book(book_id: int, retry: int = 3) -> Optional[BookDetail]:
|
||||
async def get_book(
|
||||
book_id: int, retry: int = 3, last_exp: Exception | None = None
|
||||
) -> Optional[BookDetail]:
|
||||
if retry == 0:
|
||||
if last_exp:
|
||||
capture_exception(last_exp)
|
||||
|
||||
return None
|
||||
|
||||
try:
|
||||
@@ -61,8 +67,8 @@ async def get_book(book_id: int, retry: int = 3) -> 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)
|
||||
except httpx.HTTPError as e:
|
||||
return await get_book(book_id, retry=retry - 1, last_exp=e)
|
||||
|
||||
|
||||
async def get_books(page: int, page_size: int) -> Page[Book]:
|
||||
|
||||
Reference in New Issue
Block a user