* 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
44 lines
1.8 KiB
Python
44 lines
1.8 KiB
Python
"""Scheduled task for updating user preferences based on stream viewing."""
|
|
|
|
import json
|
|
|
|
from celery import shared_task
|
|
from database.database import Database
|
|
import redis
|
|
|
|
REDIS_URL = "redis://redis:6379/1"
|
|
r = redis.from_url(REDIS_URL, decode_responses=True)
|
|
|
|
@shared_task
|
|
def user_preferences():
|
|
"""
|
|
Updates users preferences on different stream categories
|
|
based on the streams they are currently watching
|
|
"""
|
|
stats = r.hget("current_viewers", "viewers")
|
|
# If there are any current viewers
|
|
if stats:
|
|
stats = json.loads(stats)
|
|
with Database() as db:
|
|
# Loop over all users and their currently watching streams
|
|
for user_id, stream_ids in stats.items():
|
|
# For each user and stream combination
|
|
for stream_id in stream_ids:
|
|
# Retrieves category associated with stream
|
|
current_category = db.fetchone(
|
|
"""SELECT category_id FROM streams
|
|
WHERE user_id = ?
|
|
""", (stream_id,))
|
|
# If stream is still live then update the
|
|
# user_preferences table to reflect their preferences
|
|
if current_category:
|
|
db.execute(
|
|
"""INSERT INTO user_preferences
|
|
(user_id,category_id,favourability)
|
|
VALUES (?,?,?)
|
|
ON CONFLICT(user_id, category_id)
|
|
DO UPDATE SET
|
|
favourability = favourability + 1
|
|
""", (user_id,
|
|
current_category["category_id"], 1))
|