karaoke-gen 0.101.0__py3-none-any.whl → 0.103.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- backend/api/routes/audio_search.py +4 -32
- backend/api/routes/file_upload.py +18 -83
- backend/api/routes/jobs.py +2 -2
- backend/api/routes/rate_limits.py +428 -0
- backend/api/routes/users.py +79 -19
- backend/config.py +16 -0
- backend/exceptions.py +66 -0
- backend/main.py +25 -1
- backend/services/email_validation_service.py +646 -0
- backend/services/firestore_service.py +21 -0
- backend/services/job_defaults_service.py +113 -0
- backend/services/job_manager.py +41 -2
- backend/services/rate_limit_service.py +641 -0
- backend/tests/conftest.py +7 -1
- backend/tests/test_audio_search.py +12 -8
- backend/tests/test_email_validation_service.py +298 -0
- backend/tests/test_file_upload.py +8 -6
- backend/tests/test_made_for_you.py +6 -4
- backend/tests/test_rate_limit_service.py +396 -0
- backend/tests/test_rate_limits_api.py +392 -0
- backend/workers/video_worker_orchestrator.py +26 -0
- {karaoke_gen-0.101.0.dist-info → karaoke_gen-0.103.1.dist-info}/METADATA +1 -1
- {karaoke_gen-0.101.0.dist-info → karaoke_gen-0.103.1.dist-info}/RECORD +26 -18
- {karaoke_gen-0.101.0.dist-info → karaoke_gen-0.103.1.dist-info}/WHEEL +0 -0
- {karaoke_gen-0.101.0.dist-info → karaoke_gen-0.103.1.dist-info}/entry_points.txt +0 -0
- {karaoke_gen-0.101.0.dist-info → karaoke_gen-0.103.1.dist-info}/licenses/LICENSE +0 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Centralized job defaults service.
|
|
3
|
+
|
|
4
|
+
This module provides consistent handling of job creation defaults across all
|
|
5
|
+
endpoints (file_upload, audio_search, made-for-you webhook, etc.).
|
|
6
|
+
|
|
7
|
+
Centralizing these defaults prevents divergence between code paths and ensures
|
|
8
|
+
all jobs receive the same default configuration.
|
|
9
|
+
"""
|
|
10
|
+
from dataclasses import dataclass
|
|
11
|
+
from typing import Optional, Tuple
|
|
12
|
+
|
|
13
|
+
from backend.config import get_settings
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass
|
|
17
|
+
class EffectiveDistributionSettings:
|
|
18
|
+
"""Distribution settings with defaults applied from environment variables."""
|
|
19
|
+
dropbox_path: Optional[str]
|
|
20
|
+
gdrive_folder_id: Optional[str]
|
|
21
|
+
discord_webhook_url: Optional[str]
|
|
22
|
+
brand_prefix: Optional[str]
|
|
23
|
+
enable_youtube_upload: bool
|
|
24
|
+
youtube_description: Optional[str]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def get_effective_distribution_settings(
|
|
28
|
+
dropbox_path: Optional[str] = None,
|
|
29
|
+
gdrive_folder_id: Optional[str] = None,
|
|
30
|
+
discord_webhook_url: Optional[str] = None,
|
|
31
|
+
brand_prefix: Optional[str] = None,
|
|
32
|
+
enable_youtube_upload: Optional[bool] = None,
|
|
33
|
+
youtube_description: Optional[str] = None,
|
|
34
|
+
) -> EffectiveDistributionSettings:
|
|
35
|
+
"""
|
|
36
|
+
Get distribution settings with defaults applied from environment variables.
|
|
37
|
+
|
|
38
|
+
This ensures consistent handling of defaults across all job creation endpoints.
|
|
39
|
+
Each parameter, if not provided (None), falls back to the corresponding
|
|
40
|
+
environment variable configured in settings.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
dropbox_path: Explicit Dropbox path or None for default
|
|
44
|
+
gdrive_folder_id: Explicit Google Drive folder ID or None for default
|
|
45
|
+
discord_webhook_url: Explicit Discord webhook URL or None for default
|
|
46
|
+
brand_prefix: Explicit brand prefix or None for default
|
|
47
|
+
enable_youtube_upload: Explicit flag or None for default
|
|
48
|
+
youtube_description: Explicit description or None for default
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
EffectiveDistributionSettings with defaults applied
|
|
52
|
+
"""
|
|
53
|
+
settings = get_settings()
|
|
54
|
+
return EffectiveDistributionSettings(
|
|
55
|
+
dropbox_path=dropbox_path or settings.default_dropbox_path,
|
|
56
|
+
gdrive_folder_id=gdrive_folder_id or settings.default_gdrive_folder_id,
|
|
57
|
+
discord_webhook_url=discord_webhook_url or settings.default_discord_webhook_url,
|
|
58
|
+
brand_prefix=brand_prefix or settings.default_brand_prefix,
|
|
59
|
+
enable_youtube_upload=enable_youtube_upload if enable_youtube_upload is not None else settings.default_enable_youtube_upload,
|
|
60
|
+
youtube_description=youtube_description or settings.default_youtube_description,
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def resolve_cdg_txt_defaults(
|
|
65
|
+
theme_id: Optional[str],
|
|
66
|
+
enable_cdg: Optional[bool] = None,
|
|
67
|
+
enable_txt: Optional[bool] = None,
|
|
68
|
+
) -> Tuple[bool, bool]:
|
|
69
|
+
"""
|
|
70
|
+
Resolve CDG/TXT settings based on theme and explicit settings.
|
|
71
|
+
|
|
72
|
+
The resolution logic is:
|
|
73
|
+
1. If explicit True/False is provided, use that value
|
|
74
|
+
2. Otherwise, if a theme is set, use the server defaults (settings.default_enable_cdg/txt)
|
|
75
|
+
3. If no theme is set, default to False (CDG/TXT require style configuration)
|
|
76
|
+
|
|
77
|
+
This ensures CDG/TXT are only enabled when:
|
|
78
|
+
- A theme is configured (provides necessary style params), AND
|
|
79
|
+
- The server defaults allow it (DEFAULT_ENABLE_CDG=true by default)
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
theme_id: Theme identifier (if any)
|
|
83
|
+
enable_cdg: Explicit CDG setting (None means use default)
|
|
84
|
+
enable_txt: Explicit TXT setting (None means use default)
|
|
85
|
+
|
|
86
|
+
Returns:
|
|
87
|
+
Tuple of (resolved_enable_cdg, resolved_enable_txt)
|
|
88
|
+
"""
|
|
89
|
+
settings = get_settings()
|
|
90
|
+
|
|
91
|
+
# Default based on whether theme is set AND server defaults
|
|
92
|
+
# Theme is required because CDG/TXT need style configuration
|
|
93
|
+
theme_is_set = theme_id is not None
|
|
94
|
+
default_cdg = theme_is_set and settings.default_enable_cdg
|
|
95
|
+
default_txt = theme_is_set and settings.default_enable_txt
|
|
96
|
+
|
|
97
|
+
# Explicit values override defaults, None uses computed default
|
|
98
|
+
resolved_cdg = enable_cdg if enable_cdg is not None else default_cdg
|
|
99
|
+
resolved_txt = enable_txt if enable_txt is not None else default_txt
|
|
100
|
+
|
|
101
|
+
return resolved_cdg, resolved_txt
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
# Singleton instance (optional, for convenience)
|
|
105
|
+
_service_instance = None
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def get_job_defaults_service():
|
|
109
|
+
"""Get the job defaults service (module-level functions work fine, this is for consistency)."""
|
|
110
|
+
return {
|
|
111
|
+
'get_effective_distribution_settings': get_effective_distribution_settings,
|
|
112
|
+
'resolve_cdg_txt_defaults': resolve_cdg_txt_defaults,
|
|
113
|
+
}
|
backend/services/job_manager.py
CHANGED
|
@@ -14,6 +14,7 @@ from datetime import datetime
|
|
|
14
14
|
from typing import Optional, Dict, Any, List
|
|
15
15
|
|
|
16
16
|
from backend.config import settings
|
|
17
|
+
from backend.exceptions import RateLimitExceededError
|
|
17
18
|
from backend.models.job import Job, JobStatus, JobCreate, STATE_TRANSITIONS
|
|
18
19
|
from backend.models.worker_log import WorkerLogEntry
|
|
19
20
|
from backend.services.firestore_service import FirestoreService
|
|
@@ -41,16 +42,44 @@ class JobManager:
|
|
|
41
42
|
self.firestore = FirestoreService()
|
|
42
43
|
self.storage = StorageService()
|
|
43
44
|
|
|
44
|
-
def create_job(self, job_create: JobCreate) -> Job:
|
|
45
|
+
def create_job(self, job_create: JobCreate, is_admin: bool = False) -> Job:
|
|
45
46
|
"""
|
|
46
47
|
Create a new job with initial state PENDING.
|
|
47
48
|
|
|
48
49
|
Jobs start in PENDING state and transition to DOWNLOADING
|
|
49
50
|
when a worker picks them up.
|
|
50
51
|
|
|
52
|
+
Args:
|
|
53
|
+
job_create: Job creation parameters
|
|
54
|
+
is_admin: Whether the requesting user is an admin (bypasses rate limits)
|
|
55
|
+
|
|
51
56
|
Raises:
|
|
52
57
|
ValueError: If theme_id is not provided (all jobs require a theme)
|
|
58
|
+
RateLimitExceededError: If user has exceeded their daily job limit
|
|
53
59
|
"""
|
|
60
|
+
# Check rate limit FIRST (before any other validation)
|
|
61
|
+
# This prevents wasted work if user is rate limited
|
|
62
|
+
if job_create.user_email:
|
|
63
|
+
from backend.services.rate_limit_service import get_rate_limit_service
|
|
64
|
+
|
|
65
|
+
rate_limit_service = get_rate_limit_service()
|
|
66
|
+
allowed, remaining, message = rate_limit_service.check_user_job_limit(
|
|
67
|
+
user_email=job_create.user_email,
|
|
68
|
+
is_admin=is_admin
|
|
69
|
+
)
|
|
70
|
+
if not allowed:
|
|
71
|
+
from backend.services.rate_limit_service import _seconds_until_midnight_utc
|
|
72
|
+
|
|
73
|
+
# Get actual current count - remaining is clamped to 0 which loses info
|
|
74
|
+
current_count = rate_limit_service.get_user_job_count_today(job_create.user_email)
|
|
75
|
+
raise RateLimitExceededError(
|
|
76
|
+
message=message,
|
|
77
|
+
limit_type="jobs_per_day",
|
|
78
|
+
remaining_seconds=_seconds_until_midnight_utc(),
|
|
79
|
+
current_count=current_count,
|
|
80
|
+
limit_value=settings.rate_limit_jobs_per_day
|
|
81
|
+
)
|
|
82
|
+
|
|
54
83
|
# Enforce theme requirement - all jobs must have a theme
|
|
55
84
|
# This prevents unstyled videos from ever being generated
|
|
56
85
|
if not job_create.theme_id:
|
|
@@ -105,7 +134,17 @@ class JobManager:
|
|
|
105
134
|
|
|
106
135
|
self.firestore.create_job(job)
|
|
107
136
|
logger.info(f"Created new job {job_id} with status PENDING")
|
|
108
|
-
|
|
137
|
+
|
|
138
|
+
# Record job creation for rate limiting (after successful persistence)
|
|
139
|
+
if job_create.user_email:
|
|
140
|
+
try:
|
|
141
|
+
from backend.services.rate_limit_service import get_rate_limit_service
|
|
142
|
+
rate_limit_service = get_rate_limit_service()
|
|
143
|
+
rate_limit_service.record_job_creation(job_create.user_email, job_id)
|
|
144
|
+
except Exception as e:
|
|
145
|
+
# Don't fail job creation if rate limit recording fails
|
|
146
|
+
logger.warning(f"Failed to record job creation for rate limiting: {e}")
|
|
147
|
+
|
|
109
148
|
return job
|
|
110
149
|
|
|
111
150
|
def get_job(self, job_id: str) -> Optional[Job]:
|