import os import yt_dlp from .. import helpers def get(request, check = "huh"): try: if request.method == 'POST': signature = request.form['signature'] else: signature = request.args['signature'] except: return {"status": "error", "details": { "error_code": 103, "error_details": "No signature" }} if not helpers.checkSignature(signature): return {"status": "error", "details": { "error_code": 105, "error_details": "Invalid signature" }} try: if request.method == 'POST': url = request.form['url'] else: url = request.args['url'] if url.strip() in ['', None]: raise Exception() except: return {"status": "error", "details": { "error_code": 101, "error_details": "No link provided" }} try: if request.method == 'POST': bitrate = str(request.form['bitrate']) else: bitrate = str(request.args['bitrate']) except: bitrate = "64k" try: if request.method == 'POST': quality = request.form['quality'] else: quality = request.args['quality'] if quality.lower() not in ['best', 'worst']: raise Exception() except: quality = 'worst' urlcode = None try: urlcode = url.partition('?v=')[2] except: urlcode = helpers.randString() if urlcode in ['', None]: urlcode = helpers.randString() if os.path.exists("/home/ubuntu/api/static/{}/{}.ogg".format(check, urlcode)): return {"status": "pass", 'done-or-not': True, 'ytdlp-code': 0, 'urlcode': urlcode, "path": "/home/ubuntu/api/static/{}/{}.ogg".format(check, urlcode), "quality": quality, "bitrate": bitrate} if os.path.exists("/home/ubuntu/api/static/temp/{}.ogg".format(urlcode)): return {"status": "pass", 'done-or-not': False, 'ytdlp-code': 0, 'urlcode': urlcode, "path": "/home/ubuntu/api/static/temp/{}.ogg".format(urlcode), "quality": quality, "bitrate": bitrate} ydl_opts = { 'format': f'ogg/{quality}audio/{quality}', 'outtmpl': "/home/ubuntu/api/static/temp/{}.ogg".format(urlcode), '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)}} return {"status": "pass", 'done-or-not': False, 'ytdlp-code': error_code, 'urlcode': urlcode, "path": "/home/ubuntu/api/static/temp/{}.ogg".format(urlcode), "quality": quality, "bitrate": bitrate}