Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/clis/douyin/_shared/sts2.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { describe, expect, it } from 'vitest';
import { AuthRequiredError } from '../../../errors.js';
import { getSts2Credentials } from './sts2.js';

describe('douyin sts2 credentials', () => {
it('accepts top-level credential fields returned by creator center', async () => {
const page = {
evaluate: async () => ({
access_key_id: 'ak',
secret_access_key: 'sk',
session_token: 'token',
expired_time: 1_234_567_890,
}),
};

await expect(getSts2Credentials(page as never)).resolves.toEqual({
access_key_id: 'ak',
secret_access_key: 'sk',
session_token: 'token',
expired_time: 1_234_567_890,
});
});

it('still rejects responses without credential fields', async () => {
const page = {
evaluate: async () => ({ status_code: 8 }),
};

await expect(getSts2Credentials(page as never)).rejects.toBeInstanceOf(AuthRequiredError);
});
});
14 changes: 11 additions & 3 deletions src/clis/douyin/_shared/sts2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@ const STS2_URL =
*/
export async function getSts2Credentials(page: IPage): Promise<Sts2Credentials> {
const js = `fetch(${JSON.stringify(STS2_URL)}, { credentials: 'include' }).then(r => r.json())`;
const res = await page.evaluate(js) as { data: Sts2Credentials };
if (!res?.data?.access_key_id) {
const res = await page.evaluate(js) as Sts2Credentials | { data?: Sts2Credentials };
const credentials = (
typeof res === 'object' &&
res !== null &&
'data' in res &&
res.data
)
? res.data
: (res as Sts2Credentials);
if (!credentials?.access_key_id) {
throw new AuthRequiredError('creator.douyin.com', 'STS2 credentials missing');
}
return res.data;
return credentials;
}
42 changes: 41 additions & 1 deletion src/clis/douyin/activities.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { describe, expect, it } from 'vitest';
import { beforeEach, describe, expect, it, vi } from 'vitest';

const { browserFetchMock } = vi.hoisted(() => ({
browserFetchMock: vi.fn(),
}));

vi.mock('./_shared/browser-fetch.js', () => ({
browserFetch: browserFetchMock,
}));

import { getRegistry } from '../../registry.js';
import './activities.js';

describe('douyin activities registration', () => {
beforeEach(() => {
browserFetchMock.mockReset();
});

it('registers the activities command', () => {
const registry = getRegistry();
const cmd = [...registry.values()].find(c => c.site === 'douyin' && c.name === 'activities');
Expand All @@ -22,4 +35,31 @@ describe('douyin activities registration', () => {
const cmd = [...registry.values()].find(c => c.site === 'douyin' && c.name === 'activities');
expect(cmd?.strategy).toBe('cookie');
});

it('maps the current activity payload shape returned by creator center', async () => {
const registry = getRegistry();
const cmd = [...registry.values()].find(c => c.site === 'douyin' && c.name === 'activities');
expect(cmd?.func).toBeDefined();
if (!cmd?.func) throw new Error('douyin activities command not registered');

browserFetchMock.mockResolvedValueOnce({
activity_list: [
{
activity_id: '200',
activity_name: '超会玩派对',
show_end_time: '2026.05.31',
},
],
});

const rows = await cmd.func({} as never, {});

expect(rows).toEqual([
{
activity_id: '200',
title: '超会玩派对',
end_time: '2026.05.31',
},
]);
});
});
15 changes: 12 additions & 3 deletions src/clis/douyin/activities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,21 @@ cli({
func: async (page, _kwargs) => {
const url = 'https://creator.douyin.com/web/api/media/activity/get/?aid=1128';
const res = await browserFetch(page, 'GET', url) as {
activity_list: Array<{ activity_id: string; title: string; end_time: number }>
activity_list: Array<{
activity_id: string;
title?: string;
activity_name?: string;
end_time?: number;
show_end_time?: string;
}>
};
return (res.activity_list ?? []).map(a => ({
activity_id: a.activity_id,
title: a.title,
end_time: new Date(a.end_time * 1000).toLocaleString('zh-CN', { timeZone: 'Asia/Tokyo' }),
title: a.title ?? a.activity_name ?? '',
end_time:
typeof a.end_time === 'number'
? new Date(a.end_time * 1000).toLocaleString('zh-CN', { timeZone: 'Asia/Tokyo' })
: (a.show_end_time ?? ''),
}));
},
});
Loading