Files
MediaCrawler/tests/test_cmd_arg_media.py
程序员阿江(Relakkes) 0ca7b29cf0 feat(media): 重构媒体下载,支持 xhs/dy/ks/bili/wb 五平台
旧实现只覆盖 4 个平台,且把整个文件读进内存、无重试与完整性校验,
代码按平台复制粘贴了 4 份。本次用统一下载器替换:

- 新增 media_downloader/:流式写入、Range 续传、指数退避重试、大小校验、
  路径穿越防护;B 站 DASH 音视频分轨下载后交由 ffmpeg 无损合流
- 新增 media_platform/<平台>/media.py:从平台原始响应提取媒体地址,
  与下载器解耦;快手首次接入下载能力
- 开关:config.ENABLE_GET_MEDIA 与 --get_media,并打通 API/WebUI;
  同时修正旧配置项 ENABLE_GET_MEIDAS 的拼写
- 落盘按帖子聚合:{SAVE_DATA_PATH 或 data}/{platform}/media/{内容ID}/
- B 站装好 ffmpeg 时走 DASH 最高画质,否则降级 mp4 直链(产物 video-durl.mp4,
  避免低清文件阻塞后续的高清路径)
- 删除 4 个 *_store_media.py、AbstractStoreImage/Video 及各 client 的媒体 GET 方法

媒体下载失败只记录日志,不中断爬取主流程。
2026-09-17 22:54:22 +08:00

74 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
# Copyright (c) 2025 relakkes@gmail.com
#
# This file is part of MediaCrawler project.
# Repository: https://github.com/NanmiCoder/MediaCrawler/blob/main/tests/test_cmd_arg_media.py
# GitHub: https://github.com/NanmiCoder
# Licensed under NON-COMMERCIAL LEARNING LICENSE 1.1
#
"""--get_media 命令行开关测试。
注意parse_cmd 会就地覆盖全局 config因此每个用例都用 monkeypatch 还原,
避免污染其他测试。
"""
from __future__ import annotations
import config
import pytest
from cmd_arg import parse_cmd
@pytest.fixture(autouse=True)
def _isolate_config(monkeypatch):
monkeypatch.setattr(config, "ENABLE_GET_MEDIA", False)
monkeypatch.setattr(config, "PLATFORM", "xhs")
monkeypatch.setattr(config, "CRAWLER_TYPE", "search")
yield
BASE_ARGS = ["--platform", "xhs", "--type", "detail", "--specified_id", "note-1"]
@pytest.mark.asyncio
async def test_get_media_true_enables_switch():
await parse_cmd([*BASE_ARGS, "--get_media", "true"])
assert config.ENABLE_GET_MEDIA is True
@pytest.mark.asyncio
async def test_get_media_false_keeps_switch_off():
await parse_cmd([*BASE_ARGS, "--get_media", "false"])
assert config.ENABLE_GET_MEDIA is False
@pytest.mark.asyncio
async def test_get_media_defaults_to_config_value(monkeypatch):
monkeypatch.setattr(config, "ENABLE_GET_MEDIA", True)
args = await parse_cmd(BASE_ARGS)
assert config.ENABLE_GET_MEDIA is True
assert args.get_media is True
@pytest.mark.parametrize(
("raw", "expected"),
[("yes", True), ("y", True), ("1", True), ("no", False), ("n", False), ("0", False)],
)
@pytest.mark.asyncio
async def test_get_media_accepts_boolean_aliases(raw, expected):
await parse_cmd([*BASE_ARGS, "--get_media", raw])
assert config.ENABLE_GET_MEDIA is expected
@pytest.mark.asyncio
async def test_get_media_returns_value_in_namespace():
args = await parse_cmd([*BASE_ARGS, "--get_media", "true"])
assert args.get_media is True