Fix/pylint cleanup (#8)
* 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
This commit is contained in:
committed by
GitHub
parent
fed1a2f288
commit
2758be8680
@@ -1,7 +1,12 @@
|
||||
from celery import Celery, shared_task, Task
|
||||
"""Celery configuration and Flask app context setup for async tasks."""
|
||||
|
||||
from celery import Celery, Task
|
||||
|
||||
|
||||
def celery_init_app(app) -> Celery:
|
||||
"""Initialize Celery with Flask application context."""
|
||||
class FlaskTask(Task):
|
||||
"""Celery task that runs within Flask app context."""
|
||||
def __call__(self, *args: object, **kwargs: object) -> object:
|
||||
with app.app_context():
|
||||
return self.run(*args, **kwargs)
|
||||
@@ -14,6 +19,10 @@ def celery_init_app(app) -> Celery:
|
||||
'schedule': 30.0,
|
||||
},
|
||||
}
|
||||
celery_app.conf.include = [
|
||||
'celery_tasks.preferences',
|
||||
'celery_tasks.streaming',
|
||||
]
|
||||
celery_app.set_default()
|
||||
app.extensions["celery"] = celery_app
|
||||
return celery_app
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
"""Celery app initialization with Flask."""
|
||||
|
||||
from blueprints import create_app
|
||||
|
||||
flask_app = create_app()
|
||||
celery_app = flask_app.extensions["celery"]
|
||||
celery_app = flask_app.extensions["celery"]
|
||||
|
||||
@@ -1,15 +1,19 @@
|
||||
"""Scheduled task for updating user preferences based on stream viewing."""
|
||||
|
||||
import json
|
||||
|
||||
from celery import shared_task
|
||||
from database.database import Database
|
||||
import redis
|
||||
import json
|
||||
|
||||
redis_url = "redis://redis:6379/1"
|
||||
r = redis.from_url(redis_url, decode_responses=True)
|
||||
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
|
||||
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
|
||||
@@ -21,13 +25,19 @@ def user_preferences():
|
||||
# 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
|
||||
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))
|
||||
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))
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
from celery import Celery, shared_task, Task
|
||||
from datetime import datetime
|
||||
from celery_tasks.preferences import user_preferences
|
||||
from utils.stream_utils import generate_thumbnail, get_streamer_live_status, get_custom_thumbnail_status, remove_hls_files, get_video_duration
|
||||
from time import sleep
|
||||
from os import listdir, remove
|
||||
from utils.path_manager import PathManager
|
||||
"""Async tasks for stream thumbnail updates, VOD creation, and image conversion."""
|
||||
|
||||
import subprocess
|
||||
from os import listdir
|
||||
|
||||
from celery import shared_task
|
||||
from utils.stream_utils import (
|
||||
generate_thumbnail, get_streamer_live_status,
|
||||
get_custom_thumbnail_status, remove_hls_files, get_video_duration
|
||||
)
|
||||
from utils.path_manager import PathManager
|
||||
|
||||
path_manager = PathManager()
|
||||
|
||||
@@ -16,10 +19,13 @@ def update_thumbnail(user_id, stream_file, thumbnail_file, sleep_time, second_ca
|
||||
"""
|
||||
|
||||
# Check if stream is still live and custom thumbnail has not been set
|
||||
if get_streamer_live_status(user_id)['is_live'] and not get_custom_thumbnail_status(user_id)['custom_thumbnail']:
|
||||
if (get_streamer_live_status(user_id)['is_live']
|
||||
and not get_custom_thumbnail_status(user_id)['custom_thumbnail']):
|
||||
print("Updating thumbnail...")
|
||||
generate_thumbnail(stream_file, thumbnail_file)
|
||||
update_thumbnail.apply_async((user_id, stream_file, thumbnail_file, sleep_time, second_capture), countdown=sleep_time)
|
||||
update_thumbnail.apply_async(
|
||||
(user_id, stream_file, thumbnail_file, sleep_time, second_capture),
|
||||
countdown=sleep_time)
|
||||
else:
|
||||
print(f"Stopping thumbnail updates for stream of {user_id}")
|
||||
|
||||
@@ -34,12 +40,12 @@ def combine_ts_stream(stream_path, vods_path, vod_file_name, thumbnail_file) ->
|
||||
ts_files.sort()
|
||||
|
||||
# Create temp file listing all ts files
|
||||
with open(f"{stream_path}/list.txt", "w") as f:
|
||||
with open(f"{stream_path}/list.txt", "w", encoding="utf-8") as f:
|
||||
for ts_file in ts_files:
|
||||
f.write(f"file '{ts_file}'\n")
|
||||
|
||||
|
||||
# Concatenate all ts files into a single vod
|
||||
|
||||
|
||||
vod_command = [
|
||||
"ffmpeg",
|
||||
"-f",
|
||||
@@ -53,7 +59,7 @@ def combine_ts_stream(stream_path, vods_path, vod_file_name, thumbnail_file) ->
|
||||
vod_file_path
|
||||
]
|
||||
|
||||
subprocess.run(vod_command)
|
||||
subprocess.run(vod_command, check=True)
|
||||
|
||||
# Remove HLS files, even if user is not streaming
|
||||
remove_hls_files(stream_path)
|
||||
@@ -78,4 +84,4 @@ def convert_image_to_png(image_path, png_path):
|
||||
png_path
|
||||
]
|
||||
|
||||
subprocess.run(image_command)
|
||||
subprocess.run(image_command, check=True)
|
||||
|
||||
Reference in New Issue
Block a user