移植自 mac-agent-os 的 mediacrawler_adapter:不起子进程、不开页面,用浏览器里那份
登录态直接调抖音 Web 接口。产物键名照抄 store/douyin,所以 ingest 那条链路一个字不用改。
**目前能用的(真环境实测,非推断)**:
profile/other : 200, 7075 字节 —— 博主主页指标(粉丝/获赞/作品数/昵称)
aweme/detail : 200, 45425 字节 —— 单条作品详情(含点赞/评论/收藏/分享)
**目前不能用的:作品列表 `aweme/post`。** 两个互相独立的原因:
1. 这个接口被抖音单独升级成了真校验:不带 x-tt-argus 回 403「Uifid Not Found」,
带上 dummy 值回 200 + **空 body**。也就是说「头在不在」骗得过,「真校验」过不了。
同一套头打 profile/other 和 aweme/detail 都是通的 —— 抖音是挑着接口加保护的,
挑中的恰好是「批量拉作品列表」这个最敏感的动作。
2. 改走页面截获也不行:CDP 浏览器打开博主主页会落到「验证码中间页」(当天大量探测的
代价,过几小时要重测)。
所以现在的边界是:**已知作品的指标刷新能做,自动发现新作品做不了**。
**排查中控住变量后得到的两条事实**(都写进注释了):
· `Accept` / `Accept-Language` / `Referer` 才是主页接口能返回真数据的原因 —— 只有
UA+client hints+Cookie 时是 200 但仅 121 字节的空壳,补上这三个头变 7074 字节。
(我先前猜的 sec-ch-ua 不是关键。)
· 因此 UA 与 client hints 必须**成套地取自同一个浏览器**,所以 BrowserIdentity 一次
从 CDP 取齐 cookie + UA + hints,而不是各自写死。
「200 + 空 body 必须当场报错」也是刻意写死的:放过去它会在下游变成「这个博主没作品」,
把一次失败伪装成一条正常结果 —— 爬虫那条路正是这么栽的,还被翻译成「账号被封」。
测试 +11:cookie 解析、请求头成套性(含 uifid 缺失/回退)、产物键名与 store 对齐、
以及 _get 的三条失败路径(空 body / 403 带网关原话 / 正常返回)。
137 lines
4.4 KiB
Python
137 lines
4.4 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
身份工厂 — 创建新身份(生成固化指纹 + 目录 + 配置)
|
|
|
|
用法:
|
|
python create_identity.py <identity_name> [--platform douyin]
|
|
|
|
示例:
|
|
python create_identity.py douyin_01
|
|
python create_identity.py douyin_02 --platform douyin
|
|
|
|
目录结构:
|
|
identities/{name}/
|
|
├── config.yaml ← 身份配置(可编辑、可版本控制)
|
|
├── fingerprint.pkl ← BrowserForge 固化指纹(不可编辑)
|
|
└── user_data/ ← Camoufox 持久化 Profile(自动生成,不提交)
|
|
"""
|
|
import argparse
|
|
import os
|
|
import pickle
|
|
import sys
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
try:
|
|
import yaml
|
|
except ImportError:
|
|
print("❌ 需要 PyYAML: pip install pyyaml")
|
|
sys.exit(1)
|
|
|
|
try:
|
|
from browserforge.fingerprints import FingerprintGenerator
|
|
except ImportError:
|
|
print("❌ 需要 browserforge: pip install browserforge")
|
|
sys.exit(1)
|
|
|
|
|
|
# ─── 路径配置 ───
|
|
|
|
SCRIPTS_DIR = Path(__file__).parent
|
|
TOOL_DIR = SCRIPTS_DIR.parent
|
|
# identities 目录放在 agent-local/tools/matrix/identities/
|
|
from matrix_mgmt import AGENT_LOCAL
|
|
IDENTITIES_ROOT = AGENT_LOCAL / "tools" / "matrix" / "identities"
|
|
|
|
|
|
def create_identity(name: str, platform: str = "douyin", proxy: str = None):
|
|
"""创建新的虚拟身份"""
|
|
|
|
identity_dir = IDENTITIES_ROOT / name
|
|
|
|
# 检查是否已存在
|
|
if identity_dir.exists():
|
|
print(f"⚠️ 身份 '{name}' 已存在: {identity_dir}")
|
|
confirm = input(" 覆盖? (y/N): ").strip().lower()
|
|
if confirm != 'y':
|
|
print("❌ 已取消")
|
|
return False
|
|
import shutil
|
|
shutil.rmtree(identity_dir)
|
|
|
|
# 创建目录
|
|
os.makedirs(identity_dir, exist_ok=True)
|
|
user_data_dir = identity_dir / "user_data"
|
|
os.makedirs(user_data_dir, exist_ok=True)
|
|
|
|
# ── 生成 BrowserForge 指纹 ──
|
|
print(f"\n🔄 生成指纹 (Windows/Firefox)...")
|
|
fg = FingerprintGenerator(browser='firefox', os=('windows',))
|
|
fp = fg.generate()
|
|
|
|
# 保存 pickle(供 Camoufox 加载)
|
|
fp_path = identity_dir / "fingerprint.pkl"
|
|
with open(fp_path, 'wb') as f:
|
|
pickle.dump(fp, f)
|
|
|
|
# ── 生成可读的 config.yaml ──
|
|
config = {
|
|
'identity': {
|
|
'name': name,
|
|
'platform': platform,
|
|
'proxy': proxy,
|
|
'notes': '',
|
|
'created_at': datetime.now().isoformat(),
|
|
'fingerprint_file': 'fingerprint.pkl',
|
|
},
|
|
'window': [702, 783], # 窗口尺寸(可手动修改)
|
|
'fingerprint_summary': {
|
|
'userAgent': fp.headers['User-Agent'],
|
|
'platform': fp.navigator.platform,
|
|
'screen': f"{fp.screen.width}x{fp.screen.height}",
|
|
'hardwareConcurrency': fp.navigator.hardwareConcurrency,
|
|
'fonts_count': len(fp.fonts),
|
|
'videoCard': fp.videoCard.renderer[:60] if fp.videoCard else 'N/A',
|
|
},
|
|
}
|
|
|
|
config_path = identity_dir / "config.yaml"
|
|
with open(config_path, 'w', encoding='utf-8') as f:
|
|
yaml.dump(config, f, default_flow_style=False, allow_unicode=True)
|
|
|
|
# ── 输出摘要 ──
|
|
print(f"\n{'='*55}")
|
|
print(f" ✅ 新身份已创建: {name}")
|
|
print(f"{'='*55}")
|
|
print(f" 路径: {identity_dir}")
|
|
print(f" User-Agent: {config['fingerprint_summary']['userAgent'][:70]}")
|
|
print(f" 平台: {config['fingerprint_summary']['platform']}")
|
|
print(f" 屏幕: {config['fingerprint_summary']['screen']}")
|
|
print(f" 核心数: {config['fingerprint_summary']['hardwareConcurrency']}")
|
|
print(f" 字体数量: {config['fingerprint_summary']['fonts_count']}")
|
|
print(f" GPU: {config['fingerprint_summary']['videoCard']}")
|
|
print()
|
|
print(f" 📋 下一步: 运行登录脚本")
|
|
print(f" python login_identity.py {name}")
|
|
print()
|
|
|
|
return True
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="身份工厂 — 创建新虚拟身份")
|
|
parser.add_argument("name", help="身份名称 (如 douyin_01)")
|
|
parser.add_argument("--platform", "-p", default="douyin", help="目标平台 (默认 douyin)")
|
|
parser.add_argument("--proxy", help="可选代理地址 (如 socks5://127.0.0.1:1080)")
|
|
args = parser.parse_args()
|
|
|
|
create_identity(
|
|
name=args.name,
|
|
platform=args.platform,
|
|
proxy=args.proxy,
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|