diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..22930f4 --- /dev/null +++ b/.envrc @@ -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 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..033df5f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.venv +__pycache__ diff --git a/README.md b/README.md index e69de29..c915229 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,6 @@ +```shell +export DB_NAME=test +export DB_USER=test +export DB_PASSWORD=test +FLASK_APP=steam_chat_viewer flask run +``` diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..46bc436 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +flask +pg8000 diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..b3c6e2f --- /dev/null +++ b/setup.py @@ -0,0 +1,6 @@ +from setuptools import find_packages, setup + +setup( + name="steam_chat_viewer", + packages=find_packages(), +) diff --git a/steam_chat_viewer/__init__.py b/steam_chat_viewer/__init__.py new file mode 100644 index 0000000..1f78b6d --- /dev/null +++ b/steam_chat_viewer/__init__.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 + +from os import environ +from flask import Flask, request +from subprocess import check_output +import json + +from .pg import query + +app = Flask(__name__) + +@app.route('/') +def home(): + return str(query( + ''' + SELECT FROM steam_chat_logger + ORDER BY date + LIMIT 10 + ''' + ).rows) diff --git a/steam_chat_viewer/pg.py b/steam_chat_viewer/pg.py new file mode 100644 index 0000000..3db22fd --- /dev/null +++ b/steam_chat_viewer/pg.py @@ -0,0 +1,40 @@ +from os import environ +from collections import namedtuple +import pg8000 + +from flask import g + +DBResult = namedtuple('Result', 'rows rowcount') + +def db(): + if not hasattr(g, 'db_connection'): + g.db_connection = pg8000.connect( + database=environ['DB_NAME'], + user=environ['DB_USER'], + password=environ['DB_PASSWORD'], + ) + + return g.db_connection + +def query(query, **params): + try: + cursor = db().cursor() + cursor.paramstyle = "named" + cursor.execute(query, params) + db().commit() + + if cursor._cached_rows: + columns = [x[0] for x in cursor.description] + rows = [dict(zip(columns, row)) for row in cursor.fetchall()] + # rdef = namedtuple('row', ' '.join([x[0] for x in cursor.description])) + # rows = list(map(rdef._make, cursor.fetchall())) + else: + rows = [] + + return DBResult( + rows=rows, + rowcount=cursor.rowcount, + ) + except: + db().rollback() + raise diff --git a/zprofile b/zprofile new file mode 100644 index 0000000..358ac29 --- /dev/null +++ b/zprofile @@ -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