vidnavigator 2.0.0 → 2.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.
@@ -1,4 +1,5 @@
1
1
  import { UsageBlock } from './Usage';
2
+ import { VideoInfo, VideoInfoJSON } from './VideoInfo';
2
3
 
3
4
  /** Async job kinds returned by the `/transcribe/async`, `/extract/video/async` and `/tweet/statement/async` endpoints. */
4
5
  export type AsyncJobType = 'transcribe' | 'extract_video' | 'tweet_statement';
@@ -83,6 +84,8 @@ export interface AsyncJobJSON {
83
84
  check_status_url?: string;
84
85
  webhook?: AsyncJobWebhookStatus | null;
85
86
  result?: any;
87
+ /** `extract_video` jobs, once completed: the `video_info` the synchronous endpoint returns. */
88
+ video_info?: VideoInfoJSON;
86
89
  error?: AsyncJobError | null;
87
90
  }
88
91
 
@@ -105,6 +108,8 @@ export class AsyncJob<TResult> {
105
108
  check_status_url?: string;
106
109
  webhook?: AsyncJobWebhookStatus | null;
107
110
  result: TResult | null;
111
+ /** `extract_video` jobs, once completed: metadata of the video the data was extracted from. */
112
+ video_info?: VideoInfo;
108
113
  error: AsyncJobError | null;
109
114
  /** Per-call usage, populated only when polled with `include_usage: true` and `task_status === 'completed'`. */
110
115
  usage?: UsageBlock;
@@ -121,6 +126,7 @@ export class AsyncJob<TResult> {
121
126
  this.check_status_url = data.check_status_url;
122
127
  this.webhook = data.webhook;
123
128
  this.result = data.result !== undefined && data.result !== null ? parseResult(data.result) : null;
129
+ this.video_info = data.video_info ? VideoInfo.fromJSON(data.video_info) : undefined;
124
130
  this.error = data.error ?? null;
125
131
  }
126
132
 
@@ -47,6 +47,11 @@ export interface TikTokSearchRequest {
47
47
  max_likes?: number;
48
48
  min_views?: number;
49
49
  max_views?: number;
50
+ /**
51
+ * Two-letter country code (`'fr'`, `'de'`, `'jp'`, ...) whose TikTok results you get. Default `'gb'`.
52
+ * `'us'` is refused: TikTok asks US visitors to log in. The API lists the accepted codes.
53
+ */
54
+ country?: string;
50
55
  /**
51
56
  * Where to POST a notification when the task finishes. Overrides the account-level default
52
57
  * configured in Studio → API; pass an empty string to opt this task out of that default.
@@ -110,13 +115,24 @@ function normalizeStats(stats: TikTokProfileStats | undefined): TikTokProfileSta
110
115
 
111
116
  function normalizeSearchStats(stats: TikTokSearchStats | undefined): TikTokSearchStats | undefined {
112
117
  if (!stats) return undefined;
113
- return {
118
+ const normalized: TikTokSearchStats = {
114
119
  pages_fetched: toOptionalInteger(stats.pages_fetched),
120
+ videos_fetched: toOptionalInteger(stats.videos_fetched),
115
121
  results_count: toOptionalInteger(stats.results_count),
116
122
  next_search_cursor: toInteger(stats.next_search_cursor),
123
+ parallel_search_slices: toOptionalInteger(stats.parallel_search_slices),
117
124
  sort_by: stats.sort_by,
118
125
  published_within: stats.published_within,
126
+ country: stats.country,
127
+ items_examined: toOptionalInteger(stats.items_examined),
128
+ items_filtered_out: toOptionalInteger(stats.items_filtered_out),
129
+ items_deduplicated: toOptionalInteger(stats.items_deduplicated),
119
130
  };
131
+ // Leave out what the API did not send (the items_* counters are conditional).
132
+ for (const key of Object.keys(normalized) as Array<keyof TikTokSearchStats>) {
133
+ if (normalized[key] === undefined) delete normalized[key];
134
+ }
135
+ return normalized;
120
136
  }
121
137
 
122
138
  function normalizePagination(
@@ -173,6 +189,7 @@ export interface TikTokSearchSubmissionJSON {
173
189
  task_id: string;
174
190
  task_status: 'processing';
175
191
  query: string;
192
+ country?: string;
176
193
  max_results?: number;
177
194
  parallel_search_slices?: number;
178
195
  filters?: Record<string, unknown>;
@@ -186,6 +203,8 @@ export class TikTokSearchSubmission {
186
203
  task_id: string;
187
204
  task_status: 'processing';
188
205
  query: string;
206
+ /** Country whose TikTok results the search returns. */
207
+ country?: string;
189
208
  max_results?: number;
190
209
  parallel_search_slices?: number;
191
210
  filters?: Record<string, unknown>;
@@ -199,6 +218,7 @@ export class TikTokSearchSubmission {
199
218
  this.task_id = data.task_id;
200
219
  this.task_status = data.task_status;
201
220
  this.query = data.query;
221
+ this.country = data.country;
202
222
  this.max_results = toOptionalInteger(data.max_results);
203
223
  this.parallel_search_slices = toOptionalInteger(data.parallel_search_slices);
204
224
  this.filters = data.filters;
@@ -386,12 +406,27 @@ export interface TikTokSearchFilters {
386
406
 
387
407
  export interface TikTokSearchStats {
388
408
  pages_fetched?: number;
409
+ /**
410
+ * Distinct videos TikTok returned, including those your filters dropped. Billed at
411
+ * 1 `residential_request` per 30, minimum 1 per search.
412
+ */
413
+ videos_fetched?: number;
389
414
  results_count?: number;
390
415
  next_search_cursor?: number | null;
416
+ /** Slice count the search actually ran with. */
417
+ parallel_search_slices?: number;
391
418
  /** Sort order the search ran with (`relevance` when `sort_by` was omitted). */
392
419
  sort_by?: TikTokSearchSortBy;
393
420
  /** Date window the search ran with; `all` means no window. */
394
421
  published_within?: TikTokPublishedWithin;
422
+ /** Country whose TikTok results the search returned. */
423
+ country?: string;
424
+ /** Only when filters were set: videos checked against them. */
425
+ items_examined?: number;
426
+ /** Only when filters were set: videos they dropped. */
427
+ items_filtered_out?: number;
428
+ /** Only with `parallel_search_slices` > 1: videos several slices returned, counted once. */
429
+ items_deduplicated?: number;
395
430
  }
396
431
 
397
432
  export interface TikTokProfileTaskJSON {
@@ -461,6 +496,7 @@ export interface TikTokSearchTaskJSON {
461
496
  task_id: string;
462
497
  task_status: 'processing' | 'completed' | 'failed';
463
498
  query?: string;
499
+ country?: string;
464
500
  parallel_search_slices?: number;
465
501
  filters?: TikTokSearchFilters;
466
502
  stats?: TikTokSearchStats;
@@ -479,6 +515,8 @@ export class TikTokSearchTask {
479
515
  task_id: string;
480
516
  task_status: 'processing' | 'completed' | 'failed';
481
517
  query?: string;
518
+ /** Country whose TikTok results the search returned (`gb` by default). */
519
+ country?: string;
482
520
  parallel_search_slices?: number;
483
521
  filters?: TikTokSearchFilters;
484
522
  stats?: TikTokSearchStats;
@@ -501,6 +539,7 @@ export class TikTokSearchTask {
501
539
  this.task_id = data.task_id;
502
540
  this.task_status = data.task_status;
503
541
  this.query = data.query;
542
+ this.country = data.country;
504
543
  this.parallel_search_slices = toOptionalInteger(data.parallel_search_slices);
505
544
  this.filters = normalizeSearchFilters(data.filters);
506
545
  this.stats = normalizeSearchStats(data.stats);
@@ -519,3 +558,265 @@ export class TikTokSearchTask {
519
558
  return new TikTokSearchTask(json);
520
559
  }
521
560
  }
561
+
562
+ // --- LIVE search ---
563
+
564
+ export interface TikTokLiveSearchRequest {
565
+ query: string;
566
+ /** Cap on rooms. `0` or omitted means as many as TikTok serves, about 50 per keyword. */
567
+ max_results?: number;
568
+ /** Two-letter country code whose LIVE rooms you get. Default `'us'`. The API lists the accepted codes. */
569
+ country?: string;
570
+ /**
571
+ * The API returns each room's playback URLs by default (FLV and HLS by quality, plus an
572
+ * audio-only stream). Pass `false` to leave them out.
573
+ */
574
+ include_stream_urls?: boolean;
575
+ /**
576
+ * Where to POST a notification when the task finishes. Overrides the account-level default
577
+ * configured in Studio → API; pass an empty string to opt this task out of that default.
578
+ * The event's `data.result` is a summary; read the rooms with `vn.tiktokLiveSearch.resume(task_id).result()`.
579
+ */
580
+ webhook_url?: string;
581
+ }
582
+
583
+ export interface TikTokLiveSearchSubmissionJSON {
584
+ task_id: string;
585
+ task_status: 'processing';
586
+ query: string;
587
+ country?: string;
588
+ max_results?: number;
589
+ include_stream_urls?: boolean;
590
+ expires_at?: string;
591
+ check_status_url?: string;
592
+ message?: string;
593
+ webhook_url?: string | null;
594
+ }
595
+
596
+ export class TikTokLiveSearchSubmission {
597
+ task_id: string;
598
+ task_status: 'processing';
599
+ query: string;
600
+ country?: string;
601
+ max_results?: number;
602
+ include_stream_urls?: boolean;
603
+ expires_at?: string;
604
+ check_status_url?: string;
605
+ message?: string;
606
+ /** The URL this task's result notification will be POSTed to, or `null`. */
607
+ webhook_url?: string | null;
608
+
609
+ constructor(data: TikTokLiveSearchSubmissionJSON) {
610
+ this.task_id = data.task_id;
611
+ this.task_status = data.task_status;
612
+ this.query = data.query;
613
+ this.country = data.country;
614
+ this.max_results = toOptionalInteger(data.max_results);
615
+ this.include_stream_urls = data.include_stream_urls;
616
+ this.expires_at = data.expires_at;
617
+ this.check_status_url = data.check_status_url;
618
+ this.message = data.message;
619
+ this.webhook_url = data.webhook_url;
620
+ }
621
+
622
+ static fromJSON(json: TikTokLiveSearchSubmissionJSON): TikTokLiveSearchSubmission {
623
+ return new TikTokLiveSearchSubmission(json);
624
+ }
625
+ }
626
+
627
+ export interface TikTokLiveAuthor {
628
+ id?: string | null;
629
+ /** Username, without @. */
630
+ unique_id?: string | null;
631
+ nickname?: string | null;
632
+ sec_uid?: string | null;
633
+ followers?: number | null;
634
+ avatar_url?: string | null;
635
+ }
636
+
637
+ export interface TikTokLiveStats {
638
+ /** Watching when the search ran. */
639
+ viewers?: number | null;
640
+ /** Since the LIVE started. */
641
+ total_viewers?: number | null;
642
+ likes?: number | null;
643
+ comments?: number | null;
644
+ shares?: number | null;
645
+ }
646
+
647
+ /** Quality keys of the playback URLs: those the LIVE offers, best first. */
648
+ export type TikTokLiveQuality = 'origin' | 'uhd' | 'hd' | 'sd' | 'ld' | (string & {});
649
+
650
+ export interface TikTokLiveStreamUrlsJSON {
651
+ default_quality?: TikTokLiveQuality | null;
652
+ flv?: Record<string, string>;
653
+ hls?: Record<string, string>;
654
+ audio_only?: string | null;
655
+ expires_at?: string | null;
656
+ }
657
+
658
+ /** Playback URLs of a LIVE room. They stop working when the LIVE ends. */
659
+ export interface TikTokLiveStreamUrls {
660
+ /** The quality TikTok plays by default (a key of `flv`). */
661
+ default_quality: TikTokLiveQuality | null;
662
+ /** HTTP-FLV URLs keyed by quality. */
663
+ flv: Record<string, string>;
664
+ /** HLS (`.m3u8`) URLs keyed by quality, when TikTok provides them. Can be empty. */
665
+ hls: Record<string, string>;
666
+ /** HTTP-FLV URL of the audio-only stream. */
667
+ audio_only: string | null;
668
+ /** Expiry of the URL signatures. */
669
+ expires_at: Date | null;
670
+ }
671
+
672
+ export interface TikTokLiveRoomJSON {
673
+ id?: string | null;
674
+ title?: string | null;
675
+ url?: string | null;
676
+ started_at?: string | null;
677
+ author?: TikTokLiveAuthor;
678
+ stats?: TikTokLiveStats;
679
+ hashtags?: string[];
680
+ category?: string | null;
681
+ game_tags?: string[];
682
+ cover_url?: string | null;
683
+ stream_urls?: TikTokLiveStreamUrlsJSON;
684
+ }
685
+
686
+ /** A TikTok LIVE room that was broadcasting when the search ran. */
687
+ export class TikTokLiveRoom {
688
+ id?: string | null;
689
+ title?: string | null;
690
+ /** `https://www.tiktok.com/@username/live` */
691
+ url?: string | null;
692
+ started_at?: Date | null;
693
+ author?: TikTokLiveAuthor;
694
+ stats?: TikTokLiveStats;
695
+ /** `#tags` written in the title, without `#`. */
696
+ hashtags: string[];
697
+ /** The LIVE's category on TikTok, e.g. `'Gaming'`. */
698
+ category?: string | null;
699
+ game_tags: string[];
700
+ cover_url?: string | null;
701
+ /** Absent when the search was submitted with `include_stream_urls: false`. */
702
+ stream_urls?: TikTokLiveStreamUrls;
703
+
704
+ constructor(data: TikTokLiveRoomJSON) {
705
+ this.id = data.id;
706
+ this.title = data.title;
707
+ this.url = data.url;
708
+ this.started_at = toNullableDate(data.started_at);
709
+ this.author = data.author
710
+ ? { ...data.author, followers: toInteger(data.author.followers) }
711
+ : undefined;
712
+ this.stats = data.stats
713
+ ? {
714
+ viewers: toInteger(data.stats.viewers),
715
+ total_viewers: toInteger(data.stats.total_viewers),
716
+ likes: toInteger(data.stats.likes),
717
+ comments: toInteger(data.stats.comments),
718
+ shares: toInteger(data.stats.shares),
719
+ }
720
+ : undefined;
721
+ this.hashtags = data.hashtags ?? [];
722
+ this.category = data.category;
723
+ this.game_tags = data.game_tags ?? [];
724
+ this.cover_url = data.cover_url;
725
+ if (data.stream_urls) {
726
+ this.stream_urls = {
727
+ default_quality: data.stream_urls.default_quality ?? null,
728
+ flv: data.stream_urls.flv ?? {},
729
+ hls: data.stream_urls.hls ?? {},
730
+ audio_only: data.stream_urls.audio_only ?? null,
731
+ expires_at: toNullableDate(data.stream_urls.expires_at) ?? null,
732
+ };
733
+ }
734
+ }
735
+
736
+ static fromJSON(json: TikTokLiveRoomJSON): TikTokLiveRoom {
737
+ return new TikTokLiveRoom(json);
738
+ }
739
+ }
740
+
741
+ export interface TikTokLiveSearchStats {
742
+ /** TikTok pages fetched (up to 50 rooms each). */
743
+ pages_fetched?: number;
744
+ /**
745
+ * Distinct rooms TikTok returned, including any past `max_results`. Billed at
746
+ * 1 `residential_request` per 30, minimum 1 per search.
747
+ */
748
+ rooms_fetched?: number;
749
+ results_count?: number;
750
+ /** Whether TikTok had more rooms when the search stopped. */
751
+ has_more?: boolean;
752
+ country?: string;
753
+ }
754
+
755
+ export interface TikTokLiveSearchTaskJSON {
756
+ task_id: string;
757
+ task_status: 'processing' | 'completed' | 'failed';
758
+ query?: string;
759
+ country?: string;
760
+ stats?: TikTokLiveSearchStats;
761
+ results?: TikTokLiveRoomJSON[];
762
+ pagination?: TikTokProfilePagination;
763
+ download_url?: string | null;
764
+ error_message?: string | null;
765
+ created_at?: string | null;
766
+ completed_at?: string | null;
767
+ expires_at?: string | null;
768
+ error?: AsyncJobError | null;
769
+ webhook?: AsyncJobWebhookStatus | null;
770
+ }
771
+
772
+ export class TikTokLiveSearchTask {
773
+ task_id: string;
774
+ task_status: 'processing' | 'completed' | 'failed';
775
+ query?: string;
776
+ /** Country whose LIVE rooms the search returned (`us` by default). */
777
+ country?: string;
778
+ stats?: TikTokLiveSearchStats;
779
+ /** Rooms in TikTok's ranking. */
780
+ results: TikTokLiveRoom[];
781
+ pagination?: TikTokProfilePagination;
782
+ download_url?: string | null;
783
+ /** @deprecated Use `error`, which carries a machine-readable code. */
784
+ error_message?: string | null;
785
+ created_at?: string | null;
786
+ completed_at?: string | null;
787
+ expires_at?: string | null;
788
+ /** Present when `task_status === 'failed'`: error code, message and HTTP status. */
789
+ error: AsyncJobError | null;
790
+ /** Webhook delivery state, or `null` when no webhook was configured. */
791
+ webhook?: AsyncJobWebhookStatus | null;
792
+ /** Per-call usage, populated only when polled with `include_usage=true` and `task_status=completed`. */
793
+ usage?: UsageBlock;
794
+
795
+ constructor(data: TikTokLiveSearchTaskJSON) {
796
+ this.task_id = data.task_id;
797
+ this.task_status = data.task_status;
798
+ this.query = data.query;
799
+ this.country = data.country;
800
+ this.stats = data.stats
801
+ ? {
802
+ ...data.stats,
803
+ pages_fetched: toOptionalInteger(data.stats.pages_fetched),
804
+ rooms_fetched: toOptionalInteger(data.stats.rooms_fetched),
805
+ results_count: toOptionalInteger(data.stats.results_count),
806
+ }
807
+ : undefined;
808
+ this.results = data.results?.map(TikTokLiveRoom.fromJSON) ?? [];
809
+ this.pagination = normalizePagination(data.pagination);
810
+ this.download_url = data.download_url;
811
+ this.error_message = data.error_message;
812
+ this.created_at = data.created_at;
813
+ this.completed_at = data.completed_at;
814
+ this.expires_at = data.expires_at;
815
+ this.error = data.error ?? null;
816
+ this.webhook = data.webhook;
817
+ }
818
+
819
+ static fromJSON(json: TikTokLiveSearchTaskJSON): TikTokLiveSearchTask {
820
+ return new TikTokLiveSearchTask(json);
821
+ }
822
+ }
package/src/webhooks.ts CHANGED
@@ -13,9 +13,11 @@ export type WebhookEventType =
13
13
  | 'tiktok_profile.completed'
14
14
  | 'tiktok_profile.failed'
15
15
  | 'tiktok_search.completed'
16
- | 'tiktok_search.failed';
16
+ | 'tiktok_search.failed'
17
+ | 'tiktok_live_search.completed'
18
+ | 'tiktok_live_search.failed';
17
19
 
18
- /** `data.result` of a `tiktok_profile.*` / `tiktok_search.*` event: a summary, not the videos. */
20
+ /** `data.result` of a `tiktok_profile.*` / `tiktok_search.*` / `tiktok_live_search.*` event: a summary, not the items. */
19
21
  export interface TikTokWebhookResult {
20
22
  /** Same fields as the task's `stats` (e.g. `videos_matched`, or `results_count` / `sort_by` for searches). */
21
23
  stats?: Record<string, unknown>;
@@ -44,6 +46,8 @@ export interface WebhookEvent {
44
46
  result?: Record<string, unknown> | TikTokWebhookResult | null;
45
47
  /** True when the result exceeded 256 KB; fetch it with `resume(task_id).result()` instead. */
46
48
  result_truncated?: boolean;
49
+ /** `extract_video.completed` only: raw `video_info` of the video. Sent even when `result_truncated` is true. */
50
+ video_info?: Record<string, unknown>;
47
51
  error?: AsyncJobError | null;
48
52
  };
49
53
  }