Files
lair.moe/blueprints/risdeveau/__init__.py
T
Sweetbread 23bd539799
Docker Build and Push / build-and-push (push) Successful in 55s
Add Steam info
2026-02-04 00:34:16 +03:00

75 lines
1.8 KiB
Python

import os
from pathlib import Path
from htmlmin import minify
from datetime import datetime, timedelta
from musicbrainzngs import get_image_front
from flask import (
Blueprint,
render_template,
send_file,
send_from_directory,
make_response,
abort,
)
from .modules.api.lb import listens, listening
from .modules.api.steam import recent, owned
def tmsmp(min: int) -> str:
if min < 60:
return f"{min} m"
elif min < 60*24:
return f"{min/60:.1f} h"
else:
return f"{min/60/24:.1f} d"
def rtmsmp(unix: int) -> str:
return datetime \
.utcfromtimestamp(unix) \
.strftime('%Y-%m-%d %H:%M:%S')
bp = Blueprint(
"risdeveau",
__name__,
subdomain="risdeveau",
template_folder="..",
static_folder=None
)
def render_tmpl(filename: str, **kwargs) -> str:
template_path = os.path.join("risdeveau/templates", filename)
return minify(
render_template(template_path, **kwargs),
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(get_image_front(mbid, "250"))
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
@bp.route("/")
def index():
print(recent)
return render_tmpl(
'index.html',
lb=listens,
lb_now=listening,
recent=recent.get('data', {}),
owned=owned.get('data', {}),
tmsmp=tmsmp,
rtmsmp=rtmsmp
)