notebooklm-py / export_from_chrome.py
ASEM12345's picture
Upload folder using huggingface_hub
aaee22d verified
"""
从 Chrome 导出 NotebookLM 认证信息
方法:使用 Chrome Remote Debugging 连接已登录的浏览器
步骤:
1. 关闭所有 Chrome 窗口
2. 用以下命令启动 Chrome(开启远程调试):
"C:\Program Files\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9222
3. 在 Chrome 中登录 https://notebooklm.google.com
4. 运行此脚本
"""
import asyncio
import json
import os
async def export_auth():
try:
from playwright.async_api import async_playwright
except ImportError:
print("请先安装: pip install playwright")
return
storage_path = os.path.expanduser("~/.notebooklm/storage_state.json")
os.makedirs(os.path.dirname(storage_path), exist_ok=True)
print("正在连接到 Chrome (端口 9222)...")
print("确保你已用以下命令启动 Chrome:")
print(' "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe" --remote-debugging-port=9222')
print()
async with async_playwright() as p:
try:
browser = await p.chromium.connect_over_cdp("http://127.0.0.1:9222")
print("✅ 已连接到 Chrome")
except Exception as e:
print(f"❌ 无法连接: {e}")
print("\n请确保:")
print("1. 关闭所有 Chrome 窗口")
print("2. 用远程调试模式启动 Chrome")
return
# 获取默认上下文
contexts = browser.contexts
if not contexts:
print("❌ 没有找到浏览器上下文")
return
context = contexts[0]
# 保存 storage state
await context.storage_state(path=storage_path)
print(f"\n✅ 认证信息已保存到: {storage_path}")
# 读取并显示
with open(storage_path, "r", encoding="utf-8") as f:
content = f.read()
print("\n" + "=" * 60)
print("复制以下内容作为 HF Space Secret (NOTEBOOKLM_AUTH_JSON):")
print("=" * 60)
print(content)
if __name__ == "__main__":
asyncio.run(export_auth())