vidnavigator 2.1.0 → 2.3.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/src/index.ts CHANGED
@@ -38,6 +38,15 @@ import {
38
38
  TikTokSearchSubmissionJSON,
39
39
  TikTokSearchTask,
40
40
  TikTokSearchTaskJSON,
41
+ TikTokLiveSearchRequest,
42
+ TikTokLiveSearchSubmission,
43
+ TikTokLiveSearchSubmissionJSON,
44
+ TikTokLiveSearchTask,
45
+ TikTokLiveSearchTaskJSON,
46
+ TikTokLiveRoomRequest,
47
+ TikTokLiveRoomResult,
48
+ TikTokLiveRoomResultJSON,
49
+ tiktokLiveRoomResultFromJSON,
41
50
  TweetStatement,
42
51
  AsyncJob,
43
52
  AsyncJobJSON,
@@ -56,7 +65,13 @@ export { Job } from './jobs';
56
65
  export type { TaskStatus, PollableTask, PollOptions, JobOperation, SubmitOptions, RunOptions, JobInfo } from './jobs';
57
66
 
58
67
  /** SDK version (keep in sync with package.json) */
59
- export const SDK_VERSION = '2.1.0';
68
+ export const SDK_VERSION = '2.3.0';
69
+
70
+ /**
71
+ * A LIVE room usually answers in 1-5 s, up to ~30 s when the API has to open a TikTok page, and two
72
+ * minutes at worst. A shorter configured axios timeout is raised to this for that call only.
73
+ */
74
+ const LIVE_ROOM_TIMEOUT_MS = 120_000;
60
75
 
61
76
  //region --- Interfaces ---
62
77
  export interface SDKConfig {
@@ -172,6 +187,8 @@ export type TweetStatementJob = Job<TweetStatementAsyncJob, TweetStatement>;
172
187
  export type TikTokProfileJob = Job<TikTokProfileTask, TikTokProfileTask>;
173
188
  /** `result()` resolves to the completed task with **all** pages of `results` collected. */
174
189
  export type TikTokSearchJob = Job<TikTokSearchTask, TikTokSearchTask>;
190
+ /** `result()` resolves to the completed task with **all** pages of rooms collected. */
191
+ export type TikTokLiveSearchJob = Job<TikTokLiveSearchTask, TikTokLiveSearchTask>;
175
192
 
176
193
  export type UploadFileSuccessResult = {
177
194
  status: 'success';
@@ -438,6 +455,13 @@ export class VidNavigatorClient {
438
455
  */
439
456
  readonly tiktokSearch: JobOperation<string | TikTokSearchRequest, TikTokSearchTask, TikTokSearchTask>;
440
457
 
458
+ /**
459
+ * TikTok LIVE search, built on `POST /tiktok/live/search`: rooms broadcasting now for a keyword,
460
+ * with their FLV / HLS / audio-only stream URLs. The result is the completed task with every
461
+ * page of `results` collected. Same shapes as {@link VidNavigatorClient.transcribe}.
462
+ */
463
+ readonly tiktokLiveSearch: JobOperation<string | TikTokLiveSearchRequest, TikTokLiveSearchTask, TikTokLiveSearchTask>;
464
+
441
465
  constructor(config: SDKConfig) {
442
466
  if (!config?.apiKey) {
443
467
  throw new Error('An API key is required to use the VidNavigator SDK.');
@@ -520,6 +544,22 @@ export class VidNavigatorClient {
520
544
  options.include_usage
521
545
  ),
522
546
  });
547
+
548
+ this.tiktokLiveSearch = this.defineOperation({
549
+ jobType: 'tiktok_live_search',
550
+ submit: async (input: string | TikTokLiveSearchRequest) => {
551
+ const sub = await this.submitTikTokLiveSearch(typeof input === 'string' ? { query: input } : input);
552
+ return { ...sub, job_type: 'tiktok_live_search' };
553
+ },
554
+ fetchTask: (task_id) => this.getTikTokLiveSearch(task_id, { limit: 1 }),
555
+ toResult: (task, options) =>
556
+ collectAllPages(
557
+ (query) => this.getTikTokLiveSearch(task.task_id, query),
558
+ (page) => page.results,
559
+ (page, all) => { page.results = all; },
560
+ options.include_usage
561
+ ),
562
+ });
523
563
  }
524
564
 
525
565
  private defineOperation<TInput, TTask extends PollableTask, TResult>(
@@ -584,8 +624,13 @@ export class VidNavigatorClient {
584
624
  url: string,
585
625
  data?: any,
586
626
  params?: any,
587
- extraHeaders?: any
627
+ extraHeaders?: any,
628
+ minTimeoutMs?: number
588
629
  ): Promise<T> {
630
+ // A call known to run long raises a configured timeout, never lowers it
631
+ // (0 / unset means no timeout at all).
632
+ const configured = this.client.defaults.timeout;
633
+ const timeout = minTimeoutMs && configured ? Math.max(configured, minTimeoutMs) : undefined;
589
634
  try {
590
635
  const response = await this.client.request<T>({
591
636
  method,
@@ -593,6 +638,7 @@ export class VidNavigatorClient {
593
638
  data,
594
639
  params,
595
640
  headers: extraHeaders,
641
+ ...(timeout !== undefined ? { timeout } : {}),
596
642
  });
597
643
  return response.data;
598
644
  } catch (error: any) {
@@ -606,7 +652,7 @@ export class VidNavigatorClient {
606
652
  //region --- Transcripts ---
607
653
  /**
608
654
  * Get a transcript for any supported video. The endpoint auto-detects the
609
- * platform (YouTube, Vimeo, X/Twitter, TikTok, Facebook, Dailymotion, Loom, etc.)
655
+ * platform (YouTube, Vimeo, X/Twitter, TikTok, Facebook, Dailymotion, Loom, Snapchat, etc.)
610
656
  * from `video_url`. For Instagram, use {@link VidNavigatorClient.transcribeVideo}.
611
657
  */
612
658
  async getTranscript(payload: TranscriptRequestPayload): Promise<TranscriptResult> {
@@ -697,6 +743,68 @@ export class VidNavigatorClient {
697
743
  if (response.usage) task.usage = UsageBlock.fromJSON(response.usage);
698
744
  return task;
699
745
  }
746
+
747
+ async submitTikTokLiveSearch(payload: TikTokLiveSearchRequest): Promise<TikTokLiveSearchSubmission> {
748
+ const response = await this.request<ApiSuccessResponse<TikTokLiveSearchSubmissionJSON>>(
749
+ 'POST',
750
+ '/tiktok/live/search',
751
+ payload
752
+ );
753
+ return TikTokLiveSearchSubmission.fromJSON(response.data);
754
+ }
755
+
756
+ async getTikTokLiveSearch(
757
+ task_id: string,
758
+ query?: { cursor?: string; limit?: number; include_usage?: boolean }
759
+ ): Promise<TikTokLiveSearchTask> {
760
+ const response = await this.request<ApiSuccessResponse<TikTokLiveSearchTaskJSON>>(
761
+ 'GET',
762
+ `/tiktok/live/search/${task_id}`,
763
+ undefined,
764
+ buildTikTokPollParams(query)
765
+ );
766
+ const task = TikTokLiveSearchTask.fromJSON(response.data);
767
+ if (response.usage) task.usage = UsageBlock.fromJSON(response.usage);
768
+ return task;
769
+ }
770
+
771
+ /**
772
+ * One TikTok LIVE room, right away (`POST /tiktok/live/room`, no job): by `room_id`, `username`
773
+ * or `url`, exactly one. The room says whether it is live or ended (and when), its viewers and new
774
+ * followers, the streamer's bio, and more. With `include_comments: true` the result also has the
775
+ * latest comments (up to 50), the pinned comment, the intro and the top viewers.
776
+ *
777
+ * The room has no likes, comment count or share count: TikTok gives those to LIVE search only.
778
+ * A room or account that does not exist throws `NotFoundError` (`error_code` `live_not_found`)
779
+ * and is not billed. Billing: 1 `residential_request`, 2 with `include_comments`.
780
+ */
781
+ async getTikTokLiveRoom(payload: TikTokLiveRoomRequest): Promise<TikTokLiveRoomResult> {
782
+ const targets = (['room_id', 'username', 'url'] as const).filter((key) => payload?.[key]);
783
+ if (targets.length !== 1) {
784
+ throw new VidNavigatorError('Pass exactly one of room_id, username or url');
785
+ }
786
+ let body: TikTokLiveRoomRequest = payload;
787
+ const roomId: unknown = payload.room_id;
788
+ if (typeof roomId === 'number') {
789
+ if (!Number.isSafeInteger(roomId)) {
790
+ throw new VidNavigatorError(
791
+ 'room_id has more digits than a JavaScript number holds: pass it as a string'
792
+ );
793
+ }
794
+ body = { ...payload, room_id: String(roomId) };
795
+ }
796
+ const response = await this.request<ApiSuccessResponse<TikTokLiveRoomResultJSON>>(
797
+ 'POST',
798
+ '/tiktok/live/room',
799
+ body,
800
+ undefined,
801
+ undefined,
802
+ LIVE_ROOM_TIMEOUT_MS
803
+ );
804
+ const result = tiktokLiveRoomResultFromJSON(response.data);
805
+ if (response.usage) result.usage = UsageBlock.fromJSON(response.usage);
806
+ return result;
807
+ }
700
808
  //endregion
701
809
 
702
810
  //region --- Files ---