From 2ab85bfb6d5a4a8271436ca3f9abd2c938e25ac8 Mon Sep 17 00:00:00 2001 From: Lili Date: Wed, 24 Aug 2022 13:39:40 +0200 Subject: [PATCH] move helper functions to own file --- tlapbot/owncastHelpers.py | 39 ++++++++++++++++++++++++++++++ tlapbot/owncastWebhooks.py | 49 +++----------------------------------- 2 files changed, 42 insertions(+), 46 deletions(-) create mode 100644 tlapbot/owncastHelpers.py diff --git a/tlapbot/owncastHelpers.py b/tlapbot/owncastHelpers.py new file mode 100644 index 0000000..be99041 --- /dev/null +++ b/tlapbot/owncastHelpers.py @@ -0,0 +1,39 @@ +from flask import current_app +import requests +from sqlite3 import Error + +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) + +def sendChat(message): # TODO: url to constant? + 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}) + return r.json() \ No newline at end of file diff --git a/tlapbot/owncastWebhooks.py b/tlapbot/owncastWebhooks.py index 0029fcd..fbeebec 100644 --- a/tlapbot/owncastWebhooks.py +++ b/tlapbot/owncastWebhooks.py @@ -1,66 +1,26 @@ -from flask import Flask,request,json,g,Blueprint,current_app +from flask import Flask,request,json,Blueprint from sqlite3 import Error from tlapbot.db import get_db -import requests +from tlapbot.owncastHelpers import userExists, addUserToDatabase, sendChat 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) - -def sendChat(message): # TODO: url to constant? - 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}) - return r.json() - @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"] - + 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 = ?", @@ -69,11 +29,9 @@ def owncastWebhook(): message = "{}'s points: {}".format(display_name, cursor.fetchone()[0]) print(message) sendChat(message) - 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( @@ -84,5 +42,4 @@ def owncastWebhook(): except Error as e: print("Error occured giving DEBUG points:", e.args[0]) print("To user:", user_id) - return data \ No newline at end of file