diff --git a/backend/app/yt_long_db.py b/backend/app/yt_long_db.py index fe1a2b7..e0f5838 100644 --- a/backend/app/yt_long_db.py +++ b/backend/app/yt_long_db.py @@ -197,10 +197,17 @@ def mark_yt_long_api_limited(*, reason: str = "") -> None: def effective_yt_long_channel_daily_max(cfg: dict[str, Any] | None = None) -> int: - """Per-channel Beijing-day upload target: 5 normally, 4 when API-limited.""" + """Per-channel Beijing-day upload target from operator setting. + + When shared YouTube API quota is limited, soft-cap at YT_LONG_CHANNEL_LIMITED + (never raise above the saved default). + """ + base = yt_long_channel_daily_max(cfg) + if base <= 0: + return 0 if youtube_api_upload_limited(cfg): - return YT_LONG_CHANNEL_LIMITED - return YT_LONG_CHANNEL_DEFAULT + return min(base, YT_LONG_CHANNEL_LIMITED) + return base def count_oauth_yt_long_channels(*, enabled_only: bool = True) -> int: @@ -215,26 +222,19 @@ def count_oauth_yt_long_channels(*, enabled_only: bool = True) -> int: def reconcile_yt_long_upload_caps(cfg: dict[str, Any] | None = None) -> dict[str, Any]: - """Keep global + per-channel caps aligned: 5/ch normal, 4/ch when API limited.""" + """Align global upload cap with per-channel defaults. + + Respects operator-saved ``yt_long_channel_daily_max`` (and per-channel rows). + Does **not** reset the global default back to 5. When API-limited, only clamps + channel rows that exceed the effective per-channel cap. + """ from . import db settings = cfg if cfg is not None else db.get_settings_map() limited = youtube_api_upload_limited(settings) - # Persist the *normal* target at 5 so UI/settings match policy - try: - stored_ch = int(settings.get("yt_long_channel_daily_max") or 0) - except Exception: - stored_ch = 0 - if stored_ch != YT_LONG_CHANNEL_DEFAULT: - try: - db.update_settings_map({"yt_long_channel_daily_max": YT_LONG_CHANNEL_DEFAULT}) - settings = db.get_settings_map() - except Exception: - pass - - per = YT_LONG_CHANNEL_LIMITED if limited else YT_LONG_CHANNEL_DEFAULT + per = effective_yt_long_channel_daily_max(settings) n = count_oauth_yt_long_channels(enabled_only=True) - global_max = max(per, n * per) if n else YT_LONG_DAILY_DEFAULT + global_max = max(per, n * per) if n else max(per, YT_LONG_DAILY_DEFAULT) try: stored_global = int(settings.get("yt_long_daily_max") or 0) except Exception: @@ -247,19 +247,21 @@ def reconcile_yt_long_upload_caps(cfg: dict[str, Any] | None = None) -> dict[str pass touched = 0 - for ch in list_yt_long_channels(enabled=None): - if not str(ch.get("refresh_token") or "").strip(): - continue - try: - cur = int(ch.get("daily_max") or 0) - except Exception: - cur = 0 - if cur != per: + # Only clamp when API-limited — never force every channel back to a hardcoded 5. + if limited and per >= 0: + for ch in list_yt_long_channels(enabled=None): + if not str(ch.get("refresh_token") or "").strip(): + continue try: - update_yt_long_channel(str(ch.get("id") or ""), daily_max=per) - touched += 1 + cur = int(ch.get("daily_max") or 0) except Exception: - pass + cur = 0 + if cur > per: + try: + update_yt_long_channel(str(ch.get("id") or ""), daily_max=per) + touched += 1 + except Exception: + pass try: from .clip_cascade import ensure_source_covers_platforms diff --git a/backend/app/yt_long_pipeline.py b/backend/app/yt_long_pipeline.py index e6b309c..b2c8f55 100644 --- a/backend/app/yt_long_pipeline.py +++ b/backend/app/yt_long_pipeline.py @@ -419,8 +419,9 @@ class YtLongPipeline: "source_daily_max": yt_long_db.yt_long_source_daily_max(cfg), "upload_gap": yt_long_db.yt_long_upload_gap(day, cfg), "upload_used": yt_long_db.yt_long_upload_used(day), - "channel_daily_max": effective_ch, + "channel_daily_max": yt_long_db.yt_long_channel_daily_max(cfg), "channel_daily_max_normal": yt_long_db.YT_LONG_CHANNEL_DEFAULT, + "channel_daily_max_effective": effective_ch, "api_quota_limited": limited, "last_error": self.last_error, "stats": yt_long_db.yt_long_stats(day), diff --git a/backend/tests/test_quota_defaults.py b/backend/tests/test_quota_defaults.py index d2e1e57..abc0aec 100644 --- a/backend/tests/test_quota_defaults.py +++ b/backend/tests/test_quota_defaults.py @@ -29,3 +29,12 @@ def test_tiktok_quota_defaults_clamp(temp_db): out = db.set_tiktok_quota_defaults(daily_min=40, daily_max=1, apply_all=False) assert out["daily_min"] == 30 assert out["daily_max"] == 30 + + +def test_yt_long_reconcile_respects_saved_channel_daily_max(temp_db): + from app import yt_long_db + + db.update_settings_map({"yt_long_channel_daily_max": 9, "yt_long_quota_limited_day": ""}) + yt_long_db.reconcile_yt_long_upload_caps() + assert yt_long_db.yt_long_channel_daily_max() == 9 + assert yt_long_db.effective_yt_long_channel_daily_max() == 9