Add downloading translator books

This commit is contained in:
2023-06-07 01:33:05 +02:00
parent d7e74b6b5e
commit a0718c637e
4 changed files with 29 additions and 1 deletions

View File

@@ -148,7 +148,7 @@ async def create_archive(task_id: uuid.UUID, redis: Redis = TaskiqDepends(get_re
item = await LibraryClient.get_sequence(task.object_id)
assert item
name = item.name
case ObjectType.AUTHOR:
case ObjectType.AUTHOR | ObjectType.TRANSLATOR:
item = await LibraryClient.get_author(task.object_id)
assert item
names = [item.first_name, item.last_name, item.middle_name]

View File

@@ -17,6 +17,11 @@ class AuthorBook(BaseModel):
available_types: list[str]
class TranslatorBook(BaseModel):
id: int
available_types: list[str]
Item = TypeVar("Item", bound=BaseModel)
@@ -81,6 +86,26 @@ class LibraryClient:
return Page[AuthorBook].parse_raw(response.text)
@staticmethod
async def get_translator_books(
translator_id: int, allowed_langs: list[str], page: int = 1
) -> Page[TranslatorBook] | None:
async with httpx.AsyncClient() as client:
response = await client.get(
f"{env_config.LIBRARY_URL}/api/v1/translators/{translator_id}/books",
params={
"page": page,
"allowed_langs": allowed_langs,
"is_deleted": "false",
},
headers={"Authorization": env_config.LIBRARY_API_KEY},
)
if response.status_code != 200:
return None
return Page[TranslatorBook].parse_raw(response.text)
@staticmethod
async def get_sequence(sequence_id: int) -> Sequence | None:
async with httpx.AsyncClient() as client:

View File

@@ -27,6 +27,8 @@ class TaskCreator:
books_getter = LibraryClient.get_sequence_books
case ObjectType.AUTHOR:
books_getter = LibraryClient.get_author_books
case ObjectType.TRANSLATOR:
books_getter = LibraryClient.get_translator_books
while current_page <= pages_count:
book_page = await books_getter(object_id, allowed_langs, page=current_page)

View File

@@ -14,6 +14,7 @@ class TaskStatusEnum(enum.StrEnum):
class ObjectType(enum.StrEnum):
SEQUENCE = "sequence"
AUTHOR = "author"
TRANSLATOR = "translator"
class Task(BaseModel):