Files
StudioLift/skills/youtube-studio-csv-download/SKILL.md

74 lines
4.2 KiB
Markdown
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.

---
name: "youtube-studio-csv-download"
description: "Download YouTube Studio analytics CSV export (zip) via a Playwright script that reuses a logged-in browser user-data dir or debug port, auto-saving to the Downloads folder with (n) dedup. Use when the user wants to download/save YT Studio CSV, or supplies a user-data path / remote-debugging-port."
---
# 下载 YouTube Studio 分析 CSV(脚本版)
把 YouTube Studio 内容管理器「导出当前视图 → 逗号分隔值 (.csv)」的下载,改由 Playwright 拦截脚本完成:复用已登录的 Chrome/Edge 会话,自动保存 zip 到下载文件夹,重名自动追加 ` (n)` 后缀。
## 与 MCP 浏览器版技能的区别
- 本技能:用户**主动给了浏览器 user-data 目录或调试端口**、或明确要"跑脚本下载"时使用。
- `youtube-studio-analytics-export`:走 MCP `integrated_browser`、界面点击兜底,适合交互式/按 URL 直连。
两者产物相同(zip 内含 `表格数据.csv` / `图表数据.csv` / `总计.csv`),只选一条路径即可。
## 脚本与依赖
- 脚本:`scripts/youtube_export_download.py`(本技能目录下;若缺失,用代码库全局搜索按 `youtube_export_download.py` 定位)
- 依赖与环境:见 `yt-download-place` 技能——工作区内走 `uv sync`;脱离工作区时脚本头部带 PEP 723 声明,`uv run <脚本>` 自动备环境(复用系统 Chrome/Edge 时无需 `playwright install chromium`)。
- 登录态必须已存在:脚本不登录,只复用已登录会话。
## 步骤 1:确认登录态来源(二选一)
复用已登录 YouTube Studio 的浏览器会话,否则会跳到 Google 登录页。
- 方式 A(能关浏览器):用已登录的**用户数据目录**。
- 方式 B(浏览器不能关):用**调试端口**附加。
**完成判据**:得到下面两者之一——
- user-data 目录路径(Chrome:`<LOCALAPPDATA>\Google\Chrome\User Data`;Edge:`<LOCALAPPDATA>\Microsoft\Edge\User Data`),或
- 调试端口号(如 `9222`)。
## 步骤 2:运行脚本
路径写法用正斜杠 + 引号,PowerShell / Git Bash / cmd 通用(`C:/Users/<you>/...` 即 `$env:LOCALAPPDATA` 指向的位置)。
方式 A:
```powershell
uv run python scripts/youtube_export_download.py --url "<explore URL>" --channel chrome --user-data-dir "C:/Users/<you>/AppData/Local/Google/Chrome/User Data"
```
方式 B(先 `chrome.exe --remote-debugging-port=9222` 或 `msedge.exe --remote-debugging-port=9222` 打开已登录浏览器):
```powershell
uv run python scripts/youtube_export_download.py --url "<explore URL>" --connect http://localhost:9222
```
- 脱离项目环境时省略 `python <脚本>` 前缀,直接 `uv run "<技能目录>/scripts/youtube_export_download.py" ...`(单脚本模式)。
- 默认下载到 `D:\Downloads`;运行在沙箱/受限环境的 agent(如部分 IDE 内置终端、云环境)看不到该盘符,**必须传 `--download-dir` 指向工作区内已存在的目录**。
- 脚本会自动重试一次:未捕获到有效导出响应时再点一遍导出按钮。
- `--channel` 只允许 `chrome` / `msedge`,且必须与 `--user-data-dir` 指向的浏览器**同一品牌**。
**完成判据**:终端打印 `已保存: <完整路径> (<字节数>)`,退出码 0。
## 步骤 3:校验产物
列出下载目录最新的 zip(按所用 shell 选一条):
```powershell
Get-ChildItem -Path "<download-dir>" -Filter "*.zip" | Sort-Object LastWriteTime -Descending | Select-Object -First 3 Name, Length, LastWriteTime
```
```bash
ls -lt "<download-dir>"/*.zip | head -3
```
**完成判据**:存在最新 `<维度标签> <起始日>_<结束日> <账号名>.zip`(如 `内容 2026-07-23_2026-08-20 WL Media.zip`);同名重复时后缀为 ` (1)`、` (2)`。脚本落盘前已做 zip 完整性校验,能写盘即可解压。
## 遇到问题
查 `references/troubleshooting.md`(未登录跳转、目录被占用、CDP 连不上、channel 不匹配、未捕获响应、文件名回退等)。
> **批量场景**:多份 URL 循环下载时,由调用方外层逐条调用本脚本并复用同一登录态,脚本保持单 URL 语义;每条失败可独立重跑。