feat(sound_played): add sound play tracking and user statistics endpoints; enhance VLC service to record play events

This commit is contained in:
JSC
2025-07-03 21:50:17 +02:00
parent 97b998fd9e
commit c3b8205f83
4 changed files with 353 additions and 4 deletions

View File

@@ -9,6 +9,7 @@ from typing import Dict, List, Optional
from app.database import db
from app.models.sound import Sound
from app.models.sound_played import SoundPlayed
class VLCService:
@@ -19,7 +20,7 @@ class VLCService:
self.processes: Dict[str, subprocess.Popen] = {}
self.lock = threading.Lock()
def play_sound(self, sound_id: int) -> bool:
def play_sound(self, sound_id: int, user_id: int | None = None, ip_address: str | None = None, user_agent: str | None = None) -> bool:
"""Play a sound by ID using VLC subprocess."""
try:
# Get sound from database
@@ -79,6 +80,19 @@ class VLCService:
# Increment play count
sound.increment_play_count()
# Record play event if user is provided
if user_id:
try:
SoundPlayed.create_play_record(
user_id=user_id,
sound_id=sound_id,
ip_address=ip_address,
user_agent=user_agent,
commit=True,
)
except Exception as e:
print(f"Error recording play event: {e}")
# Schedule cleanup after sound duration
threading.Thread(
target=self._cleanup_after_playback,