basic fnctionality

This commit is contained in:
mwiegand 2022-06-01 19:24:36 +02:00
parent f86dec5676
commit bbffe78912
12 changed files with 165 additions and 0 deletions

6
.envrc Normal file
View file

@ -0,0 +1,6 @@
#!/usr/bin/env bash
python3 -m venv .venv
source ./.venv/bin/activate
PATH_add .venv/bin
python3 -m pip install --upgrade pip

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
.venv
__pycache__

View file

@ -0,0 +1,6 @@
```shell
export DB_NAME=test
export DB_USER=test
export DB_PASSWORD=test
FLASK_APP=steam_chat_viewer flask run
```

2
requirements.txt Normal file
View file

@ -0,0 +1,2 @@
flask
pg8000

6
setup.py Normal file
View file

@ -0,0 +1,6 @@
from setuptools import find_packages, setup
setup(
name="steam_chat_viewer",
packages=find_packages(),
)

View file

@ -0,0 +1,52 @@
#!/usr/bin/env python3
from os import environ
from flask import Flask, request, render_template
from .pg import query, select
app = Flask(__name__)
@app.route('/')
def home():
if 'filter' in request.args:
messages = select(
'''
SELECT * FROM messages
WHERE from_url = :filter
OR to_url = :filter
ORDER BY date DESC
''',
filter=request.args['filter']
)
else:
messages = select(
'''
SELECT * FROM messages
ORDER BY date DESC
LIMIT 1000
'''
)
return render_template(
"home.html",
contacts=select(
'''
SELECT DISTINCT ON (url) url, name
FROM (
(
SELECT DISTINCT ON (from_url) from_url as url, from_name as name
FROM messages
ORDER BY url, date DESC
)
UNION
(
SELECT DISTINCT ON (to_url) to_url as url, to_name as name
FROM messages
ORDER BY url, date DESC
)
) as nested;
'''
),
messages=messages,
)

32
steam_chat_viewer/pg.py Normal file
View file

@ -0,0 +1,32 @@
from os import environ
from collections import namedtuple
import pg8000
from flask import g
def db():
if not hasattr(g, 'db_connection'):
g.db_connection = pg8000.connect(
host=environ['DB_HOST'],
database=environ['DB_NAME'],
user=environ['DB_USER'],
password=environ['DB_PASSWORD'],
)
return g.db_connection
def query(querystring, **params):
try:
cursor = db().cursor()
cursor.paramstyle = "named"
cursor.execute(querystring, params)
db().commit()
return cursor
except:
db().rollback()
raise
def select(querystring, **params):
cursor = query(querystring, **params)
columns = [x[0] for x in cursor.description]
return [dict(zip(columns, row)) for row in cursor.fetchall()]

View file

View file

View file

View file

@ -0,0 +1,35 @@
<!DOCTYPE html>
<html>
<head>
<title>STEAM Chat Logs</title>
<link rel="icon" type="image/svg+xml" href="{{ url_for('static', filename='icon.svg') }}">
<link rel=stylesheet href="{{ url_for('static', filename='style.css') }}">
<script src="{{ url_for('static', filename='script.js') }}"></script>
</head>
<body>
<header>
<h1 class=logo><a href="{{ url_for('home') }}">STEAM Chat Logs <img src="{{ url_for('static', filename='icon.svg') }}"></a></h1>
</header>
<main>
<ul class=contacts>
{% for contact in contacts: -%}
<li><a href="{{ url_for('home', filter=contact['url']) }}">{{ contact['name'] }}</a></li>
{% endfor -%}
</ul>
<ul class=messages>
{% for message in messages: -%}
<li class=message>
<div class=metadata>
<div class=from><a href="{{ url_for('home', filter=message['from_url']) }}">{{ message['from_name'] }}</a></div>
to
<div class=to><a href="{{ url_for('home', filter=message['to_url']) }}">{{ message['to_name'] }}</a></div>
at
<div class=date>{{ message['date'] }}</div>
</div>
<div class=body>{{ message['message'] }}</div>
</li>
{% endfor -%}
</ul>
</main>
</body>
</html>

24
zprofile Normal file
View file

@ -0,0 +1,24 @@
# /etc/zsh/zprofile: system-wide .zprofile file for zsh(1).
#
# This file is sourced only for login shells (i.e. shells
# invoked with "-" as the first character of argv[0], and
# shells invoked with the -l flag.)
#
# Global Order: zshenv, zprofile, zshrc, zlogin
alias s='sudo su - root -s /usr/bin/zsh'
function hhtop {
# mkdir -p ~/.config/htop
# cp /etc/htoprc.global ~/.config/htop/htoprc
# cp /etc/htoprc.global ~/.htoprc
rm -rf ~/.config/htop ~/.htoprc
htop
}
ZSH_THEME=bw
DISABLE_AUTO_UPDATE=true
plugins=(
zsh-autosuggestions
)
source /etc/zsh/oh-my-zsh/oh-my-zsh.sh