ebook add
This commit is contained in:
parent
2c7a3cf605
commit
f02582b835
@ -2,6 +2,28 @@ import os, yt_dlp, json, requests, re
|
|||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
from urllib.parse import urlsplit
|
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():
|
class grab():
|
||||||
def season(url):
|
def season(url):
|
||||||
page_html=requests.get(url)
|
page_html=requests.get(url)
|
||||||
|
|||||||
13
app/main.py
13
app/main.py
@ -20,6 +20,19 @@ loop = asyncio.get_running_loop()
|
|||||||
cached_data = None
|
cached_data = None
|
||||||
|
|
||||||
# api
|
# 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")
|
@app.get("/dropout/update")
|
||||||
async def dropoutUpdate(force: bool = False):
|
async def dropoutUpdate(force: bool = False):
|
||||||
global cached_data
|
global cached_data
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user