2023-06-02 09:48:48 +02:00
|
|
|
#! /bin/python3
|
|
|
|
|
2023-06-06 22:47:34 +02:00
|
|
|
"""
|
|
|
|
Simple A/B choice game
|
|
|
|
"""
|
|
|
|
|
2023-06-02 10:24:06 +02:00
|
|
|
from configparser import ConfigParser
|
2023-06-02 12:31:55 +02:00
|
|
|
from datetime import datetime
|
2023-06-06 14:36:24 +02:00
|
|
|
from email.message import EmailMessage
|
|
|
|
import smtplib
|
2023-06-04 09:31:29 +02:00
|
|
|
import random
|
2023-06-02 12:31:55 +02:00
|
|
|
import pytz
|
2023-06-06 22:47:34 +02:00
|
|
|
from flask import Flask, render_template, request
|
2023-06-02 10:24:06 +02:00
|
|
|
|
2023-06-02 09:48:48 +02:00
|
|
|
app = Flask(__name__,
|
|
|
|
static_url_path='',
|
|
|
|
static_folder='static',
|
|
|
|
template_folder='templates')
|
|
|
|
|
2023-06-02 10:24:06 +02:00
|
|
|
config = ConfigParser()
|
|
|
|
config.read('config.ini')
|
2023-06-02 20:41:01 +02:00
|
|
|
tz = pytz.timezone(config.get('main', 'timezone'))
|
2023-06-02 12:31:55 +02:00
|
|
|
|
2023-06-06 10:42:00 +02:00
|
|
|
i18n = {
|
|
|
|
'lang': config.get('i18n', 'lang'),
|
|
|
|
'title': config.get('i18n', 'title'),
|
|
|
|
'more': config.get('i18n', 'more'),
|
|
|
|
'desc': config.get('i18n', 'desc'),
|
|
|
|
'questions_prefix': config.get('i18n', 'questions_prefix'),
|
|
|
|
'questions_suffix': config.get('i18n', 'questions_suffix'),
|
|
|
|
'separator': config.get('i18n', 'separator'),
|
|
|
|
'mailtext': config.get('i18n', 'mail_link'),
|
|
|
|
'helptext': config.get('i18n', 'help_link'),
|
2023-06-06 14:36:24 +02:00
|
|
|
'help': config.get('i18n', 'help'),
|
|
|
|
'submit': config.get('i18n', 'submit'),
|
|
|
|
'submit_questions': config.get('i18n', 'submit_questions'),
|
|
|
|
'submit_thanks_title': config.get('i18n', 'submit_thanks_title'),
|
|
|
|
'submit_thanks': config.get('i18n', 'submit_thanks')
|
2023-06-06 10:42:00 +02:00
|
|
|
}
|
|
|
|
conf = {
|
|
|
|
'separator_char': config.get('main', 'separator_char'),
|
2023-06-06 14:36:24 +02:00
|
|
|
'mailform': config.get('main', 'mailform').lower(),
|
|
|
|
'mailserver': config.get('main', 'mailserver'),
|
|
|
|
'mailto': config.get('main', 'mailto'),
|
|
|
|
'mailfrom': config.get('main', 'mailfrom'),
|
|
|
|
'mailsubject': config.get('main', 'mailsubject'),
|
2023-06-06 10:42:00 +02:00
|
|
|
'url': config.get('main', 'base_url'),
|
|
|
|
'theme': config.get('main', 'theme'),
|
|
|
|
'animations': config.get('main', 'animations')
|
|
|
|
}
|
|
|
|
|
2023-06-06 22:47:34 +02:00
|
|
|
with open("ab.txt", "r", encoding="utf-8") as f:
|
2023-06-06 14:36:24 +02:00
|
|
|
num_lines = sum(1 for _ in f)
|
|
|
|
|
2023-06-06 22:47:34 +02:00
|
|
|
def get_epoch():
|
|
|
|
"""
|
|
|
|
Get current time as epoch timestamp
|
|
|
|
"""
|
|
|
|
|
2023-06-06 10:42:00 +02:00
|
|
|
now = datetime.now(tz=tz)
|
|
|
|
epoch = now.timestamp()
|
|
|
|
epoch = int(epoch)
|
|
|
|
|
|
|
|
return epoch
|
|
|
|
|
2023-06-04 12:18:28 +02:00
|
|
|
@app.errorhandler(404)
|
2023-06-06 22:47:34 +02:00
|
|
|
def page_not_found():
|
|
|
|
"""
|
|
|
|
404 Error Page
|
|
|
|
"""
|
|
|
|
epoch = get_epoch()
|
2023-06-06 10:42:00 +02:00
|
|
|
return render_template('404.html', config=conf, i18n=i18n, epoch=epoch), 404
|
2023-06-04 12:18:28 +02:00
|
|
|
|
2023-06-04 14:35:24 +02:00
|
|
|
@app.errorhandler(500)
|
2023-06-06 22:47:34 +02:00
|
|
|
def internal_server_error():
|
|
|
|
"""
|
|
|
|
500 Error Page
|
|
|
|
"""
|
|
|
|
epoch = get_epoch()
|
|
|
|
return render_template(
|
|
|
|
'500.html',
|
|
|
|
config=conf,
|
|
|
|
i18n=i18n,
|
|
|
|
epoch=epoch
|
|
|
|
), 500
|
2023-06-04 14:35:24 +02:00
|
|
|
|
2023-06-06 14:36:24 +02:00
|
|
|
if conf['mailform'] == "true":
|
|
|
|
@app.route("/form")
|
|
|
|
def mailform():
|
2023-06-06 22:47:34 +02:00
|
|
|
"""
|
|
|
|
Mail form
|
|
|
|
"""
|
|
|
|
epoch = get_epoch()
|
|
|
|
return render_template(
|
|
|
|
'mailform.html',
|
|
|
|
config=conf,
|
|
|
|
i18n=i18n,
|
|
|
|
epoch=epoch,
|
|
|
|
num_lines=num_lines
|
|
|
|
)
|
2023-06-06 14:36:24 +02:00
|
|
|
|
|
|
|
@app.route("/send", methods=['POST', 'GET'])
|
|
|
|
def sendmail():
|
2023-06-06 22:47:34 +02:00
|
|
|
"""
|
|
|
|
Send form data via E-Mail
|
|
|
|
"""
|
|
|
|
epoch = get_epoch()
|
2023-06-06 14:36:24 +02:00
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
mailcontent = request.form['questions']
|
|
|
|
else:
|
|
|
|
mailcontent = request.args.get('questions')
|
|
|
|
|
|
|
|
message = EmailMessage()
|
|
|
|
message.set_content(mailcontent)
|
|
|
|
message['Subject'] = conf['mailsubject']
|
|
|
|
message['From'] = conf['mailfrom']
|
|
|
|
message['To'] = conf['mailto']
|
|
|
|
|
|
|
|
smtp_server = smtplib.SMTP(conf['mailserver'])
|
|
|
|
smtp_server.send_message(message)
|
|
|
|
smtp_server.quit()
|
|
|
|
|
2023-06-06 22:47:34 +02:00
|
|
|
return render_template(
|
|
|
|
'thanks.html',
|
|
|
|
config=conf,
|
|
|
|
i18n=i18n,
|
|
|
|
epoch=epoch,
|
|
|
|
questions=mailcontent
|
|
|
|
)
|
2023-06-06 14:36:24 +02:00
|
|
|
|
2023-06-02 09:48:48 +02:00
|
|
|
@app.route("/")
|
|
|
|
def hello():
|
2023-06-06 22:47:34 +02:00
|
|
|
"""
|
|
|
|
Default/Main page
|
|
|
|
"""
|
2023-06-02 10:11:06 +02:00
|
|
|
ablines = []
|
2023-06-06 22:47:34 +02:00
|
|
|
epoch = get_epoch()
|
2023-06-02 22:34:43 +02:00
|
|
|
|
2023-06-06 22:47:34 +02:00
|
|
|
lines = get_content()
|
2023-06-03 00:14:35 +02:00
|
|
|
while len(lines) < 2:
|
2023-06-06 00:33:14 +02:00
|
|
|
print('Error reading content')
|
2023-06-03 00:36:57 +02:00
|
|
|
print(lines)
|
2023-06-06 22:47:34 +02:00
|
|
|
lines = get_content()
|
2023-06-03 00:14:35 +02:00
|
|
|
|
2023-06-02 09:48:48 +02:00
|
|
|
for line in lines:
|
2023-06-06 22:47:34 +02:00
|
|
|
abq = line.split(conf['separator_char'])
|
2023-06-02 10:11:06 +02:00
|
|
|
ablines.append(
|
2023-06-06 22:47:34 +02:00
|
|
|
{'A': str(abq[0]), 'B': str(abq[1])}
|
2023-06-02 10:11:06 +02:00
|
|
|
)
|
2023-06-02 09:48:48 +02:00
|
|
|
|
2023-06-06 22:47:34 +02:00
|
|
|
return render_template(
|
|
|
|
'index.html',
|
|
|
|
content=ablines,
|
|
|
|
config=conf,
|
|
|
|
i18n=i18n,
|
|
|
|
num_lines=num_lines,
|
|
|
|
epoch=epoch
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_content():
|
|
|
|
"""
|
|
|
|
Read content from file
|
|
|
|
"""
|
|
|
|
lines = [a.strip() for a in open("ab.txt", "r", encoding="utf-8").readlines()]
|
2023-06-04 09:31:29 +02:00
|
|
|
result = random.sample(lines, 5)
|
2023-06-02 09:48:48 +02:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
2023-06-04 12:18:28 +02:00
|
|
|
app.register_error_handler(404, page_not_found)
|
2023-06-04 14:35:24 +02:00
|
|
|
app.register_error_handler(500, internal_server_error)
|
2023-06-04 12:18:28 +02:00
|
|
|
|
2023-06-02 09:48:48 +02:00
|
|
|
if __name__ == "__main__":
|
|
|
|
from waitress import serve
|
2023-06-03 14:16:15 +02:00
|
|
|
bind = config.get('main', 'bind')
|
|
|
|
port = config.get('main', 'port')
|
|
|
|
|
|
|
|
serve(app, host=bind, port=port, ident='a/b game')
|