Files
SakuraHub/tests/manual_x_smoke.py
2026-11-16 20:45:22 +08:00

71 lines
2.5 KiB
Python

from __future__ import annotations
import json
import os
from pathlib import Path
from plugins.x.plugin import XPlugin
URLS = {
"normal": "https://x.com/VMAX_IS_MY_Life/status/2079538532822179896",
"image": "https://x.com/kapipi_media/status/2069324792621420865",
"video": "https://x.com/moonlight_ruri/status/2079132957797646448",
}
def main() -> None:
plugin = XPlugin()
cookie_file = os.environ.get("SAKURA_X_COOKIE_FILE", "").strip()
if cookie_file:
plugin.config.cookie_file = cookie_file
cache_file = os.environ.get("SAKURA_X_SMOKE_CACHE_FILE", "").strip()
if cache_file:
plugin._cache_file = Path(cache_file)
plugin.config.headless = True
plugin.config.safety_mode = False
plugin.config.min_action_delay = 0.3
plugin.config.max_action_delay = 0.8
plugin.config.parse_cooldown_seconds = 0
plugin.config.wait_timeout = 30000
download_root = Path(os.environ.get("SAKURA_X_SMOKE_DOWNLOAD_ROOT", "runtime/x_smoke_downloads"))
do_download = os.environ.get("SAKURA_X_SMOKE_DOWNLOAD") == "1"
selected_kind = os.environ.get("SAKURA_X_SMOKE_KIND", "").strip().lower()
targets = URLS.items() if not selected_kind else [(selected_kind, URLS[selected_kind])]
for kind, url in targets:
try:
resources = plugin.parse(url)
downloaded: list[str] = []
if do_download and kind in {"image", "video"} and resources:
target = next(
(
resource
for resource in resources
if any(file.get("type") == kind for file in resource.files)
),
resources[0],
)
target.files = target.files[:1]
downloaded = [str(path) for path in plugin.download(target, download_root / kind)]
print(
json.dumps(
{
"kind": kind,
"ok": True,
"count": len(resources),
"file_types": [file.get("type") for resource in resources for file in resource.files],
"titles": [(resource.title or "")[:60] for resource in resources[:2]],
"downloaded": downloaded,
},
ensure_ascii=False,
)
)
except Exception as exc:
print(json.dumps({"kind": kind, "ok": False, "error": str(exc)[:500]}, ensure_ascii=False))
if __name__ == "__main__":
main()