rewrite width

This commit is contained in:
Roy Olav Purser 2021-05-13 16:44:09 +02:00
parent 39375d7478
commit 118ffa16f8
Signed by: roypur
GPG Key ID: E14D26A036F21656

View File

@ -190,41 +190,43 @@ class MainHandler(tornado.web.RequestHandler):
self.set_status(404) self.set_status(404)
self.write("HTML template missing.") self.write("HTML template missing.")
def handle_embed(self, provider): def handle_embed(self, provider):
width = self.get_query_argument("maxwidth", None) width_str = self.get_query_argument("maxwidth", None)
height = self.get_query_argument("maxheight", None) height_str = self.get_query_argument("maxheight", None)
if isinstance(width, str) and isinstance(height, str): width = 320
height = 180
try:
if isinstance(width_str, str):
width_new = int(width_str)
if width_new > 16:
width = width_new
else:
raise ValueError("invalid width")
if isinstance(height_str, str):
height_new = int(height_str)
if height_new > 9:
height = height_new
else:
raise ValueError("invalid height")
except Exception:
pass pass
if isinstance(width, str):
try:
width_num = (int(width) // 16) * 16
except Exception:
width = "320"
height = "180"
else: else:
height_num = (width_num * 9) / 16 if isinstance(width_str, str) and isinstance(height_str, str):
height = str(height_num) pass
width = str(width_num) elif isinstance(width_str, str):
elif isinstance(height, str): width = (width // 16) * 16
try: height = (width * 9) / 16
height_num = (int(height) // 9) * 9 elif isinstance(height_str, str):
except Exception: height = (height // 9) * 9
width = "320" width = (height * 16) / 9
height = "180"
else:
width_num = (height_num * 16) / 9
width = str(width_num)
height = str(height_num)
else:
width = "320"
height = "180"
origin = self.request.path origin = self.request.path
if stream_server is not None: if stream_server is not None:
origin = f'{stream_server}{self.request.path}' origin = f'{stream_server}{self.request.path}'
embed_json = {} embed_json = {}
embed_json["version"] = "1.0" embed_json["version"] = "1.0"
embed_json["type"] = "video" embed_json["type"] = "video"
embed_json["width"] = max_width embed_json["width"] = width
embed_json["height"] = max_height embed_json["height"] = height
embed_json["html"] = str(template_embed.generate(origin=origin, provider=provider), "utf-8") embed_json["html"] = str(template_embed.generate(origin=origin, provider=provider), "utf-8")
self.set_header("Content-Type", "application/json; charset=utf-8") self.set_header("Content-Type", "application/json; charset=utf-8")
self.write(json.dumps(embed_json)) self.write(json.dumps(embed_json))