Spaces:
Running
Running
slimshadow
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException, Query
|
2 |
+
from fastapi.responses import FileResponse
|
3 |
+
import os
|
4 |
+
from yt_dlp import YoutubeDL
|
5 |
+
|
6 |
+
# Define the FastAPI app
|
7 |
+
app = FastAPI()
|
8 |
+
|
9 |
+
# Directory to save downloaded videos
|
10 |
+
DOWNLOAD_DIR = "downloads"
|
11 |
+
os.makedirs(DOWNLOAD_DIR, exist_ok=True)
|
12 |
+
|
13 |
+
@app.get("/")
|
14 |
+
def read_root():
|
15 |
+
return {"message": "Welcome to the Instagram Reel Downloader API"}
|
16 |
+
|
17 |
+
@app.get("/download/")
|
18 |
+
def download_instagram_reel(
|
19 |
+
reel_url: str = Query(..., description="The URL of the Instagram Reel to download"),
|
20 |
+
):
|
21 |
+
"""
|
22 |
+
Download an Instagram Reel using yt-dlp.
|
23 |
+
|
24 |
+
Args:
|
25 |
+
reel_url (str): The URL of the Instagram Reel.
|
26 |
+
|
27 |
+
Returns:
|
28 |
+
FileResponse: The path to the downloaded file.
|
29 |
+
"""
|
30 |
+
if not reel_url:
|
31 |
+
raise HTTPException(status_code=400, detail="The 'reel_url' parameter is required.")
|
32 |
+
|
33 |
+
# Define yt-dlp options
|
34 |
+
ydl_opts = {
|
35 |
+
"outtmpl": os.path.join(DOWNLOAD_DIR, "%(title)s.%(ext)s"),
|
36 |
+
"quiet": True, # Suppress yt-dlp's output
|
37 |
+
"merge_output_format": "mp4",
|
38 |
+
}
|
39 |
+
|
40 |
+
try:
|
41 |
+
with YoutubeDL(ydl_opts) as ydl:
|
42 |
+
info = ydl.extract_info(reel_url, download=True)
|
43 |
+
filename = ydl.prepare_filename(info)
|
44 |
+
if not os.path.exists(filename):
|
45 |
+
raise HTTPException(status_code=500, detail="Failed to download the Reel.")
|
46 |
+
return FileResponse(
|
47 |
+
path=filename,
|
48 |
+
filename=os.path.basename(filename),
|
49 |
+
media_type="video/mp4"
|
50 |
+
)
|
51 |
+
except Exception as e:
|
52 |
+
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|