import os import yt_dlp from .. import helpers def get(request, check = "huh"): url = helpers.getFromRequest(request, "url") if not url: return {"status": "error", "details": { "error_code": 101, "error_details": "No link provided" }}, 400 bitrate = helpers.getFromRequest(request, "bitrate") if not bitrate: bitrate = "64k" quality = helpers.getFromRequest(request, "quality") if not quality or quality.lower() not in ['best', 'worst']: quality = 'worst' else: quality = quality.lower() urlcode = url.partition('?v=')[2] if not urlcode: urlcode = "NPRNRQh2fAo" config = helpers.configFile() if os.path.exists(f"{config['static-path']}/{check}/{urlcode}.ogg"): return {"status": "pass", 'done-or-not': True, 'ytdlp-code': 0, 'urlcode': urlcode, "path": f"{config['static-path']}/{check}/{urlcode}.ogg", "quality": quality, "bitrate": bitrate} if os.path.exists(f"{config['temp-path']}/{urlcode}.ogg"): return {"status": "pass", 'done-or-not': False, 'ytdlp-code': 0, 'urlcode': urlcode, "path": f"{config['temp-path']}/{urlcode}.ogg", "quality": quality, "bitrate": bitrate} ydl_opts = { 'format': f'ogg/{quality}audio/{quality}', 'outtmpl': f"{config['temp-path']}/{urlcode}.ogg", 'progress_hooks': [helpers.thisIsHook], } try: with yt_dlp.YoutubeDL(ydl_opts) as ydl: error_code = ydl.download(url) except Exception as e: return {"status": "error", "details": {"error_code": 102, "error_details": str(e)}}, 400 return {"status": "pass", 'done-or-not': False, 'ytdlp-code': error_code, 'urlcode': urlcode, "path": f"{config['temp-path']}/{urlcode}.ogg", "quality": quality, "bitrate": bitrate}