Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

fix(skill): retain Pexels keys after transient validation failures

Rudy Celeklia5b625f12e5b
diff --git a/docs/skill/mpt_agent.py b/docs/skill/mpt_agent.py
index d775646b..121caf14 100644
--- a/docs/skill/mpt_agent.py
+++ b/docs/skill/mpt_agent.py
@@ -542,8 +542,8 @@ def _validate_pexels_key(api_key: str) -> str:
"""
Return ``valid``, ``rejected``, or ``unknown`` for a Pexels key.
- HTTP 401, 403, and rate-limited 429 responses make a key unusable for this
- run. Network and server errors return unknown so the configuration is kept.
+ HTTP 401 and 403 responses reject credentials. Rate limits, network and
+ server errors return unknown so temporary failures cannot remove a key.
"""
# Curated and popular search requests may hit a public cache and return 200
# without valid authorization. My Collections is account-specific, requires
@@ -559,7 +559,7 @@ def _validate_pexels_key(api_key: str) -> str:
with urllib.request.urlopen(request, timeout=15) as response:
return "valid" if 200 <= response.status < 300 else "unknown"
except urllib.error.HTTPError as exc:
- if exc.code in {401, 403, 429}:
+ if exc.code in {401, 403}:
return "rejected"
return "unknown"
except (TimeoutError, urllib.error.URLError):
@@ -572,7 +572,7 @@ def validate_pexels_config(config_path: Path, cli_args: list[str]) -> bool:
Downstream code selects configured keys randomly. Keeping rejected keys can
cause intermittent 401 responses and missing material results. If at least
- one key is verified, retain only verified keys. If validation is impossible
+ one key is verified, remove only explicitly rejected keys. If validation is impossible
because of a transient network failure, keep the original configuration.
"""
if selected_video_source(cli_args) != "pexels":
@@ -584,20 +584,23 @@ def validate_pexels_config(config_path: Path, cli_args: list[str]) -> bool:
return False
valid_keys: list[str] = []
+ retained_keys: list[str] = []
rejected_count = 0
unknown_count = 0
for api_key in keys:
status = _validate_pexels_key(api_key)
if status == "valid":
valid_keys.append(api_key)
+ retained_keys.append(api_key)
elif status == "rejected":
rejected_count += 1
else:
unknown_count += 1
+ retained_keys.append(api_key)
if valid_keys:
- if valid_keys != keys:
- text = _replace_config_value(text, "pexels_api_keys", valid_keys)
+ if retained_keys != keys:
+ text = _replace_config_value(text, "pexels_api_keys", retained_keys)
config_path.write_text(text, encoding="utf-8")
log(
"Pexels key validation completed: "
diff --git a/test/services/test_skill_transient_pexels_keys.py b/test/services/test_skill_transient_pexels_keys.py
new file mode 100644
index 00000000..bfb72b80
--- /dev/null
+++ b/test/services/test_skill_transient_pexels_keys.py
@@ -0,0 +1,25 @@
+import importlib.util
+import io
+import tomllib
+import urllib.error
+from pathlib import Path
+from unittest.mock import patch
+
+
+spec = importlib.util.spec_from_file_location("mpt_transient_keys_fixture", Path(__file__).parents[2] / "docs" / "skill" / "mpt_agent.py")
+helper = importlib.util.module_from_spec(spec)
+spec.loader.exec_module(helper)
+
+
+def test_rate_limited_key_is_not_classified_as_invalid_credentials():
+ response = urllib.error.HTTPError("https://fixture.invalid", 429, "Too Many Requests", {}, io.BytesIO())
+ with patch.object(helper.urllib.request, "urlopen", side_effect=response):
+ assert helper._validate_pexels_key("fixture") == "unknown"
+
+
+def test_transient_unknown_key_survives_a_successful_peer_validation(tmp_path):
+ config = tmp_path / "config.toml"
+ config.write_text('[app]\npexels_api_keys = ["unknown-fixture", "valid-fixture", "rejected-fixture"]\n', encoding="utf-8")
+ with patch.object(helper, "_validate_pexels_key", side_effect=["unknown", "valid", "rejected"]):
+ assert helper.validate_pexels_config(config, []) is True
+ assert tomllib.loads(config.read_text(encoding="utf-8"))["app"]["pexels_api_keys"] == ["unknown-fixture", "valid-fixture"]