Spaces:
Running
Running
File size: 2,547 Bytes
0da53ab ae4117d 0da53ab ad3aa57 0da53ab ae4117d 0da53ab ae4117d c691131 0da53ab ae4117d 0da53ab ae4117d 0da53ab ae4117d ad3aa57 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
import os
import uuid
import subprocess
from fastapi import FastAPI, BackgroundTasks, HTTPException
from fastapi.responses import FileResponse
import yt_dlp
import socket
app = FastAPI()
COOKIES_FILE = "cookies.txt"
def cleanup_file(path: str):
if os.path.exists(path):
os.remove(path)
@app.get("/")
def home():
# FFmpeg තියෙනවද කියලා චෙක් කරනවා
ffmpeg_check = subprocess.run(["ffmpeg", "-version"], capture_output=True, text=True)
return {
"status": "Alive",
"ffmpeg": "Installed" if ffmpeg_check.returncode == 0 else "Not Found"
}
@app.get("/download")
async def download_song(url: str, background_tasks: BackgroundTasks):
file_id = str(uuid.uuid4())
output_filename = f"{file_id}.mp3"
ydl_opts = {
'format': 'bestaudio/best',
'outtmpl': f"{file_id}.%(ext)s",
'source_address': '0.0.0.0', # 🔥 මේ පේළිය අනිවාර්යයෙන්ම දාන්න (Force IPv4)
'nocheckcertificate': True, # SSL අවුල් මගහරින්න
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'quiet': False,
'cookiefile': COOKIES_FILE if os.path.exists(COOKIES_FILE) else None,
'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',
}
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
if os.path.exists(output_filename):
return FileResponse(
output_filename,
media_type="audio/mpeg",
filename="song.mp3",
background=background_tasks.add_task(cleanup_file, output_filename)
)
else:
return {"error": "File not found after download."}
except Exception as e:
# මෙතනින් තමයි ඇත්තම ලෙඩේ එළියට එවන්නේ
return {"error": str(e)}
import socket
@app.get("/debug")
def debug_network():
try:
# YouTube resolve වෙනවද කියලා බලනවා
host = "www.youtube.com"
ip = socket.gethostbyname(host)
return {"status": "Success", "host": host, "ip": ip}
except Exception as e:
return {"status": "Failed", "error": str(e)}
|