- 新增 download_images.py: 批量下载消息图片到 downloads/images/ - 数据库移至 downloads/messages.db,SnifferStorage 默认使用项目目录 - 图片默认保存到 downloads/images/,支持 --out-dir 自定义 - .gitignore 排除 downloads/ 避免提交二进制文件 - 补充腾讯会议号过滤 prompt - 补充 Obsidian 文档 is_read frontmatter
188 lines
5.4 KiB
Python
188 lines
5.4 KiB
Python
import json
|
|
from types import SimpleNamespace
|
|
|
|
from snooper_summary import DEFAULT_MODEL, MagpieClient
|
|
from snooper_videos import (
|
|
_download_resumable,
|
|
enrich_videos,
|
|
frame_timestamps,
|
|
video_path,
|
|
)
|
|
|
|
|
|
class FakeResponse:
|
|
status_code = 200
|
|
|
|
def json(self):
|
|
return {
|
|
"model": DEFAULT_MODEL,
|
|
"choices": [{"message": {"content": "合成画面摘要"}}],
|
|
}
|
|
|
|
|
|
class FakeSession:
|
|
def __init__(self):
|
|
self.post_calls = []
|
|
|
|
def post(self, url, **kwargs):
|
|
self.post_calls.append((url, kwargs))
|
|
return FakeResponse()
|
|
|
|
|
|
class FakeAI:
|
|
model = DEFAULT_MODEL
|
|
|
|
def __init__(self):
|
|
self.calls = []
|
|
|
|
def complete_with_images(self, images, prompt):
|
|
self.calls.append((images, prompt))
|
|
return "合成视频画面"
|
|
|
|
|
|
def video_row(msg_id):
|
|
return SimpleNamespace(
|
|
msg_id=msg_id,
|
|
msg_type=15,
|
|
chat_id="chat-test",
|
|
content="[视频]",
|
|
content_data=json.dumps({
|
|
"name": "synthetic.mp4",
|
|
"duration": 120_000,
|
|
"key": "synthetic-key",
|
|
}),
|
|
)
|
|
|
|
|
|
def test_frame_timestamps_cover_entire_video_with_per_video_cap():
|
|
timestamps = frame_timestamps(1696.4, max_frames=12)
|
|
|
|
assert len(timestamps) == 12
|
|
assert timestamps[0] > 0
|
|
assert timestamps[-1] < 1696.4
|
|
assert timestamps[-1] - timestamps[0] > 1500
|
|
assert frame_timestamps(1, max_frames=12) == [0.5]
|
|
|
|
|
|
def test_video_path_uses_non_descriptive_stable_name(tmp_path):
|
|
assert video_path(video_row("message-a"), tmp_path).name == "message-a.mp4"
|
|
|
|
|
|
def test_download_resumes_partial_file(monkeypatch, tmp_path):
|
|
path = tmp_path / "video.part"
|
|
path.write_bytes(b"abc")
|
|
calls = []
|
|
|
|
class Response:
|
|
status_code = 206
|
|
|
|
def __init__(self):
|
|
self.headers = {"content-length": "3"}
|
|
|
|
def __enter__(self):
|
|
return self
|
|
|
|
def __exit__(self, *args):
|
|
return None
|
|
|
|
def raise_for_status(self):
|
|
return None
|
|
|
|
def iter_content(self, size):
|
|
yield b"def"
|
|
|
|
def fake_get(url, **kwargs):
|
|
calls.append(kwargs)
|
|
return Response()
|
|
|
|
monkeypatch.setattr("snooper_videos.requests.get", fake_get)
|
|
_download_resumable(
|
|
SimpleNamespace(cookies={}), "https://example.invalid", path, 1024
|
|
)
|
|
|
|
assert path.read_bytes() == b"abcdef"
|
|
assert calls[0]["headers"] == {"Range": "bytes=3-"}
|
|
|
|
|
|
def test_complete_with_images_sends_all_frames_to_approved_model():
|
|
session = FakeSession()
|
|
client = MagpieClient(DEFAULT_MODEL, session=session)
|
|
|
|
result = client.complete_with_images(
|
|
[(b"first", "image/jpeg"), (b"second", "image/jpeg")],
|
|
"合成测试提示",
|
|
)
|
|
|
|
assert result == "合成画面摘要"
|
|
content = session.post_calls[0][1]["json"]["messages"][1]["content"]
|
|
assert content[0] == {"type": "text", "text": "合成测试提示"}
|
|
assert len(content) == 3
|
|
assert all(item["type"] == "image_url" for item in content[1:])
|
|
|
|
|
|
def test_enrich_videos_processes_every_row_and_local_audio(monkeypatch, tmp_path):
|
|
rows = [video_row("message-a"), video_row("message-b")]
|
|
ai = FakeAI()
|
|
|
|
def fake_download(client, row, directory):
|
|
path = video_path(row, directory)
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_bytes(b"video")
|
|
return path, True
|
|
|
|
monkeypatch.setattr("snooper_videos.ensure_video_downloaded", fake_download)
|
|
monkeypatch.setattr(
|
|
"snooper_videos.extract_video_frames",
|
|
lambda path, duration, max_frames: [(1.0, b"frame-a"), (2.0, b"frame-b")],
|
|
)
|
|
monkeypatch.setattr("snooper_videos.video_has_audio", lambda path: True)
|
|
monkeypatch.setattr("snooper_videos.transcribe_video", lambda path, model: "合成音轨")
|
|
|
|
stats = enrich_videos(
|
|
SimpleNamespace(), ai, rows,
|
|
video_dir=tmp_path / "videos", analysis_dir=tmp_path / "analysis",
|
|
)
|
|
|
|
assert stats["candidates"] == 2
|
|
assert stats["enriched"] == 2
|
|
assert stats["failed"] == 0
|
|
assert stats["audio_transcribed"] == 2
|
|
assert len(ai.calls) == 2
|
|
assert len(ai.calls[0][0]) == 2
|
|
for row in rows:
|
|
assert "[视频画面] 合成视频画面" in row.content
|
|
assert "[视频音轨转写] 合成音轨" in row.content
|
|
|
|
|
|
def test_visual_analysis_is_cached_when_audio_needs_retry(monkeypatch, tmp_path):
|
|
row = video_row("message-a")
|
|
ai = FakeAI()
|
|
|
|
def fake_download(client, row, directory):
|
|
path = video_path(row, directory)
|
|
path.parent.mkdir(parents=True, exist_ok=True)
|
|
path.write_bytes(b"video")
|
|
return path, True
|
|
|
|
monkeypatch.setattr("snooper_videos.ensure_video_downloaded", fake_download)
|
|
monkeypatch.setattr(
|
|
"snooper_videos.extract_video_frames",
|
|
lambda path, duration, max_frames: [(1.0, b"frame")],
|
|
)
|
|
monkeypatch.setattr("snooper_videos.video_has_audio", lambda path: True)
|
|
|
|
def fail_transcription(path, model):
|
|
raise RuntimeError("synthetic transcription failure")
|
|
|
|
monkeypatch.setattr("snooper_videos.transcribe_video", fail_transcription)
|
|
kwargs = {
|
|
"video_dir": tmp_path / "videos", "analysis_dir": tmp_path / "analysis"
|
|
}
|
|
first = enrich_videos(SimpleNamespace(), ai, [row], **kwargs)
|
|
second = enrich_videos(SimpleNamespace(), ai, [row], **kwargs)
|
|
|
|
assert first["audio_failed"] == 1
|
|
assert second["audio_failed"] == 1
|
|
assert second["cached"] == 1
|
|
assert len(ai.calls) == 1
|