mirror of
https://github.com/flibusta-apps/telegram_files_cache_server.git
synced 2025-12-08 09:30:40 +01:00
Add linters configs
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
import asyncio
|
||||
from typing import Optional
|
||||
|
||||
import asyncio
|
||||
|
||||
from app.services.library_client import get_books, get_book, Book
|
||||
from app.models import CachedFile
|
||||
from app.services.caption_getter import get_caption
|
||||
from app.services.downloader import download
|
||||
from app.services.files_uploader import upload_file
|
||||
from app.services.caption_getter import get_caption
|
||||
from app.models import CachedFile
|
||||
from app.services.library_client import get_books, get_book, Book
|
||||
|
||||
|
||||
PAGE_SIZE = 50
|
||||
@@ -38,7 +37,7 @@ class CacheUpdater:
|
||||
|
||||
for book in page.items:
|
||||
await self._check_book(book)
|
||||
|
||||
|
||||
self.all_books_checked = True
|
||||
|
||||
@classmethod
|
||||
@@ -55,9 +54,7 @@ class CacheUpdater:
|
||||
upload_data = await upload_file(content, filename, caption)
|
||||
|
||||
return await CachedFile.objects.create(
|
||||
object_id=book.id,
|
||||
object_type=file_type,
|
||||
data=upload_data.data
|
||||
object_id=book.id, object_type=file_type, data=upload_data.data
|
||||
)
|
||||
|
||||
async def _start_worker(self):
|
||||
@@ -69,14 +66,12 @@ class CacheUpdater:
|
||||
except asyncio.QueueEmpty:
|
||||
await asyncio.sleep(0.1)
|
||||
continue
|
||||
|
||||
|
||||
await self._cache_file(book, file_type)
|
||||
|
||||
async def _update(self):
|
||||
await asyncio.gather(
|
||||
self._start_producer(),
|
||||
self._start_worker(),
|
||||
self._start_worker()
|
||||
self._start_producer(), self._start_worker(), self._start_worker()
|
||||
)
|
||||
|
||||
@classmethod
|
||||
|
||||
@@ -6,10 +6,10 @@ def get_author_string(author: BookAuthor) -> str:
|
||||
|
||||
if author.last_name:
|
||||
author_parts.append(author.last_name)
|
||||
|
||||
|
||||
if author.first_name:
|
||||
author_parts.append(author.first_name)
|
||||
|
||||
|
||||
if author.middle_name:
|
||||
author_parts.append(author.middle_name)
|
||||
|
||||
@@ -21,13 +21,11 @@ def get_caption(book: Book) -> str:
|
||||
|
||||
caption_authors_parts = []
|
||||
for author in book.authors:
|
||||
caption_authors_parts.append(
|
||||
f"👤 {get_author_string(author)}"
|
||||
)
|
||||
|
||||
caption_authors_parts.append(f"👤 {get_author_string(author)}")
|
||||
|
||||
if not caption_authors_parts:
|
||||
return caption_title
|
||||
|
||||
|
||||
caption_authors = "\n".join(caption_authors_parts)
|
||||
|
||||
return caption_title + "\n\n" + caption_authors
|
||||
|
||||
@@ -5,17 +5,23 @@ import httpx
|
||||
from core.config import env_config
|
||||
|
||||
|
||||
async def download(source_id: int, remote_id: int, file_type: str) -> Optional[tuple[bytes, str]]:
|
||||
async def download(
|
||||
source_id: int, remote_id: int, file_type: str
|
||||
) -> Optional[tuple[bytes, str]]:
|
||||
headers = {"Authorization": env_config.DOWNLOADER_API_KEY}
|
||||
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.get(f"{env_config.DOWNLOADER_URL}/download/{source_id}/{remote_id}/{file_type}", headers=headers, timeout=5 * 60)
|
||||
response = await client.get(
|
||||
f"{env_config.DOWNLOADER_URL}/download/{source_id}/{remote_id}/{file_type}",
|
||||
headers=headers,
|
||||
timeout=5 * 60,
|
||||
)
|
||||
|
||||
if response.status_code != 200:
|
||||
return None
|
||||
|
||||
content_disposition = response.headers['Content-Disposition']
|
||||
content_disposition = response.headers["Content-Disposition"]
|
||||
|
||||
name = content_disposition.replace('attachment; filename=', '')
|
||||
name = content_disposition.replace("attachment; filename=", "")
|
||||
|
||||
return response.content, name
|
||||
|
||||
@@ -17,9 +17,15 @@ async def upload_file(content: bytes, filename: str, caption: str) -> UploadedFi
|
||||
headers = {"Authorization": env_config.FILES_SERVER_API_KEY}
|
||||
|
||||
async with httpx.AsyncClient() as client:
|
||||
form = {'caption': caption}
|
||||
files = {'file': (filename, content)}
|
||||
form = {"caption": caption}
|
||||
files = {"file": (filename, content)}
|
||||
|
||||
response = await client.post(f"{env_config.FILES_SERVER_URL}/api/v1/files/upload/", data=form, files=files, headers=headers, timeout=5 * 60)
|
||||
response = await client.post(
|
||||
f"{env_config.FILES_SERVER_URL}/api/v1/files/upload/",
|
||||
data=form,
|
||||
files=files,
|
||||
headers=headers,
|
||||
timeout=5 * 60,
|
||||
)
|
||||
|
||||
return UploadedFile.parse_obj(response.json())
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
from typing import Generic, TypeVar
|
||||
from pydantic import BaseModel
|
||||
from datetime import date
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
import httpx
|
||||
from pydantic import BaseModel
|
||||
|
||||
from core.config import env_config
|
||||
|
||||
import httpx
|
||||
|
||||
|
||||
T = TypeVar('T')
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class Page(BaseModel, Generic[T]):
|
||||
@@ -49,14 +49,22 @@ AUTH_HEADERS = {"Authorization": env_config.LIBRARY_API_KEY}
|
||||
|
||||
async def get_book(book_id: int) -> BookDetail:
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.get(f"{env_config.LIBRARY_URL}/api/v1/books/{book_id}", headers=AUTH_HEADERS)
|
||||
response = await client.get(
|
||||
f"{env_config.LIBRARY_URL}/api/v1/books/{book_id}", headers=AUTH_HEADERS
|
||||
)
|
||||
|
||||
return BookDetail.parse_obj(response.json())
|
||||
|
||||
|
||||
async def get_books(page: int, page_size: int) -> Page[Book]:
|
||||
async with httpx.AsyncClient() as client:
|
||||
response = await client.get(f"{env_config.LIBRARY_URL}/api/v1/books/?page={page}&size={page_size}&is_deleted=false", headers=AUTH_HEADERS)
|
||||
response = await client.get(
|
||||
(
|
||||
f"{env_config.LIBRARY_URL}/api/v1/books/"
|
||||
f"?page={page}&size={page_size}&is_deleted=false"
|
||||
),
|
||||
headers=AUTH_HEADERS,
|
||||
)
|
||||
|
||||
data = response.json()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user