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.write("HTML template missing.")
def handle_embed(self, provider):
width = self.get_query_argument("maxwidth", None)
height = self.get_query_argument("maxheight", None)
if isinstance(width, str) and isinstance(height, str):
width_str = self.get_query_argument("maxwidth", None)
height_str = self.get_query_argument("maxheight", None)
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
if isinstance(width, str):
try:
width_num = (int(width) // 16) * 16
except Exception:
width = "320"
height = "180"
else:
height_num = (width_num * 9) / 16
height = str(height_num)
width = str(width_num)
elif isinstance(height, str):
try:
height_num = (int(height) // 9) * 9
except Exception:
width = "320"
height = "180"
else:
width_num = (height_num * 16) / 9
width = str(width_num)
height = str(height_num)
else:
width = "320"
height = "180"
if isinstance(width_str, str) and isinstance(height_str, str):
pass
elif isinstance(width_str, str):
width = (width // 16) * 16
height = (width * 9) / 16
elif isinstance(height_str, str):
height = (height // 9) * 9
width = (height * 16) / 9
origin = self.request.path
if stream_server is not None:
origin = f'{stream_server}{self.request.path}'
embed_json = {}
embed_json["version"] = "1.0"
embed_json["type"] = "video"
embed_json["width"] = max_width
embed_json["height"] = max_height
embed_json["width"] = width
embed_json["height"] = height
embed_json["html"] = str(template_embed.generate(origin=origin, provider=provider), "utf-8")
self.set_header("Content-Type", "application/json; charset=utf-8")
self.write(json.dumps(embed_json))