improve selection erroring

This commit is contained in:
Lamp 2023-06-22 21:55:01 -07:00
parent 81ef6df1cf
commit 29a951f4fa

@ -11,10 +11,17 @@ import re
from textvid import generate_video_from_text
def best_mp4(info):
formats = info['entries'][0].get('formats') if info.get("entries") else info.get("formats")
valid = list(filter(lambda x: x['ext'] == "mp4" and x['vcodec'] != 'none' and x['acodec'] != 'none', formats))
best = max(valid, key=lambda x: x['height'])
return best['url']
selection = None
if "entries" in info:
if not info["entries"]:
raise Exception("ERROR: No videos found!")
else:
selection = info["entries"][0]
else: selection = info
valid = list(filter(lambda x: x['ext'] == "mp4" and x['vcodec'] != 'none' and x['acodec'] != 'none', selection["formats"]))
if not valid: raise Exception(f"ERROR: {selection['id']}: No suitable formats of this video available!")
best = max(valid, key=lambda x: x['height'])["url"]
return best
def get_expire(url):
alt_expire = datetime.now() + timedelta(hours=5)
@ -62,16 +69,11 @@ class Handler(BaseHTTPRequestHandler):
ctx_cache[query] = ctx = {'event': Event()}
with YoutubeDL() as ydl:
info = ydl.extract_info(query, download=False)
try:
url = best_mp4(info)
except Exception as e:
logging.exception(e)
#todo these videos are rare but need one to test
raise Exception("ERROR: Couldn't find a suitable format for this video.")
url = best_mp4(info)
ctx['url'] = url
ctx['expire'] = get_expire(url)
except Exception as e:
#logging.exception(e)
logging.exception(e)
ctx['exception'] = e
ctx['error_vid'] = generate_video_from_text(re.sub("(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]", '', str(e)))
finally: