fix(api): Use CachedSession across codebase

This commit is contained in:
TheOnlyWayUp
2024-11-30 20:57:15 +00:00
parent 28e40ece94
commit a755ddb0e4
3 changed files with 38 additions and 9 deletions
+6 -8
View File
@@ -12,7 +12,7 @@ from ebooklib import epub
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from pydantic import model_validator, field_validator from pydantic import model_validator, field_validator
from pydantic_settings import BaseSettings 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.session import CachedSession
from aiohttp_client_cache import FileBackend, RedisBackend from aiohttp_client_cache import FileBackend, RedisBackend
@@ -107,7 +107,7 @@ async def wp_get_cookies(username: str, password: str) -> dict:
dict: Authorization cookies. dict: Authorization cookies.
""" """
with start_action(action_type="api_fetch_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( async with session.post(
"https://www.wattpad.com/auth/login?nextUrl=%2F&_data=routes%2Fauth.login", "https://www.wattpad.com/auth/login?nextUrl=%2F&_data=routes%2Fauth.login",
data={ 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"])) book.set_cover("cover.jpg", await fetch_cover(data["cover"]))
chapter = epub.EpubHtml( chapter = epub.EpubHtml(
file_name="titlepage.xhtml", # Standard for cover page file_name="titlepage.xhtml", # Standard for cover page
@@ -272,11 +272,9 @@ async def add_chapters(
if download_images: if download_images:
soup = BeautifulSoup(content, "lxml") soup = BeautifulSoup(content, "lxml")
async with ( async with CachedSession(
CachedSession(headers=headers, cache=cache) headers=headers, cache=None
if not cookies ) as session: # Don't cache requests for images.
else ClientSession(headers=headers, cookies=cookies)
) as session: # Don't cache requests with Cookies.
for idx, image in enumerate(soup.find_all("img")): for idx, image in enumerate(soup.find_all("img")):
if not image["src"]: if not image["src"]:
continue continue
+1 -1
View File
@@ -151,7 +151,7 @@ async def handle_download(
metadata = await retrieve_story(story_id, cookies) metadata = await retrieve_story(story_id, cookies)
set_metadata(book, metadata) set_metadata(book, metadata)
await set_cover(book, metadata, cookies=cookies) await set_cover(book, metadata)
async for title in add_chapters( async for title in add_chapters(
book, metadata, download_images=download_images, cookies=cookies book, metadata, download_images=download_images, cookies=cookies
+31
View File
@@ -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())