import datetime import os import flask from program_parser import program_parser_factory program_parser = program_parser_factory() app = flask.Flask(__name__, template_folder='templates', static_folder='static', static_url_path='') # Strip whitespace, allowing program template to be spaced out for readability app.jinja_env.trim_blocks = True app.jinja_env.lstrip_blocks = True debug = 'PROD' not in os.environ @app.route('/top', defaults={'width': 50}) @app.route('/top/') def top(width: int): now = datetime.datetime.now() if not program_parser.override_date else program_parser.override_date parts = ['BornHack', 'S105', now.strftime("%a %d %b"), now.strftime("%R:%S")] parts_length = len(''.join(parts)) padding_length = width - parts_length padding = int(padding_length / (len(parts) - 1)) top = (' ' * padding).join(parts) remainder = width - len(top) top = top + " " * remainder return top @app.route('/update', defaults={'width': 50}) @app.route('/update/') def update(width: int): return top(width=width) + '\n' + flask.render_template('program.html', parts=text(width=width)) @app.route('/program.json', defaults={'width': 50}) @app.route('/program.json/') def program(width: int): title_max_length = width - 17 # Truncate the title minus the length of the date, icon and associated whitespace print(f'title_max_length: {title_max_length}') events_to_show: list[Event] = program_parser.get_events_to_show(program_parser.get_program(), title_max_length=title_max_length) #{event.title} {day} {start_time}-{end_time} {icon} return events_to_show @app.route("/text/", defaults={'width': 50}) @app.route("/text/") def text(width: int) -> dict[str, str]: return {part: eval(part)(width=width) for part in ['top', 'program']} @app.route('/') def index(): arg_defaults = { 'scale': 2, 'line_height': 1.5, 'width': 50, } args = {key: flask.request.args.get(key, value) for key, value in arg_defaults.items()} args['width'] = int(args['width']) return flask.render_template('index.html', parts=text(width=args['width']), **args) def main(): global app app.run(debug=debug, host='0.0.0.0', port=1337) if __name__ == '__main__': main()