first prototype of point tracking. no point redeem
This commit is contained in:
		
							parent
							
								
									9ad850b938
								
							
						
					
					
						commit
						67fc364b8e
					
				
							
								
								
									
										67
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										67
									
								
								main.py
									
									
									
									
									
								
							@ -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)
 | 
					 | 
				
			||||||
@ -25,9 +25,13 @@ def create_app(test_config=None):
 | 
				
			|||||||
        pass
 | 
					        pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    from . import db
 | 
					    from . import db
 | 
				
			||||||
 | 
					    from . import owncastWebhooks
 | 
				
			||||||
 | 
					    app.register_blueprint(owncastWebhooks.bp)
 | 
				
			||||||
    db.init_app(app)
 | 
					    db.init_app(app)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return app
 | 
					    return app
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == '__main__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
    create_app()
 | 
					    create_app()
 | 
				
			||||||
							
								
								
									
										82
									
								
								tlapbot/owncastWebhooks.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										82
									
								
								tlapbot/owncastWebhooks.py
									
									
									
									
									
										Normal file
									
								
							@ -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)
 | 
				
			||||||
@ -2,6 +2,5 @@ DROP TABLE IF EXISTS points;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
CREATE TABLE points (
 | 
					CREATE TABLE points (
 | 
				
			||||||
  id TEXT PRIMARY KEY,
 | 
					  id TEXT PRIMARY KEY,
 | 
				
			||||||
  username TEXT NOT NULL,
 | 
					 | 
				
			||||||
  points INTEGER
 | 
					  points INTEGER
 | 
				
			||||||
);
 | 
					);
 | 
				
			||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user