more style fixes everywhere else

This commit is contained in:
Lili (Tlapka) 2022-10-17 18:47:30 +02:00
parent 06114022da
commit d8230da013
5 changed files with 34 additions and 15 deletions

View File

@ -4,6 +4,7 @@ from apscheduler.schedulers.background import BackgroundScheduler
from tlapbot.db import get_db from tlapbot.db import get_db
from tlapbot.owncast_helpers import is_stream_live, give_points_to_chat from tlapbot.owncast_helpers import is_stream_live, give_points_to_chat
def create_app(test_config=None): def create_app(test_config=None):
app = Flask(__name__, instance_relative_config=True) app = Flask(__name__, instance_relative_config=True)
@ -44,12 +45,11 @@ def create_app(test_config=None):
# start scheduler that will give points to users # start scheduler that will give points to users
points_giver = BackgroundScheduler() points_giver = BackgroundScheduler()
points_giver.add_job(proxy_job, 'interval', seconds=app.config['POINTS_CYCLE_TIME']) # change to 10 minutes out of testing points_giver.add_job(proxy_job, 'interval', seconds=app.config['POINTS_CYCLE_TIME'])
points_giver.start() points_giver.start()
return app return app
if __name__ == '__main__': if __name__ == '__main__':
create_app() create_app()

View File

@ -22,6 +22,7 @@ def close_db(e=None):
if db is not None: if db is not None:
db.close() db.close()
def insert_counters(db): def insert_counters(db):
for redeem, redeem_info in current_app.config['REDEEMS'].items(): for redeem, redeem_info in current_app.config['REDEEMS'].items():
if redeem_info["type"] == "counter": if redeem_info["type"] == "counter":
@ -34,6 +35,7 @@ def insert_counters(db):
except Error as e: except Error as e:
print("Failed inserting counters to db:", e.args[0]) print("Failed inserting counters to db:", e.args[0])
def init_db(): def init_db():
db = get_db() db = get_db()
@ -50,6 +52,7 @@ def init_db_command():
init_db() init_db()
click.echo('Initialized the database.') click.echo('Initialized the database.')
def init_app(app): def init_app(app):
app.teardown_appcontext(close_db) app.teardown_appcontext(close_db)
app.cli.add_command(init_db_command) app.cli.add_command(init_db_command)

View File

