darkvibe314 commited on
Commit
3b8a142
·
verified ·
1 Parent(s): 66e27d9

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +12 -11
main.py CHANGED
@@ -17,7 +17,6 @@ app = FastAPI(
17
  version="1.0.0"
18
  )
19
 
20
- # Allow your bots to connect from anywhere
21
  app.add_middleware(
22
  CORSMiddleware,
23
  allow_origins=["*"],
@@ -25,7 +24,6 @@ app.add_middleware(
25
  allow_headers=["*"],
26
  )
27
 
28
- # Background task to clean up storage after sending the file
29
  def cleanup_files(files: list):
30
  for f in files:
31
  if os.path.exists(f):
@@ -35,12 +33,12 @@ def cleanup_files(files: list):
35
  pass
36
 
37
  # ==========================================
38
- # 📡 ENDPOINTS
39
  # ==========================================
40
 
41
  @app.get("/")
42
  def read_root():
43
- return {"status": "Online", "message": "SILENT TECH Engine is running. Visit /docs for the API Documentation!"}
44
 
45
  @app.get("/api/search")
46
  async def search_yt(query: str):
@@ -51,6 +49,10 @@ async def search_yt(query: str):
51
  data = await resp.json()
52
  if not data.get("status"):
53
  raise HTTPException(status_code=404, detail="Not found")
 
 
 
 
54
  return data
55
 
56
  @app.get("/api/ytmp4")
@@ -62,6 +64,10 @@ async def get_ytmp4(url: str):
62
  data = await resp.json()
63
  if not data.get("success"):
64
  raise HTTPException(status_code=400, detail="Failed to fetch video link")
 
 
 
 
65
  return data
66
 
67
  @app.get("/api/ytmp3")
@@ -71,11 +77,9 @@ async def get_ytmp3(url: str, background_tasks: BackgroundTasks):
71
  temp_vid = f"temp_vid_{timestamp}.mp4"
72
  temp_aud = f"temp_aud_{timestamp}.mp3"
73
 
74
- # Tell the API to delete these files AFTER the user finishes downloading them
75
  background_tasks.add_task(cleanup_files, [temp_vid, temp_aud])
76
 
77
  try:
78
- # 1. Fetch the raw MP4 stream link
79
  async with aiohttp.ClientSession() as session:
80
  api_url = f"https://apis.davidcyril.name.ng/download/ytmp4?url={urllib.parse.quote(url)}"
81
  async with session.get(api_url) as resp:
@@ -86,15 +90,13 @@ async def get_ytmp3(url: str, background_tasks: BackgroundTasks):
86
  download_url = data["result"]["download_url"]
87
  title = data["result"].get("title", "Silent_Tech_Audio").replace("/", "_")
88
 
89
- # 2. Download the MP4 file to Hugging Face Disk
90
  async with session.get(download_url) as video_resp:
91
  with open(temp_vid, 'wb') as f:
92
  while True:
93
- chunk = await video_resp.content.read(2 * 1024 * 1024) # 2MB chunks
94
  if not chunk: break
95
  f.write(chunk)
96
 
97
- # 3. SuperSonic FFmpeg Conversion
98
  command = [
99
  "ffmpeg", "-y", "-i", temp_vid,
100
  "-vn", "-acodec", "libmp3lame", "-q:a", "2",
@@ -104,7 +106,6 @@ async def get_ytmp3(url: str, background_tasks: BackgroundTasks):
104
  if process.returncode != 0:
105
  raise Exception("FFmpeg audio extraction failed.")
106
 
107
- # 4. Stream the MP3 directly back to the user/bot
108
  return FileResponse(
109
  path=temp_aud,
110
  media_type="audio/mpeg",
@@ -112,5 +113,5 @@ async def get_ytmp3(url: str, background_tasks: BackgroundTasks):
112
  )
113
 
114
  except Exception as e:
115
- background_tasks.add_task(cleanup_files, [temp_vid, temp_aud]) # Cleanup on error
116
  raise HTTPException(status_code=500, detail=str(e))
 
17
  version="1.0.0"
18
  )
19
 
 
20
  app.add_middleware(
21
  CORSMiddleware,
22
  allow_origins=["*"],
 
24
  allow_headers=["*"],
25
  )
26
 
 
27
  def cleanup_files(files: list):
28
  for f in files:
29
  if os.path.exists(f):
 
33
  pass
34
 
35
  # ==========================================
36
+ # 📡 ENDPOINTS (WHITE-LABELED)
37
  # ==========================================
38
 
39
  @app.get("/")
40
  def read_root():
41
+ return {"status": "Online", "creator": "SILENT TECH", "message": "Visit /docs for the API Documentation!"}
42
 
43
  @app.get("/api/search")
44
  async def search_yt(query: str):
 
49
  data = await resp.json()
50
  if not data.get("status"):
51
  raise HTTPException(status_code=404, detail="Not found")
52
+
53
+ # 🥷 THE NINJA MOVE: Overwrite the creator name!
54
+ data["creator"] = "SILENT TECH"
55
+
56
  return data
57
 
58
  @app.get("/api/ytmp4")
 
64
  data = await resp.json()
65
  if not data.get("success"):
66
  raise HTTPException(status_code=400, detail="Failed to fetch video link")
67
+
68
+ # 🥷 THE NINJA MOVE: Overwrite the creator name!
69
+ data["creator"] = "SILENT TECH"
70
+
71
  return data
72
 
73
  @app.get("/api/ytmp3")
 
77
  temp_vid = f"temp_vid_{timestamp}.mp4"
78
  temp_aud = f"temp_aud_{timestamp}.mp3"
79
 
 
80
  background_tasks.add_task(cleanup_files, [temp_vid, temp_aud])
81
 
82
  try:
 
83
  async with aiohttp.ClientSession() as session:
84
  api_url = f"https://apis.davidcyril.name.ng/download/ytmp4?url={urllib.parse.quote(url)}"
85
  async with session.get(api_url) as resp:
 
90
  download_url = data["result"]["download_url"]
91
  title = data["result"].get("title", "Silent_Tech_Audio").replace("/", "_")
92
 
 
93
  async with session.get(download_url) as video_resp:
94
  with open(temp_vid, 'wb') as f:
95
  while True:
96
+ chunk = await video_resp.content.read(2 * 1024 * 1024)
97
  if not chunk: break
98
  f.write(chunk)
99
 
 
100
  command = [
101
  "ffmpeg", "-y", "-i", temp_vid,
102
  "-vn", "-acodec", "libmp3lame", "-q:a", "2",
 
106
  if process.returncode != 0:
107
  raise Exception("FFmpeg audio extraction failed.")
108
 
 
109
  return FileResponse(
110
  path=temp_aud,
111
  media_type="audio/mpeg",
 
113
  )
114
 
115
  except Exception as e:
116
+ background_tasks.add_task(cleanup_files, [temp_vid, temp_aud])
117
  raise HTTPException(status_code=500, detail=str(e))