diff --git a/src/api/src/create_book.py b/src/api/src/create_book.py index ab773ae..40e4905 100644 --- a/src/api/src/create_book.py +++ b/src/api/src/create_book.py @@ -12,7 +12,7 @@ from ebooklib import epub from bs4 import BeautifulSoup from pydantic import model_validator, field_validator from pydantic_settings import BaseSettings -from aiohttp import ClientResponseError, ClientSession +from aiohttp import ClientResponseError from aiohttp_client_cache.session import CachedSession from aiohttp_client_cache import FileBackend, RedisBackend @@ -107,7 +107,7 @@ async def wp_get_cookies(username: str, password: str) -> dict: dict: Authorization cookies. """ with start_action(action_type="api_fetch_cookies"): - async with ClientSession(headers=headers) as session: + async with CachedSession(headers=headers, cache=None) as session: async with session.post( "https://www.wattpad.com/auth/login?nextUrl=%2F&_data=routes%2Fauth.login", data={ @@ -245,7 +245,7 @@ def set_metadata(book, data): ) -async def set_cover(book, data, cookies: Optional[dict] = None): +async def set_cover(book, data): book.set_cover("cover.jpg", await fetch_cover(data["cover"])) chapter = epub.EpubHtml( file_name="titlepage.xhtml", # Standard for cover page @@ -272,11 +272,9 @@ async def add_chapters( if download_images: soup = BeautifulSoup(content, "lxml") - async with ( - CachedSession(headers=headers, cache=cache) - if not cookies - else ClientSession(headers=headers, cookies=cookies) - ) as session: # Don't cache requests with Cookies. + async with CachedSession( + headers=headers, cache=None + ) as session: # Don't cache requests for images. for idx, image in enumerate(soup.find_all("img")): if not image["src"]: continue diff --git a/src/api/src/main.py b/src/api/src/main.py index 60eaa9e..e8f1c3b 100644 --- a/src/api/src/main.py +++ b/src/api/src/main.py @@ -151,7 +151,7 @@ async def handle_download( metadata = await retrieve_story(story_id, cookies) set_metadata(book, metadata) - await set_cover(book, metadata, cookies=cookies) + await set_cover(book, metadata) async for title in add_chapters( book, metadata, download_images=download_images, cookies=cookies diff --git a/src/api/src/test_break.py b/src/api/src/test_break.py new file mode 100644 index 0000000..c77f8c0 --- /dev/null +++ b/src/api/src/test_break.py @@ -0,0 +1,31 @@ +import asyncio +from datetime import datetime +import aiohttp +from rich import print + + +PORT: int = 8086 +DOWNLOAD_URL = ( + f"http://localhost:{PORT}/download/314175600?om=1&download_images=true&mode=story" +) + + +async def fetch(task_id: int, session: aiohttp.ClientSession): + print("started", task_id) + + start = datetime.now() + async with session.get(DOWNLOAD_URL) as response: + print(task_id, response.status) + end = datetime.now() + + print("time taken", (end - start).total_seconds()) + + +async def main(): + session = aiohttp.ClientSession() + + await asyncio.gather(*[fetch(i, session) for i in range(30)]) + + +if __name__ == "__main__": + asyncio.run(main())