iwm_browser/iwm_browser/utils.py
2023-06-09 14:46:23 +02:00

66 lines
1.9 KiB
Python

from imgproxy import ImgProxy
import json
import os
import urllib.parse
def convert_times(time):
# Not sure if it's the right way to do that, but it seems to work.
time_seconds = ((time % 60000) / 100) * 2
minutes, seconds = divmod(time_seconds, 60)
hours, minutes = divmod(minutes, 60)
hours = str(round(hours)).zfill(2)
minutes = str(round(minutes)).zfill(2)
seconds = "{0:,.2f}".format(seconds).zfill(5)
return (hours, minutes, seconds)
def config_value(key: str, default_value=""):
"""Searches for environ value for the specified key,
or returns the default value.
The value must be in json format!"""
if key in os.environ:
try:
return json.loads(os.environ[key])
except json.JSONDecodeError:
return os.environ[key]
else:
return default_value
def isSelected(selection, value):
"""Returns 'selected' if values are equal.
Intended for selection boxes"""
return 'selected="true"' if selection == value else ''
def combineCSS(*paths):
"""Generates the /assets/combine_css url"""
return f"/assets/combine_css?resources={','.join(paths)}"
# Specify default imgproxy instance.
global_imgproxy_url = None
global_imgproxy_params = None
def generate_thumbnail_url(
level_id, thumbnail_url, imgproxy_url=None, imgproxy_params={}
):
"""Generates thumbnail url."""
global global_imgproxy_url
global global_imgproxy_params
if imgproxy_url is None and global_imgproxy_url is not None:
imgproxy_url = global_imgproxy_url
if imgproxy_params == {} and global_imgproxy_params is not None:
imgproxy_params = global_imgproxy_params
# Create url
img_url = thumbnail_url + "/" + str(level_id) + ".png"
if imgproxy_url is not None:
img_url = ImgProxy(img_url, proxy_host=imgproxy_url, **imgproxy_params)
img_url = str(img_url)
return img_url