Update main.py
Browse files
main.py
CHANGED
|
@@ -1,16 +1,24 @@
|
|
| 1 |
import mangadex as md
|
| 2 |
from fastapi import FastAPI, Query, HTTPException
|
| 3 |
-
from fastapi.responses import JSONResponse
|
| 4 |
-
from
|
| 5 |
from reportlab.pdfgen import canvas
|
| 6 |
import tempfile
|
| 7 |
import os
|
| 8 |
from PIL import Image as PILImage
|
| 9 |
import httpx
|
|
|
|
|
|
|
| 10 |
|
| 11 |
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
auth = md.auth.Auth()
|
| 13 |
-
uploadcare = Uploadcare(public_key='ebee00bb0f7b8c564729', secret_key='cbaef6eb6db8a127ddd7')
|
| 14 |
|
| 15 |
@app.get("/mangadex")
|
| 16 |
async def fetch_and_upload_pdf(
|
|
@@ -91,14 +99,16 @@ async def fetch_and_upload_pdf(
|
|
| 91 |
|
| 92 |
c.save()
|
| 93 |
|
| 94 |
-
#
|
| 95 |
-
|
| 96 |
-
|
|
|
|
| 97 |
|
| 98 |
# Clean up temporary PDF file
|
| 99 |
os.remove(pdf_path)
|
| 100 |
|
| 101 |
-
|
|
|
|
| 102 |
|
| 103 |
except Exception as e:
|
| 104 |
return JSONResponse(content={"error": f"Failed to process the chapters: {str(e)}"}, status_code=500)
|
|
@@ -154,4 +164,4 @@ def root():
|
|
| 154 |
|
| 155 |
if __name__ == "__main__":
|
| 156 |
import uvicorn
|
| 157 |
-
uvicorn.run("main:app", host="0.0.0.0", port=8000, workers=4)
|
|
|
|
| 1 |
import mangadex as md
|
| 2 |
from fastapi import FastAPI, Query, HTTPException
|
| 3 |
+
from fastapi.responses import JSONResponse, FileResponse
|
| 4 |
+
from fastapi.staticfiles import StaticFiles
|
| 5 |
from reportlab.pdfgen import canvas
|
| 6 |
import tempfile
|
| 7 |
import os
|
| 8 |
from PIL import Image as PILImage
|
| 9 |
import httpx
|
| 10 |
+
import pikepdf
|
| 11 |
+
from pathlib import Path
|
| 12 |
|
| 13 |
app = FastAPI()
|
| 14 |
+
|
| 15 |
+
# Serve static files from the "static" directory
|
| 16 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 17 |
+
|
| 18 |
+
# Ensure the "static" directory exists
|
| 19 |
+
Path("static").mkdir(exist_ok=True)
|
| 20 |
+
|
| 21 |
auth = md.auth.Auth()
|
|
|
|
| 22 |
|
| 23 |
@app.get("/mangadex")
|
| 24 |
async def fetch_and_upload_pdf(
|
|
|
|
| 99 |
|
| 100 |
c.save()
|
| 101 |
|
| 102 |
+
# Compress the PDF
|
| 103 |
+
compressed_pdf_path = f"static/{chapter_id}_compressed.pdf"
|
| 104 |
+
with pikepdf.open(pdf_path) as pdf:
|
| 105 |
+
pdf.save(compressed_pdf_path)
|
| 106 |
|
| 107 |
# Clean up temporary PDF file
|
| 108 |
os.remove(pdf_path)
|
| 109 |
|
| 110 |
+
# Return the URL to the compressed PDF
|
| 111 |
+
return JSONResponse(content={"file_url": f"/static/{chapter_id}_compressed.pdf"})
|
| 112 |
|
| 113 |
except Exception as e:
|
| 114 |
return JSONResponse(content={"error": f"Failed to process the chapters: {str(e)}"}, status_code=500)
|
|
|
|
| 164 |
|
| 165 |
if __name__ == "__main__":
|
| 166 |
import uvicorn
|
| 167 |
+
uvicorn.run("main:app", host="0.0.0.0", port=8000, workers=4)
|