abgame/game.py

139 lines
4.5 KiB
Python
Raw Normal View History

2023-06-02 09:48:48 +02:00
#! /bin/python3
from random import choice
2023-06-02 12:31:55 +02:00
from flask import Flask, render_template, request
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-04 09:31:29 +02:00
import random
2023-06-02 12:31:55 +02:00
import logging
import pytz
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 12:39:57 +02:00
logging.basicConfig(encoding='utf-8', level=logging.INFO, format='%(message)s')
2023-06-02 12:31:55 +02:00
logger = logging.getLogger('waitress')
2023-06-02 20:41:01 +02:00
tz = pytz.timezone(config.get('main', 'timezone'))
2023-06-02 12:31:55 +02:00
@app.after_request
def log_the_request(response):
now = datetime.now(tz=tz)
2023-06-02 15:55:18 +02:00
if 'X-Forwarded-For' in request.headers:
remote_addr = request.headers['X-Forwarded-For']
else:
remote_addr = request.remote_addr
2023-06-02 12:31:55 +02:00
if not request.remote_user:
remote_user = "-"
else:
remote_user = request.remote_user
2023-06-02 15:25:02 +02:00
2023-06-02 12:31:55 +02:00
if not request.referrer:
referrer = "-"
else:
referrer = request.referrer
if request.full_path[-1] == '?':
full_path = request.full_path[:-1]
else:
full_path = request.full_path
2023-06-02 12:31:55 +02:00
log = {
2023-06-02 15:55:18 +02:00
'remote_addr': remote_addr,
2023-06-02 12:31:55 +02:00
'remote_user': remote_user,
'url': full_path,
2023-06-02 12:31:55 +02:00
'date': now.strftime("%d/%b/%Y:%H:%M:%S %z"),
'referrer': referrer,
'user_agent': request.user_agent.string,
'method': request.method,
'content_length': response.content_length,
'status_code': response.status_code
}
logfile = "{} - {} [{}] \"{} {}\" {} {} \"{}\" \"{}\"".format(log['remote_addr'], log['remote_user'], log['date'], log['method'], log['url'], log['status_code'], log['content_length'], log['referrer'], log['user_agent'])
logger.info(logfile)
return response
2023-06-02 10:24:06 +02:00
2023-06-04 12:18:28 +02:00
@app.errorhandler(404)
def page_not_found(e):
lang = config.get('i18n', 'lang')
title = config.get('i18n', 'title')
2023-06-04 12:32:46 +02:00
url = config.get('main', 'base_url')
theme = config.get('main', 'theme')
desc = config.get('i18n', 'desc')
return render_template('404.html', title=title, lang=lang, desc=desc, theme=theme, url=url), 404
2023-06-04 12:18:28 +02:00
2023-06-04 14:35:24 +02:00
@app.errorhandler(500)
def internal_server_error(e):
lang = config.get('i18n', 'lang')
title = config.get('i18n', 'title')
url = config.get('main', 'base_url')
theme = config.get('main', 'theme')
desc = config.get('i18n', 'desc')
return render_template('500.html', title=title, lang=lang, desc=desc, theme=theme, url=url), 500
2023-06-02 09:48:48 +02:00
@app.route("/")
def hello():
lang = config.get('i18n', 'lang')
title = config.get('i18n', 'title')
more = config.get('i18n', 'more')
2023-06-03 12:42:41 +02:00
desc = config.get('i18n', 'desc')
questions_prefix = config.get('i18n', 'questions_prefix')
questions_suffix = config.get('i18n', 'questions_suffix')
send_questions = config.get('i18n', 'send_questions')
separator = config.get('i18n', 'separator')
2023-06-02 10:24:06 +02:00
separator_char = config.get('main', 'separator_char')
2023-06-03 15:30:12 +02:00
mail = config.get('main', 'mail')
2023-06-05 10:32:01 +02:00
mailtext = config.get('i18n', 'mailtext')
help = config.get('i18n', 'help_title')
help1 = config.get('i18n', 'help_1')
help2 = config.get('i18n', 'help_2')
help3 = config.get('i18n', 'help_3')
2023-06-05 10:52:12 +02:00
help4 = config.get('i18n', 'help_4')
2023-06-03 12:42:41 +02:00
url = config.get('main', 'base_url')
2023-06-03 15:30:12 +02:00
theme = config.get('main', 'theme')
2023-06-02 10:11:06 +02:00
ablines = []
2023-06-02 09:48:48 +02:00
now = datetime.now(tz=tz)
epoch = now.timestamp()
2023-06-03 12:04:59 +02:00
epoch = int(epoch)
2023-06-02 09:48:48 +02:00
lines = getContent()
2023-06-03 00:14:35 +02:00
while len(lines) < 2:
2023-06-03 00:36:57 +02:00
logger.error('Error reading content')
print(lines)
2023-06-03 00:14:35 +02:00
lines = getContent()
2023-06-02 09:48:48 +02:00
for line in lines:
2023-06-02 10:24:06 +02:00
ab = line.split(separator_char)
2023-06-02 10:11:06 +02:00
ablines.append(
{'A': str(ab[0]), 'B': str(ab[1])}
)
2023-06-02 09:48:48 +02:00
2023-06-02 15:55:18 +02:00
with open("ab.txt", "r") as f:
2023-06-02 15:25:02 +02:00
num_lines = sum(1 for _ in f)
2023-06-05 10:52:12 +02:00
return render_template('index.html', title=title, separator=separator, content=ablines, num_lines=num_lines, epoch=epoch, mailto=mail, more=more, questions_prefix=questions_prefix, questions_suffix=questions_suffix, send_questions=send_questions, lang=lang, url=url, desc=desc, theme=theme, mailtext=mailtext, help=help, help_1=help1, help_2=help2, help_3=help3, help_4=help4)
2023-06-02 09:48:48 +02:00
def getContent():
2023-06-02 15:55:18 +02:00
lines = [a.strip() for a in open("ab.txt", "r").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')