From ed1cc7a1f866f5cc8bb039e93747118c7fcb9fce Mon Sep 17 00:00:00 2001 From: Christian Heller <c.heller@plomlompom.de> Date: Tue, 25 Jul 2023 01:57:07 +0200 Subject: [PATCH] Add energy tracker script. --- energy_tracker.py | 110 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 energy_tracker.py diff --git a/energy_tracker.py b/energy_tracker.py new file mode 100644 index 0000000..204b582 --- /dev/null +++ b/energy_tracker.py @@ -0,0 +1,110 @@ +from http.server import BaseHTTPRequestHandler, HTTPServer +import os +import json + + + +def build_page(entries): + return """<html> +<meta charset="UTF-8"> +<style> +.energy_cell { + text-align: center; +} +</style> +<body> +<form action="/" method="POST"> +energy level: +<input name="energy" type="number" step="1" min="-2" max="2" value="0" /> +comment: +<input name="comment" type="text" /> +<input type="submit" value="submit!" /> +</form>""" + f""" +<details> +<summary> +[tracking] +</summary> +<table> +<tr><th>datetime</th><th>energy level</th><th>comment</th></tr> +{entries} +</table>""" + """ +</details> +</body> +</html>""" + + + +class Database: + + def __init__(self): + db_name = "energy_tracker" + self.db_file = db_name + ".json" + self.lock_file = db_name+ ".lock" + self.entries = {} + if os.path.exists(self.db_file): + with open(self.db_file, "r") as f: + self.from_dict(json.load(f)) + + def from_dict(self, d): + self.entries = d + + def to_dict(self): + return self.entries + + def write(self): + import shutil + if os.path.exists(self.lock_file): + raise LockFileDetected + if os.path.exists(self.db_file): + shutil.copy(self.db_file, self.db_file + ".bak") + f = open(self.lock_file, "w+") + f.close() + with open(self.db_file, "w") as f: + json.dump(self.to_dict(), f) + os.remove(self.lock_file) + + + +class MyServer(BaseHTTPRequestHandler): + + def do_POST(self): + import datetime + from urllib.parse import parse_qs + length = int(self.headers['content-length']) + postvars = parse_qs(self.rfile.read(length), keep_blank_values=1) + db = Database() + db.entries[str(datetime.datetime.now())[:19]] = [int(postvars[b'energy'][0].decode()), postvars[b'comment'][0].decode()] + try: + db.write() + self.send_response(302) + self.send_header('Location', '/') + self.end_headers() + except LockFileDetected: + self.send_response(400) + self.end_headers() + self.wfile.write(bytes("Sorry, lock file!", "utf-8")) + + def do_GET(self): + self.send_response(200) + self.send_header("Content-type", "text/html") + self.end_headers() + db = Database() + entries = "" + for datetime, data in db.entries.items(): + entries = "<tr><td>%s</td><td class=\"energy_cell\">%s</td><td>%s</td></tr>\n" % (datetime, data[0], data[1]) + entries + page = build_page(entries) + self.wfile.write(bytes(page, "utf-8")) + + + +hostName = "localhost" +serverPort = 8080 +if __name__ == "__main__": + webServer = HTTPServer((hostName, serverPort), MyServer) + print("Server started http://%s:%s" % (hostName, serverPort)) + try: + webServer.serve_forever() + except KeyboardInterrupt: + pass + webServer.server_close() + print("Server stopped.") -- 2.30.2