slimshadow commited on
Commit
1a2a733
·
verified ·
1 Parent(s): 83a551c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -8
app.py CHANGED
@@ -1,5 +1,5 @@
1
  from fastapi import FastAPI, HTTPException, Query
2
- from fastapi.responses import FileResponse
3
  import os
4
  from yt_dlp import YoutubeDL
5
 
@@ -19,13 +19,13 @@ 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.")
@@ -43,10 +43,21 @@ def download_instagram_reel(
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)}")
 
 
 
 
 
 
 
 
 
 
 
1
  from fastapi import FastAPI, HTTPException, Query
2
+ from fastapi.responses import JSONResponse
3
  import os
4
  from yt_dlp import YoutubeDL
5
 
 
19
  reel_url: str = Query(..., description="The URL of the Instagram Reel to download"),
20
  ):
21
  """
22
+ Download an Instagram Reel using yt-dlp and provide a download link.
23
 
24
  Args:
25
  reel_url (str): The URL of the Instagram Reel.
26
 
27
  Returns:
28
+ JSONResponse: A JSON object containing the download link.
29
  """
30
  if not reel_url:
31
  raise HTTPException(status_code=400, detail="The 'reel_url' parameter is required.")
 
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
+
47
+ # Construct the download link
48
+ file_name = os.path.basename(filename)
49
+ download_link = f"http://your-domain.com/files/{file_name}"
50
+
51
+ return JSONResponse(content={"download_link": download_link})
52
  except Exception as e:
53
  raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
54
+
55
+ @app.get("/files/{file_name}")
56
+ def serve_file(file_name: str):
57
+ """
58
+ Serve the downloaded file for users to download.
59
+ """
60
+ file_path = os.path.join(DOWNLOAD_DIR, file_name)
61
+ if not os.path.exists(file_path):
62
+ raise HTTPException(status_code=404, detail="File not found.")
63
+ return FileResponse(path=file_path, filename=file_name, media_type="video/mp4")