From 8238810a127669b0169b17c10a12e29b62bd9279 Mon Sep 17 00:00:00 2001 From: stupidcomputer Date: Sun, 12 Jan 2025 20:21:17 -0600 Subject: [PATCH] initial code --- .gitignore | 3 ++ app.py | 27 +++++++++++ appendbase.py | 28 ++++++++++++ shell.nix | 8 ++++ templates/base.html | 107 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 173 insertions(+) create mode 100644 .gitignore create mode 100644 app.py create mode 100644 appendbase.py create mode 100644 shell.nix create mode 100644 templates/base.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d9d47a5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +__pycache__/ +*.pyc +base.json diff --git a/app.py b/app.py new file mode 100644 index 0000000..f6a3e58 --- /dev/null +++ b/app.py @@ -0,0 +1,27 @@ +from flask import Flask, request, render_template +import json + +from appendbase import Appendbase + +submit_secret = "test" +app = Flask(__name__) +base = Appendbase.from_file("base.json") + +@app.route('//submit', methods=[ "POST"]) +def handle_new_data(secret): + if secret != submit_secret: + return '' + print(request.form) + base.append(request.form.to_dict()) + base.to_file("base.json") + + return "accepted" + +@app.route('//view') +def return_monitor_page(secret): + if secret != submit_secret: + return '' + + data_js = "let location_data = " + json.dumps(base.stuff) + ";" + + return render_template("base.html", data_js=data_js) diff --git a/appendbase.py b/appendbase.py new file mode 100644 index 0000000..166f8b4 --- /dev/null +++ b/appendbase.py @@ -0,0 +1,28 @@ +# an append-only database made with json +# is this google hyper-scaleable? no +# do i care? no + +import json +from typing import Any + +class Appendbase: + stuff: list[Any] + + def __init__(self, stuff: list[Any]): + self.stuff = stuff + + def append(self, thing: Any): + self.stuff.append(thing) + + @classmethod + def from_file(cls, filename: str): + try: + with open(filename, "r") as file: + stuff = json.loads(file.read()) + except FileNotFoundError: + return cls([]) + return cls(stuff) + + def to_file(self, filename: str): + with open(filename, "w+") as file: + file.write(json.dumps(self.stuff)) diff --git a/shell.nix b/shell.nix new file mode 100644 index 0000000..9e455d7 --- /dev/null +++ b/shell.nix @@ -0,0 +1,8 @@ +{ pkgs ? import {} }: + pkgs.mkShell { + nativeBuildInputs = [ + pkgs.python3 + pkgs.python311Packages.flask + ]; + } + diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..5bec8cd --- /dev/null +++ b/templates/base.html @@ -0,0 +1,107 @@ + + + + + + + + +
+
+
+

+ Latest point uploaded seconds ago at battery level . + This page was last updated 0 seconds ago. + Click here to update it. + (Or, update automatically.) +

+ How do uploads work? + + The phone uploads a point when both conditions are satisifed: + +
    +
  • The phone moves a meaningful distance, and
  • +
  • The phone is able to reach this server (i.e. has a working Internet connection)
  • +
+
+

+
+
+ + + +