Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +17 -0
- app.py +54 -0
- requirements.txt +3 -0
Dockerfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use an official Python runtime as a parent image
|
| 2 |
+
FROM python:3.11-slim
|
| 3 |
+
|
| 4 |
+
# Set the working directory in the container
|
| 5 |
+
WORKDIR /code
|
| 6 |
+
|
| 7 |
+
# Copy the dependencies file to the working directory
|
| 8 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 9 |
+
|
| 10 |
+
# Install any needed packages specified in requirements.txt
|
| 11 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 12 |
+
|
| 13 |
+
# Copy the content of the local src directory to the working directory
|
| 14 |
+
COPY ./app.py /code/app.py
|
| 15 |
+
|
| 16 |
+
# Command to run the application
|
| 17 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware # Import the CORS middleware
|
| 4 |
+
from yt_dlp import YoutubeDL
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
# --- CORS Configuration ---
|
| 10 |
+
# This is the part you need to add.
|
| 11 |
+
# It allows your API to be called from any website.
|
| 12 |
+
|
| 13 |
+
# List of origins that are allowed to make requests to your API.
|
| 14 |
+
# Using ["*"] allows all origins, which is simple for a public API.
|
| 15 |
+
origins = ["*"]
|
| 16 |
+
|
| 17 |
+
app.add_middleware(
|
| 18 |
+
CORSMiddleware,
|
| 19 |
+
allow_origins=origins,
|
| 20 |
+
allow_credentials=True,
|
| 21 |
+
allow_methods=["*"], # Allows all methods (GET, POST, etc.)
|
| 22 |
+
allow_headers=["*"], # Allows all headers
|
| 23 |
+
)
|
| 24 |
+
# --- End of CORS Configuration ---
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@app.get("/")
|
| 28 |
+
async def root():
|
| 29 |
+
return {"message": "Welcome to the All-In-One Downloader API!"}
|
| 30 |
+
|
| 31 |
+
@app.get("/download")
|
| 32 |
+
async def download_media(url: str):
|
| 33 |
+
if not url:
|
| 34 |
+
raise HTTPException(status_code=400, detail="URL parameter is required.")
|
| 35 |
+
|
| 36 |
+
# These options are fine. No changes needed here.
|
| 37 |
+
ydl_opts = {
|
| 38 |
+
'noplaylist': True,
|
| 39 |
+
'quiet': True,
|
| 40 |
+
'dump_single_json': True,
|
| 41 |
+
'format': 'best' # You can add this to prefer the best quality format
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
with YoutubeDL(ydl_opts) as ydl:
|
| 46 |
+
# The 'download=False' is correct. It just extracts info.
|
| 47 |
+
info_dict = ydl.extract_info(url, download=False)
|
| 48 |
+
|
| 49 |
+
# Returning the JSON response is also correct.
|
| 50 |
+
return JSONResponse(content=info_dict)
|
| 51 |
+
|
| 52 |
+
# Your error handling is good. No changes needed here.
|
| 53 |
+
except Exception as e:
|
| 54 |
+
raise HTTPException(status_code=500, detail=str(e))
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
yt-dlp
|