Files
lair.moe/blueprints/risdeveau/__init__.py
T

107 lines
2.8 KiB
Python
Raw Normal View History

2026-01-17 18:55:25 +03:00
import os
2026-02-06 23:24:41 +03:00
from datetime import datetime, timedelta
2026-01-17 18:55:25 +03:00
from pathlib import Path
2026-02-04 00:34:16 +03:00
from time import time
2026-02-06 23:24:41 +03:00
from flask import (
Blueprint,
2026-02-06 23:24:41 +03:00
abort,
make_response,
render_template,
2026-02-06 23:24:41 +03:00
request,
send_file,
)
2026-02-06 23:24:41 +03:00
import magic
from htmlmin import minify
from musicbrainzngs import get_image_front
2026-04-08 22:55:27 +03:00
from .modules.api.lb import data as lb_data, refresh_cache as lb_refresh
from .modules.api.steam import data as steam_data, refresh_cache as steam_refresh
2026-02-04 00:34:16 +03:00
def tmsmp(sec: int) -> str:
if sec == 0:
return 0
elif sec < 60:
return f"{sec} s"
elif sec < 60*60:
minutes = round(sec / 60, 1)
return f"{minutes:.0f} m" if minutes.is_integer() else f"{minutes:.1f} m"
elif sec < 60*60*24:
hours = round(sec / 3600, 1)
return f"{hours:.0f} h" if hours.is_integer() else f"{hours:.1f} h"
else:
days = round(sec / 86400, 1)
return f"{days:.0f} d" if days.is_integer() else f"{days:.1f} d"
def utmsmp(unix: int) -> str:
return datetime \
.utcfromtimestamp(unix) \
.strftime('%Y-%m-%d %H:%M:%S')
def rtmsmp(unix: int) -> str:
return tmsmp(int(time() - unix))
2026-01-17 18:55:25 +03:00
bp = Blueprint(
"risdeveau",
__name__,
subdomain="risdeveau",
template_folder="..",
static_folder=None
)
def render_tmpl(filename: str, **kwargs) -> str:
2026-01-17 18:55:25 +03:00
template_path = os.path.join("risdeveau/templates", filename)
return minify(
render_template(template_path, **kwargs),
2026-01-17 18:55:25 +03:00
remove_empty_space=True
)
@bp.route("/static/<path:filename>")
def static(filename: str):
static_folders = ("static", "../root/static")
for dir in static_folders:
if os.path.exists(path := os.path.join("blueprints/risdeveau", dir, filename)):
return send_file(path)
return abort(404)
@bp.route("/asset/mb/<mbid>")
def mb_cover(mbid):
r = make_response(image := get_image_front(mbid, "250"))
r.headers['Content-Type'] = magic.from_buffer(image[:2048], mime=True)
r.headers['Cache-Control'] = 'public, max-age=86400'
r.headers['Expires'] = (datetime.now() + timedelta(days=1)) \
.strftime('%a, %d %b %Y %H:%M:%S GMT')
return r
2026-02-04 00:34:16 +03:00
args = {
2026-02-06 23:24:41 +03:00
"lb": lb_data,
"steam": steam_data,
2026-02-04 00:34:16 +03:00
"tmsmp": tmsmp,
"utmsmp": utmsmp,
"rtmsmp": rtmsmp
}
2026-01-17 18:55:25 +03:00
@bp.route("/")
def index():
2026-04-08 22:55:27 +03:00
lb_refresh()
steam_refresh()
2026-03-31 16:09:57 +03:00
return render_tmpl('index.pug', **args)
2026-02-05 14:20:03 +03:00
@bp.route("/m/<module>")
def module(module):
2026-04-08 22:55:27 +03:00
if module == "listenbrainz":
lb_refresh()
elif module == "steam":
steam_refresh()
2026-03-31 16:09:57 +03:00
if none_match := request.headers.get('if-none-match'):
2026-02-06 18:52:55 +03:00
match module:
case "listenbrainz":
2026-02-06 23:24:41 +03:00
if none_match == lb_data['etag']:
2026-02-06 18:52:55 +03:00
return '', 304
case "steam":
2026-02-06 23:24:41 +03:00
if none_match == steam_data['etag']:
2026-02-06 18:52:55 +03:00
return '', 304
2026-03-31 16:09:57 +03:00
return render_tmpl(f'{module}.pug', **args)