@ -5,6 +5,7 @@ import click
from flask.cli import with_appcontext from flask.cli import with_appcontext
from tlapbot.db import get_db from tlapbot.db import get_db
# # # requests stuff # # # # # # requests stuff # # #
def is_stream_live(): def is_stream_live():
url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/status' url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/status'
@ -12,6 +13,7 @@ def is_stream_live():
print(r.json()["online"]) print(r.json()["online"])
return r.json()["online"] return r.json()["online"]
def give_points_to_chat(db): def give_points_to_chat(db):
url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/integrations/clients' url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/integrations/clients'
headers = {"Authorization": "Bearer " + current_app.config['OWNCAST_ACCESS_TOKEN']} headers = {"Authorization": "Bearer " + current_app.config['OWNCAST_ACCESS_TOKEN']}
@ -22,6 +24,7 @@ def give_points_to_chat(db):
user_id, user_id,
current_app.config['POINTS_AMOUNT_GIVEN']) current_app.config['POINTS_AMOUNT_GIVEN'])
def send_chat(message): def send_chat(message):
url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/integrations/chat/send' url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/integrations/chat/send'
headers = {"Authorization": "Bearer " + current_app.config['OWNCAST_ACCESS_TOKEN']} headers = {"Authorization": "Bearer " + current_app.config['OWNCAST_ACCESS_TOKEN']}
@ -41,6 +44,7 @@ def read_users_points(db, user_id):
print("Error occured reading points:", e.args[0]) print("Error occured reading points:", e.args[0])
print("To user:", user_id) print("To user:", user_id)
def give_points_to_user(db, user_id, points): def give_points_to_user(db, user_id, points):
try: try:
db.execute( db.execute(
@ -52,6 +56,7 @@ def give_points_to_user(db, user_id, points):
print("Error occured giving points:", e.args[0]) print("Error occured giving points:", e.args[0])
print("To user:", user_id, " amount of points:", points) print("To user:", user_id, " amount of points:", points)
def use_points(db, user_id, points): def use_points(db, user_id, points):
try: try:
db.execute( db.execute(
@ -65,27 +70,29 @@ def use_points(db, user_id, points):
print("From user:", user_id, " amount of points:", points) print("From user:", user_id, " amount of points:", points)
return False return False
def user_exists(db, user_id): def user_exists(db, user_id):
try: try:
cursor = db.execute( cursor = db.execute(
"SELECT points FROM points WHERE id = ?", "SELECT points FROM points WHERE id = ?",
(user_id,) (user_id,)
) )
if cursor.fetchone() == None: if cursor.fetchone() is None:
return False return False
return True return True
except Error as e: except Error as e:
print("Error occured checking if user exists:", e.args[0]) print("Error occured checking if user exists:", e.args[0])
print("To user:", user_id) print("To user:", user_id)
""" Adds a new user to the database. Does nothing if user is already in."""
def add_user_to_database(db, user_id, display_name): def add_user_to_database(db, user_id, display_name):
""" Adds a new user to the database. Does nothing if user is already in."""
try: try:
cursor = db.execute( cursor = db.execute(
"SELECT points FROM points WHERE id = ?", "SELECT points FROM points WHERE id = ?",
(user_id,) (user_id,)
) )
if cursor.fetchone() == None: if cursor.fetchone() is None:
cursor.execute( cursor.execute(
"INSERT INTO points(id, name, points) VALUES(?, ?, 10)", "INSERT INTO points(id, name, points) VALUES(?, ?, 10)",
(user_id, display_name) (user_id, display_name)
@ -95,6 +102,7 @@ def add_user_to_database(db, user_id, display_name):
print("Error occured adding user to db:", e.args[0]) print("Error occured adding user to db:", e.args[0])
print("To user:", user_id, display_name) print("To user:", user_id, display_name)
def change_display_name(db, user_id, new_name): def change_display_name(db, user_id, new_name):
try: try:
cursor = db.execute( cursor = db.execute(
@ -118,6 +126,7 @@ def add_to_counter(db, counter_name):
print("Error occured adding to counter:", e.args[0]) print("Error occured adding to counter:", e.args[0])
print("To counter:", counter_name) print("To counter:", counter_name)
def add_to_redeem_queue(db, user_id, redeem_name, note=None): def add_to_redeem_queue(db, user_id, redeem_name, note=None):
try: try:
cursor = db.execute( cursor = db.execute(
@ -129,6 +138,7 @@ def add_to_redeem_queue(db, user_id, redeem_name, note=None):
print("Error occured adding to redeem queue:", e.args[0]) print("Error occured adding to redeem queue:", e.args[0])
print("To user:", user_id, " with redeem:", redeem_name, "with note:", note) print("To user:", user_id, " with redeem:", redeem_name, "with note:", note)
def clear_redeem_queue(db): def clear_redeem_queue(db):
try: try:
cursor = db.execute( cursor = db.execute(
@ -141,6 +151,7 @@ def clear_redeem_queue(db):
except Error as e: except Error as e:
print("Error occured deleting redeem queue:", e.args[0]) print("Error occured deleting redeem queue:", e.args[0])
def all_counters(db): def all_counters(db):
try: try:
cursor = db.execute( cursor = db.execute(
@ -150,6 +161,7 @@ def all_counters(db):
except Error as e: except Error as e:
print("Error occured selecting all counters:", e.args[0]) print("Error occured selecting all counters:", e.args[0])
def pretty_redeem_queue(db): def pretty_redeem_queue(db):
try: try:
cursor = db.execute( cursor = db.execute(
@ -162,6 +174,7 @@ def pretty_redeem_queue(db):
except Error as e: except Error as e:
print("Error occured selecting pretty redeem queue:", e.args[0]) print("Error occured selecting pretty redeem queue:", e.args[0])
def whole_redeem_queue(db): def whole_redeem_queue(db):
try: try:
cursor = db.execute( cursor = db.execute(
@ -171,6 +184,7 @@ def whole_redeem_queue(db):
except Error as e: except Error as e:
print("Error occured selecting redeem queue:", e.args[0]) print("Error occured selecting redeem queue:", e.args[0])
@click.command('clear-queue') @click.command('clear-queue')
@with_appcontext @with_appcontext
def clear_queue_command(): def clear_queue_command():

View File

@ -5,7 +5,8 @@ from datetime import datetime, timezone
bp = Blueprint('redeem_dashboard', __name__) bp = Blueprint('redeem_dashboard', __name__)
@bp.route('/dashboard',methods=['GET'])
@bp.route('/dashboard', methods=['GET'])
def dashboard(): def dashboard():
db = get_db() db = get_db()
queue = pretty_redeem_queue(db) queue = pretty_redeem_queue(db)

View File

@ -2,12 +2,13 @@ from flask import Flask, request, json, Blueprint
from sqlite3 import Error from sqlite3 import Error
from tlapbot.db import get_db from tlapbot.db import get_db
from tlapbot.owncast_helpers import (add_user_to_database, change_display_name, from tlapbot.owncast_helpers import (add_user_to_database, change_display_name,
user_exists, send_chat, read_users_points) user_exists, send_chat, read_users_points)
from tlapbot.redeems_handler import handle_redeem from tlapbot.redeems_handler import handle_redeem
bp = Blueprint('owncast_webhooks', __name__) bp = Blueprint('owncast_webhooks', __name__)
@bp.route('/owncastWebhook',methods=['POST'])
@bp.route('/owncastWebhook', methods=['POST'])
def owncast_webhook(): def owncast_webhook():
data = request.json data = request.json
db = get_db() db = get_db()
@ -42,6 +43,6 @@ def owncast_webhook():
# Forces name update in case bot didn't catch the NAME_CHANGE # Forces name update in case bot didn't catch the NAME_CHANGE
# event. Theoretically only needed when bot was off. # event. Theoretically only needed when bot was off.
change_display_name(db, user_id, display_name) change_display_name(db, user_id, display_name)
elif data["eventData"]["body"].startswith("!"): # TODO: make prefix configurable elif data["eventData"]["body"].startswith("!"): # TODO: make prefix configurable
handle_redeem(data["eventData"]["body"], user_id) handle_redeem(data["eventData"]["body"], user_id)
return data return data