feat(monitor): 抖音多身份池 —— 目标按登录态串行分片
把 44 个博主打散给多个抖音号,降低**单个账号**每轮的请求量(约 35 个博主后开始
出现限流是已知现象)。**默认串行,不是并行**:同一个出口 IP 上并发多个 cookie,
平台视角是一台设备在不停换访客,比单账号高频更容易被判异常。并发数做成了设置项,
但要先给每个账号配独立出口 IP 再考虑调大。
新增:
* `monitor_identity` 表:label / source(stored|browser) / cookie / **user_agent** /
client_hints / status / enabled / 冷却字段。cookie 与 UA 必须成套存 —— a_bogus
把 UA 算进签名,空 UA 签出来的请求一定被挡。
* `api/monitor/identities.py`:可用性判定、解析、**Rendezvous Hashing 分片**
(加一个身份只迁移约 1/(n+1) 的目标)、健康状态与指数退避冷却。
* `douyin_api`:四个接口都接受显式 `identity`,走身份池时**不再回读浏览器**(否则
浏览器里的 cookie 会把指定账号顶掉);新增 `shared_fingerprint()` 给粘贴 cookie
的身份兜底 UA。
* `douyin_fetch.collect`:按身份分片执行,分片之间默认串行、可配切换间隔;落盘前
按 note_id 去重(并发时两个分片可能刷到同一件作品)。
* `runner`:池模式下加载身份、不足 `identity_min_available` 就快速失败(不产生请求),
结束后按各自分片结果回写健康。
* 设置项:identity_mode / identity_min_available / identity_switch_delay_sec /
identity_concurrency。
* API:GET/POST/PATCH/DELETE `/api/monitor/identities` + `/{id}/check`(真请求一次,
没有可探测博主时退化成格式检查并在 verified 里标 false)。
* 前端:设置页(仅抖音)新增「多身份(分片采集)」面板。
测试 9 条新增:分片确定性与覆盖、加身份只迁移一小部分、缺 UA/cookie 拒绝、
两次失败进冷却、成功清零、冷却中不可用、采集按身份分片。
This commit is contained in:
@@ -490,3 +490,81 @@ export function useCheckUpstream() {
|
||||
onError: (error: Error) => toast.error(`检查失败:${error.message}`),
|
||||
})
|
||||
}
|
||||
|
||||
// --- 身份池(抖音多登录态) ------------------------------------------------
|
||||
|
||||
export function useMonitorIdentities() {
|
||||
const platform = usePlatformParam()
|
||||
return useQuery({
|
||||
queryKey: ['monitorIdentities', platform],
|
||||
queryFn: async () => (await monitorApi.listIdentities(platform)).data.identities,
|
||||
// 状态会随着采集轮次变化,慢点刷就够了。
|
||||
refetchInterval: 15_000,
|
||||
})
|
||||
}
|
||||
|
||||
export function useCreateIdentity() {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (payload: {
|
||||
label: string
|
||||
cookie: string
|
||||
user_agent?: string
|
||||
platform?: string
|
||||
}) => monitorApi.createIdentity(payload),
|
||||
onSuccess: () => {
|
||||
toast.success('身份已添加')
|
||||
queryClient.invalidateQueries({ queryKey: ['monitorIdentities'] })
|
||||
},
|
||||
onError: (error: Error) => {
|
||||
// 后端会把「拿不到 UA」这类原因写在 detail 里,axios 默认会把它压成状态码。
|
||||
const detail = (error as { response?: { data?: { detail?: string } } })?.response
|
||||
?.data?.detail
|
||||
toast.error(`添加失败:${detail ?? error.message}`)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useUpdateIdentity() {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: ({
|
||||
id,
|
||||
payload,
|
||||
}: {
|
||||
id: number
|
||||
payload: Partial<{ label: string; cookie: string; user_agent: string; enabled: boolean }>
|
||||
}) => monitorApi.updateIdentity(id, payload),
|
||||
onSuccess: () => queryClient.invalidateQueries({ queryKey: ['monitorIdentities'] }),
|
||||
onError: (error: Error) => toast.error(`更新失败:${error.message}`),
|
||||
})
|
||||
}
|
||||
|
||||
export function useDeleteIdentity() {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (id: number) => monitorApi.deleteIdentity(id),
|
||||
onSuccess: () => {
|
||||
toast.success('身份已删除')
|
||||
queryClient.invalidateQueries({ queryKey: ['monitorIdentities'] })
|
||||
},
|
||||
onError: (error: Error) => toast.error(`删除失败:${error.message}`),
|
||||
})
|
||||
}
|
||||
|
||||
export function useCheckIdentity() {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (id: number) => monitorApi.checkIdentity(id),
|
||||
onSuccess: (response) => {
|
||||
const data = response.data
|
||||
if (data.ok) {
|
||||
toast.success(data.verified ? '验证通过' : '格式检查通过(没有可探测的博主)')
|
||||
} else {
|
||||
toast.error(`不可用:${data.error || '未知原因'}`)
|
||||
}
|
||||
queryClient.invalidateQueries({ queryKey: ['monitorIdentities'] })
|
||||
},
|
||||
onError: (error: Error) => toast.error(`验证失败:${error.message}`),
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user