Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import yt_dlp
|
| 2 |
+
import requests
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
+
import json
|
| 6 |
+
import time
|
| 7 |
+
from fastapi import FastAPI, Query
|
| 8 |
+
from fastapi.responses import JSONResponse, FileResponse
|
| 9 |
+
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
def process(url, audio=False):
|
| 13 |
+
tmpdir = tempfile.mkdtemp()
|
| 14 |
+
filename = os.path.join(tmpdir, "%(title)s.%(ext)s")
|
| 15 |
+
ydl_opts = {
|
| 16 |
+
"outtmpl": filename,
|
| 17 |
+
"quiet": True,
|
| 18 |
+
"format": "bestaudio/best" if audio else "bestvideo+bestaudio/best",
|
| 19 |
+
"postprocessors": [
|
| 20 |
+
{"key": "FFmpegExtractAudio", "preferredcodec": "mp3", "preferredquality": "192"}
|
| 21 |
+
] if audio else [
|
| 22 |
+
{"key": "FFmpegVideoConvertor", "preferedformat": "mp4"}
|
| 23 |
+
],
|
| 24 |
+
}
|
| 25 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 26 |
+
info = ydl.extract_info(url, download=True)
|
| 27 |
+
final_file = ydl.prepare_filename(info)
|
| 28 |
+
if audio:
|
| 29 |
+
final_file = os.path.splitext(final_file)[0] + ".mp3"
|
| 30 |
+
else:
|
| 31 |
+
final_file = os.path.splitext(final_file)[0] + ".mp4"
|
| 32 |
+
video_id = info.get("id")
|
| 33 |
+
dislikes = {}
|
| 34 |
+
try:
|
| 35 |
+
r = requests.get(f"https://returnyoutubedislikeapi.com/votes?videoId={video_id}")
|
| 36 |
+
dislikes = r.json()
|
| 37 |
+
except:
|
| 38 |
+
pass
|
| 39 |
+
metadata = {
|
| 40 |
+
"title": info.get("title"),
|
| 41 |
+
"uploader": info.get("uploader"),
|
| 42 |
+
"duration": info.get("duration"),
|
| 43 |
+
"view_count": info.get("view_count"),
|
| 44 |
+
"like_count": info.get("like_count"),
|
| 45 |
+
"dislikes": dislikes.get("dislikes"),
|
| 46 |
+
"id": video_id,
|
| 47 |
+
"ext": "mp3" if audio else "mp4",
|
| 48 |
+
"download_path": final_file
|
| 49 |
+
}
|
| 50 |
+
return metadata
|
| 51 |
+
|
| 52 |
+
@app.get("/download")
|
| 53 |
+
async def download(url: str = Query(...), type: str = Query("video")):
|
| 54 |
+
audio = type == "audio"
|
| 55 |
+
meta = process(url, audio)
|
| 56 |
+
return JSONResponse(meta)
|
| 57 |
+
|
| 58 |
+
@app.get("/file")
|
| 59 |
+
async def file(path: str):
|
| 60 |
+
if os.path.exists(path):
|
| 61 |
+
resp = FileResponse(path, filename=os.path.basename(path))
|
| 62 |
+
def cleanup():
|
| 63 |
+
time.sleep(2)
|
| 64 |
+
if os.path.exists(path):
|
| 65 |
+
os.remove(path)
|
| 66 |
+
os.rmdir(os.path.dirname(path))
|
| 67 |
+
import threading
|
| 68 |
+
threading.Thread(target=cleanup).start()
|
| 69 |
+
return resp
|
| 70 |
+
return JSONResponse({"error": "file not found"}, status_code=404)
|