diff --git a/app/download.py b/app/download.py index e390f71..880cf43 100644 --- a/app/download.py +++ b/app/download.py @@ -2,6 +2,28 @@ import os, yt_dlp, json, requests, re from bs4 import BeautifulSoup from urllib.parse import urlsplit + +def ebook(url, author): + destination = f"/ebooks/{author}" + os.makedirs(destination, exist_ok=True) # Create the folder if it doesn't exist + + response = requests.get(url, stream=True) + response.raise_for_status() # Good practice to raise error on bad status + + # Try to extract filename from the headers + cd = response.headers.get('Content-Disposition') + if cd and 'filename=' in cd: + filename = cd.split('filename=')[1].strip('";') + else: + # Fallback: get the last part of the URL + filename = os.path.basename(url) + + file_path = os.path.join(destination, filename) + + with open(file_path, 'wb') as f: + for chunk in response.iter_content(chunk_size=8192): + f.write(chunk) + class grab(): def season(url): page_html=requests.get(url) diff --git a/app/main.py b/app/main.py index 1879415..8fcd2a4 100644 --- a/app/main.py +++ b/app/main.py @@ -20,6 +20,19 @@ loop = asyncio.get_running_loop() cached_data = None # api +@app.post("/ebook/download", description="Download an ebook via a url.") +async def ebookDownload( + background_tasks: BackgroundTasks, + url: str = Form(...), + author: str = Form(...) +): + try: + background_tasks.add_task(download.ebook,url,author) + # download.dropout.show(show,season,episode) + return JSONResponse(status_code=200, content={"status": "success", "message": "Book downloaded."}) + except Exception as e: + return JSONResponse(status_code=500, content={"status": "error", "message": str(e)}) + @app.get("/dropout/update") async def dropoutUpdate(force: bool = False): global cached_data