commit 7565ead8c999731d40c70893d00a4bc1027f9742 Author: Justin C. Miller Date: Sat Jan 28 15:04:42 2023 -0800 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4d43738 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +__pycache__ +/venv diff --git a/poem.py b/poem.py new file mode 100644 index 0000000..f2d3370 --- /dev/null +++ b/poem.py @@ -0,0 +1,88 @@ +from flask import Flask +from pyhash import fnv1a_32 + +app = Flask(__name__) +hash = fnv1a_32() + +ROUND_TRIP = 29132 +KM_TO_MI = 0.6213712 +num_format = "{:,.0f}" + +def validate_key(key): + from os.path import exists + from flask import abort + + if not key: + return + + try: + int(key, base=16) + except ValueError: + abort(400) + + if not exists(f"poems/{key}.txt"): + abort(404) + +def response_plain(text): + from flask import make_response + resp = make_response(text) + resp.headers["Content-type"] = "text/plain" + return resp + +@app.route("/", methods=["GET"]) +@app.route("/", methods=["GET"]) +def poem_view(key=""): + from flask import render_template + validate_key(key) + + poem = "" + stats = {} + if key: + poem = open(f"poems/{key}.txt", 'r', encoding='utf-8').read() + lines = poem.splitlines()[1:] + words = sum([len(l.split()) for l in lines]) + breaks = len(lines) - 1 + posts = words + breaks + km = posts * ROUND_TRIP + mi = km * KM_TO_MI + + stats = dict(words=words, lines=breaks, + roundtrip=num_format.format(ROUND_TRIP), + km=num_format.format(km), + mi=num_format.format(mi), + ) + + return render_template("index.html", key=key, poem=poem, stats=stats) + +@app.route("/poem/", methods=["GET"]) +def poem_get(key): + validate_key(key) + + if not key: + return response_plain("") + return response_plain(open(filename, 'r', encoding='utf-8').read()) + +@app.route("/poem/", methods=["POST"]) +@app.route("/poem/", methods=["POST"]) +def poem_post(key=""): + from flask import request + + validate_key(key) + + poem = "" + if key: + poem = open(f"poems/{key}.txt", 'r', encoding='utf-8').read() + + addend = request.get_data(as_text=True) + if addend != "\n": + addend = addend.split()[0] + poem = f"{poem}{addend} " + else: + poem += "\n" + + newhash = "{0:08x}".format(hash(poem.encode('utf-8'))) + + with open(f"poems/{newhash}.txt", 'w', encoding='utf-8') as f: + f.write(poem) + + return response_plain(newhash) diff --git a/poems/.gitignore b/poems/.gitignore new file mode 100644 index 0000000..2211df6 --- /dev/null +++ b/poems/.gitignore @@ -0,0 +1 @@ +*.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..76b0d2c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +flask == 2.2 +pyhash == 0.9 diff --git a/static/main.js b/static/main.js new file mode 100644 index 0000000..ccf34c3 --- /dev/null +++ b/static/main.js @@ -0,0 +1,26 @@ +function update(key, word) { + let url = "/poem/" + key; + return fetch(url, { + "method": "POST", + "body": word, + }) + .then( resp => resp.text() ) + .then( text => { + console.log("new key", text); + return text; + }); +} + +export function update_all(starting_key, words) { + var promise = Promise.resolve(starting_key); + for (const word of words) { + if (word.length < 1) continue; + promise = promise.then( key => { + return update(key, word); + }); + } + + return promise.then( key => { + return update(key, "\n"); + }); +} diff --git a/static/route.png b/static/route.png new file mode 100755 index 0000000..4472c7e Binary files /dev/null and b/static/route.png differ diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..0f3c91b --- /dev/null +++ b/templates/index.html @@ -0,0 +1,111 @@ + + + + + POST mortem + + + +

POST mortem

+ + {% if poem %} +
{{ poem }}
+ {% else %} +
+ No poem has been started yet, add some words! +
+ {% endif %} + +
+
+ + +
+ +
+ + + + {% if key %} +
+ Mail this URL on to the next person! +
+ +
+ + +
+ POST distance + + + + + +
Number of words (excl. title){{ stats.words }}
Number of newlines (excl. title){{ stats.lines }}
Round-trip distance{{ stats.roundtrip }} km
Total distance{{ stats.km }} km ({{ stats.mi }} mi)
+ +
+ {% endif %} + +