import os import magic 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/") 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/") 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 @bp.route("/") def index(): return render_tmpl( 'index.html', lb=listens, lb_now=listening, recent=recent.get('data', {}), owned=owned.get('data', {}), tmsmp=tmsmp, rtmsmp=rtmsmp )