Mercurial > personal > weather-server
annotate weather_server/server.py @ 17:fdb874e0b270
graph: Update style and do HiDPI properly.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sat, 12 Oct 2019 20:40:11 -0400 |
parents | 9a609bcf0809 |
children | beb42c835c52 |
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 |
5
368f732f13d0
Actually make the server runnable.
Paul Fisher <paul@pfish.zone>
parents:
4
diff
changeset
|
3 import sys |
4
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
4 |
1
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
5 import bson |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
6 import flask |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
7 |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
8 from . import common |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
9 from . import locations |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
10 from . import types |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
11 |
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 def build_app(root_directory: str) -> flask.Flask: |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
14 locs = locations.Locations(root_directory) |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
15 app = flask.Flask(__name__) |
8
d54155a199d8
Improve template; add stylesheet.
Paul Fisher <paul@pfish.zone>
parents:
5
diff
changeset
|
16 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
|
17 |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
18 @app.route('/favicon.ico') |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
19 def favicon(): |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
20 return flask.send_file('static/favicon.ico') |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
21 |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
22 @app.route('/') |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
23 def home(): |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
24 return 'Weather server' |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
25 |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
26 @app.route('/_submit', methods=['POST']) |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
27 def submit(): |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
28 req = flask.request |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
29 reader = bson.decode_file_iter( |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
30 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
|
31 try: |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
32 preamble = next(reader) |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
33 loc_name = preamble['location'] |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
34 password = str(preamble['password']) |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
35 loc, logger = locs.get(loc_name) |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
36 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
|
37 flask.abort(400) |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
38 entries = [ |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
39 types.Reading.from_now( |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
40 sample_time=item['sample_time'], |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
41 temp_c=item['temp_c'], |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
42 rh_pct=item['rh_pct'], |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
43 ) |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
44 for item in reader |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
45 ] |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
46 except (KeyError, bson.InvalidBSON): |
b42c4bfe57c7
server: Use a "preamble" object in the POST to auth.
Paul Fisher <paul@pfish.zone>
parents:
2
diff
changeset
|
47 flask.abort(400) |
2
cda47993a193
server: fix bugs and improve template.
Paul Fisher <paul@pfish.zone>
parents:
1
diff
changeset
|
48 logger.write_rows(entries) |
cda47993a193
server: fix bugs and improve template.
Paul Fisher <paul@pfish.zone>
parents:
1
diff
changeset
|
49 return flask.jsonify({'status': 'OK'}) |
1
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
50 |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
51 @app.route('/<location>') |
11
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
52 def show(location: str): |
1
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
53 try: |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
54 loc, logger = locs.get(location) |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
55 except KeyError: |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
56 flask.abort(404) |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
57 data = logger.data |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
58 if data: |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
59 last_reading = data[-1] |
2
cda47993a193
server: fix bugs and improve template.
Paul Fisher <paul@pfish.zone>
parents:
1
diff
changeset
|
60 tz = loc.timezone() |
1
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
61 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
|
62 else: |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
63 last_reading = None |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
64 date = None |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
65 return flask.render_template( |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
66 'location.html', |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
67 location=loc, |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
68 last_reading=last_reading, |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
69 date=date) |
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
70 |
11
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
71 @app.route('/<location>/recent') |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
72 def recent(location: str): |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
73 try: |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
74 loc, logger = locs.get(location) |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
75 except KeyError: |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
76 flask.abort(404) |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
77 req = flask.request |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
78 |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
79 try: |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
80 seconds = int(req.args['seconds']) |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
81 except (KeyError, ValueError): |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
82 flask.abort(400) |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
83 |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
84 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
|
85 |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
86 readings = [ |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
87 r.as_dict() for r in logger.data |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
88 if start < r.sample_time |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
89 ] |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
90 resp = flask.Response() |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
91 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
|
92 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
|
93 'timezone': loc.tz_name, |
9a609bcf0809
Port main script to TypeScript and prepare for serving it.
Paul Fisher <paul@pfish.zone>
parents:
11
diff
changeset
|
94 'readings': readings, |
9a609bcf0809
Port main script to TypeScript and prepare for serving it.
Paul Fisher <paul@pfish.zone>
parents:
11
diff
changeset
|
95 }) |
11
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
96 return resp |
52ef21607b31
server: Create endpoint to get some recent readings.
Paul Fisher <paul@pfish.zone>
parents:
8
diff
changeset
|
97 |
1
f66df122f18d
get the skeleton of a webserver up?
Paul Fisher <paul@pfish.zone>
parents:
diff
changeset
|
98 return app |
4
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 def main(argv): |
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
102 """Main function for a simple local demo.""" |
5
368f732f13d0
Actually make the server runnable.
Paul Fisher <paul@pfish.zone>
parents:
4
diff
changeset
|
103 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
|
104 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
|
105 |
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
106 |
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
107 if __name__ == '__main__': |
e7c8dcc5fc15
Make the weather server pip-installable and locally runnable.
Paul Fisher <paul@pfish.zone>
parents:
3
diff
changeset
|
108 main(sys.argv[1:]) |