Repository navigation
Expand file tree
/
Copy pathtypes.ts
More file actions
367 lines (319 loc) · 11.3 KB
/
Copy pathtypes.ts
File metadata and controls
367 lines (319 loc) · 11.3 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
/**
* 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;
[key: string]: unknown;
}
export interface ApiSuccessBody<T> {
success: true;
data: T;
}
// ---------------------------------------------------------------------------
// GET /transcript
// ---------------------------------------------------------------------------
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'. */
language?: string;
/**
* Also return per-line timing as `segments`. Same 1 credit. The query
* param is only sent when this is `true`.
*/
timestamps?: boolean;
}
/** 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, `null` = unknown (older transcripts). */
export type CaptionType = "manual" | "auto" | 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[];
}
export interface TranscriptData extends TranscriptFields {
/** `true` when served from the stored copy rather than fetched from YouTube just now. */
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
// ---------------------------------------------------------------------------
export interface ListChannelVideosParams {
/** Channel @handle, URL, or UC... id. Required unless `continuation` is set. */
channel?: string;
/** Opaque continuation token from a previous response. */
continuation?: string;
}
// ---------------------------------------------------------------------------
// GET /credits (free)
// ---------------------------------------------------------------------------
export interface CreditsData {
plan_credits_left: number;
topup_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;
}