From c5f31946d3d261ebd6a6194721e5cf178665e2cf Mon Sep 17 00:00:00 2001 From: Lili Date: Wed, 24 Aug 2022 15:35:16 +0200 Subject: [PATCH] moving code out into helper functions --- tlapbot/owncast_helpers.py | 38 +++++++++++++++++++++++++++++++++---- tlapbot/owncast_webhooks.py | 34 ++++++++++----------------------- 2 files changed, 44 insertions(+), 28 deletions(-) diff --git a/tlapbot/owncast_helpers.py b/tlapbot/owncast_helpers.py index 5041fee..1546f6f 100644 --- a/tlapbot/owncast_helpers.py +++ b/tlapbot/owncast_helpers.py @@ -2,8 +2,38 @@ from flask import current_app import requests from sqlite3 import Error +def read_users_points(db, user_id): + try: + cursor = db.execute( + "SELECT points FROM points WHERE id = ?", + (user_id,) + ) + return cursor.fetchone()[0] + + except Error as e: + print("Error occured reading points:", e.args[0]) + print("To user:", user_id) -def user_exists(user_id, db): +def give_points_to_user(db, user_id, points): + try: + db.execute( + "UPDATE points SET points = points + ? WHERE id = ?", + (points, user_id,) + ) + db.commit() + except Error as e: + print("Error occured giving DEBUG points:", e.args[0]) + print("To user:", user_id) + +def give_points_to_chat(db): + points_given = 10 + url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/integrations/clients' + headers = {"Authorization": "Bearer " + current_app.config['OWNCAST_ACCESS_TOKEN']} + r = requests.post(url, headers=headers) + for user_object in r.json(): + give_points_to_user(db, user_object["user"]["id"], points_given) + +def user_exists(db, user_id): try: cursor = db.execute( "SELECT points FROM points WHERE id = ?", @@ -16,8 +46,8 @@ def user_exists(user_id, db): print("Error occured checking if user exists:", e.args[0]) print("To user:", user_id) -# only adds user if they aren't already in. -def add_user_to_database(user_id, db): +""" Adds a new user to the database. Does nothing if user is already in.""" +def add_user_to_database(db, user_id): try: cursor = db.execute( "SELECT points FROM points WHERE id = ?", @@ -33,7 +63,7 @@ def add_user_to_database(user_id, db): print("Error occured adding user to db:", e.args[0]) print("To user:", user_id) -def send_chat(message): # TODO: url to constant? +def send_chat(message): url = current_app.config['OWNCAST_INSTANCE_URL'] + '/api/integrations/chat/send' headers = {"Authorization": "Bearer " + current_app.config['OWNCAST_ACCESS_TOKEN']} r = requests.post(url, headers=headers, json={"body": message}) diff --git a/tlapbot/owncast_webhooks.py b/tlapbot/owncast_webhooks.py index 68d075c..c72de4b 100644 --- a/tlapbot/owncast_webhooks.py +++ b/tlapbot/owncast_webhooks.py @@ -1,7 +1,7 @@ from flask import Flask,request,json,Blueprint from sqlite3 import Error from tlapbot.db import get_db -from tlapbot.owncast_helpers import user_exists, add_user_to_database, send_chat +from tlapbot.owncast_helpers import user_exists, add_user_to_database, send_chat, give_points_to_user, read_users_points bp = Blueprint('owncast_webhooks', __name__) @@ -11,7 +11,8 @@ def owncast_webhook(): db = get_db() if data["type"] == "USER_JOINED": user_id = data["eventData"]["user"]["id"] - add_user_to_database(user_id, db) + # CONSIDER: join points for joining stream + add_user_to_database(db, user_id) elif data["type"] == "CHAT": display_name = data["eventData"]["user"]["displayName"] print("New chat message:") @@ -19,27 +20,12 @@ def owncast_webhook(): print(f'{data["eventData"]["body"]}') user_id = data["eventData"]["user"]["id"] if "!points" in data["eventData"]["body"]: - if not user_exists(user_id, db): - add_user_to_database(user_id, db) - try: - cursor = db.execute( - "SELECT points FROM points WHERE id = ?", - (user_id,) - ) - message = "{}'s points: {}".format(display_name, cursor.fetchone()[0]) - print(message) - send_chat(message) - except Error as e: - print("Error occured reading points:", e.args[0]) - print("To user:", user_id) + if not user_exists(db, user_id): + add_user_to_database(db, user_id) + points = read_users_points(db, user_id) + message = "{}'s points: {}".format(display_name, points) + print(message) + send_chat(message) else: # DEBUG: give points for message - try: - db.execute( - "UPDATE points SET points = points + 10 WHERE id = ?", - (user_id,) - ) - db.commit() - except Error as e: - print("Error occured giving DEBUG points:", e.args[0]) - print("To user:", user_id) + give_points_to_user(db, user_id, 10) return data \ No newline at end of file