feat(scripts): 添加 PEP 723 脚本元数据并修复 HTTP/2 伪头过滤问题
- 为 build_studio_urls.py 和 lookup_groups.py 添加 PEP 723 依赖声明 - 修复 HTTP/2 伪头导致 requests 抛 InvalidHeader 的问题 - 添加 CDP 端口连通性预检查及启动指引
This commit is contained in:
@@ -14,6 +14,8 @@ description: "把一批群组名批量解析为 entity_id(即 groupId),并
|
||||
- **建 URL 前置**:要生成 explore 报告 URL,但清单里只有群组名、没有 entity_id。
|
||||
- **排查失败**:回放遇到 401 / 查不到 / 大小写差异,需要定位修复。
|
||||
|
||||
> **流程位置**:本技能是 `yt-studio-url-builder` 的前置——url-builder 只认 `entity_id`(groupId);清单里只有群组名时必须先跑本技能把 ID 填进清单,再交给 url-builder。
|
||||
|
||||
## 领域词汇
|
||||
|
||||
- **套件(Bundle)**:从页面捕获的一次 `search_groups` 请求 = 完整请求头(`Authorization: SAPISIDHASH...`、`Cookie`、`X-YouTube-Delegation-Context` 等)+ 请求体模板(含 `context.user.delegationContext`)。一个内容所有者一份。手写必 401,唯一可靠来源。
|
||||
@@ -24,8 +26,8 @@ description: "把一批群组名批量解析为 entity_id(即 groupId),并
|
||||
|
||||
## 脚本与依赖
|
||||
|
||||
- 脚本:`scripts/lookup_groups.py`(本技能目录下;若缺失,用 SearchCodebase 按 `lookup_groups.py` 定位)。
|
||||
- 依赖:playwright、requests、openpyxl 已统一在根 `pyproject.toml` 声明(`uv sync` 自动装),环境准备见 `uv-env-setup` 技能。
|
||||
- 脚本:`scripts/lookup_groups.py`(本技能目录下;若缺失,用代码库全局搜索按 `lookup_groups.py` 定位)。
|
||||
- 依赖与环境:见 `uv-env-setup` 技能——项目环境走 `uv sync`;脱离项目时脚本头部带 PEP 723 声明,`uv run <脚本>` 自动备环境。
|
||||
- 登录态必须已存在:脚本不登录,只复用已登录 YouTube Studio 会话,否则跳 Google 登录页。
|
||||
|
||||
## 选择运行分支
|
||||
@@ -44,7 +46,7 @@ description: "把一批群组名批量解析为 entity_id(即 groupId),并
|
||||
### 步骤 0:环境与自测(分支 D)
|
||||
|
||||
```bash
|
||||
uv run python scripts\lookup_groups.py --selftest
|
||||
uv run python scripts/lookup_groups.py --selftest
|
||||
```
|
||||
|
||||
**完成判据**:打印 `selftest OK`,退出码 0(无需浏览器/网络)。
|
||||
@@ -54,7 +56,8 @@ uv run python scripts\lookup_groups.py --selftest
|
||||
复用已登录 YouTube Studio 的浏览器会话,否则跳 Google 登录页。与 `youtube-studio-csv-download` 同一套约定:
|
||||
|
||||
- 方式 A(能关浏览器):`--user-data-dir <目录> --channel chrome|msedge`,二者必须同品牌。
|
||||
- 方式 B(浏览器不能关):先 `chrome.exe --remote-debugging-port=9222` 或 `msedge.exe --remote-debugging-port=9222`,再加 `--connect http://localhost:9222`。
|
||||
- 方式 B(浏览器不能关):先 `chrome.exe --remote-debugging-port=9222` 或 `msedge.exe --remote-debugging-port=9222`,再加 `--connect http://localhost:9222`。脚本附加前会先探测端口;报 `CDP 端口不可达` 时按提示排查——最常见是**旧 Chrome/Edge 残留进程吞掉了调试端口参数**,需彻底退出后重启或改用独立 user-data-dir 启动。
|
||||
- 注意:cookie 存在不等于会话有效。profile 目录里看得到 SAPISID/SID 等 cookie 也可能仍跳登录页,此时由用户在浏览器手动登录一次再重跑。
|
||||
|
||||
**完成判据**:得到任意一种可复用的登录态(用户数据目录路径,或调试端口号)。
|
||||
|
||||
@@ -67,19 +70,19 @@ uv run python scripts\lookup_groups.py --selftest
|
||||
|
||||
### 步骤 3:运行脚本
|
||||
|
||||
分支 A(在线捕获 + 回放):
|
||||
分支 A(在线捕获 + 回放;路径写法统一正斜杠 + 引号,PowerShell / Git Bash / cmd 通用):
|
||||
|
||||
```powershell
|
||||
# 单所有者
|
||||
uv run python scripts\lookup_groups.py --url "<owner URL>" --names 名单.xlsx --channel chrome --user-data-dir "$env:LOCALAPPDATA\Google\Chrome\User Data"
|
||||
uv run python scripts/lookup_groups.py --url "<owner URL>" --names 名单.xlsx --channel chrome --user-data-dir "C:/Users/<you>/AppData/Local/Google/Chrome/User Data"
|
||||
# 多所有者:--url 重复;浏览器不能关就改用 --connect
|
||||
uv run python scripts\lookup_groups.py --url "<owner1 URL>" --url "<owner2 URL>" --names 名单.csv --connect http://localhost:9222 --save-bundles bundles.json
|
||||
uv run python scripts/lookup_groups.py --url "<owner1 URL>" --url "<owner2 URL>" --names 名单.csv --connect http://localhost:9222 --save-bundles bundles.json
|
||||
```
|
||||
|
||||
分支 B(离线回放,套件已存盘,无需浏览器):
|
||||
|
||||
```powershell
|
||||
uv run python scripts\lookup_groups.py --bundles bundles.json --names 名单.json --out result.xlsx
|
||||
uv run python scripts/lookup_groups.py --bundles bundles.json --names 名单.json --out result.xlsx
|
||||
```
|
||||
|
||||
分支 C(仅捕获/校验套件,暂不查询):
|
||||
@@ -90,8 +93,9 @@ uv run python scripts\lookup_groups.py --url "<owner URL>" --save-bundles bundle
|
||||
|
||||
**关键行为**:
|
||||
- 打开页面后先**自动触发一次群组搜索**(猜搜索框);猜不中会提示「在浏览器顶部搜索框输入任意词并回车」,只需触发一次 `search_groups` 请求即可。
|
||||
- 捕到请求后,脚本校验 `delegationContext.externalOwnerId` 与 URL ownerId 是否一致、鉴权头是否齐全(缺 Authorization/Cookie/delegation 是 401 根因,会在捕获时直接打警告)。
|
||||
- 捕到请求后,脚本校验 `delegationContext.externalOwnerId` 与 URL ownerId 是否一致、鉴权头是否齐全(缺 Authorization/Cookie/delegation 是 401 根因,会在捕获时直接打警告),并**自动过滤 HTTP/2 伪头**(`:authority` 等,流入 requests 会报 InvalidHeader)。
|
||||
- 回放用线程本地 Session,并发(默认 8 线程)逐个名字 × 逐个所有者,**首个精确命中即停**。
|
||||
- 套件是一次性凭据:鉴权头逐请求计算,回放前如间隔较久或全量 401,直接重新在线捕获更新 `bundles.json`。
|
||||
|
||||
**完成判据**:终端打印 `[输出] 精确命中 N/M -> <路径>`,退出码 0,且生成结果文件。
|
||||
|
||||
|
||||
Reference in New Issue
Block a user