aboutsummaryrefslogtreecommitdiff
path: root/serve.py
blob: 411a5b2adff55df30281d74144801c86771020f0 (plain) (blame)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
#
# Help in Daily Bulletin template development by dynamically filling templates
# with flask as the templates are being worked on. DO NOT USE IN PRODUCTION.
# Copyright (C) 2024 Runxi Yu <https://runxiyu.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#


from __future__ import annotations
from typing import Union, TypeAlias
import json
import datetime
import zoneinfo
import os
import configparser
from jinja2 import StrictUndefined
from werkzeug.wrappers.response import Response as werkzeugResponse
from flask import (
    Flask,
    Response,
    render_template,
)

ResponseType: TypeAlias = Union[Response, werkzeugResponse, str]

app = Flask(__name__)
app.jinja_env.undefined = StrictUndefined

config = configparser.ConfigParser()
config.read("config.ini")


# extra_data = {
#     "aod": data["aods"][0],  # FIXME
#     "stddate": "2024-04-01",
#     "weekday_english": "Monday",
#     "weekday_abbrev": "Mon",
#     "next_weekday_abbrev": "Tue",
#     "weekday_chinese": "周一",
#     "day_of_cycle": "SA",
#     "today_breakfast": ("1", "2", "3", "4", "5", "6", "7", "8"),
#     "today_lunch": ("1", "2", "3", "4", "5", "6", "7", "8"),
#     "today_dinner": ("1", "2", "3", "4", "5", "6", "7", "8"),
#     "next_breakfast": ("1", "2", "3", "4", "5", "6", "7", "8"),
# }
#
# data = data | extra_data


@app.route("/")
def index() -> ResponseType:
    with open(
        os.path.join(
            config["general"]["build_path"],
            "day-%s.json" % (datetime.datetime.now(tz=zoneinfo.ZoneInfo("Asia/Shanghai")) + datetime.timedelta(days=1)).strftime("%Y%m%d"),
        ),
        "r",
        encoding="utf-8",
    ) as fd:
        data = json.load(fd)
    return render_template("template.html", **data)


@app.route("/<date>")
def date(date: str) -> ResponseType:
    with open(
        os.path.join(config["general"]["build_path"], "day-%s.json" % date),
        "r",
        encoding="utf-8",
    ) as fd:
        data = json.load(fd)
    return render_template("template.html", **data)


# The lack of the __name__ check is intentional. This script should not be used
# in a production server.

app.run(port=8000, debug=True, use_reloader=True)