slimshadow commited on
Commit
abfaaff
·
verified ·
1 Parent(s): a51256a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -5
app.py CHANGED
@@ -1,5 +1,5 @@
1
  from fastapi import FastAPI, HTTPException, Query
2
- from fastapi.responses import JSONResponse
3
  import os
4
  from yt_dlp import YoutubeDL
5
 
@@ -40,13 +40,22 @@ def download_instagram_reel(
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
 
 
 
 
 
 
 
 
 
47
  # Construct the download link
48
- file_name = os.path.basename(filename)
49
- download_link = f"https://slimshadow-instagram-r-api.hf.space/files/{file_name}"
50
 
51
  return JSONResponse(content={"download_link": download_link})
52
  except Exception as e:
 
1
  from fastapi import FastAPI, HTTPException, Query
2
+ from fastapi.responses import JSONResponse, FileResponse
3
  import os
4
  from yt_dlp import YoutubeDL
5
 
 
40
  try:
41
  with YoutubeDL(ydl_opts) as ydl:
42
  info = ydl.extract_info(reel_url, download=True)
43
+ original_filename = ydl.prepare_filename(info)
44
+
45
+ # Ensure the file was created
46
+ if not os.path.exists(original_filename):
47
  raise HTTPException(status_code=500, detail="Failed to download the Reel.")
48
 
49
+ # Replace spaces with underscores in the filename
50
+ base_name = os.path.basename(original_filename)
51
+ sanitized_name = base_name.replace(" ", "_")
52
+ sanitized_path = os.path.join(DOWNLOAD_DIR, sanitized_name)
53
+
54
+ # Rename the file
55
+ os.rename(original_filename, sanitized_path)
56
+
57
  # Construct the download link
58
+ download_link = f"http://your-domain.com/files/{sanitized_name}"
 
59
 
60
  return JSONResponse(content={"download_link": download_link})
61
  except Exception as e: