FEAT: Added Celery Beat

FEAT: Added preferences Celery task to automatically record what categories users watched
This commit is contained in:
JustIceO7
2025-02-18 02:02:34 +00:00
parent 62d83aa4eb
commit 76de1b7d9d
9 changed files with 135 additions and 12 deletions

View File

@@ -1,10 +1,14 @@
from flask import Blueprint, jsonify
from flask import Blueprint, jsonify, session
from database.database import Database
from .socket import socketio
from flask_socketio import emit, join_room, leave_room
from datetime import datetime
from utils.user_utils import get_user_id
import redis
import json
redis_url = "redis://redis:6379/1"
r = redis.from_url(redis_url, decode_responses=True)
chat_bp = Blueprint("chat", __name__)
#NOTE: <---------------------- ROUTES NEEDS TO BE CHANGED TO VIDEO OR DELETED AS DEEMED APPROPRIATE ---------------------->
@@ -22,8 +26,12 @@ def handle_join(data) -> None:
"""
Allow a user to join the chat of the stream they are watching.
"""
print(data, flush=True)
stream_id = data.get("stream_id")
if stream_id:
user_id = get_user_id(data["username"])
if user_id:
add_favourability_entry(str(user_id), str(stream_id))
join_room(stream_id)
num_viewers = len(list(socketio.server.manager.get_participants("/", stream_id)))
update_viewers(stream_id, num_viewers)
@@ -40,11 +48,15 @@ def handle_leave(data) -> None:
"""
Handle what happens when a user leaves the stream they are watching in regards to the chat.
"""
print(data, flush=True)
stream_id = data.get("stream_id")
user_id = data.get("user_id")
if stream_id:
leave_room(stream_id)
if user_id:
remove_favourability_entry(data["user_id"], stream_id)
num_viewers = len(list(socketio.server.manager.get_participants("/", stream_id)))
update_viewers(stream_id, num_viewers)
update_viewers(str(user_id), str(stream_id))
emit("status",
{
"message": f"Welcome to the chat, stream_id: {stream_id}",
@@ -132,4 +144,49 @@ def update_viewers(user_id, num_viewers):
SET num_viewers = ?
WHERE user_id = ?;
""", (num_viewers, user_id))
db.close_connection
db.close_connection
#TODO: Make sure that users entry within Redis is removed if they disconnect from socket
def add_favourability_entry(user_id, stream_id):
"""
Adds entry to Redis that user is watching a streamer
"""
current_viewers = r.hget("current_viewers", "viewers")
if current_viewers:
current_viewers = json.loads(current_viewers)
else:
current_viewers = {}
# Checks if user exists already
if user_id in current_viewers:
# If already exists append stream to user
current_viewers[user_id].append(stream_id)
else:
# Creates new entry for user and stream
current_viewers[user_id] = [stream_id]
r.hset("current_viewers", "viewers", json.dumps(current_viewers))
def remove_favourability_entry(user_id, stream_id):
"""
Removes entry to Redis that user is watching a streamer
"""
current_viewers = r.hget("current_viewers", "viewers")
# If key exists
if current_viewers:
current_viewers = json.loads(current_viewers)
else:
current_viewers = {}
# Checks if user exists already
if user_id in current_viewers:
# Removes specific stream from user
current_viewers[user_id] = [stream for stream in current_viewers[user_id] if stream != stream_id]
# If user is no longer watching any streams
if not current_viewers[user_id]:
del current_viewers[user_id]
r.hset("current_viewers", "viewers", json.dumps(current_viewers))

View File

@@ -98,6 +98,7 @@ def google_auth():
session.clear()
session["username"] = user_data["username"]
session["user_id"] = user_data["user_id"]
print(f"session: {session.get('username')}. user_id: {session.get('user_id')}", flush=True)
return redirect(origin)