moving code out into helper functions
This commit is contained in:
parent
63b4c73cda
commit
c5f31946d3
|
@ -2,8 +2,38 @@ from flask import current_app
|
||||||
import requests
|
import requests
|
||||||
from sqlite3 import Error
|
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]
|
||||||
|
|
||||||
def user_exists(user_id, db):
|
except Error as e:
|
||||||
|
print("Error occured reading points:", e.args[0])
|
||||||
|
print("To user:", user_id)
|
||||||
|
|
||||||
|
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:
|
try:
|
||||||
cursor = db.execute(
|
cursor = db.execute(
|
||||||
"SELECT points FROM points WHERE id = ?",
|
"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("Error occured checking if user exists:", e.args[0])
|
||||||
print("To user:", user_id)
|
print("To user:", user_id)
|
||||||
|
|
||||||
# only adds user if they aren't already in.
|
""" Adds a new user to the database. Does nothing if user is already in."""
|
||||||
def add_user_to_database(user_id, db):
|
def add_user_to_database(db, user_id):
|
||||||
try:
|
try:
|
||||||
cursor = db.execute(
|
cursor = db.execute(
|
||||||
"SELECT points FROM points WHERE id = ?",
|
"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("Error occured adding user to db:", e.args[0])
|
||||||
print("To user:", user_id)
|
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'
|
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']}
|
||||||
r = requests.post(url, headers=headers, json={"body": message})
|
r = requests.post(url, headers=headers, json={"body": message})
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from flask import Flask,request,json,Blueprint
|
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 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__)
|
bp = Blueprint('owncast_webhooks', __name__)
|
||||||
|
|
||||||
|
@ -11,7 +11,8 @@ def owncast_webhook():
|
||||||
db = get_db()
|
db = get_db()
|
||||||
if data["type"] == "USER_JOINED":
|
if data["type"] == "USER_JOINED":
|
||||||
user_id = data["eventData"]["user"]["id"]
|
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":
|
elif data["type"] == "CHAT":
|
||||||
display_name = data["eventData"]["user"]["displayName"]
|
display_name = data["eventData"]["user"]["displayName"]
|
||||||
print("New chat message:")
|
print("New chat message:")
|
||||||
|
@ -19,27 +20,12 @@ def owncast_webhook():
|
||||||
print(f'{data["eventData"]["body"]}')
|
print(f'{data["eventData"]["body"]}')
|
||||||
user_id = data["eventData"]["user"]["id"]
|
user_id = data["eventData"]["user"]["id"]
|
||||||
if "!points" in data["eventData"]["body"]:
|
if "!points" in data["eventData"]["body"]:
|
||||||
if not user_exists(user_id, db):
|
if not user_exists(db, user_id):
|
||||||
add_user_to_database(user_id, db)
|
add_user_to_database(db, user_id)
|
||||||
try:
|
points = read_users_points(db, user_id)
|
||||||
cursor = db.execute(
|
message = "{}'s points: {}".format(display_name, points)
|
||||||
"SELECT points FROM points WHERE id = ?",
|
|
||||||
(user_id,)
|
|
||||||
)
|
|
||||||
message = "{}'s points: {}".format(display_name, cursor.fetchone()[0])
|
|
||||||
print(message)
|
print(message)
|
||||||
send_chat(message)
|
send_chat(message)
|
||||||
except Error as e:
|
|
||||||
print("Error occured reading points:", e.args[0])
|
|
||||||
print("To user:", user_id)
|
|
||||||
else: # DEBUG: give points for message
|
else: # DEBUG: give points for message
|
||||||
try:
|
give_points_to_user(db, user_id, 10)
|
||||||
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
|
return data
|
Loading…
Reference in New Issue