youtube-transcript-plus 1.2.0 → 2.0.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,5 +1,5 @@
1
- import fs from 'fs/promises';
2
- import path from 'path';
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
3
3
 
4
4
  /******************************************************************************
5
5
  Copyright (c) Microsoft Corporation.
@@ -189,7 +189,7 @@ class YoutubeTranscript {
189
189
  }
190
190
  fetchTranscript(videoId) {
191
191
  return __awaiter(this, void 0, void 0, function* () {
192
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
192
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p;
193
193
  const identifier = retrieveVideoId(videoId);
194
194
  const lang = (_a = this.config) === null || _a === void 0 ? void 0 : _a.lang;
195
195
  const userAgent = (_c = (_b = this.config) === null || _b === void 0 ? void 0 : _b.userAgent) !== null && _c !== void 0 ? _c : DEFAULT_USER_AGENT;
@@ -203,7 +203,7 @@ class YoutubeTranscript {
203
203
  try {
204
204
  return JSON.parse(cached);
205
205
  }
206
- catch (_p) {
206
+ catch (_q) {
207
207
  // ignore parse errors and continue
208
208
  }
209
209
  }
@@ -258,17 +258,17 @@ class YoutubeTranscript {
258
258
  if (!playerRes.ok) {
259
259
  throw new YoutubeTranscriptVideoUnavailableError(identifier);
260
260
  }
261
- const playerJson = yield playerRes.json();
262
- const tracklist = (_k = (_j = playerJson === null || playerJson === void 0 ? void 0 : playerJson.captions) === null || _j === void 0 ? void 0 : _j.playerCaptionsTracklistRenderer) !== null && _k !== void 0 ? _k : playerJson === null || playerJson === void 0 ? void 0 : playerJson.playerCaptionsTracklistRenderer;
261
+ const playerJson = (yield playerRes.json());
262
+ const tracklist = (_k = (_j = playerJson.captions) === null || _j === void 0 ? void 0 : _j.playerCaptionsTracklistRenderer) !== null && _k !== void 0 ? _k : playerJson.playerCaptionsTracklistRenderer;
263
263
  const tracks = tracklist === null || tracklist === void 0 ? void 0 : tracklist.captionTracks;
264
- const isPlayableOk = ((_l = playerJson === null || playerJson === void 0 ? void 0 : playerJson.playabilityStatus) === null || _l === void 0 ? void 0 : _l.status) === 'OK';
264
+ const isPlayableOk = ((_l = playerJson.playabilityStatus) === null || _l === void 0 ? void 0 : _l.status) === 'OK';
265
265
  // If `captions` is entirely missing, treat as "not available"
266
- if (!(playerJson === null || playerJson === void 0 ? void 0 : playerJson.captions) || !tracklist) {
267
- // If video is playable but captions arent provided, treat as "disabled"
266
+ if (!playerJson.captions || !tracklist) {
267
+ // If video is playable but captions aren't provided, treat as "disabled"
268
268
  if (isPlayableOk) {
269
269
  throw new YoutubeTranscriptDisabledError(identifier);
270
270
  }
271
- // Otherwise we cant assert theyre disabled; treat as "not available"
271
+ // Otherwise we can't assert they're disabled; treat as "not available"
272
272
  throw new YoutubeTranscriptNotAvailableError(identifier);
273
273
  }
274
274
  // If `captions` exists but there are zero tracks, treat as "disabled"
@@ -276,22 +276,25 @@ class YoutubeTranscript {
276
276
  throw new YoutubeTranscriptDisabledError(identifier);
277
277
  }
278
278
  // Respect requested language or fallback to first track
279
- const selectedTrack = lang ? tracks.find((t) => t.languageCode === lang) : tracks[0];
279
+ const selectedTrack = lang
280
+ ? tracks.find((t) => t.languageCode === lang)
281
+ : tracks[0];
280
282
  if (!selectedTrack) {
281
283
  const available = tracks.map((t) => t.languageCode).filter(Boolean);
282
284
  throw new YoutubeTranscriptNotAvailableLanguageError(lang, available, identifier);
283
285
  }
284
286
  // 4) Build transcript URL; prefer XML by stripping fmt if present
285
- let transcriptURL = selectedTrack.baseUrl || selectedTrack.url;
286
- if (!transcriptURL) {
287
+ const transcriptBaseURL = (_m = selectedTrack.baseUrl) !== null && _m !== void 0 ? _m : selectedTrack.url;
288
+ if (!transcriptBaseURL) {
287
289
  throw new YoutubeTranscriptNotAvailableError(identifier);
288
290
  }
291
+ let transcriptURL = transcriptBaseURL;
289
292
  transcriptURL = transcriptURL.replace(/&fmt=[^&]+/, '');
290
- if ((_m = this.config) === null || _m === void 0 ? void 0 : _m.disableHttps) {
293
+ if ((_o = this.config) === null || _o === void 0 ? void 0 : _o.disableHttps) {
291
294
  transcriptURL = transcriptURL.replace(/^https:\/\//, 'http://');
292
295
  }
293
296
  // 5) Fetch transcript XML using the same hook surface as before
294
- const transcriptResponse = ((_o = this.config) === null || _o === void 0 ? void 0 : _o.transcriptFetch)
297
+ const transcriptResponse = ((_p = this.config) === null || _p === void 0 ? void 0 : _p.transcriptFetch)
295
298
  ? yield this.config.transcriptFetch({ url: transcriptURL, lang, userAgent })
296
299
  : yield defaultFetch({ url: transcriptURL, lang, userAgent });
297
300
  if (!transcriptResponse.ok) {
@@ -318,7 +321,7 @@ class YoutubeTranscript {
318
321
  try {
319
322
  yield cache.set(cacheKey, JSON.stringify(transcript), cacheTTL);
320
323
  }
321
- catch (_q) {
324
+ catch (_r) {
322
325
  // non-fatal
323
326
  }
324
327
  }