Repository navigation
Expand file tree
/
Copy pathtypes.ts
More file actions
714 lines (625 loc) · 21.8 KB
/
Copy pathtypes.ts
File metadata and controls
714 lines (625 loc) · 21.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/**
* Shared request/response types for the GetYouTubeTranscript API.
* Mirrors https://getyoutubetranscript.com/openapi.json
*/
/** Standard error body returned by the API on any non-2xx response. */
export interface ApiErrorBody {
success: false;
code: string;
message: string;
/** Present on 402 responses. */
creditsLeft?: number;
topupCreditsLeft?: number;
/** Present on 429 responses. */
requestsThisMinute?: number;
/** Credits an AI transcript needs. Present on 402 responses to `mode: 'generate' | 'auto'`. */
credits_required?: number;
/** The failed AI transcript job, on errors from `getTranscriptJob`. */
job?: TranscriptJob;
[key: string]: unknown;
}
export interface ApiSuccessBody<T> {
success: true;
data: T;
}
// ---------------------------------------------------------------------------
// GET /transcript
// ---------------------------------------------------------------------------
/**
* `native` (default): captions only, 1 credit.
* `auto`: captions if the video has any (1 credit), otherwise an AI speech-to-text transcript.
* `generate`: always an AI transcript.
* An AI transcript costs 3 credits per minute of video, rounded up, minimum 3. Videos up to 4 hours.
*/
export type TranscriptMode = "native" | "auto" | "generate";
export interface GetTranscriptParams {
/** YouTube video URL (full or short) or an 11-character video ID. */
v: string;
/**
* Caption language code, e.g. 'en', 'es'. Defaults to 'en'. With an AI
* `mode`, the language spoken in the video; omit it to use the video's own
* audio language.
*/
language?: string;
/**
* Also return per-line timing as `segments`. Same 1 credit. The query
* param is only sent when this is `true`.
*/
timestamps?: boolean;
/** See {@link TranscriptMode}. Defaults to 'native'. Only sent when set. */
mode?: TranscriptMode;
}
export type TranscriptJobStatus = "queued" | "running" | "done" | "failed";
/** An AI transcript job, returned when the transcript takes longer than about 20 seconds. */
export interface TranscriptJob {
job_id: string;
status: TranscriptJobStatus;
video_id: string;
/** Credits taken when the job started. Refunded automatically if it fails. */
credits_charged: number;
/** Path to poll: /api/v1/transcript/jobs?id=<job_id>. */
poll_url: string;
/** How long to wait between polls (5). */
poll_interval_seconds: number;
created_at: string;
/** Present only when `status` is 'failed'. */
error?: { code?: string; message?: string };
}
/**
* One answer from GET /transcript, unwrapped: `data` is the transcript
* (HTTP 200), or `null` with a `job` to poll (HTTP 202).
*/
export interface TranscriptResponse {
data: TranscriptData | null;
/** Present on a 202, and on AI transcripts that came from a job. */
job?: TranscriptJob;
/** The X-Credits-Charged header: what this call cost. Sent on `mode: 'auto' | 'generate'` answers. */
creditsCharged?: number;
}
export interface GetTranscriptJobParams {
/** The `job_id` from a 202 answer. */
id: string;
/** Include `segments` in the finished transcript. */
timestamps?: boolean;
}
/** GET /transcript/jobs: `data` is `null` while the job is queued or running. */
export interface TranscriptJobResponse {
data: TranscriptData | null;
job: TranscriptJob;
}
export interface WaitForTranscriptOptions {
/** Milliseconds between polls. Defaults to the job's `poll_interval_seconds` (5 seconds). */
pollIntervalMs?: number;
/** Give up after this many milliseconds. Defaults to 1800000 (30 minutes). The job keeps running; poll it later with `getTranscriptJob`. */
timeoutMs?: number;
}
/** One caption line with its timing, returned in `segments` when `timestamps: true`. */
export interface Segment {
/** Start time in seconds. */
start: number;
/** Duration in seconds. */
duration: number;
text: string;
}
/** `manual` = uploaded by the creator, `auto` = YouTube speech recognition, `ai` = our AI transcript (`mode: 'auto' | 'generate'`), `null` = unknown (older transcripts). */
export type CaptionType = "manual" | "auto" | "ai" | null;
/** Fields shared by a transcript and a succeeded batch item. */
export interface TranscriptFields {
video_id: string;
/** The caption track actually returned. */
language_code: string;
/** What was asked for. Differs from `language_code` when YouTube didn't have that language. */
requested_language: string;
caption_type: CaptionType;
title: string;
author_name: string;
author_url: string;
thumbnail_url: string;
transcript: string;
word_count: number;
/** ISO 8601 time the transcript was fetched from YouTube. */
fetched_at: string | null;
/** Per-line timing. Present only when the request set `timestamps: true`. */
segments?: Segment[];
/** Always 'ai'. Present only on AI transcripts. */
source?: "ai";
/** Video length in seconds. Present only on AI transcripts; the cost is based on it. */
duration_seconds?: number;
}
export interface TranscriptData extends TranscriptFields {
/** `true` when served from the stored copy rather than fetched from YouTube just now. Absent on AI transcripts. */
cached?: boolean;
}
// ---------------------------------------------------------------------------
// GET /transcript/languages (1 credit)
// ---------------------------------------------------------------------------
export interface GetTranscriptLanguagesParams {
/** YouTube video URL (full or short) or an 11-character video ID. */
v: string;
}
export interface TranscriptLanguage {
language_code: string;
/** YouTube's display name, e.g. "English (auto-generated)". */
name: string;
caption_type: "manual" | "auto";
}
export interface TranscriptLanguagesData {
video_id: string;
/** What `getTranscript` returns with no `language`; `null` when the video has no captions. */
default_language_code: string | null;
/** One per language and caption type. Empty when the video has captions turned off. */
languages: TranscriptLanguage[];
}
// ---------------------------------------------------------------------------
// POST /batch, GET /batch
// ---------------------------------------------------------------------------
export interface CreateBatchParams {
/** 1-100 video URLs or 11-character IDs. Duplicates are fetched once. */
videos: string[];
/** Caption language code for every video. Defaults to 'en'. */
language?: string;
/** Include per-line `segments` in the results. */
timestamps?: boolean;
/** Public https URL (port 443) that receives a signed `batch.completed` POST. See `verifyWebhookSignature`. */
webhookUrl?: string;
/** Sent as the `Idempotency-Key` header: a retry with the same key returns the original batch. */
idempotencyKey?: string;
}
export interface GetBatchParams {
/** The `batch_id` from `createBatch`. */
id: string;
/** Items to skip. Defaults to 0. */
offset?: number;
/** Items per page, 1-50. Defaults to 20. */
limit?: number;
}
export interface WaitForBatchOptions {
/** Milliseconds between status checks. Defaults to 3000. */
pollIntervalMs?: number;
/** Give up after this many milliseconds. Defaults to 900000 (15 minutes). */
timeoutMs?: number;
/** Items fetched per request once complete, 1-50. Defaults to 50. */
pageSize?: number;
}
export type BatchStatus = "queued" | "processing" | "completed";
/** One video in a batch: succeeded items carry the transcript fields, failed ones an `error_code`. */
export interface BatchItem extends Partial<TranscriptFields> {
/** Index in the submitted `videos` list (after de-duplication). */
position: number;
video_id: string;
status: "pending" | "succeeded" | "failed";
/** `true` when this video used a credit. */
charged: boolean;
/** e.g. `TRANSCRIPT_DISABLED`, `VIDEO_UNAVAILABLE`, `PAYMENT_REQUIRED`. */
error_code?: string;
}
export interface BatchData {
batch_id: string;
status: BatchStatus;
language: string;
timestamps: boolean;
total: number;
succeeded: number;
failed: number;
pending: number;
webhook_status: "none" | "pending" | "delivered" | "failed";
created_at: string;
completed_at: string | null;
results_url: string;
/** `createBatch` only, and only when `webhookUrl` was given. Shown once. */
webhook_secret?: string;
/** `createBatch`: `true` when the Idempotency-Key matched an earlier batch. */
idempotent_replay?: boolean;
/** `getBatch` / `waitForBatch`. */
credits_charged?: number;
/** `getBatch`: one page. `waitForBatch`: every item. */
items?: BatchItem[];
/** `getBatch`: offset of the next page, `null` on the last one. */
next_offset?: number | null;
}
// ---------------------------------------------------------------------------
// GET /search
// ---------------------------------------------------------------------------
export interface SearchParams {
/** Search query. Required unless `pageToken` is set. */
q?: string;
/** Opaque continuation token from a previous response. */
pageToken?: string;
/** Restrict results to one kind. Defaults to 'video'. */
type?: "video" | "channel";
/** Two-letter region code, e.g. 'us'. */
country?: string;
/** Result language hint, e.g. 'en'. */
language?: string;
/** Max results for this page. */
limit?: number;
}
export interface VideoResult {
title: string;
videoId: string;
channel?: {
name?: string;
[key: string]: unknown;
};
[key: string]: unknown;
}
export interface ChannelResult {
title?: string;
channelId?: string;
[key: string]: unknown;
}
/** @deprecated The API never returns `pagination`; use `SearchData.continuation_token`. Kept for type compatibility. */
export interface SearchPagination {
next_page_token?: string;
[key: string]: unknown;
}
export interface SearchData {
query?: string;
video_results?: VideoResult[];
channel_results?: ChannelResult[];
/** Pass back as `pageToken` for the next page. Missing or null when there are no more pages. */
continuation_token?: string | null;
/** @deprecated Never returned by the API; use `continuation_token`. */
pagination?: SearchPagination;
[key: string]: unknown;
}
// ---------------------------------------------------------------------------
// GET /resolve (1 credit)
// ---------------------------------------------------------------------------
export interface ResolveChannelParams {
/** Channel @handle, channel URL, or UC... id. */
handle: string;
}
export interface ResolveChannelData {
channel_id: string;
title: string;
handle: string;
resolved_via: string;
[key: string]: unknown;
}
// ---------------------------------------------------------------------------
// GET /playlist
// ---------------------------------------------------------------------------
export interface GetPlaylistParams {
/** Playlist ID or URL. Required unless `continuation` is set. */
list?: string;
/** Opaque continuation token from a previous response. */
continuation?: string;
}
export interface PlaylistVideo {
position: number;
id: string;
title: string;
channel?: {
name?: string;
[key: string]: unknown;
};
[key: string]: unknown;
}
export interface PlaylistData {
playlist_id: string;
title: string;
videos: PlaylistVideo[];
has_more: boolean;
continuation_token?: string;
[key: string]: unknown;
}
// ---------------------------------------------------------------------------
// GET /channel/latest (1 credit)
// ---------------------------------------------------------------------------
export interface GetChannelLatestParams {
/** Channel @handle, channel URL, or UC... id. */
channel: string;
}
/**
* Field set is passed through from upstream and may grow over time -
* treat as loosely typed, per the API's own OpenAPI spec.
*/
export type ChannelLatestData = Record<string, unknown>;
// ---------------------------------------------------------------------------
// GET /channel/search
// ---------------------------------------------------------------------------
export interface SearchChannelParams {
/** Channel @handle, URL, or UC... id. Required unless `continuation` is set. */
channel?: string;
/** Query to search within the channel. Required unless `continuation` is set. */
q?: string;
/** Opaque continuation token from a previous response. */
continuation?: string;
}
export interface ChannelVideo {
id: string;
title: string;
length?: string;
published_time?: string;
thumbnail?: string;
[key: string]: unknown;
}
export interface ChannelVideosData {
videos: ChannelVideo[];
has_more: boolean;
continuation_token?: string;
[key: string]: unknown;
}
// ---------------------------------------------------------------------------
// GET /channel/videos
// ---------------------------------------------------------------------------
/** Shared by the paged channel lists: `channel` for a first page, `continuation` for the next. */
export interface ChannelPageParams {
/** Channel @handle, URL, or UC... id. Required unless `continuation` is set. */
channel?: string;
/** Opaque continuation token from a previous response. */
continuation?: string;
}
/** `streams` is the channel's Live tab. */
export type ChannelVideosTab = "videos" | "shorts" | "streams";
export type ChannelVideosSort = "latest" | "popular" | "oldest";
export interface ListChannelVideosParams extends ChannelPageParams {
/** Which list to read. Defaults to 'videos'. First page only: a continuation already carries it. */
tab?: ChannelVideosTab;
/** Order of the list. Defaults to 'latest'. First page only: a continuation already carries it. */
sort?: ChannelVideosSort;
}
// ---------------------------------------------------------------------------
// GET /channel/playlists (1 credit)
// ---------------------------------------------------------------------------
export type ListChannelPlaylistsParams = ChannelPageParams;
export interface ChannelPlaylist {
position?: number;
/** Pass to `getPlaylist` as `list` to read its videos. */
id: string;
title: string;
link?: string;
type?: string;
video_count_text?: string;
video_count?: number;
thumbnail?: string;
[key: string]: unknown;
}
export interface ChannelPlaylistsData {
playlists: ChannelPlaylist[];
has_more: boolean;
continuation_token?: string | null;
[key: string]: unknown;
}
// ---------------------------------------------------------------------------
// GET /channel/posts (1 credit)
// ---------------------------------------------------------------------------
export type ListChannelPostsParams = ChannelPageParams;
export interface ChannelPost {
position?: number;
id: string;
url?: string;
text?: string;
links?: string[];
published_time?: string;
/** As shown by YouTube, e.g. "12K". */
likes?: string;
comments?: string;
/** The post's image, video or poll, when it has one. */
attachment?: Record<string, unknown>;
[key: string]: unknown;
}
export interface ChannelPostsData {
posts: ChannelPost[];
has_more: boolean;
continuation_token?: string | null;
[key: string]: unknown;
}
// ---------------------------------------------------------------------------
// GET /channel/banner (1 credit)
// ---------------------------------------------------------------------------
export interface GetChannelBannerParams {
/** Channel @handle, URL, or UC... id. */
channel: string;
}
export interface ChannelBannerSize {
url: string;
width?: number;
height?: number;
}
export interface ChannelBannerData {
channel_id: string;
handle?: string;
title?: string;
avatar?: string;
/** `false` for a channel with no banner; `banner` is then absent. */
has_banner: boolean;
banner?: {
original?: string;
largest?: string;
sizes?: ChannelBannerSize[];
};
[key: string]: unknown;
}
// ---------------------------------------------------------------------------
// GET /video/related (1 credit)
// ---------------------------------------------------------------------------
export interface GetRelatedVideosParams {
/** YouTube video URL (full or short) or an 11-character video ID. */
v: string;
}
export interface RelatedVideo {
position?: number;
id: string;
title: string;
link?: string;
channel?: {
name?: string;
[key: string]: unknown;
};
views?: string;
published_time?: string;
length?: string;
thumbnail?: string;
[key: string]: unknown;
}
export interface RelatedVideosData {
video_id: string;
related: RelatedVideo[];
[key: string]: unknown;
}
// ---------------------------------------------------------------------------
// GET /video/dislikes (1 credit)
// ---------------------------------------------------------------------------
export interface GetVideoDislikesParams {
/** YouTube video URL (full or short) or an 11-character video ID. */
v: string;
}
/** YouTube no longer publishes dislikes, so every number here is an estimate. */
export interface VideoDislikesData {
video_id: string;
likes: number;
dislikes: number;
/** Estimated star rating out of 5. */
rating?: number;
view_count?: number;
/** Likes and dislikes as reported by the estimate source's own users. */
raw_likes?: number;
raw_dislikes?: number;
deleted?: boolean;
/** Always `true`. */
estimated: boolean;
source?: string;
[key: string]: unknown;
}
// ---------------------------------------------------------------------------
// GET /video (1 credit)
// ---------------------------------------------------------------------------
export interface GetVideoDetailsParams {
/** YouTube video URL (full or short) or an 11-character video ID. */
v: string;
}
export interface VideoThumbnail {
url: string;
width?: number;
height?: number;
}
export interface VideoDetailsData {
video_id: string;
title: string;
description?: string;
channel?: {
id?: string;
name?: string;
/** Rounded label as YouTube shows it, e.g. "4.56M subscribers". */
subscribers?: string;
thumbnail?: string;
[key: string]: unknown;
};
/** Exact view count as a digit string, e.g. "1840125263". */
views?: string;
/** Rounded label as YouTube shows it, e.g. "19M". */
likes?: string;
/** As YouTube shows it, e.g. "Oct 24, 2009". */
upload_date?: string;
/** Length in seconds. */
duration?: number;
duration_formatted?: string;
thumbnails?: VideoThumbnail[];
is_live?: boolean;
is_private?: boolean;
/** The video's tags. */
keywords?: string[];
[key: string]: unknown;
}
// ---------------------------------------------------------------------------
// GET /video/comments (1 credit per page)
// ---------------------------------------------------------------------------
export interface GetVideoCommentsParams {
/** YouTube video URL (full or short) or an 11-character video ID. Required on every page. */
v: string;
/**
* `continuation_token` from the previous page. Sent together with `v`
* (it does not replace it). Expires after 24 hours.
*/
continuation?: string;
}
export interface VideoComment {
comment_id: string;
link?: string;
/** The commenter. */
channel?: {
id?: string;
handle?: string;
link?: string;
thumbnail?: string;
[key: string]: unknown;
};
/** Relative, as YouTube shows it, e.g. "7 years ago (edited)". */
published_date?: string;
text: string;
/** Rounded label, e.g. "73K". */
likes?: string;
replies?: number;
[key: string]: unknown;
}
export interface VideoCommentsData {
video_id: string;
comments: VideoComment[];
has_more: boolean;
continuation_token?: string | null;
[key: string]: unknown;
}
// ---------------------------------------------------------------------------
// GET /tiktok/transcript (1 credit)
// ---------------------------------------------------------------------------
export interface GetTikTokTranscriptParams {
/** TikTok video URL, short link (vm.tiktok.com/...), or numeric video ID. */
url: string;
}
/** One TikTok caption line. Unlike `Segment`, there is no `duration`. */
export interface TikTokSegment {
/** Start time in seconds. */
start: number;
text: string;
}
export interface TikTokTranscriptData {
video_id: string;
/** The creator's username, without the @. */
author: string;
/** The video's caption text, cut to 500 characters. */
description: string;
/** Caption language, e.g. "eng-US". */
language: string;
/** True when TikTok generated the captions from speech. */
auto_generated: boolean;
segments: TikTokSegment[];
[key: string]: unknown;
}
// ---------------------------------------------------------------------------
// GET /credits (free)
// ---------------------------------------------------------------------------
export interface CreditsData {
/** Unused credits from this billing period's plan allowance. Resets on renewal. */
plan_credits_left: number;
/** Unused credits from purchased top-up batches. Spent only once plan_credits_left reaches 0. */
topup_credits_left: number;
/** plan_credits_left + topup_credits_left - check this, not plan_credits_left alone, for the real spendable balance. */
total_credits_left: number;
plan: "free" | "monthly" | "yearly";
rate_limit_per_minute: number;
[key: string]: unknown;
}
// ---------------------------------------------------------------------------
// POST /signup, POST /signup/verify (free, no key required)
// ---------------------------------------------------------------------------
export interface SignupResponse {
success: true;
message: string;
}
export interface VerifySignupResponse {
success: true;
api_key: string;
}
// ---------------------------------------------------------------------------
// Client config
// ---------------------------------------------------------------------------
export interface GetYouTubeTranscriptOptions {
/** Your API key, e.g. `sk_live_...`. Get one free at https://getyoutubetranscript.com */
apiKey: string;
/** Override the API base URL. Defaults to `https://getyoutubetranscript.com/api/v1`. */
baseUrl?: string;
/** Override the fetch implementation (mainly for testing). Defaults to global `fetch`. */
fetch?: typeof fetch;
}