Skip to content

armgit / peach

Public Imported from GitHub
0

Peach: Local-first video metadata scraper and media manager.

fix(sourcing): 浏览器采集遵循 Peach 显式直连

longmeidao9002a0b04939
diff --git a/docs/SOURCING.md b/docs/SOURCING.md
index 1ae0ebfd0..fb5632f0b 100644
--- a/docs/SOURCING.md
+++ b/docs/SOURCING.md
@@ -22,6 +22,14 @@
| 厂牌日文名、官网、Logo、小圆标 | 厂牌名与厂牌标识、站点圆标与图标合成 |
| 订阅「最近出了哪些番号」 | 番号发现源(Feed) |
+## 采集连接与网盘读取
+
+「同步媒体库」与 `scripts/scrape_codes.py` 共用正式来源链。来源设为「直连」时使用直连客户端;设为跟随 Peach 代理时,使用配置页保存的直连、自定义代理或跟随环境设置。在线封面使用同一来源传输,amane 桥把代理及逐站直连设置传给子进程。浏览器取页也遵循显式直连和自定义代理;代理地址带用户名时退回 HTTP 客户端。
+
+跟随环境时,HTTP 客户端读取进程代理环境变量,浏览器使用系统代理;两者不保证采用相同出口。应用直连不能排除操作系统 TUN。部分独立维护脚本直接使用 `HttpxTransport` 或 `page_cache.Site`,只读取环境代理,未接入页面保存的 Peach 代理,不能据此声称所有脚本设置一致。
+
+网盘媒体的探测、九宫格、时间轴和播放会读取视频数据;挂载路径的网络连接由 CloudDrive2、OpenList 对应挂载工具负责。Peach 的目录「媒体读取策略」限制后台读取和确认边界,不修改挂载工具的代理。联网采集作品文字和封面使用上述来源设置,与视频所在网盘分别配置。
+
## 采集判据
所有来源共用两条底线:采集只出复核件;没有哪个来源说了算。
diff --git a/src/peach/media_configuration.py b/src/peach/media_configuration.py
index d4562a113..85405441c 100644
--- a/src/peach/media_configuration.py
+++ b/src/peach/media_configuration.py
@@ -64,6 +64,22 @@ def rows(config, *, windows: bool, probe: bool = False) -> list[dict[str, Any]]:
return result
+def _validated_paths(row: dict, *, windows: bool) -> tuple[PureWindowsPath, PurePath]:
+ """将表单路径转换为可比较的声明根与本机绝对路径。"""
+ text = str(row.get("path", "")).strip()
+ root = text if windows else str(row.get("root", "")).strip()
+ if any(ord(char) < 32 for char in text + root):
+ raise ValueError("路径不能包含控制字符")
+ if not platform.is_windows_path(root) or ".." in PureWindowsPath(root).parts:
+ raise ValueError("Windows 中的对应路径必须包含盘符,例如 B:\\;网络共享请先映射为 Windows 盘符")
+ path = PureWindowsPath(text) if windows else PurePosixPath(text)
+ if not text or not (platform.is_windows_path(text) if windows else text.startswith("/")):
+ raise ValueError("请填写本机挂载点的绝对路径")
+ if ".." in path.parts:
+ raise ValueError("挂载点不能包含上级目录 ..")
+ return PureWindowsPath(root), path
+
+
def validate(raws: object, *, windows: bool) -> tuple[dict, dict, list[str]]:
"""离线来源允许保存;绝对路径、盘符根与来源间重叠在保存前校验。"""
if not isinstance(raws, list) or not raws or len(raws) > 100:
@@ -81,18 +97,7 @@ def validate(raws: object, *, windows: bool) -> tuple[dict, dict, list[str]]:
raise ValueError("请选择列表中的媒体来源")
if "cost" in row and row["cost"] not in ("free", "metered"):
raise ValueError("请选择代理流量保护设置")
- text = str(row.get("path", "")).strip()
- root = text if windows else str(row.get("root", "")).strip()
- if any(ord(char) < 32 for char in text + root):
- raise ValueError("路径不能包含控制字符")
- if not platform.is_windows_path(root) or ".." in PureWindowsPath(root).parts:
- raise ValueError("Windows 中的对应路径必须包含盘符,例如 B:\\;网络共享请先映射为 Windows 盘符")
- path = PureWindowsPath(text) if windows else PurePosixPath(text)
- if not text or not (platform.is_windows_path(text) if windows else text.startswith("/")):
- raise ValueError("请填写本机挂载点的绝对路径")
- if ".." in path.parts:
- raise ValueError("挂载点不能包含上级目录 ..")
- declared = PureWindowsPath(root)
+ declared, path = _validated_paths(row, windows=windows)
for other_root, other_path in seen:
if (declared == other_root or declared.is_relative_to(other_root)
or other_root.is_relative_to(declared)):
diff --git a/src/peach/media_sources.py b/src/peach/media_sources.py
index 827e58831..3e0bc8c67 100644
--- a/src/peach/media_sources.py
+++ b/src/peach/media_sources.py
@@ -50,7 +50,7 @@ def _root_costs(config):
key=lambda row: len(_root_key(row[1])), reverse=True)
-def cost_for(location: str, path: str = "", config=None) -> str:
+def cost_for(location: str, path: str | None = "", config=None) -> str:
"""实际路径按最长声明根取保护设置;空路径聚合同来源的已配置根。"""
roots = [row for row in _root_costs(_config(config)) if row[0] == location]
if not path and roots:
diff --git a/src/peach/platform.py b/src/peach/platform.py
index c3fc56785..ef4b43885 100644
--- a/src/peach/platform.py
+++ b/src/peach/platform.py
@@ -4,7 +4,7 @@
`A:\\...`(PikPak)、`B:\\...`(115)。这是不变量,不随平台改写。
真正会变的是「同一个来源在这台机器上落在哪」。`asset.location` 本来就是挂载点 ID
-(`local` / `115` / `pikpak`),设置文件用 `[media.locations]` 声明每个 ID 的账本口径根
+(来源 ID 见 `media_sources.SOURCE_REGISTRY`),设置文件用 `[media.locations]` 声明每个 ID 的账本口径根
(一个来源可以有几个根:两块硬盘都算 `local`),用 `[media.mounts]` 按同样的顺序声明每个
根在本机的落点,翻译按「声明根前缀 → 本机挂载点」进行(ADR-0023 第 2 阶段勘误)。
Windows 上盘符本身就是挂载点,路径原样返回。
diff --git a/src/peach/scraping_access.py b/src/peach/scraping_access.py
index 0167d091b..85fc7babd 100644
--- a/src/peach/scraping_access.py
+++ b/src/peach/scraping_access.py
@@ -536,7 +536,9 @@ class SourceTransport:
"""这个来源用的本机浏览器(进程共用);本机没有可驱动的浏览器或代理带凭据时 None。"""
values = values_for(self.root, source)
direct = values.get("network") == "direct"
- proxy = "" if direct else peach_proxy.client_options(self.root).get("proxy", "")
+ options = {"trust_env": False} if direct else peach_proxy.client_options(self.root)
+ proxy = options.get("proxy", "")
+ direct = not options.get("trust_env", True) and not proxy
gate = SOURCES[source].get("browser_gate")
gates = {domain: gate for domain in SOURCES[source]["domains"]} if gate else {}
return browser_transport.shared(browser_profile(self.root), direct=direct, proxy=proxy, gates=gates)
diff --git a/src/peach/settings_file.py b/src/peach/settings_file.py
index 99e1a9d5c..ad5e2945d 100644
--- a/src/peach/settings_file.py
+++ b/src/peach/settings_file.py
@@ -53,7 +53,7 @@ DEFAULT_LOCATION_ROOTS: dict[str, tuple[str, ...]] = {
"pikpak": ("A:/",),
}
-#: `[media]` 下只有这两个子表,没有任何标量键。第一阶段的 `[media] R = '...'` 是盘符键,
+#: `[media]` 下按子表保存路径、媒体库与读取策略,没有标量键。第一阶段的 `[media] R = '...'` 是盘符键,
#: 键空间和 location ID 不同,静默接受只会让整台机器脱盘,所以要显式报升级提示。
_LOCATIONS_KEY = "locations"
_MOUNTS_KEY = "mounts"
diff --git a/tests/test_complexity_ratchet.py b/tests/test_complexity_ratchet.py
index 73a32a186..cf5f276a7 100644
--- a/tests/test_complexity_ratchet.py
+++ b/tests/test_complexity_ratchet.py
@@ -62,7 +62,6 @@ BASELINE: dict[str, int] = {
"src/peach/studio_icons.py:icon_row": 32,
"scripts/find_ads.py:find_candidates": 31,
"scripts/rehome_unknown_jav.py:build_plan": 31,
- "src/peach/media_configuration.py:validate": 30,
"src/peach/resource_identification.py:ingest_results": 30,
}
diff --git a/tests/test_scraping_access.py b/tests/test_scraping_access.py
index afe321ae8..78793053d 100644
--- a/tests/test_scraping_access.py
+++ b/tests/test_scraping_access.py
@@ -74,6 +74,19 @@ class ScrapingAccessTests(unittest.TestCase):
self.assertEqual(factory.call_count, 2)
self.assertEqual(fake.close.call_count, 2)
+ def test_browser_obeys_global_direct_proxy_and_environment_modes(self):
+ transport = SourceTransport(self.root)
+ self.addCleanup(transport.close)
+ for mode, direct, proxy in (("direct", True, ""),
+ ("proxy", False, "http://127.0.0.1:7890"),
+ ("environment", False, "")):
+ with self.subTest(mode=mode):
+ peach_proxy.save(self.root, {"mode": mode, "proxy": "http://127.0.0.1:7890"})
+ with patch("peach.browser_transport.shared") as shared:
+ transport._browser_for("javten")
+ self.assertEqual(shared.call_args.kwargs["direct"], direct)
+ self.assertEqual(shared.call_args.kwargs["proxy"], proxy)
+
def test_switching_networks_releases_the_previous_exit_cooldown(self):
from peach.scraping_access import pause_source, paused_until
pause_source(self.root, 'airav', refused=True)