This commit is contained in:
gpkvt 2023-06-06 22:47:34 +02:00
parent 203777512a
commit 8b4ff7df58
1 changed files with 70 additions and 20 deletions

90
game.py
View File

@ -1,12 +1,16 @@
#! /bin/python3
from flask import Flask, render_template, request
"""
Simple A/B choice game
"""
from configparser import ConfigParser
from datetime import datetime
from email.message import EmailMessage
import smtplib
import random
import pytz
from flask import Flask, render_template, request
app = Flask(__name__,
static_url_path='',
@ -45,10 +49,14 @@ conf = {
'animations': config.get('main', 'animations')
}
with open("ab.txt", "r") as f:
with open("ab.txt", "r", encoding="utf-8") as f:
num_lines = sum(1 for _ in f)
def getEpoch():
def get_epoch():
"""
Get current time as epoch timestamp
"""
now = datetime.now(tz=tz)
epoch = now.timestamp()
epoch = int(epoch)
@ -56,24 +64,47 @@ def getEpoch():
return epoch
@app.errorhandler(404)
def page_not_found(e):
epoch = getEpoch()
def page_not_found():
"""
404 Error Page
"""
epoch = get_epoch()
return render_template('404.html', config=conf, i18n=i18n, epoch=epoch), 404
@app.errorhandler(500)
def internal_server_error(e):
epoch = getEpoch()
return render_template('500.html', config=conf, i18n=i18n, epoch=epoch), 500
def internal_server_error():
"""
500 Error Page
"""
epoch = get_epoch()
return render_template(
'500.html',
config=conf,
i18n=i18n,
epoch=epoch
), 500
if conf['mailform'] == "true":
@app.route("/form")
def mailform():
epoch = getEpoch()
return render_template('mailform.html', config=conf, i18n=i18n, epoch=epoch, num_lines=num_lines)
"""
Mail form
"""
epoch = get_epoch()
return render_template(
'mailform.html',
config=conf,
i18n=i18n,
epoch=epoch,
num_lines=num_lines
)
@app.route("/send", methods=['POST', 'GET'])
def sendmail():
epoch = getEpoch()
"""
Send form data via E-Mail
"""
epoch = get_epoch()
if request.method == 'POST':
mailcontent = request.form['questions']
@ -90,29 +121,48 @@ if conf['mailform'] == "true":
smtp_server.send_message(message)
smtp_server.quit()
return render_template('thanks.html', config=conf, i18n=i18n, epoch=epoch, questions=mailcontent)
return render_template(
'thanks.html',
config=conf,
i18n=i18n,
epoch=epoch,
questions=mailcontent
)
@app.route("/")
def hello():
"""
Default/Main page
"""
ablines = []
epoch = getEpoch()
epoch = get_epoch()
lines = getContent()
lines = get_content()
while len(lines) < 2:
print('Error reading content')
print(lines)
lines = getContent()
lines = get_content()
for line in lines:
ab = line.split(conf['separator_char'])
abq = line.split(conf['separator_char'])
ablines.append(
{'A': str(ab[0]), 'B': str(ab[1])}
{'A': str(abq[0]), 'B': str(abq[1])}
)
return render_template('index.html', content=ablines, config=conf, i18n=i18n, num_lines=num_lines, epoch=epoch)
return render_template(
'index.html',
content=ablines,
config=conf,
i18n=i18n,
num_lines=num_lines,
epoch=epoch
)
def getContent():
lines = [a.strip() for a in open("ab.txt", "r").readlines()]
def get_content():
"""
Read content from file
"""
lines = [a.strip() for a in open("ab.txt", "r", encoding="utf-8").readlines()]
result = random.sample(lines, 5)
return result