From 67fc364b8e8a6f7d7c6164e72bb85ac6bea91f95 Mon Sep 17 00:00:00 2001 From: Lili Date: Wed, 17 Aug 2022 16:33:40 +0200 Subject: [PATCH] first prototype of point tracking. no point redeem --- main.py | 67 ------------------------------- tlapbot/__init__.py | 4 ++ tlapbot/owncastWebhooks.py | 82 ++++++++++++++++++++++++++++++++++++++ tlapbot/schema.sql | 1 - 4 files changed, 86 insertions(+), 68 deletions(-) delete mode 100644 main.py create mode 100644 tlapbot/owncastWebhooks.py diff --git a/main.py b/main.py deleted file mode 100644 index 91bed90..0000000 --- a/main.py +++ /dev/null @@ -1,67 +0,0 @@ -from flask import Flask,request,json,g -from tlapbot.db import get_db -import requests - -app = Flask(__name__) - -@app.route('/owncastWebhook',methods=['POST']) -def owncastWebhook(): - data = request.json - db = get_db() - if data["type"] == "CHAT": - print("New chat message:") - print(f'from {data["eventData"]["user"]["displayName"]}:') - print(f'{data["eventData"]["body"]}') - - elif data["type"] == "USER_JOINED": - user_id = data["eventData"]["user"]["id"] - try: - cursor = db.execute( - "SELECT username FROM points WHERE id = ?", - (user_id) - ) - if cursor.fetchone() == None: - username = data["eventData"]["user"]["displayName"] - cursor.execute( - "INSERT INTO points(id, username, points) VALUES(?, ?, 0)", - (user_id, username) - ) - db.commit() - - elif data["type"] == "NAME_CHANGED": - user_id = data["eventData"]["user"]["id"] - new_username = data["eventData"]["user"]["newName"] - try: - db.execute( - "UPDATE points SET username = ? WHERE id = ?", - (new_username, user_id), - ) - db.commit() - except db.IntegrityError: - print("Integrity Error.") - print(f"User ID {user_id} probably does not exist.") - - elif data["type"] == "CHAT": - user_id = data["eventData"]["user"]["id"] - if "!points" in data["body"]: - try: - cursor = db.execute( - "SELECT points FROM points WHERE id = ?", - (user_id), - ) - print("{}'s points: {}".format(user_id, cursor.fetchone())) - else: # DEBUG: give points for message - try: - db.execute( - "UPDATE points SET points = points + 10 WHERE id = ?", - (user_id), - ) - db.commit() - except sqlite3.Error as e: - print("Error occured:", e.args[0]) - print("To user:", user_id) - - return data - -if __name__ == '__main__': - app.run(debug=True) \ No newline at end of file diff --git a/tlapbot/__init__.py b/tlapbot/__init__.py index 1fa45f0..0edf3ba 100644 --- a/tlapbot/__init__.py +++ b/tlapbot/__init__.py @@ -25,9 +25,13 @@ def create_app(test_config=None): pass from . import db + from . import owncastWebhooks + app.register_blueprint(owncastWebhooks.bp) db.init_app(app) return app + + if __name__ == '__main__': create_app() \ No newline at end of file diff --git a/tlapbot/owncastWebhooks.py b/tlapbot/owncastWebhooks.py new file mode 100644 index 0000000..2065ff6 --- /dev/null +++ b/tlapbot/owncastWebhooks.py @@ -0,0 +1,82 @@ +from flask import Flask,request,json,g,Blueprint +from sqlite3 import Error +from tlapbot.db import get_db +import requests + +bp = Blueprint('owncastWebhooks', __name__) + +def userExists(user_id, db): + try: + cursor = db.execute( + "SELECT points FROM points WHERE id = ?", + (user_id,) + ) + if cursor.fetchone() == None: + return False + return True + except Error as e: + 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 addUserToDatabase(user_id, db): + try: + cursor = db.execute( + "SELECT points FROM points WHERE id = ?", + (user_id,) + ) + if cursor.fetchone() == None: + cursor.execute( + "INSERT INTO points(id, points) VALUES(?, 10)", + (user_id,) + ) + db.commit() + except Error as e: + print("Error occured adding user to db:", e.args[0]) + print("To user:", user_id) + +@bp.route('/owncastWebhook',methods=['POST']) +def owncastWebhook(): + data = request.json + db = get_db() + + if data["type"] == "USER_JOINED": + user_id = data["eventData"]["user"]["id"] + addUserToDatabase(user_id, db) + + elif data["type"] == "CHAT": + display_name = data["eventData"]["user"]["displayName"] + print("New chat message:") + print(f'from {display_name}:') + print(f'{data["eventData"]["body"]}') + user_id = data["eventData"]["user"]["id"] + + if "!points" in data["eventData"]["body"]: + if not userExists(user_id, db): + addUserToDatabase(user_id, db) + + try: + cursor = db.execute( + "SELECT points FROM points WHERE id = ?", + (user_id,) + ) + print("{}'s points: {}".format(display_name, cursor.fetchone()[0])) + except Error as e: + print("Error occured reading points:", e.args[0]) + print("To user:", user_id) + + 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) + + return data + +if __name__ == '__main__': + app.run(debug=True) \ No newline at end of file diff --git a/tlapbot/schema.sql b/tlapbot/schema.sql index 18da7b8..b168c78 100644 --- a/tlapbot/schema.sql +++ b/tlapbot/schema.sql @@ -2,6 +2,5 @@ DROP TABLE IF EXISTS points; CREATE TABLE points ( id TEXT PRIMARY KEY, - username TEXT NOT NULL, points INTEGER ); \ No newline at end of file