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