* Fix pylint warnings across all 24 Python files in web_server - Add module, class, and function docstrings (C0114, C0115, C0116) - Fix import ordering: stdlib before third-party before local (C0411) - Replace wildcard imports with explicit named imports (W0401) - Remove trailing whitespace and add missing final newlines (C0303, C0304) - Replace dict() with dict literals (R1735) - Remove unused imports and variables (W0611, W0612) - Narrow broad Exception catches to specific exceptions (W0718) - Replace f-string logging with lazy % formatting (W1203) - Fix variable naming: UPPER_CASE for constants, snake_case for locals (C0103) - Add pylint disable comments for necessary global statements (W0603) - Fix no-else-return, simplifiable-if-expression, singleton-comparison - Fix bad indentation in stripe.py (W0311) - Add encoding="utf-8" to open() calls (W1514) - Add check=True to subprocess.run() calls (W1510) - Register Celery task modules via conf.include * Update `package-lock.json` add peer dependencies
147 lines
5.0 KiB
Python
147 lines
5.0 KiB
Python
"""Personalized stream and category recommendations based on user preferences."""
|
|
|
|
from typing import Optional, List
|
|
|
|
from database.database import Database
|
|
|
|
def get_user_preferred_category(user_id: int) -> Optional[int]:
|
|
"""
|
|
Queries user_preferences database to find users favourite
|
|
streaming category and returns the category
|
|
"""
|
|
with Database() as db:
|
|
category = db.fetchone("""
|
|
SELECT category_id
|
|
FROM user_preferences
|
|
WHERE user_id = ?
|
|
ORDER BY favourability DESC
|
|
LIMIT 1
|
|
""", (user_id,))
|
|
return category["category_id"] if category else None
|
|
|
|
|
|
def get_followed_categories_recommendations(
|
|
user_id: int, no_streams: int = 4
|
|
) -> Optional[List[dict]]:
|
|
"""
|
|
Returns top streams given a user's category following
|
|
"""
|
|
with Database() as db:
|
|
streams = db.fetchall("""
|
|
SELECT u.user_id, title, u.username, num_viewers,
|
|
category_name
|
|
FROM streams
|
|
JOIN users u ON streams.user_id = u.user_id
|
|
JOIN categories
|
|
ON streams.category_id = categories.category_id
|
|
WHERE categories.category_id IN
|
|
(SELECT category_id
|
|
FROM followed_categories WHERE user_id = ?)
|
|
ORDER BY num_viewers DESC
|
|
LIMIT ?;
|
|
""", (user_id, no_streams))
|
|
return streams
|
|
|
|
def get_followed_your_categories(user_id: int) -> Optional[List[dict]]:
|
|
"""
|
|
Returns all user followed categories
|
|
"""
|
|
with Database() as db:
|
|
categories = db.fetchall("""
|
|
SELECT categories.category_name
|
|
FROM categories
|
|
JOIN followed_categories
|
|
ON categories.category_id = followed_categories.category_id
|
|
WHERE followed_categories.user_id = ?;
|
|
""", (user_id,))
|
|
return categories
|
|
|
|
|
|
def get_streams_based_on_category(
|
|
category_id: int, no_streams: int = 4, offset: int = 0
|
|
) -> Optional[List[dict]]:
|
|
"""
|
|
Queries stream database to get top most viewed streams
|
|
based on given category
|
|
"""
|
|
with Database() as db:
|
|
streams = db.fetchall("""
|
|
SELECT u.user_id, title, username, num_viewers,
|
|
c.category_name
|
|
FROM streams s
|
|
JOIN users u ON s.user_id = u.user_id
|
|
JOIN categories c ON s.category_id = c.category_id
|
|
WHERE c.category_id = ?
|
|
ORDER BY num_viewers DESC
|
|
LIMIT ? OFFSET ?
|
|
""", (category_id, no_streams, offset))
|
|
return streams
|
|
|
|
|
|
def get_highest_view_streams(no_streams: int = 4) -> Optional[List[dict]]:
|
|
"""
|
|
Return a list of live streams by number of viewers
|
|
"""
|
|
with Database() as db:
|
|
data = db.fetchall("""
|
|
SELECT u.user_id, username, title, num_viewers,
|
|
category_name
|
|
FROM streams
|
|
JOIN users u ON streams.user_id = u.user_id
|
|
JOIN categories
|
|
ON streams.category_id = categories.category_id
|
|
ORDER BY num_viewers DESC
|
|
LIMIT ?;
|
|
""", (no_streams,))
|
|
return data
|
|
|
|
def get_highest_view_categories(
|
|
no_categories: int = 4, offset: int = 0
|
|
) -> Optional[List[dict]]:
|
|
"""
|
|
Returns a list of top most popular categories given offset
|
|
"""
|
|
with Database() as db:
|
|
categories = db.fetchall("""
|
|
SELECT categories.category_id,
|
|
categories.category_name,
|
|
COALESCE(SUM(streams.num_viewers), 0)
|
|
AS num_viewers
|
|
FROM categories
|
|
LEFT JOIN streams
|
|
ON streams.category_id = categories.category_id
|
|
GROUP BY categories.category_id,
|
|
categories.category_name
|
|
ORDER BY num_viewers DESC
|
|
LIMIT ? OFFSET ?;
|
|
""", (no_categories, offset))
|
|
return categories
|
|
|
|
def get_user_category_recommendations(
|
|
user_id=1, no_categories: int = 4
|
|
) -> Optional[List[dict]]:
|
|
"""
|
|
Queries user_preferences database to find users top favourite
|
|
streaming category and returns the category
|
|
"""
|
|
with Database() as db:
|
|
categories = db.fetchall("""
|
|
SELECT categories.category_id,
|
|
categories.category_name,
|
|
COALESCE(SUM(streams.num_viewers), 0)
|
|
AS num_viewers
|
|
FROM categories
|
|
JOIN user_preferences
|
|
ON categories.category_id
|
|
= user_preferences.category_id
|
|
LEFT JOIN streams
|
|
ON categories.category_id
|
|
= streams.category_id
|
|
WHERE user_preferences.user_id = ?
|
|
GROUP BY categories.category_id,
|
|
categories.category_name
|
|
ORDER BY user_preferences.favourability DESC
|
|
LIMIT ?
|
|
""", (user_id, no_categories))
|
|
return categories
|