fix(api): Remove unnecessary API Request, remove test script

This commit is contained in:
TheOnlyWayUp
2024-11-30 21:10:17 +00:00
parent a755ddb0e4
commit 26b9db8945
2 changed files with 10 additions and 8 deletions
+6 -4
View File
@@ -1,4 +1,4 @@
from typing import Optional from typing import Optional, Tuple
import re import re
import unicodedata import unicodedata
import logging import logging
@@ -156,20 +156,22 @@ def slugify(value, allow_unicode=False) -> str:
@backoff.on_exception(backoff.expo, ClientResponseError, max_time=15) @backoff.on_exception(backoff.expo, ClientResponseError, max_time=15)
async def fetch_story_id(part_id: int, cookies: Optional[dict] = None) -> int: async def fetch_story_id(
part_id: int, cookies: Optional[dict] = None
) -> Tuple[int, dict]:
"""Return a Story ID from a Part ID.""" """Return a Story ID from a Part ID."""
with start_action(action_type="api_fetch_storyFromPart"): with start_action(action_type="api_fetch_storyFromPart"):
async with CachedSession( async with CachedSession(
headers=headers, cache=None if cookies else cache headers=headers, cache=None if cookies else cache
) as session: # Don't cache requests with Cookies. ) as session: # Don't cache requests with Cookies.
async with session.get( async with session.get(
f"https://www.wattpad.com/api/v3/story_parts/{part_id}?fields=groupId" f"https://www.wattpad.com/api/v3/story_parts/{part_id}?fields=groupId,group(tags,id,title,createDate,modifyDate,language(name),description,completed,mature,url,isPaywalled,user(username),parts(id,title),cover)"
) as response: ) as response:
response.raise_for_status() response.raise_for_status()
body = await response.json() body = await response.json()
return body["groupId"] return body["groupId"], body["group"]
@backoff.on_exception(backoff.expo, ClientResponseError, max_time=15) @backoff.on_exception(backoff.expo, ClientResponseError, max_time=15)
+4 -4
View File
@@ -141,14 +141,14 @@ async def handle_download(
match mode: match mode:
case DownloadMode.story: case DownloadMode.story:
story_id = download_id story_id = download_id
metadata = await retrieve_story(story_id, cookies)
case DownloadMode.part: case DownloadMode.part:
story_id = await fetch_story_id(download_id, cookies) story_id, metadata = await fetch_story_id(download_id, cookies)
logger.error(f"Retrieved story id ({story_id=})") logger.error(f"Retrieved story id ({story_id=})")
book = epub.EpubBook() book = epub.EpubBook()
metadata = await retrieve_story(story_id, cookies)
set_metadata(book, metadata) set_metadata(book, metadata)
await set_cover(book, metadata) await set_cover(book, metadata)
@@ -174,7 +174,7 @@ async def handle_download(
BytesIO(book_data), BytesIO(book_data),
media_type="application/epub+zip", media_type="application/epub+zip",
headers={ headers={
"Content-Disposition": f'attachment; filename="{slugify(metadata["title"])}_{story_id}_{"images" if download_images else ""}.epub"' # Thanks https://stackoverflow.com/a/72729058 "Content-Disposition": f'attachment; filename="{slugify(metadata["title"])}_{story_id}{"_images" if download_images else ""}.epub"' # Thanks https://stackoverflow.com/a/72729058
}, },
) )
@@ -185,4 +185,4 @@ app.mount("/", StaticFiles(directory=BUILD_PATH), "static")
if __name__ == "__main__": if __name__ == "__main__":
import uvicorn import uvicorn
uvicorn.run("main:app", host="0.0.0.0", port=8086, workers=24) uvicorn.run("main:app", host="0.0.0.0", port=80, workers=16)