This repository has been archived on 2019-12-12. You can view files and clone it, but cannot push or open issues/pull-requests.
tildetown-scripts/tildetown-py/cgi.py

91 lines
2.2 KiB
Python
Raw Normal View History

2015-08-01 23:27:08 +00:00
from functools import lru_cache
2015-07-28 18:04:11 +00:00
import json
import os
2015-08-01 23:16:09 +00:00
import random
2015-07-28 18:04:11 +00:00
import time
from sys import argv
from tempfile import mkdtemp, mkstemp
from flask import Flask, render_template, request, redirect
2015-08-01 23:16:09 +00:00
from stats import get_data
2015-07-28 18:04:11 +00:00
2015-08-01 23:23:54 +00:00
## disgusting hack for python 3.4
import pkgutil
orig_get_loader = pkgutil.get_loader
def get_loader(name):
try:
return orig_get_loader(name)
except AttributeError:
pass
pkgutil.get_loader = get_loader
###########
2015-07-28 18:04:11 +00:00
SITE_NAME = 'tilde.town'
app = Flask('~cgi')
2015-08-01 23:27:08 +00:00
@lru_cache(maxsize=32)
2015-08-01 23:16:09 +00:00
def site_context():
2015-08-01 23:27:08 +00:00
return get_data()
2015-08-01 23:16:09 +00:00
2015-07-28 18:04:11 +00:00
def slurp(file_path):
contents = None
with open(file_path, 'r') as f:
contents = f.read()
return contents
def save_post(name, message):
timestamp = time.time()
data = {
'name': name,
'message': message,
'timestamp': timestamp,
}
_, file_path = mkstemp(dir=app.config['DATA_DIR'])
with open(file_path, 'w') as f:
f.write(json.dumps(data))
@app.route('/random', methods=['GET'])
def get_random():
2015-08-01 23:16:09 +00:00
user = random.choice(site_context()['live_users'])
return redirect('http://tilde.town/~{}'.format(user['username']))
2015-07-28 18:04:11 +00:00
@app.route('/guestbook', methods=['GET'])
def get_guestbook():
data_dir = app.config['DATA_DIR']
# TODO sort by timestamp
2015-08-02 00:16:36 +00:00
filename_to_json = lambda p: json.loads(slurp(os.path.join(data_dir, p)))
posts = map(filename_to_json, os.listdir(data_dir))
2015-08-02 00:19:56 +00:00
sorted_posts = sorted(posts, key=lambda p: p['timestamp'], reverse=True)
2015-07-28 18:04:11 +00:00
2015-08-01 23:16:09 +00:00
context = {
2015-08-02 00:16:36 +00:00
"posts": sorted_posts,
2015-07-28 18:04:11 +00:00
}
2015-08-01 23:16:09 +00:00
context.update(site_context())
2015-07-28 18:04:11 +00:00
return render_template('guestbook.html', **context)
@app.route('/guestbook', methods=['POST'])
def post_guestbook():
2015-08-01 23:36:22 +00:00
message = request.form['message'][0:400]
name = request.form['name'][0:140]
captcha = request.form['hmm']
2015-08-01 23:59:46 +00:00
if captcha == "scrop":
2015-08-01 23:58:45 +00:00
save_post(name, message)
2015-07-28 18:04:11 +00:00
return redirect("/guestbook")
@app.route('/helpdesk', methods=['GET'])
def helpdesk():
2015-08-01 23:16:09 +00:00
return "HELPDESK UNDER CONSTRUCTION"
2015-07-28 18:04:11 +00:00
if __name__ == '__main__':
if len(argv) == 2:
data_dir = argv[1]
else:
data_dir = mkdtemp()
app.config['DEBUG'] = True
app.config['DATA_DIR'] = data_dir
2015-08-02 00:01:29 +00:00
print("Running with data_dir=", app.config['DATA_DIR'])
2015-07-28 18:04:11 +00:00
app.run()