Skip to content

Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.

fix: reintegrate Bilibili and Douyin download support

Leggettb151acbd8472
diff --git a/apps/daemon/runtime/yt-dlp/opencreator/yt_dlp_plugins/extractor/bilibili.py b/apps/daemon/runtime/yt-dlp/opencreator/yt_dlp_plugins/extractor/bilibili.py
new file mode 100644
index 00000000..d13cfa9c
--- /dev/null
+++ b/apps/daemon/runtime/yt-dlp/opencreator/yt_dlp_plugins/extractor/bilibili.py
@@ -0,0 +1,42 @@
+from yt_dlp.extractor.bilibili import BiliBiliIE as UpstreamBiliBiliIE
+from yt_dlp.utils import ExtractorError, traverse_obj, url_or_none
+
+
+def has_media_formats(play_info):
+ return any(
+ traverse_obj(
+ play_info, (*path, ("baseUrl", "base_url", "url"), {url_or_none}), default=[]
+ )
+ for path in (
+ ("dash", ("audio", "video"), ..., {dict}),
+ ("dash", "dolby", "audio", ..., {dict}),
+ ("dash", "flac", "audio", {dict}),
+ ("durl", ..., {dict}),
+ )
+ )
+
+
+class BiliBiliIE(UpstreamBiliBiliIE, plugin_name="opencreator"):
+ def _download_playinfo(self, *args, **kwargs):
+ play_info = super()._download_playinfo(*args, **kwargs)
+ if has_media_formats(play_info):
+ return play_info
+ if isinstance(play_info, dict) and play_info.get("v_voucher"):
+ self._opencreator_risk_control = True
+ self.report_warning(
+ "Bilibili risk control rejected the playback API request; "
+ "retaining webpage media formats when available"
+ )
+ # A nonempty challenge dictionary must not replace valid webpage media.
+ return None
+
+ def _real_extract(self, url):
+ self._opencreator_risk_control = False
+ result = super()._real_extract(url)
+ if self._opencreator_risk_control and result.get("formats") == []:
+ raise ExtractorError(
+ "Bilibili risk control rejected the media request. "
+ "Wait and retry, or use a local video file.",
+ expected=True,
+ )
+ return result
diff --git a/apps/daemon/runtime/yt-dlp/opencreator/yt_dlp_plugins/extractor/douyin.py b/apps/daemon/runtime/yt-dlp/opencreator/yt_dlp_plugins/extractor/douyin.py
new file mode 100644
index 00000000..a4571cbf
--- /dev/null
+++ b/apps/daemon/runtime/yt-dlp/opencreator/yt_dlp_plugins/extractor/douyin.py
@@ -0,0 +1,56 @@
+from hashlib import md5
+from urllib.parse import urlencode, urlsplit
+
+from yt_dlp.extractor.tiktok import DouyinIE as UpstreamDouyinIE
+from yt_dlp.networking.exceptions import HTTPError
+from yt_dlp.utils import ExtractorError
+
+
+class DouyinIE(UpstreamDouyinIE, plugin_name="opencreator"):
+ def _download_json(self, url_or_request, video_id, *args, **kwargs):
+ parsed = urlsplit(url_or_request) if isinstance(url_or_request, str) else None
+ if not parsed or parsed.hostname != "www.douyin.com" or parsed.path != "/aweme/v1/web/aweme/detail/":
+ return super()._download_json(url_or_request, video_id, *args, **kwargs)
+
+ headers = dict(kwargs.get("headers") or {})
+ query = dict(kwargs.get("query") or {})
+ cookies = self._get_cookies(self._WEBPAGE_HOST)
+ identity_cookie = cookies.get("UIFID") or cookies.get("UIFID_TEMP")
+ identity = query.get("uifid") or (identity_cookie.value if identity_cookie else None)
+ if identity:
+ headers["uifid"] = identity
+ query["uifid"] = identity
+ if not query.get("x-secsdk-web-signature"):
+ # URLSearchParams encoding and checksum documented in yt-dlp issue #9667:
+ # https://github.com/yt-dlp/yt-dlp/issues/9667#issuecomment-6113069189
+ encoded_query = urlencode(query, doseq=True, safe="*").replace("~", "%7E")
+ payload = f"{identity}_{query.get('timestamp', '')}_A96D855A08C0A9707F8BEF0D9A527E4E_{encoded_query}"
+ query["x-secsdk-web-signature"] = md5(payload.encode(), usedforsecurity=False).hexdigest()
+ fatal = kwargs.get("fatal", True)
+ # The upstream nonfatal request discards the response body and labels every
+ # rejection as stale cookies. Read only the bounded, known rejection reason.
+ try:
+ return super()._download_json(url_or_request, video_id, *args, **{
+ **kwargs, "headers": headers, "query": query, "fatal": True,
+ })
+ except ExtractorError as error:
+ cause = error.cause
+ if isinstance(cause, HTTPError) and cause.status == 403:
+ body = cause.response.read(512)
+ if b"Blocked by ArgusSecurityPlugin Signature Not Found" in body:
+ raise ExtractorError(
+ "Douyin request signature is required (HTTP Error 403; Signature Not Found). "
+ "The current downloader is incompatible with this platform verification; "
+ "reconnecting cookies alone cannot fix it. Use a local video file.",
+ expected=True,
+ ) from error
+ if b"Blocked by ArgusSecurityPlugin Uifid Not Found" in body:
+ raise ExtractorError(
+ "Douyin browser identity was rejected (HTTP Error 403; Uifid Not Found). "
+ "Connect Douyin in Settings > Platform connections and retry.",
+ expected=True,
+ ) from error
+ if fatal:
+ raise
+ self.report_warning(str(error))
+ return None
diff --git a/apps/daemon/src/api/routes.platform-connections.ts b/apps/daemon/src/api/routes.platform-connections.ts
index 0268ec4f..50a941f8 100644
--- a/apps/daemon/src/api/routes.platform-connections.ts
+++ b/apps/daemon/src/api/routes.platform-connections.ts
@@ -1,28 +1,32 @@
import type { FastifyInstance } from 'fastify';
-import type { YouTubeConnectionService } from '../platform-connections/youtube.js';
+import type { PlatformConnectionService } from '../platform-connections/service.js';
import { apiError } from './errors.js';
-export async function registerPlatformConnectionRoutes(server: FastifyInstance, youtube: YouTubeConnectionService) {
+export async function registerPlatformConnectionRoutes(server: FastifyInstance, youtube: PlatformConnectionService, douyin?: PlatformConnectionService) {
server.addHook('onSend', async (request, reply, payload) => {
if (request.url.startsWith('/platform-connections/')) reply.header('Cache-Control', 'no-store');
return payload;
});
- server.get('/platform-connections/youtube', () => youtube.getState());
- server.post<{ Body: { profileId?: unknown } }>('/platform-connections/youtube/connect', (request, reply) => {
- const profileId = request.body?.profileId;
- if (profileId !== undefined && (typeof profileId !== 'string' || !/^[a-f0-9]{24}$/.test(profileId))) {
- return reply.code(400).send(apiError('VALIDATION_FAILED', '请选择有效的浏览器账号。'));
- }
- return youtube.connect(profileId as string | undefined);
- });
server.post('/platform-connections/youtube/complete-login', () => youtube.completeLogin());
- server.post('/platform-connections/youtube/cancel', () => youtube.cancel());
- server.delete('/platform-connections/youtube', () => youtube.disconnect());
- server.post<{ Body: { cookies?: unknown } }>('/platform-connections/youtube/import', async (request, reply) => {
- if (typeof request.body?.cookies !== 'string' || Buffer.byteLength(request.body.cookies) > 1024 * 1024) {
- return reply.code(400).send(apiError('VALIDATION_FAILED', '请选择有效的 Cookie 文件。'));
- }
- try { return await youtube.importCookies(request.body.cookies); }
- catch { return reply.code(400).send(apiError('VALIDATION_FAILED', '文件中没有有效的 YouTube 登录态,请检查文件或重新连接。')); }
- });
+ for (const [platform, connection] of [['youtube', youtube], ['douyin', douyin]] as const) {
+ if (connection === undefined) continue;
+ const path = `/platform-connections/${platform}`;
+ server.get(path, () => connection.getState());
+ server.post<{ Body: { profileId?: unknown } }>(`${path}/connect`, (request, reply) => {
+ const profileId = request.body?.profileId;
+ if (profileId !== undefined && (typeof profileId !== 'string' || !/^[a-f0-9]{24}$/.test(profileId))) {
+ return reply.code(400).send(apiError('VALIDATION_FAILED', '请选择有效的浏览器账号。'));
+ }
+ return connection.connect(profileId as string | undefined);
+ });
+ server.post(`${path}/cancel`, () => connection.cancel());
+ server.delete(path, () => connection.disconnect());
+ server.post<{ Body: { cookies?: unknown } }>(`${path}/import`, async (request, reply) => {
+ if (typeof request.body?.cookies !== 'string' || Buffer.byteLength(request.body.cookies) > 1024 * 1024) {
+ return reply.code(400).send(apiError('VALIDATION_FAILED', '请选择有效的 Cookie 文件。'));
+ }
+ try { return await connection.importCookies(request.body.cookies); }
+ catch { return reply.code(400).send(apiError('VALIDATION_FAILED', `文件中没有有效的${platform === 'youtube' ? 'YouTube' : '抖音'}会话,请检查文件或重新连接。`)); }
+ });
+ }
}
diff --git a/apps/daemon/src/api/server.ts b/apps/daemon/src/api/server.ts
index 9fd839bc..ddc3bf8e 100644
--- a/apps/daemon/src/api/server.ts
+++ b/apps/daemon/src/api/server.ts
@@ -264,7 +264,8 @@ import { registerTaskRoutes } from './routes.tasks.js';
import { registerThreadRoutes } from './routes.threads.js';
import { registerWorkspaceFileRoutes } from './routes.workspace-files.js';
import { registerVideoMetadataRoutes } from './routes.video-metadata.js';
-import { YouTubeConnectionService } from '../platform-connections/youtube.js';
+import { PlatformConnectionService } from '../platform-connections/service.js';
+import { supportedVideoSourcePlatform } from '@opencreator/protocol';
import { registerPlatformConnectionRoutes } from './routes.platform-connections.js';
export type BuildServerInput = {
@@ -689,18 +690,25 @@ export async function buildServer(input: BuildServerInput) {
gatewayCreatorSource = createGatewayCreatorSource({ account: gatewayAccount, manual: manualCreatorServicesConfigStore, getLocalOrigin: getAgentToolBaseUrl, readJobBinding: creatorRepository.getServiceBinding, readJobState: id => creatorRepository.getJob(id)?.state, readStageRun: creatorRepository.getStageRun, ledger: creatorProviderRequestLedger, directRequestsRoot: join(dataDir, 'gateway', 'requests') });
const creatorServicesConfigStore = gatewayCreatorSource.store;
if (stickmanContentRuntimeManager) stickmanContentRuntimeManager = createRuntimeSource(stickmanContentRuntimeManager, gatewayAccount.peekState, () => officialManagers.get('stickman'));
- let getCreatorYtDlpRuntime: (() => ReturnType<typeof resolveYtDlpRuntime>) | undefined;
- const youtubeConnection = new YouTubeConnectionService({
+ let getCreatorYtDlpRuntime: ((sourceUrl?: string) => ReturnType<typeof resolveYtDlpRuntime>) | undefined;
+ const youtubeConnection = new PlatformConnectionService({
root: join(dataDir, 'platform-connections', 'youtube'),
getRuntime: () => getCreatorYtDlpRuntime?.()
});
+ const douyinConnection = new PlatformConnectionService({
+ platform: 'douyin',
+ root: join(dataDir, 'platform-connections', 'douyin'),
+ getRuntime: () => getCreatorYtDlpRuntime?.()
+ });
await youtubeConnection.initialize();
- await registerPlatformConnectionRoutes(server, youtubeConnection);
+ await douyinConnection.initialize();
+ await registerPlatformConnectionRoutes(server, youtubeConnection, douyinConnection);
const videoMetadataService = input.videoMetadataService ?? createVideoMetadataService({
getProxy: async () => (await readCommonProxy()).trim(),
- getYtDlpRuntime: () => getCreatorYtDlpRuntime?.(),
+ getYtDlpRuntime: sourceUrl => getCreatorYtDlpRuntime?.(sourceUrl),
isYouTubeConnected: () => youtubeConnection.hasAuthentication(),
- withYouTubeCookies: operation => youtubeConnection.withCookies(operation)
+ withYouTubeCookies: operation => youtubeConnection.withCookies(operation),
+ withDouyinCookies: operation => douyinConnection.withCookies(operation)
});
const creatorService = input.creatorService ?? createCreatorService({
jobsRoot: creatorJobsRoot,
@@ -824,7 +832,7 @@ export async function buildServer(input: BuildServerInput) {
let creatorFfmpegPath: string | undefined;
let creatorFfprobePath: string | undefined;
let creatorYtDlpUpdateManager = input.creatorYtDlpUpdateManager;
- let getYtDlpRuntime: (() => ReturnType<typeof resolveYtDlpRuntime>) | undefined;
+ let getYtDlpRuntime: ((sourceUrl?: string) => ReturnType<typeof resolveYtDlpRuntime>) | undefined;
try {
const runtimeManifest = readKrillinRuntimeManifest(creatorRuntimeRoot);
let runtimeVerification: ReturnType<typeof startKrillinRuntimeVerification> | undefined;
@@ -879,9 +887,8 @@ export async function buildServer(input: BuildServerInput) {
console.warn(`yt-dlp updater is unavailable: ${formatError(error)}`);
}
}
- getYtDlpRuntime = () => youtubeConnection.authenticatedRuntime(
- creatorYtDlpUpdateManager?.getRuntime() ?? ytDlp
- );
+ getYtDlpRuntime = sourceUrl => (sourceUrl && supportedVideoSourcePlatform(sourceUrl) === 'douyin'
+ ? douyinConnection : youtubeConnection).authenticatedRuntime(creatorYtDlpUpdateManager?.getRuntime() ?? ytDlp);
getCreatorYtDlpRuntime = getYtDlpRuntime;
if (input.creatorExecutors === undefined) {
creatorExecutors.push(createKrillinExecutor({
@@ -905,7 +912,7 @@ export async function buildServer(input: BuildServerInput) {
ytDlpPath: ytDlp.executable,
ytDlpPrefixArgs: ytDlp.prefixArgs,
ytDlpEnv: ytDlp.env,
- getYtDlpRuntime: () => getYtDlpRuntime!()!,
+ getYtDlpRuntime: sourceUrl => getYtDlpRuntime!(sourceUrl)!,
ffmpegPath: creatorFfmpegPath,
ffprobePath: creatorFfprobePath
}));
@@ -1065,7 +1072,7 @@ export async function buildServer(input: BuildServerInput) {
id: executor.id,
run: stage => gatewayCreatorSource!.runForJob(stage.job.id, stage.stageRun.id, () =>
['download', 'krillinai', 'cover-analysis', 'wechat-article'].includes(executor.id)
- ? youtubeConnection.withCookies(() => executor.run(stage))
+ ? youtubeConnection.withCookies(() => douyinConnection.withCookies(() => executor.run(stage)))
: executor.run(stage))
})),
async beforeRun(job, stageId) {
@@ -1414,6 +1421,7 @@ export async function buildServer(input: BuildServerInput) {
server.addHook('onClose', async () => {
await youtubeConnection.close();
+ await douyinConnection.close();
await gatewayAccount.close();
let firstError: unknown;
const capture = async (operation: () => void | Promise<void>) => {
diff --git a/apps/daemon/src/creator/download/executor.ts b/apps/daemon/src/creator/download/executor.ts
index 45ba67e7..cf2e5c7e 100644
--- a/apps/daemon/src/creator/download/executor.ts
+++ b/apps/daemon/src/creator/download/executor.ts
@@ -8,7 +8,7 @@ import type {
DownloadOption,
DownloadProbe
} from '@opencreator/protocol';
-import { extractDouyinShareUrl, normalizeVideoSourceUrl, supportedVideoSourcePlatform, parseBilibiliVideoSource, videoSourceIdentity } from '@opencreator/protocol';
+import { extractDouyinShareUrl, normalizeVideoSourceUrl, supportedVideoSourcePlatform, parseBilibiliVideoSource, videoSourceIdentity, sanitizePublicErrorFacts } from '@opencreator/protocol';
import { creatorResultSnapshotForVersion } from '../result-snapshots.js';
import type {
CreatorExecutor,
@@ -29,7 +29,7 @@ type DownloadExecutorOptions = {
ytDlpPath: string;
ytDlpPrefixArgs?: string[];
ytDlpEnv?: NodeJS.ProcessEnv;
- getYtDlpRuntime?(): YtDlpRuntime;
+ getYtDlpRuntime?(sourceUrl?: string): YtDlpRuntime;
ffmpegPath: string;
ffmpegPrefixArgs?: string[];
ffprobePath: string;
@@ -115,7 +115,7 @@ async function probe(
percent: null,
message: 'Reading video information and available formats'
});
- const ytDlp = currentYtDlpRuntime(input);
+ const ytDlp = currentYtDlpRuntime(input, url);
const stdout = await run(
ytDlp.executable,
[
@@ -227,7 +227,7 @@ async function downloadSelectedOption(
option.mediaType,
downloadPartWeights(probe, option)
);
- const ytDlp = currentYtDlpRuntime(input);
+ const ytDlp = currentYtDlpRuntime(input, url);
const stdout = await run(
ytDlp.executable,
[...ytDlp.prefixArgs, ...YT_DLP_UTF8_ARGS, ...args],
@@ -388,7 +388,7 @@ async function downloadPlaybackSource(
message: preview ? 'Connecting to the original video source; existing subtitles are preserved' : 'Preparing the YouTube source'
});
const outputTemplate = join(stage.workdir, 'source.%(ext)s');
- const ytDlp = currentYtDlpRuntime(input);
+ const ytDlp = currentYtDlpRuntime(input, url);
const stdout = await run(
ytDlp.executable,
[
@@ -537,7 +537,7 @@ async function downloadLegacy(
);
}
const outputTemplate = join(stage.workdir, 'source.%(ext)s');
- const ytDlp = currentYtDlpRuntime(input);
+ const ytDlp = currentYtDlpRuntime(input, url);
const stdout = await run(
ytDlp.executable,
[
@@ -1183,16 +1183,34 @@ function run(
function classifyDownloadError(stderr: string, exitCode: number | null): CreatorExecutorError {
const text = stderr.toLowerCase();
+ const douyin = /\[douyin(?:\+[^\]]+)?\]/.test(text);
const failure = (
code: string,
message: string,
kind: NonNullable<CreatorExecutorError['publicFacts']>['kind'],
- httpStatus?: number
+ httpStatus?: number,
+ upstreamMessage?: string
) => new CreatorExecutorError(code, message, {}, {
+ ...sanitizePublicErrorFacts({ kind, upstreamMessage }),
kind,
provider: 'yt-dlp',
...(httpStatus === undefined ? {} : { httpStatus })
});
+ if (douyin && text.includes('douyin request signature is required')) {
+ return failure('platform_request_signature_required',
+ 'Douyin rejected the downloader because its required browser request signature is not supported. Reconnecting cookies alone cannot fix this; use a local video file.',
+ 'unsupported', 403, stderr);
+ }
+ if (douyin && (text.includes('fresh cookies') || text.includes('douyin browser identity was rejected'))) {
+ const status = /http error\s+(\d{3})\b/i.exec(stderr);
+ return failure('platform_session_required',
+ 'Douyin did not accept the browser session or requires website verification. Reconnect Douyin in Settings > Platform connections and retry; signing in is not necessarily required.',
+ 'unauthorized', status === null ? undefined : Number(status[1]), stderr);
+ }
+ if (/bilibili risk control (?:rejected the media request|blocked media extraction)/.test(text)
+ || (text.includes('bilibili risk control') && text.includes('no video formats found'))) {
+ return failure('download_failed', 'Bilibili risk control rejected the media request. Wait and retry, or use a local video file.', 'http-rejected');
+ }
if (/proxyerror|proxy connection|proxy (?:server )?(?:timed out|refused|unreachable|failed)/.test(text)) {
const reason = /timed out|timeout/.test(text) ? 'timed out' : /refused/.test(text) ? 'was refused' : 'failed';
return failure('network_unavailable', `yt-dlp proxy connection ${reason}. Check the proxy address and whether the proxy service is running.`,
@@ -1284,8 +1302,8 @@ function publicYtDlpErrorLine(stderr: string): string | undefined {
return sanitizeIssueDetail(redacted, 300);
}
-function currentYtDlpRuntime(input: DownloadExecutorOptions): YtDlpRuntime {
- return input.getYtDlpRuntime?.() ?? {
+function currentYtDlpRuntime(input: DownloadExecutorOptions, sourceUrl: string): YtDlpRuntime {
+ return input.getYtDlpRuntime?.(sourceUrl) ?? {
version: 'configured',
executable: input.ytDlpPath,
prefixArgs: input.ytDlpPrefixArgs ?? [],
diff --git a/apps/daemon/src/creator/krillin/adapter.ts b/apps/daemon/src/creator/krillin/adapter.ts
index 8345fc0e..d5c69475 100644
--- a/apps/daemon/src/creator/krillin/adapter.ts
+++ b/apps/daemon/src/creator/krillin/adapter.ts
@@ -35,7 +35,7 @@ export function createKrillinExecutor(input: {
jobsRoot: string;
dependencyLoader: KrillinDependencyLoader;
configStore: Pick<CreatorServicesConfigStore, 'read'>;
- getYtDlpRuntime?(): YtDlpRuntime | undefined;
+ getYtDlpRuntime?(sourceUrl?: string): YtDlpRuntime | undefined;
verificationCachePath?: string;
ensureRuntimeReady?(): Promise<void>;
getCodexLlmConfig?(): { baseUrl: string; apiKey: string; model: string } | undefined;
@@ -94,7 +94,7 @@ export function createKrillinExecutor(input: {
artifacts: materializedArtifacts,
source: attempt.source,
options: attempt.options,
- ytDlpRuntime: input.getYtDlpRuntime?.(),
+ ytDlpRuntime: input.getYtDlpRuntime?.(stringValue(attempt.options.sourceUrl)),
llmOverride: preflight.config.llm.source === 'codex'
? input.getCodexLlmConfig?.()
: undefined
@@ -226,6 +226,10 @@ export function normalizeKrillinFailure(error: { code?: string; kind?: string; m
message: string;
} {
const message = error?.message ?? 'KrillinAI stage failed';
+ if (/^prepare_(?:media|audio_fallback)_failed$/.test(error?.code ?? '') && /\[Douyin(?:\+[^\]]+)?\]/i.test(message)) {
+ if (/Douyin request signature is required/i.test(message)) return { code: 'platform_request_signature_required', message };
+ if (/fresh cookies|Douyin browser identity was rejected/i.test(message)) return { code: 'platform_session_required', message };
+ }
if (error?.code !== undefined && [
'audio_no_speech', 'audio_transcription_empty', 'audio_transcription_invalid_response',
'audio_transcription_timestamps_missing', 'audio_transcription_api_failed'
diff --git a/apps/daemon/src/creator/krillin/cli-runner.ts b/apps/daemon/src/creator/krillin/cli-runner.ts
index d861deb2..f2b00740 100644
--- a/apps/daemon/src/creator/krillin/cli-runner.ts
+++ b/apps/daemon/src/creator/krillin/cli-runner.ts
@@ -73,9 +73,16 @@ export class KrillinCliError extends Error {
) {
super(message, options);
this.name = 'KrillinCliError';
+ const douyinFailure = /^prepare_(?:media|audio_fallback)_failed$/.test(code) && /\[Douyin(?:\+[^\]]+)?\]/i.test(message);
+ const signatureRequired = douyinFailure && /Douyin request signature is required/i.test(message);
+ const sessionRequired = douyinFailure && /fresh cookies|Douyin browser identity was rejected/i.test(message);
+ const status = sessionRequired || signatureRequired ? /http error\s+(\d{3})\b/i.exec(message) : null;
this.publicFacts = sanitizePublicErrorFacts({
- kind: publicErrorKindForCode(code) ?? 'unknown', provider: 'krillinai',
- upstreamCode: code, upstreamMessage: message
+ kind: signatureRequired ? 'unsupported' : sessionRequired ? 'unauthorized' : /^prepare_(?:media|audio_fallback)_failed$/.test(code)
+ && /bilibili risk control (?:rejected the media request|blocked media extraction)/i.test(message)
+ ? 'http-rejected' : publicErrorKindForCode(code) ?? 'unknown', provider: 'krillinai',
+ upstreamCode: code, upstreamMessage: message,
+ ...(status === null ? {} : { httpStatus: Number(status[1]) })
});
}
}
diff --git a/apps/daemon/src/creator/yt-dlp/runtime.ts b/apps/daemon/src/creator/yt-dlp/runtime.ts
index 4eb09c07..56245888 100644
--- a/apps/daemon/src/creator/yt-dlp/runtime.ts
+++ b/apps/daemon/src/creator/yt-dlp/runtime.ts
@@ -1,5 +1,6 @@
import { existsSync } from 'node:fs';
import { resolve } from 'node:path';
+import { fileURLToPath } from 'node:url';
import type { KrillinRuntimeManifest } from '../krillin/manifest.js';
import { resolveInside } from '../krillin/manifest.js';
@@ -16,6 +17,7 @@ export function resolveYtDlpRuntime(input: {
manifest: KrillinRuntimeManifest;
overridePath?: string;
}): YtDlpRuntime | undefined {
+ const pluginArgs = ['--plugin-dirs', fileURLToPath(new URL('../../../runtime/yt-dlp', import.meta.url))];
const javascriptRuntime = input.manifest.ytDlp?.javascriptRuntime;
const javascriptArgs = javascriptRuntime === undefined ? [] : [
'--js-runtimes', `${javascriptRuntime.name}:${manifestResource(input, javascriptRuntime.executable, 'executable')}`
@@ -28,7 +30,7 @@ export function resolveYtDlpRuntime(input: {
return {
version: 'external',
executable,
- prefixArgs: javascriptArgs,
+ prefixArgs: [...pluginArgs, ...javascriptArgs],
env: {}
};
}
@@ -38,7 +40,7 @@ export function resolveYtDlpRuntime(input: {
return {
version: descriptor.version,
executable: manifestResource(input, descriptor.executable, 'executable'),
- prefixArgs: javascriptArgs,
+ prefixArgs: [...pluginArgs, ...javascriptArgs],
env: {}
};
}
@@ -53,7 +55,7 @@ export function resolveYtDlpRuntime(input: {
return {
version: descriptor.version,
executable,
- prefixArgs: ['-I', '-B', script, ...javascriptArgs],
+ prefixArgs: ['-I', '-B', script, ...pluginArgs, ...javascriptArgs],
env: {
SSL_CERT_FILE: certificateBundle
},
@@ -70,7 +72,7 @@ export function resolveYtDlpRuntime(input: {
: {
version: 'legacy',
executable: resolveInside(input.resourceRoot, legacy.path),
- prefixArgs: javascriptArgs,
+ prefixArgs: [...pluginArgs, ...javascriptArgs],
env: {}
};
}
diff --git a/apps/daemon/src/platform-connections/browser.ts b/apps/daemon/src/platform-connections/browser.ts
index 9f251350..e6a973af 100644
--- a/apps/daemon/src/platform-connections/browser.ts
+++ b/apps/daemon/src/platform-connections/browser.ts
@@ -4,8 +4,9 @@ import { homedir } from 'node:os';
import { join } from 'node:path';
import { spawnCreatorProcess } from '../creator/process-tree.js';
import type { YtDlpRuntime } from '../creator/yt-dlp/runtime.js';
-import { filterYouTubeCookies, hasYouTubeAuthentication, type YouTubeCookie } from './cookies.js';
import { BrowserCloseError } from './browser-close.js';
+import type { ConnectionPlatform } from '@opencreator/protocol';
+import { filterPlatformCookies, hasPlatformSession, type PlatformCookie } from './cookies.js';
export type BrowserProfile = { id: string; browser: 'chrome' | 'edge'; label: string; directory: string; preferred: boolean };
@@ -39,7 +40,7 @@ export async function listBrowserProfiles(): Promise<BrowserProfile[]> {
return profiles.sort((a, b) => Number(b.preferred) - Number(a.preferred));
}
-// Use yt-dlp's maintained browser decryption implementation. Only YouTube cookies
+// Use yt-dlp's maintained browser decryption implementation. Only the selected platform's cookies
// cross the private stdout pipe; no all-site plaintext cookie file is exported.
const READ_BROWSER_COOKIES = String.raw`
import sys, json, time
@@ -69,7 +70,7 @@ try:
expires = c.expires or 0
if expires > 100000000000:
expires = int(expires / 1000000 - 11644473600)
- if (host == 'youtube.com' or host.endswith('.youtube.com')) and (expires <= 0 or expires > time.time()):
+ if (host == sys.argv[4] or host.endswith('.' + sys.argv[4])) and (expires <= 0 or expires > time.time()):
cookies.append(dict(name=c.name, value=c.value, domain=c.domain, path=c.path,
secure=bool(c.secure), httpOnly=c.has_nonstandard_attr('HttpOnly'), expires=expires))
sys.stdout.write(json.dumps(dict(cookies=cookies, decryptionFailed=logger.decryption_failed)))
@@ -79,17 +80,19 @@ except Exception:
sys.exit(1)
`;
-export async function readBrowserYouTubeCookies(input: {
+export async function readBrowserPlatformCookies(input: {
profile: BrowserProfile;
runtime: YtDlpRuntime | undefined;
signal: AbortSignal;
-}): Promise<YouTubeCookie[]> {
+ platform?: ConnectionPlatform;
+}): Promise<PlatformCookie[]> {
const { runtime, profile, signal } = input;
+ const platform = input.platform ?? 'youtube';
if (!runtime?.script) throw new Error('浏览器连接需要内置 yt-dlp 组件,请更新本地组件后重试。');
signal.throwIfAborted();
const output = await new Promise<string>((resolve, reject) => {
const child = spawnCreatorProcess(runtime.executable, ['-I', '-B', '-c', READ_BROWSER_COOKIES,
- runtime.script!, profile.browser, profile.directory], {
+ runtime.script!, profile.browser, profile.directory, platform === 'youtube' ? 'youtube.com' : 'douyin.com'], {
env: { ...process.env, ...runtime.env }, stdio: ['ignore', 'pipe', 'pipe']
}, signal);
const chunks: Buffer[] = [];
@@ -116,8 +119,8 @@ export async function readBrowserYouTubeCookies(input: {
});
});
const result = JSON.parse(output) as { cookies?: unknown; decryptionFailed?: boolean };
- const cookies = filterYouTubeCookies(result.cookies);
- if (process.platform === 'win32' && result.decryptionFailed && !hasYouTubeAuthentication(cookies)) {
+ const cookies = filterPlatformCookies(result.cookies, platform);
+ if (process.platform === 'win32' && result.decryptionFailed && !hasPlatformSession(cookies, platform)) {
throw windowsDecryptionError();
}
if (result.decryptionFailed && cookies.length === 0) {
@@ -129,3 +132,4 @@ export async function readBrowserYouTubeCookies(input: {
function windowsDecryptionError(): Error {
return new Error('无法解密 YouTube 登录态,请断开后重新连接并登录。');
}
+export const readBrowserYouTubeCookies = readBrowserPlatformCookies;
diff --git a/apps/daemon/src/platform-connections/cookies.ts b/apps/daemon/src/platform-connections/cookies.ts
index fd829ec2..cc1e7f28 100644
--- a/apps/daemon/src/platform-connections/cookies.ts
+++ b/apps/daemon/src/platform-connections/cookies.ts
@@ -1,4 +1,6 @@
-export type YouTubeCookie = {
+import type { ConnectionPlatform } from '@opencreator/protocol';
+
+export type PlatformCookie = {
name: string;
value: string;
domain: string;
@@ -8,17 +10,28 @@ export type YouTubeCookie = {
expires: number;
};
+export type YouTubeCookie = PlatformCookie;
+
export function isYouTubeDomain(domain: string): boolean {
+ return isPlatformDomain(domain, 'youtube');
+}
+
+function isPlatformDomain(domain: string, platform: ConnectionPlatform): boolean {
const host = domain.replace(/^\./, '').toLowerCase();
- return host === 'youtube.com' || host.endsWith('.youtube.com');
+ const base = platform === 'youtube' ? 'youtube.com' : 'douyin.com';
+ return host === base || host.endsWith(`.${base}`);
}
export function filterYouTubeCookies(value: unknown, now = Date.now()): YouTubeCookie[] {
+ return filterPlatformCookies(value, 'youtube', now);
+}
+
+export function filterPlatformCookies(value: unknown, platform: ConnectionPlatform, now = Date.now()): PlatformCookie[] {
if (!Array.isArray(value)) return [];
return value.filter((cookie): cookie is YouTubeCookie => {
if (!cookie || typeof cookie !== 'object') return false;
const c = cookie as Record<string, unknown>;
- return typeof c.domain === 'string' && isYouTubeDomain(c.domain)
+ return typeof c.domain === 'string' && isPlatformDomain(c.domain, platform)
&& typeof c.name === 'string' && !!c.name && typeof c.value === 'string'
&& typeof c.path === 'string' && c.path.startsWith('/')
&& [c.domain, c.name, c.value, c.path].every(field => !/[\t\r\n\0]/.test(field as string))
@@ -34,14 +47,27 @@ export function hasYouTubeAuthentication(cookies: YouTubeCookie[]): boolean {
&& cookies.some(c => ['SAPISID', '__Secure-1PAPISID', '__Secure-3PAPISID'].includes(c.name) && !!c.value);
}
+export function hasPlatformSession(cookies: PlatformCookie[], platform: ConnectionPlatform): boolean {
+ return platform === 'youtube' ? hasYouTubeAuthentication(cookies)
+ : cookies.some(c => ['s_v_web_id', '__ac_signature', 'ttwid', 'sessionid', 'sessionid_ss'].includes(c.name) && !!c.value);
+}
+
export function serializeYouTubeCookies(cookies: YouTubeCookie[]): string {
- return '# Netscape HTTP Cookie File\n' + filterYouTubeCookies(cookies).map(c => [
+ return serializePlatformCookies(cookies, 'youtube');
+}
+
+export function serializePlatformCookies(cookies: PlatformCookie[], platform: ConnectionPlatform): string {
+ return '# Netscape HTTP Cookie File\n' + filterPlatformCookies(cookies, platform).map(c => [
`${c.httpOnly ? '#HttpOnly_' : ''}${c.domain}`, c.domain.startsWith('.') ? 'TRUE' : 'FALSE',
c.path, c.secure ? 'TRUE' : 'FALSE', Math.max(0, Math.floor(c.expires)), c.name, c.value
].join('\t')).join('\n') + '\n';
}
export function parseYouTubeCookies(source: string): YouTubeCookie[] {
+ return parsePlatformCookies(source, 'youtube');
+}
+
+export function parsePlatformCookies(source: string, platform: ConnectionPlatform): PlatformCookie[] {
if (Buffer.byteLength(source) > 1024 * 1024 || !/^\uFEFF?# (?:Netscape )?HTTP Cookie File/.test(source)) {
throw new Error('Invalid Netscape cookie file');
}
@@ -53,5 +79,5 @@ export function parseYouTubeCookies(source: string): YouTubeCookie[] {
return [{ domain, path, secure: secure === 'TRUE', expires: Number(expires), name, value,
httpOnly: line.startsWith('#HttpOnly_') }];
});
- return filterYouTubeCookies(cookies);
+ return filterPlatformCookies(cookies, platform);
}
diff --git a/apps/daemon/src/platform-connections/service.ts b/apps/daemon/src/platform-connections/service.ts
new file mode 100644
index 00000000..28ee715b
--- /dev/null
+++ b/apps/daemon/src/platform-connections/service.ts
@@ -0,0 +1,259 @@
+import { AsyncLocalStorage } from 'node:async_hooks';
+import { chmod, mkdir, mkdtemp, readdir, rm, writeFile } from 'node:fs/promises';
+import { join } from 'node:path';
+import type { ConnectionPlatform, PlatformConnectionState } from '@opencreator/protocol';
+import { readPrivateJsonFile, writePrivateJsonFile } from '../config/private-json-file.js';
+import type { YtDlpRuntime } from '../creator/yt-dlp/runtime.js';
+import { listBrowserProfiles, readBrowserPlatformCookies, type BrowserProfile } from './browser.js';
+import { LoginBrowserCloseError, openWindowsYouTubeLogin, type BrowserLoginSession } from './browser-login.js';
+import { filterPlatformCookies, hasPlatformSession, parsePlatformCookies,
+ serializePlatformCookies, type PlatformCookie } from './cookies.js';
+
+type Credential = { version: 1; cookies: PlatformCookie[]; connectedAt: string;
+ browser?: 'chrome' | 'edge'; profileId?: string; invalidated?: boolean };
+type ConnectionAttempt = { controller: AbortController; work: Promise<void>; completeLogin?: () => void };
+
+type ConnectionState<P extends ConnectionPlatform> = PlatformConnectionState & { platform: P };
+export type PlatformConnectionOptions = {
+ root: string;
+ platform?: ConnectionPlatform;
+ getRuntime(): YtDlpRuntime | undefined;
+ listProfiles?: typeof listBrowserProfiles;
+ readBrowserCookies?: typeof readBrowserPlatformCookies;
+ openLoginBrowser?: typeof openWindowsYouTubeLogin;
+ connectionTimeoutMs?: number;
+ osPlatform?: NodeJS.Platform;
+};
+
+export class PlatformConnectionService<P extends ConnectionPlatform = ConnectionPlatform> {
+ private state: ConnectionState<P>;
+ private readonly platform: P;
+ private attempt?: ConnectionAttempt;
+ private context = new AsyncLocalStorage<{ path: string; credential: Credential }>();
+ private mutations: Promise<unknown> = Promise.resolve();
+ private readonly credentialFile: string;
+ constructor(private input: PlatformConnectionOptions & { platform?: P }) {
+ this.platform = (input.platform ?? 'youtube') as P;
+ this.state = { platform: this.platform, status: 'disconnected' };
+ this.credentialFile = join(input.root, 'credentials.json');
+ }
+
+ private get managedLogin(): boolean {
+ return this.platform === 'youtube' && (this.input.osPlatform ?? process.platform) === 'win32';
+ }
+
+ async initialize(): Promise<void> {
+ await mkdir(this.input.root, { recursive: true, mode: 0o700 });
+ if (process.platform !== 'win32') await chmod(this.input.root, 0o700);
+ for (const entry of await readdir(this.input.root)) {
+ if (/^(?:login-|task-)/.test(entry)) await rm(join(this.input.root, entry), { recursive: true, force: true });
+ }
+ await this.refreshState();
+ }
+ async getState(): Promise<ConnectionState<P>> {
+ if (!this.attempt && this.state.status !== 'error') await this.refreshState();
+ return this.publicState();
+ }
+ async hasAuthentication(): Promise<boolean> {
+ const credential = await this.readCredential();
+ return !!credential && !credential.invalidated && hasPlatformSession(filterPlatformCookies(credential.cookies, this.platform), this.platform);
+ }
+ connect(profileId?: string): Promise<ConnectionState<P>> {
+ return this.serialize(async () => {
+ if (this.attempt) return this.publicState();
+ if (this.managedLogin) {
+ if (await this.hasAuthentication()) { await this.refreshState(); return this.publicState(); }
+ const attempt: ConnectionAttempt = { controller: new AbortController(), work: Promise.resolve() };
+ this.attempt = attempt;
+ this.state = { platform: this.platform, status: 'connecting', message: '正在打开 YouTube 登录窗口' };
+ attempt.work = this.finishConnection(attempt);
+ return this.publicState();
+ }
+ const profiles = await (this.input.listProfiles ?? listBrowserProfiles)();
+ const profile = profileId ? profiles.find(p => p.id === profileId) : profiles[0];
+ if (!profile) {
+ this.state = { platform: this.platform, status: 'error', message: this.platform === 'youtube'
+ ? '未找到可用的浏览器账号,请先在 Chrome 或 Edge 中登录 YouTube。'
+ : '未找到可用的浏览器账号,请先在 Chrome 或 Edge 中打开抖音并完成网页验证。' };
+ return this.publicState();
+ }
+ const attempt = { controller: new AbortController(), work: Promise.resolve() };
+ this.attempt = attempt;
+ this.state = { platform: this.platform, status: 'connecting', browser: profile.browser, profileId: profile.id };
+ attempt.work = this.finishConnection(attempt, profile);
+ return this.publicState();
+ });
+ }
+ completeLogin(): Promise<ConnectionState<P>> {
+ return this.serialize(async () => {
+ if (this.attempt?.completeLogin) {
+ this.attempt.completeLogin();
+ this.state = { platform: this.platform, status: 'connecting', browser: 'chrome', message: '正在保存 YouTube 登录态' };
+ }
+ return this.publicState();
+ });
+ }
+ cancel(): Promise<ConnectionState<P>> {
+ return this.serialize(async () => { await this.stopConnection(); await this.refreshState(); return this.publicState(); });
+ }
+ disconnect(): Promise<ConnectionState<P>> {
+ return this.serialize(async () => {
+ await this.stopConnection();
+ await rm(this.credentialFile, { force: true });
+ if (this.managedLogin) {
+ for (const browser of ['chrome', 'edge']) {
+ await rm(join(this.input.root, `browser-${browser}`), { recursive: true, force: true, maxRetries: 3, retryDelay: 200 });
+ }
+ }
+ this.state = { platform: this.platform, status: 'disconnected' };
+ return this.publicState();
+ });
+ }
+ importCookies(source: string): Promise<ConnectionState<P>> {
+ return this.serialize(async () => {
+ const cookies = parsePlatformCookies(source, this.platform);
+ if (!hasPlatformSession(cookies, this.platform)) throw new Error(this.platform === 'youtube'
+ ? 'Cookie 文件中没有有效的 YouTube 登录态。' : 'Cookie 文件中没有有效的抖音会话。');
+ await this.stopConnection();
+ await writePrivateJsonFile(this.credentialFile, { version: 1, cookies, connectedAt: new Date().toISOString() } satisfies Credential);
+ await this.refreshState();
+ return this.publicState();
+ });
+ }
+ authenticatedRuntime(runtime: YtDlpRuntime | undefined): YtDlpRuntime | undefined {
+ const context = this.context.getStore();
+ return runtime && context ? { ...runtime, prefixArgs: [...runtime.prefixArgs, '--cookies', context.path] } : runtime;
+ }
+ async withCookies<T>(operation: () => Promise<T>): Promise<T> {
+ if (this.context.getStore()) return operation();
+ // Browser reads can prompt for keychain access. Only explicit connections
+ // refresh the saved credential; background requests and tasks use snapshots.
+ const credential = await this.readCredential();
+ const cookies = filterPlatformCookies(credential?.cookies, this.platform);
+ if (!credential || credential.invalidated || !hasPlatformSession(cookies, this.platform)) return operation();
+ const directory = await mkdtemp(join(this.input.root, 'task-'));
+ if (process.platform !== 'win32') await chmod(directory, 0o700);
+ const path = join(directory, 'cookies.txt');
+ try {
+ await writeFile(path, serializePlatformCookies(cookies, this.platform), { mode: 0o600, flag: 'wx' });
+ return await this.context.run({ path, credential }, operation);
+ } catch (error) {
+ // A media 403 or bot challenge alone is not proof that the account expired.
+ if (error instanceof Error && /cookies (?:are )?no longer valid|cookies? (?:have |has )?expired/i.test(error.message)) {
+ await this.serialize(async () => {
+ const current = await this.readCredential();
+ if (current?.connectedAt === credential.connectedAt) {
+ await writePrivateJsonFile(this.credentialFile, { ...current, invalidated: true });
+ await this.refreshState();
+ }
+ });
+ }
+ throw error;
+ } finally { await rm(directory, { recursive: true, force: true, maxRetries: 3, retryDelay: 200 }); }
+ }
+ async close(): Promise<void> { await this.serialize(() => this.stopConnection()); }
+ private async finishConnection(attempt: ConnectionAttempt, profile?: BrowserProfile): Promise<void> {
+ const managed = this.managedLogin;
+ const timeout = setTimeout(() => attempt.controller.abort(), this.input.connectionTimeoutMs ?? (managed ? 600_000 : 120_000));
+ let login: BrowserLoginSession | undefined;
+ let browserClosed = false;
+ try {
+ if (managed) {
+ login = await (this.input.openLoginBrowser ?? openWindowsYouTubeLogin)({ root: this.input.root, signal: attempt.controller.signal });
+ profile = login.profile;
+ let message = '请在打开的 Chrome 窗口中登录 YouTube';
+ let actionRequired: 'complete-login' | 'close-login-browser' = 'complete-login';
+ let watchExit = true;
+ while (!browserClosed) {
+ this.state = { platform: this.platform, status: 'connecting', browser: 'chrome', profileId: profile.id, message, actionRequired };
+ await this.waitForLoginCompletion(attempt, login, watchExit);
+ watchExit = false;
+ attempt.controller.signal.throwIfAborted();
+ this.state = { platform: this.platform, status: 'connecting', browser: 'chrome', profileId: profile.id, message: '正在保存 YouTube 登录态' };
+ try { await login.close(attempt.controller.signal); browserClosed = true; }
+ catch (error) {
+ if (!(error instanceof LoginBrowserCloseError)) throw error;
+ message = error.message; actionRequired = 'close-login-browser';
+ }
+ }
+ }
+ attempt.controller.signal.throwIfAborted();
+ if (!profile) throw new Error('未找到可用的浏览器账号,请重新连接。');
+ const cookies = filterPlatformCookies(await (this.input.readBrowserCookies ?? readBrowserPlatformCookies)({
+ profile, runtime: this.input.getRuntime(), signal: attempt.controller.signal,
+ ...(this.platform === 'douyin' ? { platform: 'douyin' as const } : {})
+ }), this.platform);
+ attempt.controller.signal.throwIfAborted();
+ if (!hasPlatformSession(cookies, this.platform)) throw new Error(managed
+ ? '尚未完成 YouTube 登录,请重新连接并在登录窗口中完成登录。'
+ : this.platform === 'youtube'
+ ? '此浏览器账号尚未登录 YouTube,或登录已过期。请在该账号中登录后重新连接。'
+ : '此浏览器账号没有有效的抖音会话,请先打开抖音视频并完成网页验证后重新连接。');
+ const connectedAt = new Date().toISOString();
+ await writePrivateJsonFile(this.credentialFile, {
+ version: 1, cookies, connectedAt, browser: profile.browser, profileId: profile.id
+ } satisfies Credential);
+ this.state = { platform: this.platform, status: 'connected', connectedAt, browser: profile.browser, profileId: profile.id };
+ } catch (error) {
+ this.state = { platform: this.platform, status: 'error', browser: profile?.browser, profileId: profile?.id,
+ message: attempt.controller.signal.aborted ? '连接已取消或超时,请重新连接。' : publicConnectionError(error) };
+ } finally {
+ clearTimeout(timeout);
+ if (login) {
+ if (!browserClosed) await login.close(AbortSignal.timeout(12_000)).catch(() => undefined);
+ await login.dispose().catch(() => undefined);
+ }
+ if (this.attempt === attempt) this.attempt = undefined;
+ }
+ }
+ private waitForLoginCompletion(attempt: ConnectionAttempt, login: BrowserLoginSession, watchExit: boolean): Promise<void> {
+ const signal = attempt.controller.signal;
+ signal.throwIfAborted();
+ return new Promise((resolve, reject) => {
+ const cleanup = () => {
+ signal.removeEventListener('abort', abort);
+ if (attempt.completeLogin === complete) attempt.completeLogin = undefined;
+ };
+ const complete = () => { cleanup(); resolve(); };
+ const abort = () => { cleanup(); reject(signal.reason); };
+ attempt.completeLogin = complete;
+ signal.addEventListener('abort', abort, { once: true });
+ if (watchExit) void login.exited.then(complete);
+ });
+ }
+ private async stopConnection(): Promise<void> {
+ const attempt = this.attempt;
+ if (!attempt) return;
+ attempt.controller.abort();
+ await attempt.work;
+ if (this.attempt === attempt) this.attempt = undefined;
+ }
+ private async readCredential(): Promise<Credential | undefined> {
+ const raw = await readPrivateJsonFile(this.credentialFile);
+ if (!raw || typeof raw !== 'object') return undefined;
+ const value = raw as Partial<Credential>;
+ if (value.version !== 1 || !Array.isArray(value.cookies) || typeof value.connectedAt !== 'string') return undefined;
+ return value as Credential;
+ }
+ private async refreshState(): Promise<void> {
+ const credential = await this.readCredential();
+ this.state = credential ? {
+ platform: this.platform, status: !credential.invalidated && hasPlatformSession(filterPlatformCookies(credential.cookies, this.platform), this.platform) ? 'connected' : 'expired',
+ connectedAt: credential.connectedAt, browser: credential.browser, profileId: credential.profileId
+ } : { platform: this.platform, status: 'disconnected' };
+ }
+ private async publicState(): Promise<ConnectionState<P>> {
+ if (this.managedLogin) return { ...this.state, connectionMethod: 'browser-login' };
+ const profiles = await (this.input.listProfiles ?? listBrowserProfiles)();
+ return { ...this.state, profiles: profiles.map(({ id, browser, label }) => ({ id, browser, label })) };
+ }
+ private serialize<T>(operation: () => Promise<T>): Promise<T> {
+ const work = this.mutations.then(operation);
+ this.mutations = work.catch(() => undefined);
+ return work;
+ }
+}
+function publicConnectionError(error: unknown): string {
+ const message = error instanceof Error ? error.message : '';
+ return /^[\u4e00-\u9fff]/.test(message) ? message : '读取浏览器登录态失败,请重试或选择其他浏览器账号。';
+}
diff --git a/apps/daemon/src/platform-connections/youtube.ts b/apps/daemon/src/platform-connections/youtube.ts
index 6b409dd2..6be9a8d7 100644
--- a/apps/daemon/src/platform-connections/youtube.ts
+++ b/apps/daemon/src/platform-connections/youtube.ts
@@ -1,239 +1,7 @@
-import { AsyncLocalStorage } from 'node:async_hooks';
-import { chmod, mkdir, mkdtemp, readdir, rm, writeFile } from 'node:fs/promises';
-import { join } from 'node:path';
-import type { YouTubeConnectionState } from '@opencreator/protocol';
-import { readPrivateJsonFile, writePrivateJsonFile } from '../config/private-json-file.js';
-import type { YtDlpRuntime } from '../creator/yt-dlp/runtime.js';
-import { listBrowserProfiles, readBrowserYouTubeCookies, type BrowserProfile } from './browser.js';
-import { LoginBrowserCloseError, openWindowsYouTubeLogin, type BrowserLoginSession } from './browser-login.js';
-import { filterYouTubeCookies, hasYouTubeAuthentication, parseYouTubeCookies,
- serializeYouTubeCookies, type YouTubeCookie } from './cookies.js';
+import { PlatformConnectionService, type PlatformConnectionOptions } from './service.js';
-type Credential = { version: 1; cookies: YouTubeCookie[]; connectedAt: string;
- browser?: 'chrome' | 'edge'; profileId?: string; invalidated?: boolean };
-type ConnectionAttempt = { controller: AbortController; work: Promise<void>; completeLogin?: () => void };
-
-export class YouTubeConnectionService {
- private state: YouTubeConnectionState = { platform: 'youtube', status: 'disconnected' };
- private attempt?: ConnectionAttempt;
- private context = new AsyncLocalStorage<{ path: string; credential: Credential }>();
- private mutations: Promise<unknown> = Promise.resolve();
- private readonly credentialFile: string;
- constructor(private input: {
- root: string;
- getRuntime(): YtDlpRuntime | undefined;
- listProfiles?: typeof listBrowserProfiles;
- readBrowserCookies?: typeof readBrowserYouTubeCookies;
- openLoginBrowser?: typeof openWindowsYouTubeLogin;
- connectionTimeoutMs?: number;
- platform?: NodeJS.Platform;
- }) { this.credentialFile = join(input.root, 'credentials.json'); }
-
- async initialize(): Promise<void> {
- await mkdir(this.input.root, { recursive: true, mode: 0o700 });
- if (process.platform !== 'win32') await chmod(this.input.root, 0o700);
- for (const entry of await readdir(this.input.root)) {
- if (/^(?:login-|task-)/.test(entry)) await rm(join(this.input.root, entry), { recursive: true, force: true });
- }
- await this.refreshState();
- }
- async getState(): Promise<YouTubeConnectionState> {
- if (!this.attempt && this.state.status !== 'error') await this.refreshState();
- return this.publicState();
- }
- async hasAuthentication(): Promise<boolean> {
- const credential = await this.readCredential();
- return !!credential && !credential.invalidated && hasYouTubeAuthentication(filterYouTubeCookies(credential.cookies));
- }
- connect(profileId?: string): Promise<YouTubeConnectionState> {
- return this.serialize(async () => {
- if (this.attempt) return this.publicState();
- if ((this.input.platform ?? process.platform) === 'win32') {
- if (await this.hasAuthentication()) { await this.refreshState(); return this.publicState(); }
- const attempt: ConnectionAttempt = { controller: new AbortController(), work: Promise.resolve() };
- this.attempt = attempt;
- this.state = { platform: 'youtube', status: 'connecting', message: '正在打开 YouTube 登录窗口' };
- attempt.work = this.finishConnection(attempt);
- return this.publicState();
- }
- const profiles = await (this.input.listProfiles ?? listBrowserProfiles)();
- const profile = profileId ? profiles.find(p => p.id === profileId) : profiles[0];
- if (!profile) {
- this.state = { platform: 'youtube', status: 'error', message: '未找到可用的浏览器账号,请先在 Chrome 或 Edge 中登录 YouTube。' };
- return this.publicState();
- }
- const attempt = { controller: new AbortController(), work: Promise.resolve() };
- this.attempt = attempt;
- this.state = { platform: 'youtube', status: 'connecting', browser: profile.browser, profileId: profile.id };
- attempt.work = this.finishConnection(attempt, profile);
- return this.publicState();
- });
- }
- completeLogin(): Promise<YouTubeConnectionState> {
- return this.serialize(async () => {
- if (this.attempt?.completeLogin) {
- this.attempt.completeLogin();
- this.state = { platform: 'youtube', status: 'connecting', browser: 'chrome', message: '正在保存 YouTube 登录态' };
- }
- return this.publicState();
- });
- }
- cancel(): Promise<YouTubeConnectionState> {
- return this.serialize(async () => { await this.stopConnection(); await this.refreshState(); return this.publicState(); });
- }
- disconnect(): Promise<YouTubeConnectionState> {
- return this.serialize(async () => {
- await this.stopConnection();
- await rm(this.credentialFile, { force: true });
- if ((this.input.platform ?? process.platform) === 'win32') {
- for (const browser of ['chrome', 'edge']) {
- await rm(join(this.input.root, `browser-${browser}`), { recursive: true, force: true, maxRetries: 3, retryDelay: 200 });
- }
- }
- this.state = { platform: 'youtube', status: 'disconnected' };
- return this.publicState();
- });
- }
- importCookies(source: string): Promise<YouTubeConnectionState> {
- return this.serialize(async () => {
- const cookies = parseYouTubeCookies(source);
- if (!hasYouTubeAuthentication(cookies)) throw new Error('Cookie 文件中没有有效的 YouTube 登录态。');
- await this.stopConnection();
- await writePrivateJsonFile(this.credentialFile, { version: 1, cookies, connectedAt: new Date().toISOString() } satisfies Credential);
- await this.refreshState();
- return this.publicState();
- });
- }
- authenticatedRuntime(runtime: YtDlpRuntime | undefined): YtDlpRuntime | undefined {
- const context = this.context.getStore();
- return runtime && context ? { ...runtime, prefixArgs: [...runtime.prefixArgs, '--cookies', context.path] } : runtime;
+export class YouTubeConnectionService extends PlatformConnectionService<'youtube'> {
+ constructor({ platform, ...input }: Omit<PlatformConnectionOptions, 'platform' | 'osPlatform'> & { platform?: NodeJS.Platform }) {
+ super({ ...input, platform: 'youtube', osPlatform: platform });
}
- async withCookies<T>(operation: () => Promise<T>): Promise<T> {
- if (this.context.getStore()) return operation();
- // Browser reads can prompt for keychain access. Only explicit connections
- // refresh the saved credential; background requests and tasks use snapshots.
- const credential = await this.readCredential();
- const cookies = filterYouTubeCookies(credential?.cookies);
- if (!credential || credential.invalidated || !hasYouTubeAuthentication(cookies)) return operation();
- const directory = await mkdtemp(join(this.input.root, 'task-'));
- if (process.platform !== 'win32') await chmod(directory, 0o700);
- const path = join(directory, 'cookies.txt');
- try {
- await writeFile(path, serializeYouTubeCookies(cookies), { mode: 0o600, flag: 'wx' });
- return await this.context.run({ path, credential }, operation);
- } catch (error) {
- // A media 403 or bot challenge alone is not proof that the account expired.
- if (error instanceof Error && /cookies (?:are )?no longer valid|cookies? (?:have |has )?expired/i.test(error.message)) {
- await this.serialize(async () => {
- const current = await this.readCredential();
- if (current?.connectedAt === credential.connectedAt) {
- await writePrivateJsonFile(this.credentialFile, { ...current, invalidated: true });
- await this.refreshState();
- }
- });
- }
- throw error;
- } finally { await rm(directory, { recursive: true, force: true, maxRetries: 3, retryDelay: 200 }); }
- }
- async close(): Promise<void> { await this.serialize(() => this.stopConnection()); }
- private async finishConnection(attempt: ConnectionAttempt, profile?: BrowserProfile): Promise<void> {
- const managed = (this.input.platform ?? process.platform) === 'win32';
- const timeout = setTimeout(() => attempt.controller.abort(), this.input.connectionTimeoutMs ?? (managed ? 600_000 : 120_000));
- let login: BrowserLoginSession | undefined;
- let browserClosed = false;
- try {
- if (managed) {
- login = await (this.input.openLoginBrowser ?? openWindowsYouTubeLogin)({ root: this.input.root, signal: attempt.controller.signal });
- profile = login.profile;
- let message = '请在打开的 Chrome 窗口中登录 YouTube';
- let actionRequired: 'complete-login' | 'close-login-browser' = 'complete-login';
- let watchExit = true;
- while (!browserClosed) {
- this.state = { platform: 'youtube', status: 'connecting', browser: 'chrome', profileId: profile.id, message, actionRequired };
- await this.waitForLoginCompletion(attempt, login, watchExit);
- watchExit = false;
- attempt.controller.signal.throwIfAborted();
- this.state = { platform: 'youtube', status: 'connecting', browser: 'chrome', profileId: profile.id, message: '正在保存 YouTube 登录态' };
- try { await login.close(attempt.controller.signal); browserClosed = true; }
- catch (error) {
- if (!(error instanceof LoginBrowserCloseError)) throw error;
- message = error.message; actionRequired = 'close-login-browser';
- }
- }
- }
- attempt.controller.signal.throwIfAborted();
- if (!profile) throw new Error('未找到可用的浏览器账号,请重新连接。');
- const cookies = await (this.input.readBrowserCookies ?? readBrowserYouTubeCookies)({
- profile, runtime: this.input.getRuntime(), signal: attempt.controller.signal });
- attempt.controller.signal.throwIfAborted();
- if (!hasYouTubeAuthentication(cookies)) throw new Error(managed
- ? '尚未完成 YouTube 登录,请重新连接并在登录窗口中完成登录。'
- : '此浏览器账号尚未登录 YouTube,或登录已过期。请在该账号中登录后重新连接。');
- const connectedAt = new Date().toISOString();
- await writePrivateJsonFile(this.credentialFile, {
- version: 1, cookies, connectedAt, browser: profile.browser, profileId: profile.id
- } satisfies Credential);
- this.state = { platform: 'youtube', status: 'connected', connectedAt, browser: profile.browser, profileId: profile.id };
- } catch (error) {
- this.state = { platform: 'youtube', status: 'error', browser: profile?.browser, profileId: profile?.id,
- message: attempt.controller.signal.aborted ? '连接已取消或超时,请重新连接。' : publicConnectionError(error) };
- } finally {
- clearTimeout(timeout);
- if (login) {
- if (!browserClosed) await login.close(AbortSignal.timeout(12_000)).catch(() => undefined);
- await login.dispose().catch(() => undefined);
- }
- if (this.attempt === attempt) this.attempt = undefined;
- }
- }
- private waitForLoginCompletion(attempt: ConnectionAttempt, login: BrowserLoginSession, watchExit: boolean): Promise<void> {
- const signal = attempt.controller.signal;
- signal.throwIfAborted();
- return new Promise((resolve, reject) => {
- const cleanup = () => {
- signal.removeEventListener('abort', abort);
- if (attempt.completeLogin === complete) attempt.completeLogin = undefined;
- };
- const complete = () => { cleanup(); resolve(); };
- const abort = () => { cleanup(); reject(signal.reason); };
- attempt.completeLogin = complete;
- signal.addEventListener('abort', abort, { once: true });
- if (watchExit) void login.exited.then(complete);
- });
- }
- private async stopConnection(): Promise<void> {
- const attempt = this.attempt;
- if (!attempt) return;
- attempt.controller.abort();
- await attempt.work;
- if (this.attempt === attempt) this.attempt = undefined;
- }
- private async readCredential(): Promise<Credential | undefined> {
- const raw = await readPrivateJsonFile(this.credentialFile);
- if (!raw || typeof raw !== 'object') return undefined;
- const value = raw as Partial<Credential>;
- if (value.version !== 1 || !Array.isArray(value.cookies) || typeof value.connectedAt !== 'string') return undefined;
- return value as Credential;
- }
- private async refreshState(): Promise<void> {
- const credential = await this.readCredential();
- this.state = credential ? {
- platform: 'youtube', status: !credential.invalidated && hasYouTubeAuthentication(filterYouTubeCookies(credential.cookies)) ? 'connected' : 'expired',
- connectedAt: credential.connectedAt, browser: credential.browser, profileId: credential.profileId
- } : { platform: 'youtube', status: 'disconnected' };
- }
- private async publicState(): Promise<YouTubeConnectionState> {
- if ((this.input.platform ?? process.platform) === 'win32') return { ...this.state, connectionMethod: 'browser-login' };
- const profiles = await (this.input.listProfiles ?? listBrowserProfiles)();
- return { ...this.state, profiles: profiles.map(({ id, browser, label }) => ({ id, browser, label })) };
- }
- private serialize<T>(operation: () => Promise<T>): Promise<T> {
- const work = this.mutations.then(operation);
- this.mutations = work.catch(() => undefined);
- return work;
- }
-}
-function publicConnectionError(error: unknown): string {
- const message = error instanceof Error ? error.message : '';
- return /^[\u4e00-\u9fff]/.test(message) ? message : '读取浏览器登录态失败,请重试或选择其他浏览器账号。';
}
diff --git a/apps/daemon/src/video-metadata/service.ts b/apps/daemon/src/video-metadata/service.ts
index 948e94a1..80242142 100644
--- a/apps/daemon/src/video-metadata/service.ts
+++ b/apps/daemon/src/video-metadata/service.ts
@@ -22,8 +22,9 @@ export type VideoMetadataService = {
export function createVideoMetadataService(input: {
fetchImpl?: typeof fetch;
getProxy?(): string | Promise<string>;
- getYtDlpRuntime?(): YtDlpRuntime | undefined;
+ getYtDlpRuntime?(sourceUrl?: string): YtDlpRuntime | undefined;
withYouTubeCookies?<T>(operation: () => Promise<T>): Promise<T>;
+ withDouyinCookies?<T>(operation: () => Promise<T>): Promise<T>;
isYouTubeConnected?(): Promise<boolean>;
} = {}): VideoMetadataService {
const cache = new Map<string, { payload: unknown; expiresAt: number }>();
@@ -36,19 +37,23 @@ export function createVideoMetadataService(input: {
return input.withYouTubeCookies(async () => {
try {
const { previewUrl: _previewUrl, ...metadata } = await resolveOnlineVideoMetadata({ url: normalized, platform,
- runtime: input.getYtDlpRuntime!()!, proxy: await resolveProxy(input.getProxy) });
+ runtime: input.getYtDlpRuntime!(normalized)!, proxy: await resolveProxy(input.getProxy) });
return metadata;
} catch { throw new VideoMetadataError('UPSTREAM_ERROR', 'YouTube 视频信息暂不可用,请检查网络或平台连接后重试'); }
});
}
if (platform !== null && platform !== 'youtube' && platform !== 'bilibili') {
- const runtime = input.getYtDlpRuntime?.();
- if (runtime === undefined) throw new VideoMetadataError('UPSTREAM_ERROR', 'Online video preview resolver is unavailable');
- try {
- return await resolveOnlineVideoMetadata({ url: normalized, platform, runtime, proxy: await resolveProxy(input.getProxy) });
- } catch {
- throw new VideoMetadataError('UPSTREAM_ERROR', 'Online video preview is unavailable for this source');
- }
+ const resolveMetadata = async () => {
+ const runtime = input.getYtDlpRuntime?.(normalized);
+ if (runtime === undefined) throw new VideoMetadataError('UPSTREAM_ERROR', 'Online video preview resolver is unavailable');
+ try {
+ return await resolveOnlineVideoMetadata({ url: normalized, platform, runtime, proxy: await resolveProxy(input.getProxy) });
+ } catch {
+ throw new VideoMetadataError('UPSTREAM_ERROR', 'Online video preview is unavailable for this source');
+ }
+ };
+ return platform === 'douyin' && input.withDouyinCookies
+ ? input.withDouyinCookies(resolveMetadata) : resolveMetadata();
}
const source = parseVideoSource(value);
const endpoint = createMetadataEndpoint(source);
diff --git a/apps/daemon/test/fixtures/yt-dlp-bilibili-regression.py b/apps/daemon/test/fixtures/yt-dlp-bilibili-regression.py
new file mode 100644
index 00000000..d612af3c
--- /dev/null
+++ b/apps/daemon/test/fixtures/yt-dlp-bilibili-regression.py
@@ -0,0 +1,118 @@
+import json
+import sys
+import unittest
+from types import SimpleNamespace
+from unittest.mock import patch
+
+sys.path.insert(0, sys.argv[1])
+plugin_root = sys.argv[2]
+sys.argv = [sys.argv[0]]
+
+import yt_dlp
+from yt_dlp.globals import plugin_dirs
+from yt_dlp.utils import DownloadError
+
+plugin_dirs.value = [plugin_root]
+
+
+def audio(format_id):
+ return {
+ "id": format_id, "baseUrl": "https://media.example.com/audio.m4s",
+ "mimeType": "audio/mp4", "codecs": "mp4a.40.2", "bandwidth": format_id,
+ }
+
+
+def playinfo(format_id):
+ return {"dash": {"audio": [audio(format_id)]}, "timelength": 10000}
+
+
+class Logger:
+ def __init__(self):
+ self.messages = []
+
+ def debug(self, message):
+ pass
+
+ def warning(self, message):
+ self.messages.append(message)
+
+ def error(self, message):
+ self.messages.append(message)
+
+
+class BilibiliRegression(unittest.TestCase):
+ def extract(self, webpage_info, api_response, format_selector="bestaudio[ext=m4a]"):
+ webpage = "<script>window.__INITIAL_STATE__=" + json.dumps({
+ "videoData": {"bvid": "BV18Qhf6hE8P", "aid": 117323440657857,
+ "cid": 42150005194, "title": "fixture",
+ "pages": [{"cid": 42150005194}]}
+ }) + ";window.__playinfo__=" + json.dumps({"data": webpage_info}) + ";</script>"
+
+ def response(url, *args, **kwargs):
+ if "/playurl" in url:
+ return api_response
+ if "/pagelist" in url:
+ return {"data": [{"page": 1, "cid": 42150005194}]}
+ return {"data": {}}
+
+ self.logger = Logger()
+ with yt_dlp.YoutubeDL({
+ "logger": self.logger, "noplaylist": True, "skip_download": True,
+ "format": format_selector,
+ }) as ydl:
+ extractor = ydl.get_info_extractor("BiliBili")
+ self.assertEqual(extractor.PLUGIN_NAME, "opencreator")
+ with patch.object(extractor, "_download_webpage", return_value=webpage), \
+ patch.object(extractor, "_download_webpage_handle", return_value=(webpage, SimpleNamespace(url="https://www.bilibili.com/video/BV18Qhf6hE8P?p=1"))), \
+ patch.object(extractor, "_download_json", side_effect=response), \
+ patch.object(extractor, "_sign_wbi", side_effect=lambda params, video_id: params), \
+ patch.object(ydl, "urlopen", side_effect=AssertionError("Unexpected network request")):
+ return ydl.extract_info("https://www.bilibili.com/video/BV18Qhf6hE8P?p=1", download=False)
+
+ def test_challenge_keeps_webpage_audio(self):
+ result = self.extract(playinfo(30216), {"code": 0, "data": {"v_voucher": "secret-voucher"}})
+ self.assertEqual(result["format_id"], "30216")
+ self.assertNotIn("secret-voucher", "\n".join(self.logger.messages))
+
+ def test_empty_api_response_keeps_webpage_audio(self):
+ result = self.extract(playinfo(30216), {"code": 0, "data": {"dash": {"audio": []}}})
+ self.assertEqual(result["format_id"], "30216")
+
+ def test_challenge_keeps_webpage_video_and_audio(self):
+ info = playinfo(30216)
+ info["dash"]["video"] = [{
+ "id": 80, "baseUrl": "https://media.example.com/video.m4s",
+ "mimeType": "video/mp4", "codecs": "avc1.640028", "height": 1080,
+ }]
+ result = self.extract(info, {"code": 0, "data": {"v_voucher": "secret-voucher"}}, "bestvideo+bestaudio")
+ self.assertEqual([stream["format_id"] for stream in result["requested_formats"]], ["80", "30216"])
+
+ def test_malformed_streams_keep_webpage_audio(self):
+ result = self.extract(playinfo(30216), {"code": 0, "data": {"dash": {"audio": [{"id": 30280}]}}})
+ self.assertEqual(result["format_id"], "30216")
+
+ def test_valid_api_formats_still_take_precedence(self):
+ result = self.extract(playinfo(30216), {"code": 0, "data": playinfo(30280)})
+ self.assertEqual(result["format_id"], "30280")
+
+ def test_no_webpage_formats_reports_risk_control(self):
+ with self.assertRaisesRegex(DownloadError, "Bilibili risk control rejected the media request"):
+ self.extract({}, {"code": 0, "data": {"v_voucher": "secret-voucher"}})
+
+ def test_missing_formats_without_challenge_is_not_labeled_risk_control(self):
+ with self.assertRaisesRegex(DownloadError, "No video formats found"):
+ self.extract({}, {"code": 0, "data": {}})
+ self.assertNotIn("risk control", "\n".join(self.logger.messages))
+
+ def test_dolby_flac_and_legacy_formats_are_preserved(self):
+ for info in [
+ {"dash": {"dolby": {"audio": [audio(30280)]}}},
+ {"dash": {"flac": {"audio": audio(30280)}}},
+ {"durl": [{"url": "https://media.example.com/source.mp4", "size": 100, "length": 10000}]},
+ ]:
+ with self.subTest(info=info):
+ result = self.extract({}, {"code": 0, "data": info}, "best")
+ self.assertTrue(result["formats"])
+
+
+unittest.main()
diff --git a/apps/daemon/test/fixtures/yt-dlp-douyin-regression.py b/apps/daemon/test/fixtures/yt-dlp-douyin-regression.py
new file mode 100644
index 00000000..b32ef05d
--- /dev/null
+++ b/apps/daemon/test/fixtures/yt-dlp-douyin-regression.py
@@ -0,0 +1,101 @@
+import io
+import sys
+import unittest
+from types import SimpleNamespace
+from unittest.mock import patch
+
+sys.path.insert(0, sys.argv[1])
+plugin_root = sys.argv[2]
+sys.argv = [sys.argv[0]]
+
+import yt_dlp
+from yt_dlp.extractor.tiktok import DouyinIE as UpstreamDouyinIE
+from yt_dlp.globals import plugin_dirs
+from yt_dlp.networking.common import Response
+from yt_dlp.networking.exceptions import HTTPError
+from yt_dlp.utils import ExtractorError
+
+plugin_dirs.value = [plugin_root]
+URL = "https://www.douyin.com/video/7693859410975690010"
+API = "https://www.douyin.com/aweme/v1/web/aweme/detail/"
+
+
+class DouyinRegression(unittest.TestCase):
+ def setUp(self):
+ self.ydl = yt_dlp.YoutubeDL({"quiet": True, "skip_download": True})
+ self.extractor = self.ydl.get_info_extractor("Douyin")
+ self.assertEqual(self.extractor.PLUGIN_NAME, "opencreator")
+
+ def tearDown(self):
+ self.ydl.close()
+
+ def error(self, body, status=403):
+ response = Response(io.BytesIO(body), API, {}, status=status)
+ return ExtractorError("Unable to download JSON metadata", cause=HTTPError(response))
+
+ def test_signed_detail_request_preserves_upstream_media_parsing(self):
+ detail = {"aweme_id": "7693859410975690010"}
+ with patch.object(self.extractor, "_get_cookies", return_value={"UIFID": SimpleNamespace(value="private-identity")}), \
+ patch.object(UpstreamDouyinIE, "_download_json", return_value={"aweme_detail": detail}) as request, \
+ patch.object(self.extractor, "_parse_aweme_video_app", return_value={"id": detail["aweme_id"]}) as parse:
+ self.assertEqual(self.extractor._real_extract(URL), {"id": detail["aweme_id"]})
+ self.assertEqual(request.call_args.kwargs["headers"]["uifid"], "private-identity")
+ self.assertEqual(request.call_args.kwargs["query"], {
+ "aweme_id": detail["aweme_id"], "uifid": "private-identity",
+ "x-secsdk-web-signature": "62911035327db934b912fe8bcdc903bc",
+ })
+ self.assertTrue(request.call_args.kwargs["fatal"])
+ parse.assert_called_once_with(detail)
+
+ def test_temporary_identity_cookie_can_sign_without_a_logged_in_account(self):
+ with patch.object(self.extractor, "_get_cookies", return_value={"UIFID_TEMP": SimpleNamespace(value="private-identity")}), \
+ patch.object(UpstreamDouyinIE, "_download_json", return_value={}) as request:
+ self.extractor._download_json(API, "id", query={"aweme_id": "7693859410975690010"})
+ self.assertEqual(request.call_args.kwargs["query"]["x-secsdk-web-signature"], "62911035327db934b912fe8bcdc903bc")
+
+ def test_signature_matches_urlsearchparams_encoding_and_optional_timestamp(self):
+ query = {"aweme_id": "7693859410975690010", "timestamp": "1787265814"}
+ with patch.object(self.extractor, "_get_cookies", return_value={"UIFID": SimpleNamespace(value="identity /+~*&")}), \
+ patch.object(UpstreamDouyinIE, "_download_json", return_value={}) as request:
+ self.extractor._download_json(API, "id", query=query)
+ self.assertEqual(request.call_args.kwargs["query"]["x-secsdk-web-signature"], "b930c2b3bf997a7ba1d52f1c6216d5cb")
+ self.assertEqual(query, {"aweme_id": "7693859410975690010", "timestamp": "1787265814"})
+
+ def test_upstream_signed_requests_are_preserved(self):
+ query = {"aweme_id": "id", "uifid": "upstream-identity", "x-secsdk-web-signature": "upstream-signature"}
+ with patch.object(self.extractor, "_get_cookies", return_value={"UIFID": SimpleNamespace(value="other-identity")}), \
+ patch.object(UpstreamDouyinIE, "_download_json", return_value={}) as request:
+ self.extractor._download_json(API, "id", query=query)
+ self.assertEqual(request.call_args.kwargs["query"], query)
+ self.assertEqual(request.call_args.kwargs["headers"]["uifid"], "upstream-identity")
+
+ def test_signature_rejection_is_not_mislabeled_as_stale_cookies(self):
+ with patch.object(UpstreamDouyinIE, "_download_json", side_effect=self.error(
+ b"Blocked by ArgusSecurityPlugin Signature Not Found private-server-token")):
+ with self.assertRaises(ExtractorError) as result:
+ self.extractor._real_extract(URL)
+ message = str(result.exception)
+ self.assertIn("Douyin request signature is required", message)
+ self.assertIn("HTTP Error 403", message)
+ self.assertNotIn("Fresh cookies", message)
+ self.assertNotIn("private-server-token", message)
+
+ def test_missing_identity_is_reported_as_a_connection_requirement(self):
+ with patch.object(UpstreamDouyinIE, "_download_json", side_effect=self.error(
+ b"Blocked by ArgusSecurityPlugin Uifid Not Found")):
+ with self.assertRaisesRegex(ExtractorError, "Douyin browser identity was rejected"):
+ self.extractor._real_extract(URL)
+
+ def test_unrecognized_nonfatal_rejection_preserves_upstream_behavior(self):
+ with patch.object(UpstreamDouyinIE, "_download_json", side_effect=self.error(b"unrecognized")), \
+ patch.object(self.extractor, "report_warning"):
+ with self.assertRaisesRegex(ExtractorError, "Fresh cookies"):
+ self.extractor._real_extract(URL)
+
+ def test_other_json_endpoints_keep_their_original_headers_and_error_policy(self):
+ with patch.object(UpstreamDouyinIE, "_download_json", return_value={}) as request:
+ self.extractor._download_json("https://api.example.com/metadata", "id", headers={"test": "value"}, fatal=False)
+ self.assertEqual(request.call_args.kwargs, {"headers": {"test": "value"}, "fatal": False})
+
+
+unittest.main()
diff --git a/apps/daemon/test/integration/creator-yt-dlp-bilibili.test.ts b/apps/daemon/test/integration/creator-yt-dlp-bilibili.test.ts
new file mode 100644
index 00000000..d72b8f7e
--- /dev/null
+++ b/apps/daemon/test/integration/creator-yt-dlp-bilibili.test.ts
@@ -0,0 +1,28 @@
+import { execFile } from 'node:child_process';
+import { existsSync, readFileSync } from 'node:fs';
+import { resolve } from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { promisify } from 'node:util';
+import { expect, it } from 'vitest';
+import type { KrillinRuntimeManifest } from '../../src/creator/krillin/manifest.js';
+import { resolveYtDlpRuntime } from '../../src/creator/yt-dlp/runtime.js';
+
+const resourceRoot = process.env.OPENCREATOR_CREATOR_RUNTIME_ROOT
+ ?? fileURLToPath(new URL('../../../desktop/.pack/creator-runtime/krillinai', import.meta.url));
+const manifestPath = resolve(resourceRoot, 'manifest.json');
+const manifest = existsSync(manifestPath)
+ ? JSON.parse(readFileSync(manifestPath, 'utf8')) as KrillinRuntimeManifest : undefined;
+const runtime = manifest?.ytDlp?.mode === 'python'
+ ? resolveYtDlpRuntime({ resourceRoot, manifest }) : undefined;
+
+it.runIf(runtime !== undefined && existsSync(runtime.executable) && existsSync(runtime.script!))(
+ 'loads the Bilibili plugin and preserves media through real yt-dlp extraction with recorded response shapes',
+ async () => {
+ const { stderr } = await promisify(execFile)(runtime!.executable, [
+ '-I', '-B', fileURLToPath(new URL('../fixtures/yt-dlp-bilibili-regression.py', import.meta.url)),
+ runtime!.script!, fileURLToPath(new URL('../../runtime/yt-dlp', import.meta.url))
+ ], { env: { ...process.env, ...runtime!.env }, timeout: 20_000 });
+ expect(stderr).toContain('Ran 8 tests');
+ expect(stderr).toMatch(/\bOK\b/);
+ }, 25_000
+);
diff --git a/apps/daemon/test/integration/creator-yt-dlp-douyin.test.ts b/apps/daemon/test/integration/creator-yt-dlp-douyin.test.ts
new file mode 100644
index 00000000..c0a79dc6
--- /dev/null
+++ b/apps/daemon/test/integration/creator-yt-dlp-douyin.test.ts
@@ -0,0 +1,28 @@
+import { execFile } from 'node:child_process';
+import { existsSync, readFileSync } from 'node:fs';
+import { resolve } from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { promisify } from 'node:util';
+import { expect, it } from 'vitest';
+import type { KrillinRuntimeManifest } from '../../src/creator/krillin/manifest.js';
+import { resolveYtDlpRuntime } from '../../src/creator/yt-dlp/runtime.js';
+
+const resourceRoot = process.env.OPENCREATOR_CREATOR_RUNTIME_ROOT
+ ?? fileURLToPath(new URL('../../../desktop/.pack/creator-runtime/krillinai', import.meta.url));
+const manifestPath = resolve(resourceRoot, 'manifest.json');
+const manifest = existsSync(manifestPath)
+ ? JSON.parse(readFileSync(manifestPath, 'utf8')) as KrillinRuntimeManifest : undefined;
+const runtime = manifest?.ytDlp?.mode === 'python'
+ ? resolveYtDlpRuntime({ resourceRoot, manifest }) : undefined;
+
+it.runIf(runtime !== undefined && existsSync(runtime.executable) && existsSync(runtime.script!))(
+ 'loads the Douyin plugin, signs detail requests and preserves upstream parsing and rejection reasons',
+ async () => {
+ const { stderr } = await promisify(execFile)(runtime!.executable, [
+ '-I', '-B', fileURLToPath(new URL('../fixtures/yt-dlp-douyin-regression.py', import.meta.url)),
+ runtime!.script!, fileURLToPath(new URL('../../runtime/yt-dlp', import.meta.url))
+ ], { env: { ...process.env, ...runtime!.env }, timeout: 20_000 });
+ expect(stderr).toContain('Ran 8 tests');
+ expect(stderr).toMatch(/\bOK\b/);
+ }, 25_000
+);
diff --git a/apps/daemon/test/integration/douyin-connection.test.ts b/apps/daemon/test/integration/douyin-connection.test.ts
new file mode 100644
index 00000000..7c2252a6
--- /dev/null
+++ b/apps/daemon/test/integration/douyin-connection.test.ts
@@ -0,0 +1,120 @@
+import { access, mkdtemp, readFile, readdir, rm, stat, writeFile } from 'node:fs/promises';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import Fastify from 'fastify';
+import { afterEach, expect, it, vi } from 'vitest';
+import { PlatformConnectionService } from '../../src/platform-connections/service.js';
+import { filterPlatformCookies, hasPlatformSession, parsePlatformCookies, serializePlatformCookies } from '../../src/platform-connections/cookies.js';
+import { readBrowserPlatformCookies, type BrowserProfile } from '../../src/platform-connections/browser.js';
+import { registerPlatformConnectionRoutes } from '../../src/api/routes.platform-connections.js';
+import { requireAuth } from '../../src/api/auth.js';
+
+const cookie = { name: 's_v_web_id', value: 'private-verification-session', domain: '.douyin.com',
+ path: '/', secure: true, httpOnly: true, expires: 0 };
+const profile: BrowserProfile = { id: 'b'.repeat(24), browser: 'chrome', label: 'Chrome Test', directory: '/private/profile', preferred: true };
+const runtime = { executable: '/yt-dlp', prefixArgs: ['--plugin-dirs', '/plugins'], version: 'test', env: {} };
+const roots: string[] = [];
+const services: PlatformConnectionService[] = [];
+afterEach(async () => {
+ await Promise.all(services.splice(0).map(service => service.close()));
+ await Promise.all(roots.splice(0).map(root => rm(root, { recursive: true, force: true })));
+});
+
+async function setup(readBrowserCookies?: typeof readBrowserPlatformCookies, platform: 'youtube' | 'douyin' = 'douyin') {
+ const root = await mkdtemp(join(tmpdir(), 'douyin-connection-'));
+ roots.push(root);
+ const service = new PlatformConnectionService({ root, platform, getRuntime: () => runtime,
+ listProfiles: async () => [profile], readBrowserCookies });
+ services.push(service);
+ await service.initialize();
+ return { root, service };
+}
+
+it('accepts anonymous Douyin verification cookies and filters other platforms, expired cookies and injected fields', () => {
+ const filtered = filterPlatformCookies([cookie, { ...cookie, domain: '.youtube.com' },
+ { ...cookie, domain: '.douyin.com.evil.test' }, { ...cookie, expires: 1 },
+ { ...cookie, value: 'injected\nvalue' }], 'douyin');
+ expect(filtered).toEqual([cookie]);
+ expect(hasPlatformSession(filtered, 'douyin')).toBe(true);
+ expect(parsePlatformCookies(serializePlatformCookies(filtered, 'douyin'), 'douyin')).toEqual([cookie]);
+ expect(filterPlatformCookies(filtered, 'youtube')).toEqual([]);
+});
+
+it('reads only the explicitly selected platform through the browser cookie adapter', async () => {
+ if (process.platform === 'win32') return;
+ const { root } = await setup();
+ const executable = join(root, 'python-fixture');
+ await writeFile(executable, `#!/bin/sh\n[ "$8" = "douyin.com" ] || exit 1\nprintf '%s' '${JSON.stringify({ cookies: [cookie, { ...cookie, domain: '.youtube.com' }] })}'\n`, { mode: 0o700 });
+ expect(await readBrowserPlatformCookies({ profile, platform: 'douyin',
+ runtime: { ...runtime, executable, script: '/yt-dlp.zip' }, signal: new AbortController().signal })).toEqual([cookie]);
+});
+
+it('connects without requiring a logged-in account and returns no credentials in public state', async () => {
+ const read = vi.fn(async () => [cookie, { ...cookie, domain: '.youtube.com' }]);
+ const { root, service } = await setup(read);
+ expect((await service.connect(profile.id)).status).toBe('connecting');
+ await vi.waitFor(async () => expect((await service.getState()).status).toBe('connected'));
+ expect(read).toHaveBeenCalledWith(expect.objectContaining({ platform: 'douyin', profile }));
+ expect(JSON.stringify(await service.getState())).not.toMatch(/private-verification-session|s_v_web_id|\/private\/profile/);
+ expect(JSON.parse(await readFile(join(root, 'credentials.json'), 'utf8')).cookies).toEqual([cookie]);
+ if (process.platform !== 'win32') expect((await stat(join(root, 'credentials.json'))).mode & 0o777).toBe(0o600);
+});
+
+it('isolates concurrent task files and platform credentials and removes snapshots after failure', async () => {
+ const { root, service } = await setup();
+ const { service: youtube } = await setup(undefined, 'youtube');
+ await service.importCookies(serializePlatformCookies([cookie], 'douyin'));
+ await youtube.importCookies(serializePlatformCookies(['SID', 'SAPISID'].map(name => ({ ...cookie, name,
+ domain: '.youtube.com', value: 'private-youtube' })), 'youtube'));
+ const paths: string[] = [];
+ await youtube.withCookies(() => Promise.all(Array.from({ length: 3 }, () => service.withCookies(async () => {
+ const path = service.authenticatedRuntime(runtime)!.prefixArgs.at(-1)!;
+ paths.push(path);
+ const text = await readFile(path, 'utf8');
+ expect(text).toContain('private-verification-session');
+ expect(text).not.toContain('youtube');
+ expect(await readFile(youtube.authenticatedRuntime(runtime)!.prefixArgs.at(-1)!, 'utf8')).not.toContain('douyin');
+ }))));
+ expect(new Set(paths).size).toBe(3);
+ for (const path of paths) await expect(access(path)).rejects.toThrow();
+ await expect(service.withCookies(async () => { throw new Error('HTTP Error 403'); })).rejects.toThrow('403');
+ expect((await service.getState()).status).toBe('connected');
+ expect(await readdir(root)).toEqual(['credentials.json']);
+});
+
+it('cancels a browser read, preserves the saved session, and requires explicit reconnect after expiry', async () => {
+ const read: typeof readBrowserPlatformCookies = ({ signal }) => new Promise((_resolve, reject) => {
+ signal.addEventListener('abort', () => reject(new Error('cancelled')), { once: true });
+ });
+ const { service } = await setup(read);
+ await service.importCookies(serializePlatformCookies([cookie], 'douyin'));
+ await service.connect();
+ expect((await service.cancel()).status).toBe('connected');
+ await expect(service.withCookies(async () => { throw new Error('The cookies are no longer valid'); })).rejects.toThrow('no longer valid');
+ expect((await service.getState()).status).toBe('expired');
+ await service.withCookies(async () => expect(service.authenticatedRuntime(runtime)).toBe(runtime));
+ await service.disconnect();
+ expect((await service.getState()).status).toBe('disconnected');
+});
+
+it('protects the Douyin API, rejects foreign cookies and invalid profiles, and disconnects only Douyin', async () => {
+ const { service } = await setup();
+ const { service: youtube } = await setup(undefined, 'youtube');
+ const server = Fastify();
+ server.addHook('onRequest', requireAuth('runtime-token'));
+ await registerPlatformConnectionRoutes(server, youtube, service);
+ const headers = { authorization: 'Bearer runtime-token' };
+ try {
+ expect((await server.inject({ url: '/platform-connections/douyin' })).statusCode).toBe(401);
+ expect((await server.inject({ method: 'POST', url: '/platform-connections/douyin/connect', headers, payload: { profileId: '../profile' } })).statusCode).toBe(400);
+ const foreign = serializePlatformCookies(['SID', 'SAPISID'].map(name => ({ ...cookie, name, domain: '.youtube.com' })), 'youtube');
+ expect((await server.inject({ method: 'POST', url: '/platform-connections/douyin/import', headers, payload: { cookies: foreign } })).statusCode).toBe(400);
+ const imported = await server.inject({ method: 'POST', url: '/platform-connections/douyin/import', headers, payload: { cookies: serializePlatformCookies([cookie], 'douyin') } });
+ expect(imported.statusCode).toBe(200);
+ expect(imported.headers['cache-control']).toBe('no-store');
+ expect(imported.json()).toMatchObject({ platform: 'douyin', status: 'connected' });
+ expect(imported.body).not.toContain(cookie.value);
+ expect((await youtube.getState()).status).toBe('disconnected');
+ expect((await server.inject({ method: 'DELETE', url: '/platform-connections/douyin', headers })).json().status).toBe('disconnected');
+ } finally { await server.close(); }
+});
diff --git a/apps/daemon/test/integration/video-metadata-api.test.ts b/apps/daemon/test/integration/video-metadata-api.test.ts
index 82c92c5f..f623597a 100644
--- a/apps/daemon/test/integration/video-metadata-api.test.ts
+++ b/apps/daemon/test/integration/video-metadata-api.test.ts
@@ -72,15 +72,34 @@ describe('video metadata API', () => {
expect(fetchImpl).not.toHaveBeenCalled();
});
- it('resolves a Douyin share link into metadata and an online video URL without downloading', async () => {
+ it.each([
+ ['https://v.douyin.com/example/', 'https://v.douyin.com/example/'],
+ ['https://www.douyin.com/jingxuan/search/qianjidong?aid=2052a92f-0793-47c1-b739-c0af829b6103&modal_id=7693859410975690010&type=general',
+ 'https://www.douyin.com/video/7693859410975690010']
+ ])('resolves a Douyin link into metadata and an online video URL without downloading: %s', async (url, expectedUrl) => {
const payload = { title: 'Douyin preview', thumbnail: 'https://media.example.com/cover.jpg',
formats: [{ url: 'https://media.example.com/video.mp4', ext: 'mp4', protocol: 'https', vcodec: 'h264', acodec: 'aac', width: 1080, height: 1920 }] };
- await registerVideoMetadataRoutes(server, createVideoMetadataService({ getProxy: () => '', getYtDlpRuntime: () => ({
- version: 'test', executable: process.execPath, prefixArgs: ['-e', `if(!process.argv.includes('--skip-download'))process.exit(1);process.stdout.write(${JSON.stringify(JSON.stringify(payload))})`, '--'], env: {}
- }) }));
- const response = await server.inject({ method: 'GET', url: `/video-metadata?${new URLSearchParams({ url: 'https://v.douyin.com/example/' })}` });
+ let inDouyinScope = false;
+ const youtubeScope = vi.fn();
+ const getYtDlpRuntime = vi.fn((sourceUrl?: string) => {
+ expect(inDouyinScope).toBe(true);
+ expect(sourceUrl).toBe(expectedUrl);
+ return {
+ version: 'test', executable: process.execPath, prefixArgs: ['-e', `if(!process.argv.includes('--skip-download')||!process.argv.includes(${JSON.stringify(expectedUrl)}))process.exit(1);process.stdout.write(${JSON.stringify(JSON.stringify(payload))})`, '--'], env: {}
+ };
+ });
+ await registerVideoMetadataRoutes(server, createVideoMetadataService({ getProxy: () => '', getYtDlpRuntime,
+ withYouTubeCookies: async operation => { youtubeScope(); return operation(); },
+ withDouyinCookies: async operation => {
+ inDouyinScope = true;
+ try { return await operation(); } finally { inDouyinScope = false; }
+ }
+ }));
+ const response = await server.inject({ method: 'GET', url: `/video-metadata?${new URLSearchParams({ url })}` });
expect(response.statusCode).toBe(200);
expect(response.json()).toMatchObject({ platform: 'douyin', title: 'Douyin preview', previewUrl: 'https://media.example.com/video.mp4', width: 1080, height: 1920 });
+ expect(getYtDlpRuntime).toHaveBeenCalledWith(expectedUrl);
+ expect(youtubeScope).not.toHaveBeenCalled();
});
it('returns title information for a Bilibili video', async () => {
diff --git a/apps/daemon/test/unit/creator-download-executor.test.ts b/apps/daemon/test/unit/creator-download-executor.test.ts
index 4be43c54..1c6f93e0 100644
--- a/apps/daemon/test/unit/creator-download-executor.test.ts
+++ b/apps/daemon/test/unit/creator-download-executor.test.ts
@@ -437,13 +437,15 @@ describe('creator download executor', () => {
}
});
- it('converts a Douyin jingxuan modal link into a video URL for probing and downloading', async () => {
+ it.each([
+ ['https://www.douyin.com/jingxuan?modal_id=7687030616353823355', 'https://www.douyin.com/video/7687030616353823355'],
+ ['https://www.douyin.com/jingxuan/search/qianjidong?aid=2052a92f-0793-47c1-b739-c0af829b6103&modal_id=7693859410975690010&type=general',
+ 'https://www.douyin.com/video/7693859410975690010']
+ ])('converts a Douyin jingxuan modal link into a video URL for probing and downloading: %s', async (sourceUrl, videoUrl) => {
const binaries = await fakeBinaries();
const executor = createDownloadExecutor(binaries);
const workdir = join(tempDir, 'douyin-featured');
await mkdir(workdir, { recursive: true });
- const sourceUrl = 'https://www.douyin.com/jingxuan?modal_id=7687030616353823355';
- const videoUrl = 'https://www.douyin.com/video/7687030616353823355';
const result = await executor.run(stageInput({
workdir, stageId: 'probe', state: { sourceUrl }
}));
@@ -470,6 +472,8 @@ describe('creator download executor', () => {
for (const sourceUrl of [
'https://www.douyin.com/jingxuan',
'https://www.douyin.com/jingxuan?modal_id=abc',
+ 'https://www.douyin.com/jingxuan/search/qianjidong',
+ 'https://www.douyin.com/jingxuan/search/qianjidong?modal_id=abc',
'https://notdouyin.com/jingxuan?modal_id=123'
]) {
await expect(executor.run(stageInput({
@@ -487,6 +491,44 @@ describe('creator download executor', () => {
}))).rejects.toMatchObject({ code: 'login_required' });
});
+ it('preserves the Douyin session requirement when the preceding API warning contains HTTP 403', async () => {
+ const binaries = await fakeBinaries({ failure: 'WARNING: [Douyin] Failed to download web detail JSON: HTTP Error 403: Forbidden\nERROR: [Douyin] Fresh cookies (not necessarily logged in) are needed' });
+ const executor = createDownloadExecutor(binaries);
+ await expect(executor.run(stageInput({
+ workdir: tempDir, stageId: 'probe', state: { sourceUrl: 'https://www.douyin.com/video/7693859410975690010' }
+ }))).rejects.toMatchObject({
+ code: 'platform_session_required',
+ publicFacts: { kind: 'unauthorized', provider: 'yt-dlp', httpStatus: 403,
+ upstreamMessage: expect.stringContaining('Fresh cookies (not necessarily logged in) are needed') }
+ });
+ });
+
+ it('selects the runtime session using the queued source instead of the edited draft URL', async () => {
+ const binaries = await fakeBinaries();
+ const sourceUrl = 'https://www.douyin.com/video/7693859410975690010';
+ const workdir = join(tempDir, 'queued-douyin-cookie');
+ await mkdir(workdir, { recursive: true });
+ const probeArtifact = await writeProbeArtifact(workdir, { ...parsedProbe(), requestedUrl: sourceUrl, url: sourceUrl });
+ const getYtDlpRuntime = vi.fn(() => ({ executable: binaries.ytDlpPath,
+ prefixArgs: [...binaries.ytDlpPrefixArgs!, '--cookies', '/test/douyin-cookies.txt'], env: {}, version: 'test' }));
+ const input = stageInput({ workdir, stageId: 'download',
+ state: { sourceUrl: 'https://www.youtube.com/watch?v=draft', mediaType: 'video', selectedOptionId: 'video-360-2' },
+ inputArtifacts: [probeArtifact] });
+ input.stageRun.progress.sourceUrl = sourceUrl;
+ await createDownloadExecutor({ ...binaries, getYtDlpRuntime }).run(input);
+ expect(getYtDlpRuntime).toHaveBeenCalledWith(sourceUrl);
+ expect(JSON.parse(await readFile(join(workdir, 'args.json'), 'utf8'))).toEqual(expect.arrayContaining(['--cookies', '/test/douyin-cookies.txt', sourceUrl]));
+ });
+
+ it.each(['Douyin', 'Douyin+opencreator'])('distinguishes the confirmed %s signature incompatibility from cookie and model permissions', async prefix => {
+ const binaries = await fakeBinaries({ failure: `ERROR: [${prefix}] Douyin request signature is required (HTTP Error 403; Signature Not Found). Reconnecting cookies alone cannot fix it.` });
+ await expect(createDownloadExecutor(binaries).run(stageInput({ workdir: tempDir, stageId: 'probe',
+ state: { sourceUrl: 'https://www.douyin.com/video/7693859410975690010' }
+ }))).rejects.toMatchObject({ code: 'platform_request_signature_required',
+ publicFacts: { kind: 'unsupported', provider: 'yt-dlp', httpStatus: 403,
+ upstreamMessage: expect.stringContaining('Signature Not Found') } });
+ });
+
it('recognizes Instagram extractor formats', () => {
for (const extractor_key of ['Instagram', 'Instagram:Story']) {
const probe = parseDownloadProbe({
@@ -1322,6 +1364,33 @@ describe('creator download executor', () => {
});
});
+ it('identifies a Bilibili risk-control rejection even when the extractor also reports no formats', async () => {
+ const binaries = await fakeBinaries({
+ failure: 'WARNING: Bilibili risk control rejected the playback API request\nERROR: No video formats found!'
+ });
+ const workdir = join(tempDir, 'bilibili-risk-control-work');
+ await mkdir(workdir, { recursive: true });
+ const executor = createDownloadExecutor(binaries);
+ await expect(executor.run(stageInput({
+ workdir, stageId: 'probe', state: { sourceUrl: 'https://www.bilibili.com/video/BV18Qhf6hE8P?p=1' }
+ }))).rejects.toMatchObject({
+ code: 'download_failed',
+ message: 'Bilibili risk control rejected the media request. Wait and retry, or use a local video file.',
+ publicFacts: { kind: 'http-rejected', provider: 'yt-dlp' }
+ });
+ });
+
+ it('does not attribute a later format selection failure to successful Bilibili risk-control fallback', async () => {
+ const binaries = await fakeBinaries({
+ failure: 'WARNING: Bilibili risk control rejected the playback API request; retaining webpage media formats when available\nERROR: Requested format is not available'
+ });
+ const workdir = join(tempDir, 'bilibili-format-work');
+ await mkdir(workdir, { recursive: true });
+ await expect(createDownloadExecutor(binaries).run(stageInput({
+ workdir, stageId: 'probe', state: { sourceUrl: 'https://www.bilibili.com/video/BV18Qhf6hE8P?p=1' }
+ }))).rejects.toMatchObject({ publicFacts: { kind: 'not-found' } });
+ });
+
it('rejects misleading domains that only end with a supported domain name', async () => {
const config = createDefaultCreatorServicesConfig();
const executor = createDownloadExecutor({
diff --git a/apps/daemon/test/unit/creator-krillin-adapter-dependency.test.ts b/apps/daemon/test/unit/creator-krillin-adapter-dependency.test.ts
index 230f163e..27a010dc 100644
--- a/apps/daemon/test/unit/creator-krillin-adapter-dependency.test.ts
+++ b/apps/daemon/test/unit/creator-krillin-adapter-dependency.test.ts
@@ -36,6 +36,8 @@ describe('KrillinAI configured transcription dependency', () => {
it.each([
['分享视频 https://v.douyin.com/abc123/ 复制打开抖音', 'https://v.douyin.com/abc123/'],
['https://www.douyin.com/jingxuan?modal_id=123', 'https://www.douyin.com/video/123'],
+ ['https://www.douyin.com/jingxuan/search/qianjidong?aid=2052a92f-0793-47c1-b739-c0af829b6103&modal_id=7693859410975690010&type=general',
+ 'https://www.douyin.com/video/7693859410975690010'],
['https://www.xiaohongshu.com/explore/6a9149f3000000001f01d20a?xsec_token=sample%3D',
'https://www.xiaohongshu.com/explore/6a9149f3000000001f01d20a?xsec_token=sample%3D']
])('transcribes added translation sources with normalized URLs and preserved tokens: %s', async (sourceUrl, expected) => {
@@ -53,6 +55,7 @@ describe('KrillinAI configured transcription dependency', () => {
expect(fixture.ensure).toHaveBeenCalledTimes(1);
expect(runKrillinCli).toHaveBeenCalledTimes(1);
expect(runKrillinCli.mock.calls[0]?.[0]).toMatchObject({ source: expected, options: { sourceUrl: expected } });
+ expect(fixture.getYtDlpRuntime).toHaveBeenCalledWith(expected);
expect(runKrillinCli.mock.calls[0]?.[0].options.captionSource).not.toBe('platform');
});
it('uses the original YouTube URL without preparing transcription when platform captions succeed', async () => {
@@ -283,9 +286,11 @@ function setup() {
config
});
const ensure = vi.fn(async (_input: { reportProgress(progress: Record<string, unknown>): void }) => undefined);
+ const getYtDlpRuntime = vi.fn(() => undefined);
const executor = createKrillinExecutor({
resourceRoot,
jobsRoot,
+ getYtDlpRuntime,
configStore: {
read: vi.fn(async () => config)
} as never,
@@ -320,7 +325,7 @@ function setup() {
workdir: join(jobsRoot, 'auto-clip-job', 'subtitle-stage'),
reportProgress: vi.fn()
} as never;
- return { ensure, executor, stage };
+ return { ensure, executor, stage, getYtDlpRuntime };
}
function writeTargetSubtitle(jobsRoot: string) {
diff --git a/apps/daemon/test/unit/creator-krillin-error-mapping.test.ts b/apps/daemon/test/unit/creator-krillin-error-mapping.test.ts
index c86beef4..ca11619d 100644
--- a/apps/daemon/test/unit/creator-krillin-error-mapping.test.ts
+++ b/apps/daemon/test/unit/creator-krillin-error-mapping.test.ts
@@ -1,7 +1,32 @@
import { describe, expect, it } from 'vitest';
import { normalizeKrillinFailure } from '../../src/creator/krillin/adapter.js';
+import { KrillinCliError } from '../../src/creator/krillin/cli-runner.js';
describe('KrillinAI error mapping', () => {
+ it.each(['Douyin', 'Douyin+opencreator'])('preserves the confirmed %s signature incompatibility in translation failures', prefix => {
+ const error = new KrillinCliError('prepare_media_failed',
+ `ERROR: [${prefix}] Douyin request signature is required (HTTP Error 403; Signature Not Found).`);
+ expect(normalizeKrillinFailure(error).code).toBe('platform_request_signature_required');
+ expect(error.publicFacts).toMatchObject({ kind: 'unsupported', httpStatus: 403, upstreamCode: 'prepare_media_failed' });
+ });
+ it.each(['prepare_media_failed', 'prepare_audio_fallback_failed'])('maps the confirmed Douyin session requirement during %s and preserves upstream evidence', code => {
+ const message = 'linkToFile download audio yt-dlp error: exit status 1: WARNING: [Douyin] Failed to download web detail JSON: HTTP Error 403: Forbidden\nERROR: [Douyin] Fresh cookies (not necessarily logged in) are needed';
+ const error = new KrillinCliError(code, message);
+ expect(normalizeKrillinFailure(error)).toEqual({ code: 'platform_session_required', message });
+ expect(error.publicFacts).toMatchObject({ kind: 'unauthorized', provider: 'krillinai', httpStatus: 403,
+ upstreamCode: code, upstreamMessage: expect.stringContaining('Fresh cookies (not necessarily logged in) are needed') });
+ expect(normalizeKrillinFailure(new KrillinCliError(code, 'HTTP Error 403: Forbidden')).code).toBe(code);
+ });
+ it('reports a confirmed Bilibili risk-control rejection without inventing an HTTP status', () => {
+ const error = new KrillinCliError('prepare_media_failed',
+ 'Bilibili risk control blocked media extraction after 3 attempts; wait and retry');
+ expect(error.publicFacts).toMatchObject({ kind: 'http-rejected', provider: 'krillinai' });
+ expect(error.publicFacts.httpStatus).toBeUndefined();
+ expect(new KrillinCliError('prepare_media_failed', 'No video formats found!').publicFacts.kind).toBe('unknown');
+ expect(new KrillinCliError('prepare_media_failed',
+ 'WARNING: Bilibili risk control rejected the playback API request; retaining webpage media formats when available\nERROR: Requested format is not available'
+ ).publicFacts.kind).toBe('unknown');
+ });
it('maps generic CLI usage errors to actionable creator configuration codes', () => {
expect(normalizeKrillinFailure({
code: 'usage',
diff --git a/apps/daemon/test/unit/creator-video-translation-workflow.test.ts b/apps/daemon/test/unit/creator-video-translation-workflow.test.ts
index 6807b9f9..8665c02a 100644
--- a/apps/daemon/test/unit/creator-video-translation-workflow.test.ts
+++ b/apps/daemon/test/unit/creator-video-translation-workflow.test.ts
@@ -96,6 +96,7 @@ describe('video translation workflow', () => {
'https://www.instagram.com/reel/abc123/',
'分享视频 https://v.douyin.com/abc123/ 复制打开抖音',
'https://www.douyin.com/jingxuan?modal_id=123',
+ 'https://www.douyin.com/jingxuan/search/qianjidong?aid=2052a92f-0793-47c1-b739-c0af829b6103&modal_id=7693859410975690010&type=general',
'https://www.facebook.com/watch/?v=123',
'https://www.xiaohongshu.com/explore/6a9149f3000000001f01d20a?xsec_token=sample%3D',
'https://www.pinterest.com/pin/123/'
diff --git a/apps/daemon/test/unit/creator-yt-dlp-runtime.test.ts b/apps/daemon/test/unit/creator-yt-dlp-runtime.test.ts
index f3123205..fb69b90a 100644
--- a/apps/daemon/test/unit/creator-yt-dlp-runtime.test.ts
+++ b/apps/daemon/test/unit/creator-yt-dlp-runtime.test.ts
@@ -1,11 +1,13 @@
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
+import { fileURLToPath } from 'node:url';
import { afterEach, describe, expect, it } from 'vitest';
import type { KrillinRuntimeManifest } from '../../src/creator/krillin/manifest.js';
import { resolveYtDlpRuntime } from '../../src/creator/yt-dlp/runtime.js';
let root = '';
+const pluginArgs = ['--plugin-dirs', fileURLToPath(new URL('../../runtime/yt-dlp', import.meta.url))];
afterEach(async () => {
if (root) await rm(root, { recursive: true, force: true });
@@ -45,7 +47,7 @@ describe('yt-dlp runtime resolver', () => {
})).toEqual({
version: '2026.08.29.232711',
executable,
- prefixArgs: ['-I', '-B', script, '--js-runtimes', `node:${node}`],
+ prefixArgs: ['-I', '-B', script, ...pluginArgs, '--js-runtimes', `node:${node}`],
env: {
SSL_CERT_FILE: certificateBundle
},
@@ -80,7 +82,7 @@ describe('yt-dlp runtime resolver', () => {
})).toEqual({
version: '2026.08.29.232711',
executable,
- prefixArgs: [],
+ prefixArgs: pluginArgs,
env: {}
});
@@ -90,7 +92,7 @@ describe('yt-dlp runtime resolver', () => {
})).toEqual({
version: 'legacy',
executable,
- prefixArgs: [],
+ prefixArgs: pluginArgs,
env: {}
});
});
@@ -127,7 +129,7 @@ describe('yt-dlp runtime resolver', () => {
})).toEqual({
version: 'external',
executable: resolve(overridePath),
- prefixArgs: [],
+ prefixArgs: pluginArgs,
env: {}
});
});
diff --git a/apps/daemon/test/unit/creator-yt-dlp-update-manager.test.ts b/apps/daemon/test/unit/creator-yt-dlp-update-manager.test.ts
index c5925f5e..e9bfac35 100644
--- a/apps/daemon/test/unit/creator-yt-dlp-update-manager.test.ts
+++ b/apps/daemon/test/unit/creator-yt-dlp-update-manager.test.ts
@@ -65,6 +65,7 @@ describe('yt-dlp update manager', () => {
const release = releaseFixture(LATEST_VERSION);
const fetchImpl = releaseFetch(release);
const updatesRoot = join(root, 'updates');
+ bundledRuntime.prefixArgs.push('--plugin-dirs', join(root, 'plugins'));
const manager = await createManager(updatesRoot, bundledRuntime, fetchImpl);
await manager.update();
@@ -83,6 +84,9 @@ describe('yt-dlp update manager', () => {
script: join(updatesRoot, 'versions', LATEST_VERSION, 'yt-dlp')
});
expect(manager.getRuntime().script).not.toBe(bundledRuntime.script);
+ expect(manager.getRuntime().prefixArgs).toEqual([
+ manager.getRuntime().script, '--plugin-dirs', join(root, 'plugins')
+ ]);
const restored = await createManager(updatesRoot, bundledRuntime, fetchImpl);
expect(restored.status()).toMatchObject({
@@ -93,6 +97,7 @@ describe('yt-dlp update manager', () => {
expect(restored.getRuntime().script).toBe(
join(updatesRoot, 'versions', LATEST_VERSION, 'yt-dlp')
);
+ expect(restored.getRuntime().prefixArgs.slice(1)).toEqual(['--plugin-dirs', join(root, 'plugins')]);
expect(fetchImpl).toHaveBeenCalledTimes(2);
});
diff --git a/apps/web/src/features/dashboard/VideoDownloadWorkspace.test.tsx b/apps/web/src/features/dashboard/VideoDownloadWorkspace.test.tsx
index 68f9454d..c7b622eb 100644
--- a/apps/web/src/features/dashboard/VideoDownloadWorkspace.test.tsx
+++ b/apps/web/src/features/dashboard/VideoDownloadWorkspace.test.tsx
@@ -587,6 +587,7 @@ describe('VideoDownloadWorkspace', () => {
'https://www.douyin.com/video/123',
'https://v.douyin.com/abc123/',
'https://www.douyin.com/jingxuan?modal_id=7687030616353823355',
+ 'https://www.douyin.com/jingxuan/search/qianjidong?aid=2052a92f-0793-47c1-b739-c0af829b6103&modal_id=7693859410975690010&type=general',
'https://www.facebook.com/watch/?v=123',
'https://www.facebook.com/reel/123',
'https://fb.watch/abc123/',
diff --git a/apps/web/src/features/dashboard/VideoDownloadWorkspace.tsx b/apps/web/src/features/dashboard/VideoDownloadWorkspace.tsx
index c270e9ae..1119c4ef 100644
--- a/apps/web/src/features/dashboard/VideoDownloadWorkspace.tsx
+++ b/apps/web/src/features/dashboard/VideoDownloadWorkspace.tsx
@@ -1649,6 +1649,12 @@ function formatDownloadError(
if (/login_required/i.test(message)) {
return l('平台要求登录或有效 Cookie,当前无法下载', 'This video requires sign-in or fresh platform cookies.');
}
+ if (/platform_session_required/i.test(message)) {
+ return l('抖音未接受浏览器会话或需要网页验证,请在设置的平台连接中重新连接抖音后重试', 'Douyin did not accept the browser session or requires website verification. Reconnect Douyin in Settings > Platform connections and retry.');
+ }
+ if (/platform_request_signature_required/i.test(message)) {
+ return l('当前下载器尚未兼容抖音请求签名校验,请使用本地视频文件继续', 'The downloader does not yet support Douyin request signatures. Use a local video file to continue.');
+ }
if (/region_or_copyright_restricted/i.test(message)) {
return l('该视频受地区或版权限制,当前无法下载', 'This video is region or copyright restricted.');
}
diff --git a/apps/web/src/features/issues/issue-catalog.test.ts b/apps/web/src/features/issues/issue-catalog.test.ts
index d8ac055a..a0371cdb 100644
--- a/apps/web/src/features/issues/issue-catalog.test.ts
+++ b/apps/web/src/features/issues/issue-catalog.test.ts
@@ -24,6 +24,41 @@ describe('localized issue copy', () => {
const next = { ...issue, publicFacts: { kind: 'unauthorized' as const, provider: 'seedance', httpStatus: 403 } };
expect(issueConversationText(next, 'en-US').nextStep).toContain('model access permissions');
});
+ it('explains the confirmed Douyin signature incompatibility without requesting another cookie refresh', () => {
+ const next = { ...issue, code: 'platform_request_signature_required', publicFacts: {
+ kind: 'unsupported' as const, provider: 'yt-dlp', httpStatus: 403
+ } };
+ const text = issueConversationText(next, 'zh-CN');
+ expect(text.message).toContain('尚未兼容抖音的请求签名校验');
+ expect(text.nextStep).toContain('本地视频');
+ expect(text.nextStep).toContain('无法解决');
+ });
+ it.each(['zh-CN', 'en-US', 'sv-SE'] as const)('does not send video download access failures to model permissions in %s', language => {
+ const next = { ...issue, code: 'download_http_error', publicFacts: {
+ kind: 'unauthorized' as const, provider: 'yt-dlp', httpStatus: 403
+ } };
+ const text = issueConversationText(next, language);
+ expect(text.message).toContain('HTTP 403');
+ expect(text.nextStep).toMatch(/Cookie|cookies|kakor/);
+ expect(text.nextStep).not.toMatch(/模型|model|modell/);
+ });
+
+ it('explains the recorded Douyin cookie requirement with the available connection action', () => {
+ const next = { ...issue, code: 'platform_session_required', publicFacts: {
+ kind: 'unauthorized' as const, provider: 'yt-dlp', httpStatus: 403
+ } };
+ const text = issueConversationText(next, 'zh-CN');
+ expect(text.message).toContain('浏览器会话');
+ expect(text.nextStep).toContain('平台连接');
+ expect(text.nextStep).toContain('不一定需要登录账号');
+ expect(text.nextStep).not.toContain('模型');
+ });
+
+ it('keeps model permission guidance for a model provider authorization error', () => {
+ expect(issueConversationText({ ...issue, publicFacts: {
+ kind: 'unauthorized', provider: 'openai', httpStatus: 403
+ } }).nextStep).toContain('模型访问权限');
+ });
it.each([
['audio_no_speech', '音频为静音或音量过低', '检查原音频和音量'],
['audio_transcription_empty', '尚不能确认音频是否含有语音', '换一个语音识别模型核验'],
diff --git a/apps/web/src/features/issues/issue-catalog.ts b/apps/web/src/features/issues/issue-catalog.ts
index 58de8c83..0bc0eaf6 100644
--- a/apps/web/src/features/issues/issue-catalog.ts
+++ b/apps/web/src/features/issues/issue-catalog.ts
@@ -65,6 +65,8 @@ function issuePublicFacts(issue: OpenCreatorIssue): PublicErrorFacts | undefined
}
function localizedIssueSummary(issue: OpenCreatorIssue, localize: LocalizeCopy): string {
+ if (issue.code === 'platform_request_signature_required') return localize('当前下载器尚未兼容抖音的请求签名校验。', 'The current downloader does not support Douyin request signature verification.', 'Den nuvarande nedladdaren stöder inte Douyins verifiering av begäranssignaturer.');
+ if (issue.code === 'platform_session_required') return localize('视频平台未接受浏览器会话或需要网页验证。', 'The video platform did not accept the browser session or requires website verification.', 'Videoplattformen accepterade inte webbläsarsessionen eller kräver webbplatsverifiering.');
const transcription = transcriptionIssueCopy(issue.code, localize) ?? transcriptionIssueCopy(issue.publicFacts?.upstreamCode, localize);
if (transcription !== undefined) return transcription.message;
if (issue.code === 'ERR_FS_FILE_TOO_LARGE' || issue.publicFacts?.upstreamCode === 'ERR_FS_FILE_TOO_LARGE') return localize('本地文件读取超过大小限制。', 'The local file exceeded the read-size limit.', 'Den lokala filen överskred storleksgränsen för läsning.');
@@ -182,6 +184,10 @@ export function issueConversationText(
const facts = issuePublicFacts(issue);
const transcription = transcriptionIssueCopy(issue.code, localize) ?? transcriptionIssueCopy(facts?.upstreamCode, localize);
const nextStep = transcription !== undefined ? transcription.nextStep
+ : issue.code === 'platform_request_signature_required'
+ ? localize('请使用本地视频文件继续;当前平台要求下载器尚不支持的请求签名,重新连接 Cookie 或修改模型权限无法解决。', 'Use a local video file to continue. The platform requires a request signature that the downloader does not yet support; reconnecting cookies or changing model permissions will not resolve it.', 'Använd en lokal videofil för att fortsätta. Plattformen kräver en begäranssignatur som nedladdaren ännu inte stöder. Att ansluta kakor igen eller ändra modellbehörigheter löser inte problemet.')
+ : issue.code === 'platform_session_required'
+ ? localize('请先在 Chrome 或 Edge 中打开抖音视频并完成网页验证,再到设置中的平台连接重新连接抖音后重试;不一定需要登录账号。', 'Open the Douyin video in Chrome or Edge and complete the website verification, then reconnect Douyin in Settings > Platform connections and retry; account sign-in is not necessarily required.', 'Öppna Douyin-videon i Chrome eller Edge och slutför webbplatsens verifiering. Anslut sedan Douyin igen under Inställningar > Plattformsanslutningar och försök igen. Kontoinloggning krävs inte nödvändigtvis.')
: issue.code === 'ERR_FS_FILE_TOO_LARGE' || facts?.upstreamCode === 'ERR_FS_FILE_TOO_LARGE'
? localize('请重试当前步骤;如果仍然失败,可先切分视频,并向 Agent 提供文件大小和诊断编号。', 'Retry the current step. If it still fails, split the video and provide the file size and diagnostic ID to the Agent.', 'Försök igen. Om det fortfarande misslyckas, dela videon och ge filstorleken och diagnostik-ID till Agent.')
: issue.publicFacts?.upstreamCode === 'IMAGE_REFERENCE_MISSING'
@@ -192,7 +198,7 @@ export function issueConversationText(
? localize('请根据服务返回的原因调整输入或请求参数后重试;仍有疑问时,可提供请求编号向服务方查询。', 'Review the provider reason and adjust the input or request parameters before retrying. Use the request ID to ask the provider for details if needed.', 'Läs leverantörens felorsak och justera indata eller parametrarna innan du försöker igen. Använd begärans-ID för att fråga leverantören vid behov.')
: issue.publicFacts?.kind === 'unauthorized'
? issue.publicFacts.provider === 'yt-dlp'
- ? localize('请先确认视频可以在浏览器打开,再检查代理和地区限制。部分平台需要有效的浏览器登录态,直接重试可能仍被拒绝。', 'Confirm that the video opens in your browser, then check the proxy and region restrictions. Some platforms require a valid browser session; retrying alone may still be denied.', 'Kontrollera att videon går att öppna i webbläsaren och kontrollera sedan proxy och regionbegränsningar. Vissa plattformar kräver en giltig webbläsarsession; enbart ett nytt försök kan fortfarande nekas.')
+ ? localize('请先确认视频可以在浏览器打开,再检查代理和地区限制。部分平台需要有效的浏览器会话或 Cookie,直接重试可能仍被拒绝。', 'Confirm that the video opens in your browser, then check the proxy and region restrictions. Some platforms require a valid browser session or cookies; retrying alone may still be denied.', 'Kontrollera att videon går att öppna i webbläsaren och kontrollera sedan proxy och regionbegränsningar. Vissa plattformar kräver en giltig webbläsarsession eller kakor; enbart ett nytt försök kan fortfarande nekas.')
: issue.publicFacts.provider === 'codex-native'
? localize('请在本机 Codex 中重新登录 ChatGPT 后重试。', 'Sign in to ChatGPT again in local Codex, then retry.', 'Logga in på ChatGPT igen i lokala Codex och försök igen.')
: localize('请检查服务凭据和模型访问权限后重试。', 'Check the provider credentials and model access permissions before retrying.', 'Kontrollera leverantörens autentiseringsuppgifter och modellbehörigheter innan du försöker igen.')
diff --git a/apps/web/src/features/settings/PlatformConnectionsSettingsView.test.tsx b/apps/web/src/features/settings/PlatformConnectionsSettingsView.test.tsx
index 8c498af3..ff9584de 100644
--- a/apps/web/src/features/settings/PlatformConnectionsSettingsView.test.tsx
+++ b/apps/web/src/features/settings/PlatformConnectionsSettingsView.test.tsx
@@ -1,7 +1,7 @@
-import { render, screen, waitFor } from '@testing-library/react';
+import { render, screen, waitFor, within } from '@testing-library/react';
import { userEvent } from '@testing-library/user-event';
import { expect, it, vi } from 'vitest';
-import type { YouTubeConnectionState } from '@opencreator/protocol';
+import type { PlatformConnectionState, YouTubeConnectionState } from '@opencreator/protocol';
import { PlatformConnectionsSettingsView } from './PlatformConnectionsSettingsView.js';
const windowsState: YouTubeConnectionState = { platform: 'youtube', status: 'disconnected', connectionMethod: 'browser-login' };
@@ -10,6 +10,7 @@ const loginState: YouTubeConnectionState = { ...windowsState, status: 'connectin
function api(initial: YouTubeConnectionState['status'] = 'disconnected') {
let state: YouTubeConnectionState = { platform: 'youtube', status: initial };
+ let douyin: PlatformConnectionState = { platform: 'douyin', status: 'disconnected' };
return {
getYouTube: vi.fn(async () => state),
connectYouTube: vi.fn(async (): Promise<YouTubeConnectionState> => state = { platform: 'youtube', status: 'connecting' }),
@@ -17,6 +18,11 @@ function api(initial: YouTubeConnectionState['status'] = 'disconnected') {
cancelYouTube: vi.fn(async () => state = { platform: 'youtube', status: 'disconnected' }),
disconnectYouTube: vi.fn(async () => state = { platform: 'youtube', status: 'disconnected' }),
importYouTube: vi.fn(async () => state),
+ getDouyin: vi.fn(async () => douyin),
+ connectDouyin: vi.fn(async () => douyin = { platform: 'douyin', status: 'connecting' }),
+ cancelDouyin: vi.fn(async () => douyin = { platform: 'douyin', status: 'disconnected' }),
+ disconnectDouyin: vi.fn(async () => douyin = { platform: 'douyin', status: 'disconnected' }),
+ importDouyin: vi.fn(async () => douyin = { platform: 'douyin', status: 'connected' }),
finish: () => { state = { platform: 'youtube', status: 'connected' }; }
};
}
@@ -24,24 +30,57 @@ function api(initial: YouTubeConnectionState['status'] = 'disconnected') {
it('connects with one click, polls automatic completion, and allows disconnect', async () => {
const service = api(); const user = userEvent.setup();
render(<PlatformConnectionsSettingsView connected service={service} />);
- await screen.findByText('未连接');
- await user.click(screen.getByRole('button', { name: '连接' }));
+ const row = within(screen.getByRole('group', { name: 'YouTube' }));
+ await row.findByText('未连接');
+ await user.click(row.getByRole('button', { name: '连接' }));
expect(service.connectYouTube).toHaveBeenCalledTimes(1);
- expect(screen.getByRole('status')).toHaveTextContent('正在读取浏览器登录态');
+ expect(row.getByRole('status')).toHaveTextContent('正在读取浏览器登录态');
service.finish();
- await waitFor(() => expect(screen.getByRole('status')).toHaveTextContent('已连接'), { timeout: 3000 });
- await user.click(screen.getByRole('button', { name: '断开' }));
+ await waitFor(() => expect(row.getByRole('status')).toHaveTextContent('已连接'), { timeout: 3000 });
+ await user.click(row.getByRole('button', { name: '断开' }));
expect(service.disconnectYouTube).toHaveBeenCalledTimes(1);
- expect(screen.getByRole('status')).toHaveTextContent('未连接');
+ expect(row.getByRole('status')).toHaveTextContent('未连接');
});
it('cancels a login and disables actions when the runtime is disconnected', async () => {
const service = api('connecting'); const user = userEvent.setup();
const view = render(<PlatformConnectionsSettingsView connected service={service} />);
- await user.click(await screen.findByRole('button', { name: '取消' }));
+ const row = within(screen.getByRole('group', { name: 'YouTube' }));
+ await user.click(await row.findByRole('button', { name: '取消' }));
expect(service.cancelYouTube).toHaveBeenCalledTimes(1);
view.rerender(<PlatformConnectionsSettingsView connected={false} service={service} />);
- expect(screen.getByRole('button', { name: '连接' })).toBeDisabled();
+ expect(row.getByRole('button', { name: '连接' })).toBeDisabled();
+ expect(within(screen.getByRole('group', { name: '抖音' })).getByRole('button', { name: '连接' })).toBeDisabled();
+});
+
+it('connects and cancels the Douyin session independently of YouTube', async () => {
+ const service = api(); const user = userEvent.setup();
+ render(<PlatformConnectionsSettingsView connected service={service} />);
+ const row = within(screen.getByRole('group', { name: '抖音' }));
+ await row.findByText('未连接');
+ await user.click(row.getByRole('button', { name: '连接' }));
+ expect(service.connectDouyin).toHaveBeenCalledTimes(1);
+ expect(service.connectYouTube).not.toHaveBeenCalled();
+ expect(row.getByRole('status')).toHaveTextContent('正在读取浏览器会话');
+ await user.click(row.getByRole('button', { name: '取消' }));
+ expect(service.cancelDouyin).toHaveBeenCalledTimes(1);
+ expect(row.getByRole('status')).toHaveTextContent('未连接');
+});
+
+it('imports a Douyin cookie file without changing the YouTube connection', async () => {
+ const service = api(); const user = userEvent.setup();
+ const view = render(<PlatformConnectionsSettingsView connected service={service} />);
+ const group = screen.getByRole('group', { name: '抖音' });
+ const row = within(group);
+ const file = new File(['# Netscape HTTP Cookie File\n'], 'douyin.txt', { type: 'text/plain' });
+ Object.defineProperty(file, 'text', { value: async () => '# Netscape HTTP Cookie File\n' });
+ await user.upload(group.querySelector('input[type=file]')!, file);
+ await waitFor(() => expect(service.importDouyin).toHaveBeenCalledWith('# Netscape HTTP Cookie File\n'));
+ expect(service.importYouTube).not.toHaveBeenCalled();
+ expect(row.getByRole('status')).toHaveTextContent('已连接');
+ await user.click(row.getByRole('button', { name: '断开' }));
+ expect(service.disconnectDouyin).toHaveBeenCalledTimes(1);
+ view.unmount();
});
it('preserves browser profile selection and the original macOS connection interface', async () => {
@@ -51,8 +90,9 @@ it('preserves browser profile selection and the original macOS connection interf
{ id: 'b'.repeat(24), browser: 'edge', label: 'Edge · 第二账号' }
] });
render(<PlatformConnectionsSettingsView connected service={service} />);
- await user.selectOptions(await screen.findByRole('combobox', { name: '浏览器账号' }), 'b'.repeat(24));
- await user.click(screen.getByRole('button', { name: '连接' }));
+ const row = within(screen.getByRole('group', { name: 'YouTube' }));
+ await user.selectOptions(await row.findByRole('combobox', { name: '浏览器账号' }), 'b'.repeat(24));
+ await user.click(row.getByRole('button', { name: '连接' }));
expect(service.connectYouTube).toHaveBeenCalledWith('b'.repeat(24));
expect(screen.queryByRole('button', { name: '下载浏览器连接器' })).not.toBeInTheDocument();
expect(screen.queryByLabelText('连接码')).not.toBeInTheDocument();
@@ -63,16 +103,17 @@ it('Windows 一键打开专用登录,完成后连接,不显示连接码或
service.getYouTube.mockResolvedValue(windowsState);
service.connectYouTube.mockResolvedValue(loginState);
render(<PlatformConnectionsSettingsView connected service={service} />);
- await screen.findByText('未连接');
- await user.click(screen.getByRole('button', { name: '连接' }));
+ const row = within(screen.getByRole('group', { name: 'YouTube' }));
+ await row.findByText('未连接');
+ await user.click(row.getByRole('button', { name: '连接' }));
expect(service.connectYouTube).toHaveBeenCalledWith(undefined);
expect(screen.queryByRole('alertdialog')).not.toBeInTheDocument();
expect(screen.queryByRole('combobox')).not.toBeInTheDocument();
expect(screen.queryByLabelText('连接码')).not.toBeInTheDocument();
- expect(screen.getByRole('status')).toHaveTextContent(loginState.message!);
- await user.click(screen.getByRole('button', { name: '完成登录' }));
+ expect(row.getByRole('status')).toHaveTextContent(loginState.message!);
+ await user.click(row.getByRole('button', { name: '完成登录' }));
expect(service.completeYouTubeLogin).toHaveBeenCalledTimes(1);
- expect(screen.getByRole('status')).toHaveTextContent('已连接');
+ expect(row.getByRole('status')).toHaveTextContent('已连接');
});
it('专用窗口关闭失败后可再次完成登录,提示只关闭该窗口', async () => {
@@ -104,15 +145,17 @@ it('专用登录可取消,Runtime 断开时完成登录和取消均禁用', as
it('连接状态加载完成前禁用连接', () => {
const service = api();
service.getYouTube.mockImplementation(() => new Promise(() => {}));
+ service.getDouyin.mockImplementation(() => new Promise(() => {}));
render(<PlatformConnectionsSettingsView connected service={service} />);
- expect(screen.getByRole('button', { name: '连接' })).toBeDisabled();
+ expect(within(screen.getByRole('group', { name: 'YouTube' })).getByRole('button', { name: '连接' })).toBeDisabled();
});
it('keeps file import available on Windows and disables actions when the Runtime disconnects', async () => {
const service = api();
service.getYouTube.mockResolvedValue(windowsState);
const view = render(<PlatformConnectionsSettingsView connected service={service} />);
- expect(await screen.findByRole('button', { name: '导入 Cookie 文件' })).toBeEnabled();
+ const row = within(screen.getByRole('group', { name: 'YouTube' }));
+ expect(await row.findByRole('button', { name: '导入 Cookie 文件' })).toBeEnabled();
view.rerender(<PlatformConnectionsSettingsView connected={false} service={service} />);
- expect(screen.getByRole('button', { name: '连接' })).toBeDisabled();
+ expect(row.getByRole('button', { name: '连接' })).toBeDisabled();
});
diff --git a/apps/web/src/features/settings/PlatformConnectionsSettingsView.tsx b/apps/web/src/features/settings/PlatformConnectionsSettingsView.tsx
index dc184fff..2d498cc8 100644
--- a/apps/web/src/features/settings/PlatformConnectionsSettingsView.tsx
+++ b/apps/web/src/features/settings/PlatformConnectionsSettingsView.tsx
@@ -1,13 +1,24 @@
import { useEffect, useRef, useState } from 'react';
import { CheckCircle2, Link2, Loader2, RefreshCw, Unlink, Upload, X } from 'lucide-react';
-import type { YouTubeConnectionState } from '@opencreator/protocol';
+import type { ConnectionPlatform, PlatformConnectionState } from '@opencreator/protocol';
import type { PlatformConnectionsService } from '../../services/platform-connections-service.js';
import { useLocalizedCopy } from '../../i18n/useLocalizedCopy.js';
import './platform-connections.css';
export function PlatformConnectionsSettingsView(props: { connected: boolean; service?: PlatformConnectionsService | null }) {
const l = useLocalizedCopy();
- const [state, setState] = useState<YouTubeConnectionState>();
+ return <section className="settings-section" aria-labelledby="platform-connections-title">
+ <header><h1 id="platform-connections-title">{l('平台连接', 'Platform connections')}</h1></header>
+ <PlatformConnectionRow {...props} platform="youtube" />
+ <PlatformConnectionRow {...props} platform="douyin" />
+ </section>;
+}
+
+function PlatformConnectionRow(props: { connected: boolean; service?: PlatformConnectionsService | null; platform: ConnectionPlatform }) {
+ const l = useLocalizedCopy();
+ const youtube = props.platform === 'youtube';
+ const name = youtube ? 'YouTube' : l('抖音', 'Douyin');
+ const [state, setState] = useState<PlatformConnectionState>();
const [busy, setBusy] = useState(false);
const [error, setError] = useState<string>();
const [selectedProfile, setSelectedProfile] = useState('');
@@ -22,16 +33,16 @@ export function PlatformConnectionsSettingsView(props: { connected: boolean; ser
const poll = async () => {
const version = requestVersion.current;
try {
- const next = await service.getYouTube();
+ const next = await (youtube ? service.getYouTube() : service.getDouyin());
if (!canceled && version === requestVersion.current) { setState(next); setError(undefined); }
} catch { if (!canceled) setError(l('无法读取连接状态,请重试。', 'Unable to read connection status. Try again.')); }
if (!canceled) timer = setTimeout(poll, 1500);
};
void poll();
return () => { canceled = true; clearTimeout(timer); };
- }, [props.connected, props.service, l]);
+ }, [props.connected, props.service, youtube, l]);
- async function act(operation: () => Promise<YouTubeConnectionState>) {
+ async function act(operation: () => Promise<PlatformConnectionState>) {
setBusy(true); setError(undefined); ++requestVersion.current;
try { setState(await operation()); }
catch { setError(l('操作未完成,请检查网络或重新连接。', 'The operation failed. Check your network or reconnect.')); }
@@ -46,22 +57,17 @@ export function PlatformConnectionsSettingsView(props: { connected: boolean; ser
const profiles = managed ? [] : state?.profiles ?? [];
const profileId = profiles.some(profile => profile.id === selectedProfile) ? selectedProfile
: profiles.find(profile => profile.id === state?.profileId)?.id ?? profiles[0]?.id;
- const awaitingLogin = connecting && !!state?.actionRequired;
- const label = connected ? l('已连接', 'Connected') : connecting ? state?.message ?? l('正在读取浏览器登录态', 'Reading browser session')
- : state?.status === 'expired' ? l('登录已过期', 'Session expired') : state?.status === 'error' ? l('连接失败', 'Connection failed')
+ const awaitingLogin = youtube && connecting && !!state?.actionRequired;
+ const label = connected ? l('已连接', 'Connected') : connecting ? state?.message ?? (youtube
+ ? l('正在读取浏览器登录态', 'Reading browser session') : l('正在读取浏览器会话', 'Reading browser session'))
+ : state?.status === 'expired' ? youtube ? l('登录已过期', 'Session expired') : l('会话已过期', 'Session expired') : state?.status === 'error' ? l('连接失败', 'Connection failed')
: l('未连接', 'Disconnected');
- async function connect() {
- if (!service || disabled) return;
- void act(() => service.connectYouTube(profileId));
- }
-
- return <section className="settings-section" aria-labelledby="platform-connections-title">
- <header><h1 id="platform-connections-title">{l('平台连接', 'Platform connections')}</h1></header>
+ return <div role="group" aria-label={name}>
<div className="platform-connection-row">
<div className="platform-connection-identity">
- <img src="/youtube.ico" width="36" height="36" alt="" />
- <div><h2>YouTube</h2><span className={`platform-connection-status${connected ? ' is-connected' : ''}`} role="status">
+ <img src={youtube ? '/youtube.ico' : '/platforms/douyin.png'} width="36" height="36" alt="" />
+ <div><h2>{name}</h2><span className={`platform-connection-status${connected ? ' is-connected' : ''}`} role="status">
{connected ? <CheckCircle2 size={15} /> : connecting ? <Loader2 size={15} className="platform-connection-spinner" /> : null}{label}</span></div>
</div>
<div className="platform-connection-actions">
@@ -71,11 +77,11 @@ export function PlatformConnectionsSettingsView(props: { connected: boolean; ser
</select> : null}
{awaitingLogin ? <button type="button" disabled={disabled} onClick={() => service && void act(service.completeYouTubeLogin)}>
<CheckCircle2 size={16} />{l('完成登录', 'Finish sign-in')}</button> : null}
- {connecting ? <button type="button" disabled={disabled} onClick={() => service && void act(service.cancelYouTube)}><X size={16} />{l('取消', 'Cancel')}</button>
- : <button type="button" disabled={disabled} onClick={() => void connect()}>
+ {connecting ? <button type="button" disabled={disabled} onClick={() => service && void act(youtube ? service.cancelYouTube : service.cancelDouyin)}><X size={16} />{l('取消', 'Cancel')}</button>
+ : <button type="button" disabled={disabled} onClick={() => service && void act(() => youtube ? service.connectYouTube(profileId) : service.connectDouyin(profileId))}>
{connected || state?.status === 'expired' ? <RefreshCw size={16} /> : <Link2 size={16} />}
{connected || state?.status === 'expired' ? l('重新连接', 'Reconnect') : l('连接', 'Connect')}</button>}
- {connected || state?.status === 'expired' ? <button type="button" disabled={disabled} onClick={() => service && void act(service.disconnectYouTube)}><Unlink size={16} />{l('断开', 'Disconnect')}</button> : null}
+ {connected || state?.status === 'expired' ? <button type="button" disabled={disabled} onClick={() => service && void act(youtube ? service.disconnectYouTube : service.disconnectDouyin)}><Unlink size={16} />{l('断开', 'Disconnect')}</button> : null}
{!connecting ? <button type="button" disabled={disabled} aria-label={l('导入 Cookie 文件', 'Import cookie file')} title={l('导入 Cookie 文件', 'Import cookie file')} onClick={() => fileInput.current?.click()}><Upload size={16} /></button> : null}
</div>
</div>
@@ -85,7 +91,7 @@ export function PlatformConnectionsSettingsView(props: { connected: boolean; ser
const file = event.currentTarget.files?.[0]; event.currentTarget.value = '';
if (!file || !service) return;
if (file.size > 1024 * 1024) { setError(l('文件过大,请选择有效的 Cookie 文件。', 'The file is too large. Select a valid cookie file.')); return; }
- void act(async () => service.importYouTube(await file.text()));
+ void act(async () => youtube ? service.importYouTube(await file.text()) : service.importDouyin(await file.text()));
}} />
- </section>;
+ </div>;
}
diff --git a/apps/web/src/services/platform-connections-service.ts b/apps/web/src/services/platform-connections-service.ts
index 7da66eb4..1209680a 100644
--- a/apps/web/src/services/platform-connections-service.ts
+++ b/apps/web/src/services/platform-connections-service.ts
@@ -1,8 +1,9 @@
-import type { YouTubeConnectionState } from '@opencreator/protocol';
+import type { PlatformConnectionState, YouTubeConnectionState } from '@opencreator/protocol';
import type { RuntimeClient } from '../runtime/client.js';
export function createPlatformConnectionsService(client: Pick<RuntimeClient, 'get' | 'post' | 'delete'>) {
const path = '/platform-connections/youtube';
+ const douyinPath = '/platform-connections/douyin';
return {
getYouTube(): Promise<YouTubeConnectionState> { return client.get(path); },
connectYouTube(profileId?: string): Promise<YouTubeConnectionState> {
@@ -11,7 +12,12 @@ export function createPlatformConnectionsService(client: Pick<RuntimeClient, 'ge
completeYouTubeLogin(): Promise<YouTubeConnectionState> { return client.post(`${path}/complete-login`); },
cancelYouTube(): Promise<YouTubeConnectionState> { return client.post(`${path}/cancel`); },
disconnectYouTube(): Promise<YouTubeConnectionState> { return client.delete(path); },
- importYouTube(cookies: string): Promise<YouTubeConnectionState> { return client.post(`${path}/import`, { cookies }); }
+ importYouTube(cookies: string): Promise<YouTubeConnectionState> { return client.post(`${path}/import`, { cookies }); },
+ getDouyin(): Promise<PlatformConnectionState> { return client.get(douyinPath); },
+ connectDouyin(profileId?: string): Promise<PlatformConnectionState> { return client.post(`${douyinPath}/connect`, { profileId }); },
+ cancelDouyin(): Promise<PlatformConnectionState> { return client.post(`${douyinPath}/cancel`); },
+ disconnectDouyin(): Promise<PlatformConnectionState> { return client.delete(douyinPath); },
+ importDouyin(cookies: string): Promise<PlatformConnectionState> { return client.post(`${douyinPath}/import`, { cookies }); }
};
}
diff --git a/packages/protocol/src/creator-download.ts b/packages/protocol/src/creator-download.ts
index b6ae7e1d..fe45fc22 100644
--- a/packages/protocol/src/creator-download.ts
+++ b/packages/protocol/src/creator-download.ts
@@ -61,7 +61,8 @@ function isDouyinVideoUrl(url: URL): boolean {
function isDouyinFeaturedVideoUrl(url: URL): boolean {
return ['douyin.com', 'www.douyin.com'].includes(url.hostname.toLowerCase())
- && url.pathname === '/jingxuan' && /^\d+$/.test(url.searchParams.get('modal_id') ?? '');
+ && /^\/jingxuan(?:\/search(?:\/[^/]+)?)?\/?$/.test(url.pathname)
+ && /^\d+$/.test(url.searchParams.get('modal_id') ?? '');
}
function isFacebookVideoUrl(url: URL): boolean {
diff --git a/packages/protocol/src/platform-connections.ts b/packages/protocol/src/platform-connections.ts
index 3da32442..356a09f4 100644
--- a/packages/protocol/src/platform-connections.ts
+++ b/packages/protocol/src/platform-connections.ts
@@ -1,5 +1,7 @@
-export type YouTubeConnectionState = {
- platform: 'youtube';
+export type ConnectionPlatform = 'youtube' | 'douyin';
+
+export type PlatformConnectionState = {
+ platform: ConnectionPlatform;
status: 'disconnected' | 'connecting' | 'connected' | 'expired' | 'error';
browser?: 'chrome' | 'edge';
connectedAt?: string;
@@ -9,3 +11,5 @@ export type YouTubeConnectionState = {
connectionMethod?: 'browser-login';
actionRequired?: 'complete-login' | 'close-login-browser';
};
+
+export type YouTubeConnectionState = PlatformConnectionState & { platform: 'youtube' };
diff --git a/packages/protocol/test/video-source-platforms.test.ts b/packages/protocol/test/video-source-platforms.test.ts
index b1a583b6..30a94d81 100644
--- a/packages/protocol/test/video-source-platforms.test.ts
+++ b/packages/protocol/test/video-source-platforms.test.ts
@@ -39,9 +39,27 @@ describe('shared public video sources', () => {
expect(videoSourceIdentity(featured)).toBe(videoSourceIdentity('https://www.douyin.com/video/123'));
});
+ it.each([
+ 'https://www.douyin.com/jingxuan/search/qianjidong?aid=2052a92f-0793-47c1-b739-c0af829b6103&modal_id=7693859410975690010&type=general',
+ 'https://douyin.com/jingxuan/search/%E6%97%85%E8%A1%8C/?modal_id=7693859410975690010',
+ 'https://www.douyin.com/jingxuan/search?modal_id=7693859410975690010'
+ ])('normalizes a selected Douyin search result and identifies the same video: %s', url => {
+ const videoUrl = 'https://www.douyin.com/video/7693859410975690010';
+ expect(normalizeVideoSourceUrl(url)).toBe(videoUrl);
+ expect(supportedVideoSourcePlatform(url)).toBe('douyin');
+ expect(videoSourceIdentity(url)).toBe(videoSourceIdentity(videoUrl));
+ expect(videoSourceIdentity(url.replace('7693859410975690010', '7693859410975690011')))
+ .not.toBe(videoSourceIdentity(url));
+ });
+
it.each([
'https://www.tiktok.com/@creator', 'https://www.instagram.com/creator/',
'https://www.douyin.com/user/123', 'https://www.facebook.com/creator/',
+ 'https://www.douyin.com/jingxuan/search/qianjidong?type=general',
+ 'https://www.douyin.com/jingxuan/search/qianjidong?modal_id=abc',
+ 'https://www.douyin.com/jingxuan/search/qianjidong/extra?modal_id=123',
+ 'https://notdouyin.com/jingxuan/search/qianjidong?modal_id=123',
+ 'https://www.douyin.com/jingxuanother?modal_id=123',
'https://www.xiaohongshu.com/user/profile/123', 'https://www.pinterest.com/creator/',
'https://notpinterest.com/pin/123/', 'https://instagram.com.example.com/reel/abc/',
'https://www.reddit.com/r/claude/', 'https://www.reddit.com/user/creator/',
diff --git a/runtime/krillinai/internal/service/link2file.go b/runtime/krillinai/internal/service/link2file.go
index 514553db..639bfbc2 100644
--- a/runtime/krillinai/internal/service/link2file.go
+++ b/runtime/krillinai/internal/service/link2file.go
@@ -94,8 +94,7 @@ func (s Service) linkToFile(ctx context.Context, stepParam *types.SubtitleTaskSt
if storage.FfmpegPath != "ffmpeg" {
cmdArgs = append(cmdArgs, "--ffmpeg-location", storage.FfmpegPath)
}
- cmd := storage.YtdlpCommand(cmdArgs...)
- output, err = cmd.CombinedOutput()
+ output, err = downloadBilibiliMedia(ctx, cmdArgs)
if err != nil {
log.GetLogger().Error("linkToFile download audio yt-dlp error", zap.Any("step param", stepParam), zap.String("output", string(output)), zap.Error(err))
return fmt.Errorf("linkToFile download audio yt-dlp error: %w: %s", err, compactCommandOutput(output))
@@ -140,6 +139,8 @@ func (s Service) linkToFile(ctx context.Context, stepParam *types.SubtitleTaskSt
}
if util.IsYouTubeURL(stepParam.Link) {
output, err = downloadYouTubeMedia(ctx, cmdArgs)
+ } else if sourceHost == "bilibili.com" || strings.HasSuffix(sourceHost, ".bilibili.com") {
+ output, err = downloadBilibiliMedia(ctx, cmdArgs)
} else {
output, err = storage.YtdlpCommandContext(ctx, cmdArgs...).CombinedOutput()
}
@@ -251,6 +252,44 @@ func downloadYouTubeMedia(ctx context.Context, args []string) ([]byte, error) {
return output, err
}
+func downloadBilibiliMedia(ctx context.Context, args []string) ([]byte, error) {
+ const maxAttempts = 3
+ var output []byte
+ var err error
+ for attempt := 1; attempt <= maxAttempts; attempt++ {
+ if err := ctx.Err(); err != nil {
+ return output, err
+ }
+ output, err = storage.YtdlpCommandContext(ctx, args...).CombinedOutput()
+ if ctx.Err() != nil {
+ return output, ctx.Err()
+ }
+ if err == nil {
+ return output, err
+ }
+ message := strings.ToLower(string(output))
+ riskControl := strings.Contains(message, "bilibili risk control")
+ if !strings.Contains(message, "bilibili risk control rejected the media request") && !strings.Contains(message, "no video formats found") {
+ return output, err
+ }
+ if attempt == maxAttempts {
+ if riskControl {
+ return output, fmt.Errorf("Bilibili risk control blocked media extraction after %d attempts; wait and retry or use a local video file: %w", maxAttempts, err)
+ }
+ return output, fmt.Errorf("Bilibili returned no usable media formats after %d attempts; wait and retry or use a local video file: %w", maxAttempts, err)
+ }
+ log.GetLogger().Warn("Bilibili media extraction was rejected; refreshing media info", zap.Int("attempt", attempt))
+ timer := time.NewTimer(time.Duration(attempt) * time.Second)
+ select {
+ case <-ctx.Done():
+ timer.Stop()
+ return output, ctx.Err()
+ case <-timer.C:
+ }
+ }
+ return output, err
+}
+
func youtubeAudioFallbackArgs(args []string) []string {
if !slices.Contains(args, "--extract-audio") {
return args
diff --git a/runtime/krillinai/internal/service/link2file_test.go b/runtime/krillinai/internal/service/link2file_test.go
index dc1d41e9..3991a0d8 100644
--- a/runtime/krillinai/internal/service/link2file_test.go
+++ b/runtime/krillinai/internal/service/link2file_test.go
@@ -13,6 +13,7 @@ import (
"runtime"
"strings"
"testing"
+ "time"
)
func TestYouTubeMediaRefreshesRejectedURLs(t *testing.T) {
@@ -143,6 +144,75 @@ func TestResolveLocalMediaInput(t *testing.T) {
}
}
+func TestBilibiliMediaRetriesOnlyExtractionRejections(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ t.Skip("shell downloader fixture requires POSIX")
+ }
+ originalPath, originalPrefix := storage.YtdlpPath, storage.YtdlpPrefixArgs
+ t.Cleanup(func() { storage.YtdlpPath, storage.YtdlpPrefixArgs = originalPath, originalPrefix })
+ for _, test := range []struct {
+ name, message string
+ failures, attempts int
+ wantError bool
+ }{
+ {"refresh-risk-control", "ERROR: Bilibili risk control rejected the media request", 1, 2, false},
+ {"refresh-legacy-no-formats", "ERROR: No video formats found!", 1, 2, false},
+ {"limit-risk-control", "ERROR: Bilibili risk control rejected the media request", 9, 3, true},
+ {"no-format-selector-retry", "ERROR: Requested format is not available", 9, 1, true},
+ {"no-retry-after-successful-fallback", "WARNING: Bilibili risk control rejected the playback API request; retaining webpage media formats when available\nERROR: Requested format is not available", 9, 1, true},
+ {"no-login-retry", "ERROR: Login required", 9, 1, true},
+ {"no-storage-retry", "ERROR: No space left on device", 9, 1, true},
+ } {
+ t.Run(test.name, func(t *testing.T) {
+ dir := t.TempDir()
+ counter := filepath.Join(dir, "attempts")
+ downloader := filepath.Join(dir, "yt-dlp")
+ t.Setenv("BILIBILI_MEDIA_COUNTER", counter)
+ t.Setenv("BILIBILI_MEDIA_ERROR", test.message)
+ fixture := fmt.Sprintf("#!/bin/sh\nn=0\nif [ -f \"$BILIBILI_MEDIA_COUNTER\" ]; then n=$(cat \"$BILIBILI_MEDIA_COUNTER\"); fi\nn=$((n+1))\nprintf '%%s' \"$n\" > \"$BILIBILI_MEDIA_COUNTER\"\nif [ \"$n\" -le %d ]; then printf '%%s\\n' \"$BILIBILI_MEDIA_ERROR\" >&2; exit 1; fi\nprintf 'media ready\\n'\n", test.failures)
+ if err := os.WriteFile(downloader, []byte(fixture), 0700); err != nil {
+ t.Fatal(err)
+ }
+ storage.YtdlpPath, storage.YtdlpPrefixArgs = downloader, nil
+ output, err := downloadBilibiliMedia(context.Background(), []string{"https://www.bilibili.com/video/BV18Qhf6hE8P?p=3"})
+ if (err != nil) != test.wantError {
+ t.Fatalf("error = %v, output = %s", err, output)
+ }
+ count, readErr := os.ReadFile(counter)
+ if readErr != nil || string(count) != fmt.Sprint(test.attempts) {
+ t.Fatalf("attempts = %s, error = %v", count, readErr)
+ }
+ if test.name == "limit-risk-control" && !strings.Contains(err.Error(), "Bilibili risk control blocked media extraction after 3 attempts") {
+ t.Fatalf("risk-control cause was lost: %v", err)
+ }
+ })
+ }
+ ctx, cancel := context.WithCancel(context.Background())
+ cancel()
+ if _, err := downloadBilibiliMedia(ctx, nil); !errors.Is(err, context.Canceled) {
+ t.Fatalf("error = %v, want cancellation", err)
+ }
+}
+
+func TestBilibiliMediaCancellationStopsRetryWait(t *testing.T) {
+ if runtime.GOOS == "windows" {
+ t.Skip("shell downloader fixture requires POSIX")
+ }
+ originalPath, originalPrefix := storage.YtdlpPath, storage.YtdlpPrefixArgs
+ t.Cleanup(func() { storage.YtdlpPath, storage.YtdlpPrefixArgs = originalPath, originalPrefix })
+ dir := t.TempDir()
+ downloader := filepath.Join(dir, "yt-dlp")
+ if err := os.WriteFile(downloader, []byte("#!/bin/sh\nprintf 'ERROR: Bilibili risk control rejected the media request\\n' >&2\nexit 1\n"), 0700); err != nil {
+ t.Fatal(err)
+ }
+ storage.YtdlpPath, storage.YtdlpPrefixArgs = downloader, nil
+ ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
+ defer cancel()
+ if _, err := downloadBilibiliMedia(ctx, nil); !errors.Is(err, context.DeadlineExceeded) {
+ t.Fatalf("error = %v, want deadline exceeded", err)
+ }
+}
+
func TestAdditionalPlatformMediaDownloads(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("shell downloader fixture requires POSIX")