mirror of
https://github.com/flibusta-apps/books_downloader.git
synced 2025-12-06 15:05:37 +01:00
Fix html download
This commit is contained in:
@@ -26,6 +26,8 @@ class ConvertationError(Exception):
|
|||||||
|
|
||||||
|
|
||||||
class FLDownloader(BaseDownloader):
|
class FLDownloader(BaseDownloader):
|
||||||
|
EXCLUDE_UNZIP = ["html"]
|
||||||
|
|
||||||
def __init__(self, book_id: int, file_type: str, source_id: int):
|
def __init__(self, book_id: int, file_type: str, source_id: int):
|
||||||
self.book_id = book_id
|
self.book_id = book_id
|
||||||
self.original_file_type = file_type
|
self.original_file_type = file_type
|
||||||
@@ -52,8 +54,8 @@ class FLDownloader(BaseDownloader):
|
|||||||
|
|
||||||
return get_filename(self.book_id, book, self.file_type)
|
return get_filename(self.book_id, book, self.file_type)
|
||||||
|
|
||||||
async def get_final_filename(self) -> str:
|
async def get_final_filename(self, force_zip: bool = False) -> str:
|
||||||
if self.need_zip:
|
if self.need_zip or force_zip:
|
||||||
return (await self.get_filename()) + ".zip"
|
return (await self.get_filename()) + ".zip"
|
||||||
|
|
||||||
return await self.get_filename()
|
return await self.get_filename()
|
||||||
@@ -238,7 +240,7 @@ class FLDownloader(BaseDownloader):
|
|||||||
for source in env_config.FL_SOURCES:
|
for source in env_config.FL_SOURCES:
|
||||||
tasks.add(asyncio.create_task(self._download_from_source(source)))
|
tasks.add(asyncio.create_task(self._download_from_source(source)))
|
||||||
|
|
||||||
if self.file_type in ["epub", "mobi"]:
|
if self.file_type.lower() in ["epub", "mobi"]:
|
||||||
tasks.add(asyncio.create_task(self._download_with_converting()))
|
tasks.add(asyncio.create_task(self._download_with_converting()))
|
||||||
|
|
||||||
data = await self._wait_until_some_done(tasks)
|
data = await self._wait_until_some_done(tasks)
|
||||||
@@ -249,7 +251,7 @@ class FLDownloader(BaseDownloader):
|
|||||||
client, response, is_zip = data
|
client, response, is_zip = data
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if is_zip:
|
if is_zip and self.file_type.lower() not in self.EXCLUDE_UNZIP:
|
||||||
temp_file_name = await self._unzip(response)
|
temp_file_name = await self._unzip(response)
|
||||||
else:
|
else:
|
||||||
|
|
||||||
@@ -271,8 +273,10 @@ class FLDownloader(BaseDownloader):
|
|||||||
|
|
||||||
content = cast(IO, open(content_filename, "rb"))
|
content = cast(IO, open(content_filename, "rb"))
|
||||||
|
|
||||||
|
force_zip = is_zip and self.file_type.lower() in self.EXCLUDE_UNZIP
|
||||||
|
|
||||||
async def _content_iterator() -> AsyncIterator[bytes]:
|
async def _content_iterator() -> AsyncIterator[bytes]:
|
||||||
t_file = UploadFile(await self.get_filename(), content)
|
t_file = UploadFile(await self.get_final_filename(force_zip), content)
|
||||||
try:
|
try:
|
||||||
while chunk := await t_file.read(2048):
|
while chunk := await t_file.read(2048):
|
||||||
yield cast(bytes, chunk)
|
yield cast(bytes, chunk)
|
||||||
@@ -283,7 +287,7 @@ class FLDownloader(BaseDownloader):
|
|||||||
process_pool_executor, os.remove, content_filename
|
process_pool_executor, os.remove, content_filename
|
||||||
)
|
)
|
||||||
|
|
||||||
return _content_iterator(), await self.get_final_filename()
|
return _content_iterator(), await self.get_final_filename(force_zip)
|
||||||
|
|
||||||
async def _get_book_data(self):
|
async def _get_book_data(self):
|
||||||
return await BookLibraryClient.get_remote_book(self.source_id, self.book_id)
|
return await BookLibraryClient.get_remote_book(self.source_id, self.book_id)
|
||||||
|
|||||||
Reference in New Issue
Block a user