summaryrefslogtreecommitdiff
path: root/src/text_tv.py
blob: af7d744d7a2e08a0cfec075eec7d6aafc51e9a7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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='')

debug = 'PROD' not in os.environ

@app.route('/top', defaults={'width': 50})
@app.route('/top/<width>')
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/<width>')
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/<width>')
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/<width>")
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()