mirror of
https://github.com/jo1gi/grawlix.git
synced 2025-12-16 04:09:10 +00:00
Make onlinefiles support cookies
This commit is contained in:
parent
2faa4b747b
commit
dade9db6da
@ -1,6 +1,6 @@
|
|||||||
from grawlix import Encryption
|
from grawlix import Encryption
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from typing import Optional, Union, TypeVar, Generic
|
from typing import Optional, Union, TypeVar, Generic, Any
|
||||||
|
|
||||||
@dataclass(slots=True)
|
@dataclass(slots=True)
|
||||||
class Metadata:
|
class Metadata:
|
||||||
@ -30,6 +30,7 @@ class OnlineFile:
|
|||||||
extension: str
|
extension: str
|
||||||
encryption: Optional[Encryption] = None
|
encryption: Optional[Encryption] = None
|
||||||
headers: Optional[dict[str, str]] = None
|
headers: Optional[dict[str, str]] = None
|
||||||
|
cookies: Optional[Any] = None # TODO Change type
|
||||||
|
|
||||||
@dataclass(slots=True)
|
@dataclass(slots=True)
|
||||||
class OfflineFile:
|
class OfflineFile:
|
||||||
@ -63,8 +64,8 @@ class HtmlFile:
|
|||||||
|
|
||||||
@dataclass(slots=True)
|
@dataclass(slots=True)
|
||||||
class HtmlFiles:
|
class HtmlFiles:
|
||||||
cover: OnlineFile
|
|
||||||
htmlfiles: list[HtmlFile]
|
htmlfiles: list[HtmlFile]
|
||||||
|
cover: Optional[OnlineFile] = None
|
||||||
|
|
||||||
BookData = Union[
|
BookData = Union[
|
||||||
SingleFile,
|
SingleFile,
|
||||||
|
|||||||
@ -27,9 +27,9 @@ class Epub(OutputFormat):
|
|||||||
file_count = len(html.htmlfiles) + 1 # Html files + cover
|
file_count = len(html.htmlfiles) + 1 # Html files + cover
|
||||||
|
|
||||||
async def download_cover(cover_file: OnlineFile):
|
async def download_cover(cover_file: OnlineFile):
|
||||||
cover_filename = f"cover.{html.cover.extension}"
|
cover_filename = f"cover.{cover_file.extension}"
|
||||||
epub_cover = epub.EpubCover(file_name = cover_filename)
|
epub_cover = epub.EpubCover(file_name = cover_filename)
|
||||||
epub_cover.content = await self._download_file(html.cover)
|
epub_cover.content = await self._download_file(cover_file)
|
||||||
output.add_item(epub_cover)
|
output.add_item(epub_cover)
|
||||||
epub_cover_page = epub.EpubCoverHtml(image_name = cover_filename)
|
epub_cover_page = epub.EpubCoverHtml(image_name = cover_filename)
|
||||||
if update:
|
if update:
|
||||||
@ -38,7 +38,12 @@ class Epub(OutputFormat):
|
|||||||
|
|
||||||
|
|
||||||
async def download_file(index: int, file: HtmlFile):
|
async def download_file(index: int, file: HtmlFile):
|
||||||
response = await self._client.get(file.file.url, follow_redirects=True)
|
response = await self._client.get(
|
||||||
|
file.file.url,
|
||||||
|
headers = file.file.headers,
|
||||||
|
cookies = file.file.cookies,
|
||||||
|
follow_redirects=True
|
||||||
|
)
|
||||||
soup = BeautifulSoup(response.text, "lxml")
|
soup = BeautifulSoup(response.text, "lxml")
|
||||||
selected_element = soup.find(attrs=file.selector)
|
selected_element = soup.find(attrs=file.selector)
|
||||||
epub_file = epub.EpubHtml(
|
epub_file = epub.EpubHtml(
|
||||||
@ -55,7 +60,9 @@ class Epub(OutputFormat):
|
|||||||
download_file(index, file)
|
download_file(index, file)
|
||||||
for index, file in enumerate(html.htmlfiles)
|
for index, file in enumerate(html.htmlfiles)
|
||||||
]
|
]
|
||||||
epub_files = await asyncio.gather(download_cover(html.cover), *tasks)
|
if html.cover:
|
||||||
|
tasks.append(download_cover(html.cover))
|
||||||
|
epub_files = await asyncio.gather(*tasks)
|
||||||
|
|
||||||
# Add files to epub
|
# Add files to epub
|
||||||
for epub_file in epub_files:
|
for epub_file in epub_files:
|
||||||
|
|||||||
@ -59,7 +59,7 @@ class OutputFormat:
|
|||||||
:returns: Content of downloaded file
|
:returns: Content of downloaded file
|
||||||
"""
|
"""
|
||||||
content = b""
|
content = b""
|
||||||
async with self._client.stream("GET", file.url, headers = file.headers, follow_redirects=True) as request:
|
async with self._client.stream("GET", file.url, headers = file.headers, cookies = file.cookies, follow_redirects=True) as request:
|
||||||
total_filesize = int(request.headers["Content-length"])
|
total_filesize = int(request.headers["Content-length"])
|
||||||
async for chunk in request.aiter_bytes():
|
async for chunk in request.aiter_bytes():
|
||||||
content += chunk
|
content += chunk
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user