annotate weather_server/server.py @ 25:a4147ecb18b3

ignore all .vscode nonsense.
author Paul Fisher <paul@pfish.zone>
date Sun, 10 Nov 2019 23:43:37 -0500
parents 88249e451566
children 7def5611895b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11
52ef21607b31 server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents: 8
diff changeset
1 import datetime
4
e7c8dcc5fc15 Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
2 import hmac
21
beb42c835c52 Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents: 16
diff changeset
3 import pathlib
5
368f732f13d0 Actually make the server runnable.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
4 import sys
4
e7c8dcc5fc15 Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
5
1
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
6 import bson
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
7 import flask
21
beb42c835c52 Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents: 16
diff changeset
8 import pytz
1
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
9
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
10 from . import common
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
11 from . import locations
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
12
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
13
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
14 def build_app(root_directory: str) -> flask.Flask:
21
beb42c835c52 Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents: 16
diff changeset
15 locs = locations.LocationFolder(pathlib.Path(root_directory))
1
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
16 app = flask.Flask(__name__)
8
d54155a199d8 Improve template; add stylesheet.
Paul Fisher <paul@pfish.zone>
parents: 5
diff changeset
17 app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
1
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
18
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
19 @app.route('/favicon.ico')
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
20 def favicon():
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
21 return flask.send_file('static/favicon.ico')
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
22
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
23 @app.route('/')
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
24 def home():
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
25 return 'Weather server'
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
26
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
27 @app.route('/_submit', methods=['POST'])
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
28 def submit():
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
29 req = flask.request
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
30 reader = bson.decode_file_iter(
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
31 req.stream, codec_options=common.BSON_OPTIONS)
3
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
32 try:
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
33 preamble = next(reader)
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
34 loc_name = preamble['location']
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
35 password = str(preamble['password'])
21
beb42c835c52 Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents: 16
diff changeset
36 loc = locs.get(loc_name)
3
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
37 if not hmac.compare_digest(password, loc.password):
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
38 flask.abort(400)
21
beb42c835c52 Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents: 16
diff changeset
39 entries = tuple(reader)
3
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
40 except (KeyError, bson.InvalidBSON):
b42c4bfe57c7 server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents: 2
diff changeset
41 flask.abort(400)
21
beb42c835c52 Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents: 16
diff changeset
42 now = datetime.datetime.now(tz=pytz.UTC)
beb42c835c52 Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents: 16
diff changeset
43 loc.record(entries, now)
2
cda47993a193 server: fix bugs and improve template.
Paul Fisher <paul@pfish.zone>
parents: 1
diff changeset
44 return flask.jsonify({'status': 'OK'})
1
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
45
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
46 @app.route('/<location>')
11
52ef21607b31 server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents: 8
diff changeset
47 def show(location: str):
1
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
48 try:
21
beb42c835c52 Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents: 16
diff changeset
49 loc = locs.get(location)
1
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
50 except KeyError:
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
51 flask.abort(404)
21
beb42c835c52 Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents: 16
diff changeset
52 last_reading = loc.latest()
beb42c835c52 Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents: 16
diff changeset
53 if last_reading:
2
cda47993a193 server: fix bugs and improve template.
Paul Fisher <paul@pfish.zone>
parents: 1
diff changeset
54 tz = loc.timezone()
1
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
55 date = tz.normalize(last_reading.sample_time.astimezone(tz))
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
56 else:
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
57 date = None
23
88249e451566 server: show date when last report was >12h ago.
Paul Fisher <paul@pfish.zone>
parents: 21
diff changeset
58 now = datetime.datetime.now(tz=pytz.UTC)
88249e451566 server: show date when last report was >12h ago.
Paul Fisher <paul@pfish.zone>
parents: 21
diff changeset
59 diff = (now - date) if date else None
88249e451566 server: show date when last report was >12h ago.
Paul Fisher <paul@pfish.zone>
parents: 21
diff changeset
60 is_recent = diff and diff < datetime.timedelta(hours=12)
1
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
61 return flask.render_template(
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
62 'location.html',
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
63 location=loc,
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
64 last_reading=last_reading,
23
88249e451566 server: show date when last report was >12h ago.
Paul Fisher <paul@pfish.zone>
parents: 21
diff changeset
65 date=date,
88249e451566 server: show date when last report was >12h ago.
Paul Fisher <paul@pfish.zone>
parents: 21
diff changeset
66 date_format=f'%H:%M' if is_recent else '%Y-%m-%d %H:%M')
1
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
67
11
52ef21607b31 server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents: 8
diff changeset
68 @app.route('/<location>/recent')
52ef21607b31 server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents: 8
diff changeset
69 def recent(location: str):
52ef21607b31 server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents: 8
diff changeset
70 try:
21
beb42c835c52 Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents: 16
diff changeset
71 loc = locs.get(location)
11
52ef21607b31 server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents: 8
diff changeset
72 except KeyError:
52ef21607b31 server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents: 8
diff changeset
73 flask.abort(404)
52ef21607b31 server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents: 8
diff changeset
74 req = flask.request
52ef21607b31 server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents: 8
diff changeset
75
52ef21607b31 server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents: 8
diff changeset
76 try:
52ef21607b31 server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents: 8
diff changeset
77 seconds = int(req.args['seconds'])
52ef21607b31 server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents: 8
diff changeset
78 except (KeyError, ValueError):
52ef21607b31 server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents: 8
diff changeset
79 flask.abort(400)
52ef21607b31 server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents: 8
diff changeset
80
52ef21607b31 server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents: 8
diff changeset
81 start = common.utc_now() - datetime.timedelta(seconds=seconds)
52ef21607b31 server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents: 8
diff changeset
82
21
beb42c835c52 Make weather server handle arbitrary data:
Paul Fisher <paul@pfish.zone>
parents: 16
diff changeset
83 readings = [r for r in loc.entries if start < r['sample_time']]
11
52ef21607b31 server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents: 8
diff changeset
84 resp = flask.Response()
52ef21607b31 server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents: 8
diff changeset
85 resp.content_type = 'application/json'
16
9a609bcf0809 Port main script to TypeScript and prepare for serving it.
Paul Fisher <paul@pfish.zone>
parents: 11
diff changeset
86 resp.data = common.json_dumps({
9a609bcf0809 Port main script to TypeScript and prepare for serving it.
Paul Fisher <paul@pfish.zone>
parents: 11
diff changeset
87 'timezone': loc.tz_name,
9a609bcf0809 Port main script to TypeScript and prepare for serving it.
Paul Fisher <paul@pfish.zone>
parents: 11
diff changeset
88 'readings': readings,
9a609bcf0809 Port main script to TypeScript and prepare for serving it.
Paul Fisher <paul@pfish.zone>
parents: 11
diff changeset
89 })
11
52ef21607b31 server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents: 8
diff changeset
90 return resp
52ef21607b31 server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents: 8
diff changeset
91
1
f66df122f18d get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff changeset
92 return app
4
e7c8dcc5fc15 Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
93
e7c8dcc5fc15 Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
94
e7c8dcc5fc15 Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
95 def main(argv):
e7c8dcc5fc15 Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
96 """Main function for a simple local demo."""
5
368f732f13d0 Actually make the server runnable.
Paul Fisher <paul@pfish.zone>
parents: 4
diff changeset
97 app = build_app(argv[0])
4
e7c8dcc5fc15 Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
98 app.run(host='0.0.0.0')
e7c8dcc5fc15 Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
99
e7c8dcc5fc15 Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
100
e7c8dcc5fc15 Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
101 if __name__ == '__main__':
e7c8dcc5fc15 Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents: 3
diff changeset
102 main(sys.argv[1:])