@yourname168/tiktok 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -9
- package/dist/http.js +3 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/post-body.d.ts +190 -10
- package/dist/post-body.js +80 -10
- package/dist/session.js +11 -4
- package/dist/vod-upload.js +3 -1
- package/dist/web-client.d.ts +4 -1
- package/dist/web-client.js +21 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -53,9 +53,38 @@ Keep session files outside version control. The library does not acquire session
|
|
|
53
53
|
`uploadVideo()` uploads video bytes, attaches the explicitly selected products,
|
|
54
54
|
and publishes. It defaults to **private**; there is no automatic retry.
|
|
55
55
|
`publishVideo()` publishes an already uploaded `{ videoId, width, height }` asset.
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
56
|
+
|
|
57
|
+
### Publish a photo album
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
await client.publishPhotoAlbum({
|
|
61
|
+
creationId,
|
|
62
|
+
title: '',
|
|
63
|
+
caption: '',
|
|
64
|
+
photos: uploadedPhotos,
|
|
65
|
+
products: [{ productId: 'chosen-product-id', displayName: 'Tên sản phẩm' }],
|
|
66
|
+
productRegistration: 'existing',
|
|
67
|
+
visibility: 'public',
|
|
68
|
+
music: selectedMusic,
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Each uploaded photo needs `id`, `uri`, `width` and `height`. Photo file upload
|
|
73
|
+
is outside the library. The optional music metadata contains `id`, `title`,
|
|
74
|
+
`author`, `url` and `endSeconds` from a TikTok sound selected for the post.
|
|
75
|
+
Optional fields are `startSeconds` (defaults to zero) and `album`.
|
|
76
|
+
Use `aiGenerated: true` when applying TikTok's AI-generated label.
|
|
77
|
+
`publishPhotoAlbum()` defaults to private. `publishPrivatePhotoAlbum()` always
|
|
78
|
+
submits private visibility. Both support `coverIndex` with a default of zero.
|
|
79
|
+
|
|
80
|
+
`addShowcaseProducts()` runs the same product registration used by the publish
|
|
81
|
+
methods. Set `productRegistration: 'existing'` for products already registered
|
|
82
|
+
in the account's Showcase. The default is `'add'`. Use the same option on
|
|
83
|
+
`publishVideo()` or `uploadVideo()`.
|
|
84
|
+
|
|
85
|
+
The publish response is TikTok's raw acknowledgement. Check the published item
|
|
86
|
+
for review status, visibility and whether the product anchor renders. Reuse a known
|
|
87
|
+
`creationId` and inspect TikTok before retrying after a timeout.
|
|
59
88
|
|
|
60
89
|
Optional `coverUri` references an already uploaded cover. Uploading a cover file
|
|
61
90
|
is not ported. Scheduling accepts Unix seconds, at least 20 minutes in the future.
|
|
@@ -86,8 +115,10 @@ file-based official upload and interactive OAuth authorization are not ported.
|
|
|
86
115
|
and the complete web post body. Signer entropy/time are fixed only in tests.
|
|
87
116
|
- Offline HTTP tests exercise upload/auth → apply → init → transfer → finish →
|
|
88
117
|
commit → attach products → publish; account cookies never go to storage hosts.
|
|
89
|
-
-
|
|
90
|
-
|
|
118
|
+
- Live account trials accepted image and video submissions with product anchor
|
|
119
|
+
fields. Album approval and public visibility were observed in TikTok Studio.
|
|
120
|
+
Product-card rendering and playback of the selected sound remain unverified.
|
|
121
|
+
TikTok's web API can change independently of this package.
|
|
91
122
|
- VOD upload currently ports the normal multipart route. Gateway/stream uploads,
|
|
92
123
|
dynamic slice rules, resume, retries, image upload, messaging, triggers, hashtag
|
|
93
124
|
lookup and comments are not ported. Storage hosts are restricted in vod-upload.ts.
|
|
@@ -98,8 +129,7 @@ file-based official upload and interactive OAuth authorization are not ported.
|
|
|
98
129
|
Run from the repository:
|
|
99
130
|
|
|
100
131
|
```sh
|
|
101
|
-
pnpm
|
|
102
|
-
pnpm exec tsc -p packages/tiktok/tsconfig.json --noEmit
|
|
132
|
+
pnpm --filter @gateway/tiktok typecheck
|
|
103
133
|
```
|
|
104
134
|
|
|
105
135
|
Workspace consumers import source, as established by repository conventions.
|
|
@@ -107,7 +137,7 @@ For installation outside the monorepo, generate a compiled JS + declaration tarb
|
|
|
107
137
|
|
|
108
138
|
```sh
|
|
109
139
|
pnpm --filter @gateway/tiktok pack:portable /absolute/output/directory
|
|
110
|
-
npm install /absolute/output/directory/gateway-tiktok-0.
|
|
140
|
+
npm install /absolute/output/directory/gateway-tiktok-0.2.0.tgz
|
|
111
141
|
```
|
|
112
142
|
|
|
113
143
|
Use this portable tarball instead of packing the TypeScript workspace directly:
|
|
@@ -120,7 +150,7 @@ To prepare a public release under the npm account scope:
|
|
|
120
150
|
|
|
121
151
|
```sh
|
|
122
152
|
pnpm --filter @gateway/tiktok pack:portable /absolute/output/directory --name @yourname168/tiktok
|
|
123
|
-
npm publish /absolute/output/directory/yourname168-tiktok-0.
|
|
153
|
+
npm publish /absolute/output/directory/yourname168-tiktok-0.2.0.tgz --access public
|
|
124
154
|
```
|
|
125
155
|
|
|
126
156
|
The public tarball excludes tests, session files, npm credentials and workspace
|
package/dist/http.js
CHANGED
|
@@ -33,8 +33,10 @@ export function createHttp(transport = fetch, timeoutMs = 120_000) {
|
|
|
33
33
|
const data = object(value);
|
|
34
34
|
const metadata = data.ResponseMetadata;
|
|
35
35
|
const apiError = data.error;
|
|
36
|
+
// TikTok's multipart storage endpoint uses 2000 for successful operations.
|
|
37
|
+
const storageSuccess = new URL(url).pathname.startsWith('/upload/v1/') && data.code === 2000;
|
|
36
38
|
if ((metadata && object(metadata).Error) || (apiError && object(apiError).code !== undefined && object(apiError).code !== 'ok') ||
|
|
37
|
-
(data.status_code !== undefined && data.status_code !== 0) || (data.code !== undefined && data.code !== 0)) {
|
|
39
|
+
(data.status_code !== undefined && data.status_code !== 0) || (data.code !== undefined && data.code !== 0 && !storageSuccess)) {
|
|
38
40
|
throw new Error('TikTok rejected the request');
|
|
39
41
|
}
|
|
40
42
|
return data;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export { createTikTokWebClient } from './web-client.ts';
|
|
2
2
|
export { createTikTokContentClient, refreshTikTokToken } from './content-client.ts';
|
|
3
3
|
export { parseTikTokSession } from './session.ts';
|
|
4
|
-
export { createWebPostBody, productAnchors } from './post-body.ts';
|
|
4
|
+
export { createPhotoPostBody, createWebPostBody, productAnchors } from './post-body.ts';
|
|
5
5
|
export type { TikTokSession } from './session.ts';
|
|
6
|
-
export type { ProductAnchor, UploadedVideo, WebPostOptions } from './post-body.ts';
|
|
6
|
+
export type { PhotoAlbumMusic, PhotoPostOptions, PrivatePhotoPostOptions, ProductAnchor, UploadedPhoto, UploadedVideo, WebPostOptions } from './post-body.ts';
|
|
7
7
|
export type { HttpTransport } from './http.ts';
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { createTikTokWebClient } from "./web-client.js";
|
|
2
2
|
export { createTikTokContentClient, refreshTikTokToken } from "./content-client.js";
|
|
3
3
|
export { parseTikTokSession } from "./session.js";
|
|
4
|
-
export { createWebPostBody, productAnchors } from "./post-body.js";
|
|
4
|
+
export { createPhotoPostBody, createWebPostBody, productAnchors } from "./post-body.js";
|
package/dist/post-body.d.ts
CHANGED
|
@@ -7,6 +7,35 @@ export interface UploadedVideo {
|
|
|
7
7
|
width: number;
|
|
8
8
|
height: number;
|
|
9
9
|
}
|
|
10
|
+
export interface UploadedPhoto {
|
|
11
|
+
id: string;
|
|
12
|
+
uri: string;
|
|
13
|
+
width: number;
|
|
14
|
+
height: number;
|
|
15
|
+
}
|
|
16
|
+
export interface PhotoAlbumMusic {
|
|
17
|
+
id: string;
|
|
18
|
+
title: string;
|
|
19
|
+
author: string;
|
|
20
|
+
url: string;
|
|
21
|
+
startSeconds?: number;
|
|
22
|
+
endSeconds: number;
|
|
23
|
+
album?: string;
|
|
24
|
+
}
|
|
25
|
+
export interface PrivatePhotoPostOptions {
|
|
26
|
+
creationId: string;
|
|
27
|
+
title: string;
|
|
28
|
+
caption: string;
|
|
29
|
+
photos: readonly UploadedPhoto[];
|
|
30
|
+
products: ProductAnchor[];
|
|
31
|
+
coverIndex?: number;
|
|
32
|
+
music?: PhotoAlbumMusic;
|
|
33
|
+
aiGenerated?: boolean;
|
|
34
|
+
productRegistration?: 'add' | 'existing';
|
|
35
|
+
}
|
|
36
|
+
export interface PhotoPostOptions extends PrivatePhotoPostOptions {
|
|
37
|
+
visibility?: 'public' | 'friends' | 'private';
|
|
38
|
+
}
|
|
10
39
|
export interface WebPostOptions {
|
|
11
40
|
creationId: string;
|
|
12
41
|
caption: string;
|
|
@@ -16,6 +45,7 @@ export interface WebPostOptions {
|
|
|
16
45
|
allowComments?: boolean;
|
|
17
46
|
scheduleTimeSeconds?: number;
|
|
18
47
|
products?: ProductAnchor[];
|
|
48
|
+
productRegistration?: 'add' | 'existing';
|
|
19
49
|
}
|
|
20
50
|
export declare function productAnchors(products: ProductAnchor[]): {
|
|
21
51
|
type: number;
|
|
@@ -66,16 +96,6 @@ export declare function createWebPostBody(input: WebPostOptions): {
|
|
|
66
96
|
video_id: string;
|
|
67
97
|
is_long_video: number;
|
|
68
98
|
single_post_feature_info: {
|
|
69
|
-
text: string;
|
|
70
|
-
text_extra: {
|
|
71
|
-
tag_id: string;
|
|
72
|
-
user_id: string;
|
|
73
|
-
start: number;
|
|
74
|
-
end: number;
|
|
75
|
-
type: number;
|
|
76
|
-
hashtag_name: string | undefined;
|
|
77
|
-
}[];
|
|
78
|
-
markup_text: string;
|
|
79
99
|
music_info: {};
|
|
80
100
|
cover_info: {
|
|
81
101
|
frame_duration: number;
|
|
@@ -92,6 +112,166 @@ export declare function createWebPostBody(input: WebPostOptions): {
|
|
|
92
112
|
cloud_edit_video_height: number;
|
|
93
113
|
cloud_edit_video_width: number;
|
|
94
114
|
cloud_edit_is_use_video_canvas: boolean;
|
|
115
|
+
text: string;
|
|
116
|
+
text_extra: {
|
|
117
|
+
tag_id: string;
|
|
118
|
+
user_id: string;
|
|
119
|
+
start: number;
|
|
120
|
+
end: number;
|
|
121
|
+
type: number;
|
|
122
|
+
hashtag_name: string | undefined;
|
|
123
|
+
}[];
|
|
124
|
+
markup_text: string;
|
|
125
|
+
};
|
|
126
|
+
}[];
|
|
127
|
+
};
|
|
128
|
+
export declare function createPhotoPostBody(input: PhotoPostOptions): {
|
|
129
|
+
feature_common_info_list: {
|
|
130
|
+
aigc_info: {
|
|
131
|
+
aigc_label_type: number;
|
|
132
|
+
};
|
|
133
|
+
org_game_post_use_store_region: boolean;
|
|
134
|
+
privacy_setting_info: {
|
|
135
|
+
allow_comment: number;
|
|
136
|
+
allow_duet: number;
|
|
137
|
+
allow_stitch: number;
|
|
138
|
+
allow_content_reuse: number;
|
|
139
|
+
allow_ai_remix: number;
|
|
140
|
+
visibility_type: number;
|
|
141
|
+
};
|
|
142
|
+
draft_id: string;
|
|
143
|
+
tcm_params: string;
|
|
144
|
+
anchors: {
|
|
145
|
+
type: number;
|
|
146
|
+
keyword: string;
|
|
147
|
+
content: {
|
|
148
|
+
add_from: number;
|
|
149
|
+
keyword: string;
|
|
150
|
+
product_id: string;
|
|
151
|
+
};
|
|
152
|
+
}[];
|
|
153
|
+
}[];
|
|
154
|
+
post_common_info: {
|
|
155
|
+
creation_id: string;
|
|
156
|
+
enter_post_page_from: number;
|
|
157
|
+
post_type: number;
|
|
158
|
+
};
|
|
159
|
+
single_post_req_list: {
|
|
160
|
+
batch_index: number;
|
|
161
|
+
aweme_type: number;
|
|
162
|
+
image_post_content: {
|
|
163
|
+
title: string;
|
|
164
|
+
images: {
|
|
165
|
+
id: string;
|
|
166
|
+
uri: string;
|
|
167
|
+
width_px: number;
|
|
168
|
+
height_px: number;
|
|
169
|
+
}[];
|
|
170
|
+
cover: {
|
|
171
|
+
id: string;
|
|
172
|
+
uri: string;
|
|
173
|
+
width_px: number;
|
|
174
|
+
height_px: number;
|
|
175
|
+
} | undefined;
|
|
176
|
+
};
|
|
177
|
+
single_post_feature_info: {
|
|
178
|
+
music_info?: {
|
|
179
|
+
music_id_string: string;
|
|
180
|
+
music_title: string;
|
|
181
|
+
music_author: string;
|
|
182
|
+
music_url: string;
|
|
183
|
+
music_begin_time: number;
|
|
184
|
+
music_end_time: number;
|
|
185
|
+
origin_volume: string;
|
|
186
|
+
music_volume: string;
|
|
187
|
+
music_album: string;
|
|
188
|
+
music_type: number;
|
|
189
|
+
};
|
|
190
|
+
text: string;
|
|
191
|
+
text_extra: {
|
|
192
|
+
tag_id: string;
|
|
193
|
+
user_id: string;
|
|
194
|
+
start: number;
|
|
195
|
+
end: number;
|
|
196
|
+
type: number;
|
|
197
|
+
hashtag_name: string | undefined;
|
|
198
|
+
}[];
|
|
199
|
+
markup_text: string;
|
|
200
|
+
};
|
|
201
|
+
}[];
|
|
202
|
+
};
|
|
203
|
+
export declare function createPrivatePhotoPostBody(input: PrivatePhotoPostOptions): {
|
|
204
|
+
feature_common_info_list: {
|
|
205
|
+
aigc_info: {
|
|
206
|
+
aigc_label_type: number;
|
|
207
|
+
};
|
|
208
|
+
org_game_post_use_store_region: boolean;
|
|
209
|
+
privacy_setting_info: {
|
|
210
|
+
allow_comment: number;
|
|
211
|
+
allow_duet: number;
|
|
212
|
+
allow_stitch: number;
|
|
213
|
+
allow_content_reuse: number;
|
|
214
|
+
allow_ai_remix: number;
|
|
215
|
+
visibility_type: number;
|
|
216
|
+
};
|
|
217
|
+
draft_id: string;
|
|
218
|
+
tcm_params: string;
|
|
219
|
+
anchors: {
|
|
220
|
+
type: number;
|
|
221
|
+
keyword: string;
|
|
222
|
+
content: {
|
|
223
|
+
add_from: number;
|
|
224
|
+
keyword: string;
|
|
225
|
+
product_id: string;
|
|
226
|
+
};
|
|
227
|
+
}[];
|
|
228
|
+
}[];
|
|
229
|
+
post_common_info: {
|
|
230
|
+
creation_id: string;
|
|
231
|
+
enter_post_page_from: number;
|
|
232
|
+
post_type: number;
|
|
233
|
+
};
|
|
234
|
+
single_post_req_list: {
|
|
235
|
+
batch_index: number;
|
|
236
|
+
aweme_type: number;
|
|
237
|
+
image_post_content: {
|
|
238
|
+
title: string;
|
|
239
|
+
images: {
|
|
240
|
+
id: string;
|
|
241
|
+
uri: string;
|
|
242
|
+
width_px: number;
|
|
243
|
+
height_px: number;
|
|
244
|
+
}[];
|
|
245
|
+
cover: {
|
|
246
|
+
id: string;
|
|
247
|
+
uri: string;
|
|
248
|
+
width_px: number;
|
|
249
|
+
height_px: number;
|
|
250
|
+
} | undefined;
|
|
251
|
+
};
|
|
252
|
+
single_post_feature_info: {
|
|
253
|
+
music_info?: {
|
|
254
|
+
music_id_string: string;
|
|
255
|
+
music_title: string;
|
|
256
|
+
music_author: string;
|
|
257
|
+
music_url: string;
|
|
258
|
+
music_begin_time: number;
|
|
259
|
+
music_end_time: number;
|
|
260
|
+
origin_volume: string;
|
|
261
|
+
music_volume: string;
|
|
262
|
+
music_album: string;
|
|
263
|
+
music_type: number;
|
|
264
|
+
};
|
|
265
|
+
text: string;
|
|
266
|
+
text_extra: {
|
|
267
|
+
tag_id: string;
|
|
268
|
+
user_id: string;
|
|
269
|
+
start: number;
|
|
270
|
+
end: number;
|
|
271
|
+
type: number;
|
|
272
|
+
hashtag_name: string | undefined;
|
|
273
|
+
}[];
|
|
274
|
+
markup_text: string;
|
|
95
275
|
};
|
|
96
276
|
}[];
|
|
97
277
|
};
|
package/dist/post-body.js
CHANGED
|
@@ -5,6 +5,18 @@ export function productAnchors(products) {
|
|
|
5
5
|
return { type: 33, keyword, content: { add_from: 2, keyword, product_id: string(product.productId, 'productId') } };
|
|
6
6
|
});
|
|
7
7
|
}
|
|
8
|
+
function captionFields(caption) {
|
|
9
|
+
const matches = [...caption.matchAll(/#(\w+)/g)];
|
|
10
|
+
const text_extra = matches.map((match, index) => ({ tag_id: String(index), user_id: '', start: match.index, end: match.index + match[0].length, type: 1, hashtag_name: match[1] }));
|
|
11
|
+
let markup_text = '';
|
|
12
|
+
let offset = 0;
|
|
13
|
+
matches.forEach((match, index) => {
|
|
14
|
+
markup_text += caption.slice(offset, match.index).replaceAll('\n', '<br>') + `<h id="${index}">${match[0]}</h>`;
|
|
15
|
+
offset = match.index + match[0].length;
|
|
16
|
+
});
|
|
17
|
+
markup_text += caption.slice(offset).replaceAll('\n', '<br>');
|
|
18
|
+
return { text: caption, text_extra, markup_text };
|
|
19
|
+
}
|
|
8
20
|
export function createWebPostBody(input) {
|
|
9
21
|
string(input.creationId, 'creationId');
|
|
10
22
|
string(input.video.videoId, 'videoId');
|
|
@@ -17,15 +29,6 @@ export function createWebPostBody(input) {
|
|
|
17
29
|
const visibility = input.visibility ?? 'private';
|
|
18
30
|
if (!['public', 'friends', 'private'].includes(visibility))
|
|
19
31
|
throw new Error('Invalid visibility');
|
|
20
|
-
const matches = [...input.caption.matchAll(/#(\w+)/g)];
|
|
21
|
-
const text_extra = matches.map((match, index) => ({ tag_id: String(index), user_id: '', start: match.index, end: match.index + match[0].length, type: 1, hashtag_name: match[1] }));
|
|
22
|
-
let markup_text = '';
|
|
23
|
-
let offset = 0;
|
|
24
|
-
matches.forEach((match, index) => {
|
|
25
|
-
markup_text += input.caption.slice(offset, match.index).replaceAll('\n', '<br>') + `<h id="${index}">${match[0]}</h>`;
|
|
26
|
-
offset = match.index + match[0].length;
|
|
27
|
-
});
|
|
28
|
-
markup_text += input.caption.slice(offset).replaceAll('\n', '<br>');
|
|
29
32
|
return {
|
|
30
33
|
post_common_info: { creation_id: input.creationId, enter_post_page_from: 2, post_type: 3 },
|
|
31
34
|
feature_common_info_list: [{ schedule_time: schedule, geofencing_regions: [], playlist_name: '', playlist_id: '',
|
|
@@ -34,9 +37,76 @@ export function createWebPostBody(input) {
|
|
|
34
37
|
privacy_setting_info: { visibility_type: { public: 0, friends: 2, private: 1 }[visibility], allow_duet: null,
|
|
35
38
|
allow_stitch: null, allow_comment: input.allowComments === false ? 0 : 1, allow_content_reuse: 1 }, content_check_id: '' }],
|
|
36
39
|
single_post_req_list: [{ batch_index: 0, video_id: input.video.videoId, is_long_video: 0,
|
|
37
|
-
single_post_feature_info: {
|
|
40
|
+
single_post_feature_info: { ...captionFields(input.caption), music_info: {},
|
|
38
41
|
cover_info: { frame_duration: 0, blob_url: '', cover_width: input.video.width, cover_height: input.video.height,
|
|
39
42
|
cover_type: 2, crop_type: 2, isAutoCropFirstFrame: true, cover_uri: input.coverUri ?? '', isProcessingInitialCover: false },
|
|
40
43
|
poster_delay: 0, cloud_edit_video_height: input.video.height, cloud_edit_video_width: input.video.width, cloud_edit_is_use_video_canvas: false } }],
|
|
41
44
|
};
|
|
42
45
|
}
|
|
46
|
+
function photoMusicInfo(music) {
|
|
47
|
+
const start = music.startSeconds ?? 0;
|
|
48
|
+
if (!Number.isFinite(start) || !Number.isFinite(music.endSeconds) || start < 0 || music.endSeconds <= start) {
|
|
49
|
+
throw new Error('Invalid music time range');
|
|
50
|
+
}
|
|
51
|
+
const url = new URL(music.url);
|
|
52
|
+
if (url.protocol !== 'https:' || url.username || url.password)
|
|
53
|
+
throw new Error('Invalid music URL');
|
|
54
|
+
return {
|
|
55
|
+
music_id_string: string(music.id, 'music id'),
|
|
56
|
+
music_title: string(music.title, 'music title'),
|
|
57
|
+
music_author: music.author,
|
|
58
|
+
music_url: url.href,
|
|
59
|
+
music_begin_time: start,
|
|
60
|
+
music_end_time: music.endSeconds,
|
|
61
|
+
origin_volume: '100',
|
|
62
|
+
music_volume: '100',
|
|
63
|
+
music_album: music.album ?? '',
|
|
64
|
+
music_type: 1,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
export function createPhotoPostBody(input) {
|
|
68
|
+
string(input.creationId, 'creationId');
|
|
69
|
+
if (!input.products.length)
|
|
70
|
+
throw new Error('At least one product is required');
|
|
71
|
+
if (!input.photos.length || input.photos.length > 35)
|
|
72
|
+
throw new Error('Provide 1 to 35 photos');
|
|
73
|
+
const visibility = input.visibility ?? 'private';
|
|
74
|
+
if (!['public', 'friends', 'private'].includes(visibility))
|
|
75
|
+
throw new Error('Invalid visibility');
|
|
76
|
+
const photos = input.photos.map(photo => {
|
|
77
|
+
if (![photo.width, photo.height].every(n => Number.isSafeInteger(n) && n > 0)) {
|
|
78
|
+
throw new Error('Invalid photo dimensions');
|
|
79
|
+
}
|
|
80
|
+
return { id: string(photo.id, 'photo id'), uri: string(photo.uri, 'photo uri'), width_px: photo.width, height_px: photo.height };
|
|
81
|
+
});
|
|
82
|
+
const coverIndex = input.coverIndex ?? 0;
|
|
83
|
+
if (!Number.isInteger(coverIndex) || coverIndex < 0 || coverIndex >= photos.length)
|
|
84
|
+
throw new Error('Invalid photo cover index');
|
|
85
|
+
const cover = photos[coverIndex];
|
|
86
|
+
return {
|
|
87
|
+
feature_common_info_list: [{
|
|
88
|
+
aigc_info: { aigc_label_type: input.aiGenerated === true ? 1 : 0 },
|
|
89
|
+
org_game_post_use_store_region: false,
|
|
90
|
+
privacy_setting_info: {
|
|
91
|
+
allow_comment: 0, allow_duet: 0, allow_stitch: 0, allow_content_reuse: 0, allow_ai_remix: 2,
|
|
92
|
+
visibility_type: { public: 0, friends: 2, private: 1 }[visibility],
|
|
93
|
+
},
|
|
94
|
+
draft_id: '',
|
|
95
|
+
tcm_params: '{"commerce_toggle_info":{}}',
|
|
96
|
+
anchors: productAnchors(input.products),
|
|
97
|
+
}],
|
|
98
|
+
post_common_info: { creation_id: input.creationId, enter_post_page_from: 8, post_type: 4 },
|
|
99
|
+
single_post_req_list: [{
|
|
100
|
+
batch_index: 0,
|
|
101
|
+
aweme_type: 150,
|
|
102
|
+
image_post_content: { title: input.title, images: photos, cover },
|
|
103
|
+
single_post_feature_info: {
|
|
104
|
+
...captionFields(input.caption),
|
|
105
|
+
...(input.music ? { music_info: photoMusicInfo(input.music) } : {}),
|
|
106
|
+
},
|
|
107
|
+
}],
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
export function createPrivatePhotoPostBody(input) {
|
|
111
|
+
return createPhotoPostBody({ ...input, visibility: 'private' });
|
|
112
|
+
}
|
package/dist/session.js
CHANGED
|
@@ -13,11 +13,18 @@ function stringMap(value) {
|
|
|
13
13
|
export function parseTikTokSession(value) {
|
|
14
14
|
let input = value;
|
|
15
15
|
if (typeof input === 'string') {
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
const session = input.trim();
|
|
17
|
+
const hasSessionCookie = session.split(';').some(pair => /^sessionid=[^\s;]+$/.test(pair.trim()));
|
|
18
|
+
if (hasSessionCookie && !/[\r\n]/.test(session) && /^[A-Za-z0-9_~-]+=/.test(session)) {
|
|
19
|
+
input = { headers: { cookie: session } };
|
|
18
20
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
+
else {
|
|
22
|
+
try {
|
|
23
|
+
input = JSON.parse(session);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
throw new Error('Invalid TikTok session JSON or cookie');
|
|
27
|
+
}
|
|
21
28
|
}
|
|
22
29
|
}
|
|
23
30
|
const outer = object(input), http = object(outer.http ?? outer);
|
package/dist/vod-upload.js
CHANGED
|
@@ -31,7 +31,9 @@ export async function uploadVodVideo(input) {
|
|
|
31
31
|
const store = first(node.StoreInfos);
|
|
32
32
|
const host = new URL(`https://${string(node.UploadHost, 'UploadHost')}`);
|
|
33
33
|
// Session cookies belong to TikTok, never to the separate upload host.
|
|
34
|
-
|
|
34
|
+
const allowedDomains = ['.tiktok.com', '.tiktokv.com', '.tiktokcdn.com', '.byteoversea.com', '.ibyteimg.com'];
|
|
35
|
+
if (host.username || host.password || host.port || host.pathname !== '/' || host.search || host.hash ||
|
|
36
|
+
!allowedDomains.some(domain => host.hostname.endsWith(domain)))
|
|
35
37
|
throw new Error('Unsupported upload host');
|
|
36
38
|
const uploadUrl = new URL(`/upload/v1/${string(store.StoreUri, 'StoreUri')}`, host);
|
|
37
39
|
const headers = { Authorization: string(store.Auth, 'upload Auth'), 'X-Storage-U': encodeURIComponent(input.userId) };
|
package/dist/web-client.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { HttpTransport } from './http.ts';
|
|
2
|
-
import type { WebPostOptions } from './post-body.ts';
|
|
2
|
+
import type { PhotoPostOptions, PrivatePhotoPostOptions, ProductAnchor, WebPostOptions } from './post-body.ts';
|
|
3
3
|
export declare function createTikTokWebClient(options: {
|
|
4
4
|
session: unknown;
|
|
5
5
|
transport?: HttpTransport;
|
|
@@ -15,6 +15,7 @@ export declare function createTikTokWebClient(options: {
|
|
|
15
15
|
title: string;
|
|
16
16
|
raw: import("./http.ts").JsonObject;
|
|
17
17
|
}[]>;
|
|
18
|
+
addShowcaseProducts: (products: ProductAnchor[]) => Promise<import("./http.ts").JsonObject | undefined>;
|
|
18
19
|
getUploadToken: () => Promise<{
|
|
19
20
|
currentTime?: number;
|
|
20
21
|
accessKeyId: string;
|
|
@@ -22,6 +23,8 @@ export declare function createTikTokWebClient(options: {
|
|
|
22
23
|
sessionToken: string;
|
|
23
24
|
}>;
|
|
24
25
|
publishVideo: (input: WebPostOptions) => Promise<import("./http.ts").JsonObject>;
|
|
26
|
+
publishPhotoAlbum(input: PhotoPostOptions): Promise<import("./http.ts").JsonObject>;
|
|
27
|
+
publishPrivatePhotoAlbum(input: PrivatePhotoPostOptions): Promise<import("./http.ts").JsonObject>;
|
|
25
28
|
uploadVideo(input: Omit<WebPostOptions, "creationId" | "video"> & {
|
|
26
29
|
videoPath: string;
|
|
27
30
|
creationId?: string;
|
package/dist/web-client.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto';
|
|
2
2
|
import { createHttp, object, string } from "./http.js";
|
|
3
3
|
import { parseTikTokSession } from "./session.js";
|
|
4
|
-
import { createWebPostBody } from "./post-body.js";
|
|
4
|
+
import { createPhotoPostBody, createPrivatePhotoPostBody, createWebPostBody } from "./post-body.js";
|
|
5
5
|
import { signWebPost } from "./sign-web-request.js";
|
|
6
6
|
import { uploadVodVideo } from "./vod-upload.js";
|
|
7
7
|
export function createTikTokWebClient(options) {
|
|
@@ -18,24 +18,30 @@ export function createTikTokWebClient(options) {
|
|
|
18
18
|
return { accessKeyId: string(token.access_key_id, 'access_key_id'), secretAccessKey: string(token.secret_acess_key ?? token.secret_access_key, 'secret_access_key'),
|
|
19
19
|
sessionToken: string(token.session_token, 'session_token'), ...(typeof token.current_time === 'number' ? { currentTime: token.current_time } : {}) };
|
|
20
20
|
}
|
|
21
|
-
async function
|
|
21
|
+
async function addShowcaseProducts(products) {
|
|
22
22
|
if (!products.length)
|
|
23
23
|
return;
|
|
24
24
|
const url = `https://shop.tiktok.com/aweme/v1/oec/content/creator/products?aid=1180&carrier_region=${carrierRegion}`;
|
|
25
25
|
await request(url, { method: 'OPTIONS', headers });
|
|
26
|
-
|
|
26
|
+
return request(url, { method: 'POST', headers: { ...headers, 'content-type': 'application/json' },
|
|
27
27
|
body: JSON.stringify({ add_targets: [5], products: products.map(p => ({ product_id: string(p.productId, 'productId') })) }) });
|
|
28
28
|
}
|
|
29
|
+
async function publishPost(body, products, registration = 'add') {
|
|
30
|
+
if (registration !== 'add' && registration !== 'existing')
|
|
31
|
+
throw new Error('Invalid product registration mode');
|
|
32
|
+
// Validate/sign before mutating the showcase.
|
|
33
|
+
const query = signWebPost(session, body, now());
|
|
34
|
+
if (registration === 'add')
|
|
35
|
+
await addShowcaseProducts(products);
|
|
36
|
+
return request(`https://www.tiktok.com/tiktok/web/project/post/v1/?${query}`, { method: 'POST',
|
|
37
|
+
headers: { ...headers, 'content-type': 'application/json', origin: 'https://www.tiktok.com' }, body });
|
|
38
|
+
}
|
|
29
39
|
async function publishVideo(input) {
|
|
30
40
|
const body = JSON.stringify(createWebPostBody(input));
|
|
31
41
|
const schedule = input.scheduleTimeSeconds ?? 0;
|
|
32
42
|
if (schedule > 0 && schedule < Math.floor(now() / 1000) + 1200)
|
|
33
43
|
throw new Error('Schedule must be at least 20 minutes ahead');
|
|
34
|
-
|
|
35
|
-
const query = signWebPost(session, body, now());
|
|
36
|
-
await attachProducts(input.products ?? []);
|
|
37
|
-
return request(`https://www.tiktok.com/tiktok/web/project/post/v1/?${query}`, { method: 'POST',
|
|
38
|
-
headers: { ...headers, 'content-type': 'application/json', origin: 'https://www.tiktok.com' }, body });
|
|
44
|
+
return publishPost(body, input.products ?? [], input.productRegistration);
|
|
39
45
|
}
|
|
40
46
|
return {
|
|
41
47
|
async getShowcaseProducts({ count = 6, offset = 0 } = {}) {
|
|
@@ -47,8 +53,15 @@ export function createTikTokWebClient(options) {
|
|
|
47
53
|
throw new Error('Missing products array');
|
|
48
54
|
return products.map(value => { const p = object(value); return { productId: string(p.product_id, 'product_id'), title: string(p.title, 'title'), raw: p }; });
|
|
49
55
|
},
|
|
56
|
+
addShowcaseProducts,
|
|
50
57
|
getUploadToken,
|
|
51
58
|
publishVideo,
|
|
59
|
+
async publishPhotoAlbum(input) {
|
|
60
|
+
return publishPost(JSON.stringify(createPhotoPostBody(input)), input.products, input.productRegistration);
|
|
61
|
+
},
|
|
62
|
+
async publishPrivatePhotoAlbum(input) {
|
|
63
|
+
return publishPost(JSON.stringify(createPrivatePhotoPostBody(input)), input.products, input.productRegistration);
|
|
64
|
+
},
|
|
52
65
|
async uploadVideo(input) {
|
|
53
66
|
// Validate product anchors/caption/options before sending video bytes.
|
|
54
67
|
createWebPostBody({ ...input, creationId: input.creationId ?? 'validate', video: { videoId: 'validate', width: 1, height: 1 } });
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yourname168/tiktok",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Experimental direct TikTok web-session/Shop and official Content Posting API clients, with TypeScript types and no runtime dependencies.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|