feat(api): Include username and password params, cookie retrieval

This commit is contained in:
TheOnlyWayUp
2024-06-30 07:47:05 +00:00
parent 7c91149e7a
commit 29be3c8d69
+37 -11
View File
@@ -1,8 +1,16 @@
from typing import Optional
from pathlib import Path from pathlib import Path
from fastapi import FastAPI, HTTPException from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse, HTMLResponse, StreamingResponse from fastapi.responses import FileResponse, HTMLResponse, StreamingResponse
from ebooklib import epub from ebooklib import epub
from create_book import retrieve_story, set_cover, set_metadata, add_chapters, slugify from create_book import (
retrieve_story,
set_cover,
set_metadata,
add_chapters,
slugify,
wp_get_cookies,
)
import tempfile import tempfile
from io import BytesIO from io import BytesIO
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
@@ -17,29 +25,47 @@ def home():
@app.get("/download/{story_id}") @app.get("/download/{story_id}")
async def download_book(story_id: int, download_images: bool = False): async def download_book(
data = await retrieve_story(story_id) story_id: int,
download_images: bool = False,
username: Optional[str] = None,
password: Optional[str] = None,
):
if username and not password or password and not username:
return HTMLResponse(
status_code=422,
content='Include both the username _and_ password, or neither. Support is available on the <a href="https://discord.gg/P9RHC4KCwd" target="_blank">Discord</a>',
)
if username and password:
try:
cookies = await wp_get_cookies(username=username, password=password)
except ValueError:
return HTMLResponse(
status_code=403,
content='Incorrect Username and/or Password. Support is available on the <a href="https://discord.gg/P9RHC4KCwd" target="_blank">Discord</a>',
)
else:
cookies = None
data = await retrieve_story(story_id, cookies=cookies)
book = epub.EpubBook() book = epub.EpubBook()
# Metadata and Cover are updated
try: try:
set_metadata(book, data) set_metadata(book, data)
except KeyError: except KeyError:
# raise HTTPException(
# status_code=404,
# detail='Story not found. Check the ID - Support is available on the <a href="https://discord.gg/P9RHC4KCwd" target="_blank">Discord</a>',
# )
# return FileResponse(BUILD_PATH / "index.html", status_code=404)
return HTMLResponse( return HTMLResponse(
status_code=404, status_code=404,
content='Story not found. Check the ID - Support is available on the <a href="https://discord.gg/P9RHC4KCwd" target="_blank">Discord</a>', content='Story not found. Check the ID - Support is available on the <a href="https://discord.gg/P9RHC4KCwd" target="_blank">Discord</a>',
) )
await set_cover(book, data) await set_cover(book, data, cookies=cookies)
# print("Metadata Downloaded") # print("Metadata Downloaded")
# Chapters are downloaded # Chapters are downloaded
async for title in add_chapters(book, data, download_images=download_images): async for title in add_chapters(
book, data, download_images=download_images, cookies=cookies
):
# print(f"Part ({title}) downloaded") # print(f"Part ({title}) downloaded")
... ...