Spaces:
Running
Running
gimhan commited on
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,65 +1,58 @@
|
|
| 1 |
import os
|
| 2 |
import uuid
|
|
|
|
| 3 |
from fastapi import FastAPI, BackgroundTasks, HTTPException
|
| 4 |
from fastapi.responses import FileResponse
|
| 5 |
import yt_dlp
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
-
|
| 9 |
-
# Cookies ෆයිල් එක තියෙන තැන
|
| 10 |
COOKIES_FILE = "cookies.txt"
|
| 11 |
|
| 12 |
def cleanup_file(path: str):
|
| 13 |
-
"""ෆයිල් එක යැව්වට පස්සේ මකලා දානවා (Space එක පිරෙන්නේ නැති වෙන්න)"""
|
| 14 |
if os.path.exists(path):
|
| 15 |
os.remove(path)
|
| 16 |
|
| 17 |
@app.get("/")
|
| 18 |
def home():
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@app.get("/download")
|
| 22 |
async def download_song(url: str, background_tasks: BackgroundTasks):
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
'
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
}],
|
| 39 |
-
'quiet': True,
|
| 40 |
-
'no_warnings': True,
|
| 41 |
-
# Cookies ෆයිල් එක අනිවාර්යයෙන්ම ඕනේ
|
| 42 |
-
'cookiefile': COOKIES_FILE if os.path.exists(COOKIES_FILE) else None,
|
| 43 |
-
# YouTube එකට බොරු කියනවා අපි Browser එකක් කියලා
|
| 44 |
-
'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
|
| 45 |
-
}
|
| 46 |
|
|
|
|
| 47 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 48 |
ydl.download([url])
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
media_type="audio/mpeg",
|
| 60 |
-
filename="song.mp3",
|
| 61 |
-
background=background_tasks.add_task(cleanup_file, mp3_file)
|
| 62 |
-
)
|
| 63 |
|
| 64 |
except Exception as e:
|
|
|
|
| 65 |
return {"error": str(e)}
|
|
|
|
| 1 |
import os
|
| 2 |
import uuid
|
| 3 |
+
import subprocess
|
| 4 |
from fastapi import FastAPI, BackgroundTasks, HTTPException
|
| 5 |
from fastapi.responses import FileResponse
|
| 6 |
import yt_dlp
|
| 7 |
|
| 8 |
app = FastAPI()
|
|
|
|
|
|
|
| 9 |
COOKIES_FILE = "cookies.txt"
|
| 10 |
|
| 11 |
def cleanup_file(path: str):
|
|
|
|
| 12 |
if os.path.exists(path):
|
| 13 |
os.remove(path)
|
| 14 |
|
| 15 |
@app.get("/")
|
| 16 |
def home():
|
| 17 |
+
# FFmpeg තියෙනවද කියලා චෙක් කරනවා
|
| 18 |
+
ffmpeg_check = subprocess.run(["ffmpeg", "-version"], capture_output=True, text=True)
|
| 19 |
+
return {
|
| 20 |
+
"status": "Alive",
|
| 21 |
+
"ffmpeg": "Installed" if ffmpeg_check.returncode == 0 else "Not Found"
|
| 22 |
+
}
|
| 23 |
|
| 24 |
@app.get("/download")
|
| 25 |
async def download_song(url: str, background_tasks: BackgroundTasks):
|
| 26 |
+
file_id = str(uuid.uuid4())
|
| 27 |
+
output_filename = f"{file_id}.mp3"
|
| 28 |
+
|
| 29 |
+
ydl_opts = {
|
| 30 |
+
'format': 'bestaudio/best',
|
| 31 |
+
'outtmpl': f"{file_id}.%(ext)s",
|
| 32 |
+
'postprocessors': [{
|
| 33 |
+
'key': 'FFmpegExtractAudio',
|
| 34 |
+
'preferredcodec': 'mp3',
|
| 35 |
+
'preferredquality': '192',
|
| 36 |
+
}],
|
| 37 |
+
'quiet': False, # Error එක බලාගන්න මේක False කළා
|
| 38 |
+
'cookiefile': COOKIES_FILE if os.path.exists(COOKIES_FILE) else None,
|
| 39 |
+
'user_agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
| 40 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
try:
|
| 43 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 44 |
ydl.download([url])
|
| 45 |
|
| 46 |
+
if os.path.exists(output_filename):
|
| 47 |
+
return FileResponse(
|
| 48 |
+
output_filename,
|
| 49 |
+
media_type="audio/mpeg",
|
| 50 |
+
filename="song.mp3",
|
| 51 |
+
background=background_tasks.add_task(cleanup_file, output_filename)
|
| 52 |
+
)
|
| 53 |
+
else:
|
| 54 |
+
return {"error": "File not found after download."}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
except Exception as e:
|
| 57 |
+
# මෙතනින් තමයි ඇත්තම ලෙඩේ එළියට එවන්නේ
|
| 58 |
return {"error": str(e)}
|