UPDATE: Added recommended categories as well as streamer data

BUGFIX: Fixed wrong datatypes
This commit is contained in:
JustIceO7
2025-01-31 03:52:25 +00:00
parent 908870f751
commit 1036494b7d
4 changed files with 42 additions and 25 deletions

View File

@@ -5,7 +5,8 @@ from utils.stream_utils import (
user_stream, user_stream,
followed_live_streams, followed_live_streams,
followed_streamers, followed_streamers,
stream_tags stream_tags,
streamer_data
) )
from utils.user_utils import get_user_id from utils.user_utils import get_user_id
from blueprints.utils import login_required from blueprints.utils import login_required
@@ -14,7 +15,8 @@ from utils.recommendation_utils import (
recommendations_based_on_category, recommendations_based_on_category,
user_recommendation_category, user_recommendation_category,
followed_categories_recommendations, followed_categories_recommendations,
category_recommendations category_recommendations,
user_category_recommendations
) )
from utils.utils import most_popular_category from utils.utils import most_popular_category
from database.database import Database from database.database import Database
@@ -70,16 +72,9 @@ def get_recommended_categories() -> list | list[dict]:
Queries DB to get a list of recommended categories for the user Queries DB to get a list of recommended categories for the user
""" """
username = session.get('username') user_id = session.get("user_id")
user_id = get_user_id(username) categories = user_category_recommendations(user_id)
return categories
db = Database()
categories = db.fetchall("""SELECT categories.category_id, categories.category_name, favourability
FROM categories, user_preferences
WHERE user_id = ? AND categories.category_id = user_preferences.category_id,
ORDER BY favourability DESC""", (user_id,))
return jsonify({'categories': categories})
@stream_bp.route('/get_streamer_data/<string:streamer_username>') @stream_bp.route('/get_streamer_data/<string:streamer_username>')
@@ -87,7 +82,11 @@ def get_streamer_data(streamer_username):
""" """
Returns a given streamer's data Returns a given streamer's data
""" """
return streamer_id = get_user_id(streamer_username)
if not streamer_id:
abort(404)
data = streamer_data(streamer_id)
return data
@stream_bp.route('/streamer/<string:streamer_username>/status') @stream_bp.route('/streamer/<string:streamer_username>/status')

View File

@@ -48,12 +48,4 @@ CREATE TABLE streams
category_id NOT NULL, category_id NOT NULL,
FOREIGN KEY (category_id) REFERENCES categories(category_id) ON DELETE CASCADE, FOREIGN KEY (category_id) REFERENCES categories(category_id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE
); );
SELECT users.user_id, title, username, num_viewers, category_name
FROM streams
JOIN users ON users.user_id = streams.user_id
JOIN categories ON streams.category_id = categories.category_id
ORDER BY num_viewers DESC
LIMIT 25;

View File

@@ -6,14 +6,14 @@ def user_recommendation_category(user_id: int) -> Optional[int]:
Queries user_preferences database to find users favourite streaming category and returns the category Queries user_preferences database to find users favourite streaming category and returns the category
""" """
with Database() as db: with Database() as db:
data = db.fetchone(""" category = db.fetchone("""
SELECT category_id SELECT category_id
FROM user_preferences FROM user_preferences
WHERE user_id = ? WHERE user_id = ?
ORDER BY favourability DESC ORDER BY favourability DESC
LIMIT 1 LIMIT 1
""", (user_id,)) """, (user_id,))
return data return category["category_id"] if category else None
def followed_categories_recommendations(user_id : int) -> Optional[List[dict]]: def followed_categories_recommendations(user_id : int) -> Optional[List[dict]]:
""" """
@@ -77,4 +77,19 @@ def category_recommendations() -> Optional[List[dict]]:
ORDER BY SUM(streams.num_viewers) DESC ORDER BY SUM(streams.num_viewers) DESC
LIMIT 5; LIMIT 5;
""") """)
return categories
def user_category_recommendations(user_id: int) -> Optional[List[dict]]:
"""
Queries user_preferences database to find users top 5 favourite streaming category and returns the category
"""
with Database() as db:
categories = db.fetchall("""
SELECT categories.category_id, categories.category_name
FROM categories
JOIN user_preferences ON categories.category_id = user_preferences.category_id
WHERE user_id = ?
ORDER BY favourability DESC
LIMIT 5
""", (user_id,))
return categories return categories

View File

@@ -45,7 +45,7 @@ def followed_streamers(user_id: int) -> Optional[List[dict]]:
""", (user_id,)) """, (user_id,))
return followed_streamers return followed_streamers
def streamer_most_recent_stream(user_id: int) -> dict: def streamer_most_recent_stream(user_id: int) -> Optional[dict]:
""" """
Returns data of the most recent stream by a streamer Returns data of the most recent stream by a streamer
""" """
@@ -57,6 +57,17 @@ def streamer_most_recent_stream(user_id: int) -> dict:
""", (user_id, user_id)) """, (user_id, user_id))
return most_recent_stream return most_recent_stream
def streamer_data(streamer_id: int) -> Optional[dict]:
"""
Returns information about the streamer
"""
with Database() as db:
data = db.fetchone("""
SELECT username, num_followering, isPartnered FROM users
WHERE user_id = ?
""", (streamer_id))
return data
def user_stream(user_id: int, stream_id: int) -> dict: def user_stream(user_id: int, stream_id: int) -> dict:
""" """
Returns data of a streamers selected stream Returns data of a streamers selected stream