youtube-transcript-plus 1.0.1 → 1.0.3
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/dist/index.d.ts +1 -0
- package/dist/youtube-transcript-plus.js +26 -26
- package/package.json +10 -10
package/dist/index.d.ts
CHANGED
|
@@ -34,7 +34,7 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
|
|
|
34
34
|
};
|
|
35
35
|
|
|
36
36
|
const DEFAULT_USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36';
|
|
37
|
-
const RE_YOUTUBE = /(?:
|
|
37
|
+
const RE_YOUTUBE = /(?:v=|\/|v\/|embed\/|watch\?.*v=|youtu\.be\/|\/v\/|e\/|watch\?.*vi?=|\/embed\/|\/v\/|vi?\/|watch\?.*vi?=|youtu\.be\/|\/vi?\/|\/e\/)([a-zA-Z0-9_-]{11})/i;
|
|
38
38
|
const RE_XML_TRANSCRIPT = /<text start="([^"]*)" dur="([^"]*)">([^<]*)<\/text>/g;
|
|
39
39
|
|
|
40
40
|
class YoutubeTranscriptTooManyRequestError extends Error {
|
|
@@ -85,7 +85,7 @@ function retrieveVideoId(videoId) {
|
|
|
85
85
|
throw new YoutubeTranscriptInvalidVideoIdError();
|
|
86
86
|
}
|
|
87
87
|
function defaultFetch(_a) {
|
|
88
|
-
return __awaiter(this, arguments,
|
|
88
|
+
return __awaiter(this, arguments, void 0, function* ({ url, lang, userAgent, }) {
|
|
89
89
|
return fetch(url, {
|
|
90
90
|
headers: Object.assign(Object.assign({}, (lang && { 'Accept-Language': lang })), { 'User-Agent': userAgent || DEFAULT_USER_AGENT }),
|
|
91
91
|
});
|
|
@@ -99,7 +99,7 @@ class FsCache {
|
|
|
99
99
|
fs.mkdir(cacheDir, { recursive: true }).catch(() => { });
|
|
100
100
|
}
|
|
101
101
|
get(key) {
|
|
102
|
-
return __awaiter(this,
|
|
102
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
103
103
|
const filePath = path.join(this.cacheDir, key);
|
|
104
104
|
try {
|
|
105
105
|
const data = yield fs.readFile(filePath, 'utf-8');
|
|
@@ -114,9 +114,9 @@ class FsCache {
|
|
|
114
114
|
});
|
|
115
115
|
}
|
|
116
116
|
set(key, value, ttl) {
|
|
117
|
-
return __awaiter(this,
|
|
117
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
118
118
|
const filePath = path.join(this.cacheDir, key);
|
|
119
|
-
const expires = Date.now() + (ttl !== null && ttl !==
|
|
119
|
+
const expires = Date.now() + (ttl !== null && ttl !== void 0 ? ttl : this.defaultTTL);
|
|
120
120
|
yield fs.writeFile(filePath, JSON.stringify({ value, expires }), 'utf-8');
|
|
121
121
|
});
|
|
122
122
|
}
|
|
@@ -129,7 +129,7 @@ class InMemoryCache {
|
|
|
129
129
|
this.defaultTTL = defaultTTL;
|
|
130
130
|
}
|
|
131
131
|
get(key) {
|
|
132
|
-
return __awaiter(this,
|
|
132
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
133
133
|
const entry = this.cache.get(key);
|
|
134
134
|
if (entry && entry.expires > Date.now()) {
|
|
135
135
|
return entry.value;
|
|
@@ -139,8 +139,8 @@ class InMemoryCache {
|
|
|
139
139
|
});
|
|
140
140
|
}
|
|
141
141
|
set(key, value, ttl) {
|
|
142
|
-
return __awaiter(this,
|
|
143
|
-
const expires = Date.now() + (ttl !== null && ttl !==
|
|
142
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
143
|
+
const expires = Date.now() + (ttl !== null && ttl !== void 0 ? ttl : this.defaultTTL);
|
|
144
144
|
this.cache.set(key, { value, expires });
|
|
145
145
|
});
|
|
146
146
|
}
|
|
@@ -151,17 +151,17 @@ class YoutubeTranscript {
|
|
|
151
151
|
this.config = config;
|
|
152
152
|
}
|
|
153
153
|
fetchTranscript(videoId) {
|
|
154
|
-
return __awaiter(this,
|
|
154
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
155
155
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
156
156
|
const identifier = retrieveVideoId(videoId);
|
|
157
|
-
const userAgent = ((_a = this.config) === null || _a ===
|
|
157
|
+
const userAgent = ((_a = this.config) === null || _a === void 0 ? void 0 : _a.userAgent) || DEFAULT_USER_AGENT;
|
|
158
158
|
// Use custom fetch functions if provided, otherwise use defaultFetch
|
|
159
|
-
const videoFetch = ((_b = this.config) === null || _b ===
|
|
160
|
-
const transcriptFetch = ((_c = this.config) === null || _c ===
|
|
159
|
+
const videoFetch = ((_b = this.config) === null || _b === void 0 ? void 0 : _b.videoFetch) || defaultFetch;
|
|
160
|
+
const transcriptFetch = ((_c = this.config) === null || _c === void 0 ? void 0 : _c.transcriptFetch) || defaultFetch;
|
|
161
161
|
// Cache key based on video ID and language
|
|
162
|
-
const cacheKey = `transcript:${identifier}:${((_d = this.config) === null || _d ===
|
|
162
|
+
const cacheKey = `transcript:${identifier}:${((_d = this.config) === null || _d === void 0 ? void 0 : _d.lang) || 'default'}`;
|
|
163
163
|
// Check cache first
|
|
164
|
-
if ((_e = this.config) === null || _e ===
|
|
164
|
+
if ((_e = this.config) === null || _e === void 0 ? void 0 : _e.cache) {
|
|
165
165
|
const cachedTranscript = yield this.config.cache.get(cacheKey);
|
|
166
166
|
if (cachedTranscript) {
|
|
167
167
|
return JSON.parse(cachedTranscript);
|
|
@@ -170,7 +170,7 @@ class YoutubeTranscript {
|
|
|
170
170
|
// Fetch the video page
|
|
171
171
|
const videoPageResponse = yield videoFetch({
|
|
172
172
|
url: `https://www.youtube.com/watch?v=${identifier}`,
|
|
173
|
-
lang: (_f = this.config) === null || _f ===
|
|
173
|
+
lang: (_f = this.config) === null || _f === void 0 ? void 0 : _f.lang,
|
|
174
174
|
userAgent,
|
|
175
175
|
});
|
|
176
176
|
if (!videoPageResponse.ok) {
|
|
@@ -195,24 +195,24 @@ class YoutubeTranscript {
|
|
|
195
195
|
catch (e) {
|
|
196
196
|
return undefined;
|
|
197
197
|
}
|
|
198
|
-
})()) === null || _g ===
|
|
198
|
+
})()) === null || _g === void 0 ? void 0 : _g['playerCaptionsTracklistRenderer'];
|
|
199
199
|
if (!captions) {
|
|
200
200
|
throw new YoutubeTranscriptDisabledError(identifier);
|
|
201
201
|
}
|
|
202
202
|
if (!('captionTracks' in captions)) {
|
|
203
203
|
throw new YoutubeTranscriptNotAvailableError(identifier);
|
|
204
204
|
}
|
|
205
|
-
if (((_h = this.config) === null || _h ===
|
|
206
|
-
!captions.captionTracks.some((track) => { var _a; return track.languageCode === ((_a = this.config) === null || _a ===
|
|
207
|
-
throw new YoutubeTranscriptNotAvailableLanguageError((_j = this.config) === null || _j ===
|
|
205
|
+
if (((_h = this.config) === null || _h === void 0 ? void 0 : _h.lang) &&
|
|
206
|
+
!captions.captionTracks.some((track) => { var _a; return track.languageCode === ((_a = this.config) === null || _a === void 0 ? void 0 : _a.lang); })) {
|
|
207
|
+
throw new YoutubeTranscriptNotAvailableLanguageError((_j = this.config) === null || _j === void 0 ? void 0 : _j.lang, captions.captionTracks.map((track) => track.languageCode), identifier);
|
|
208
208
|
}
|
|
209
|
-
const transcriptURL = (((_k = this.config) === null || _k ===
|
|
210
|
-
? captions.captionTracks.find((track) => { var _a; return track.languageCode === ((_a = this.config) === null || _a ===
|
|
209
|
+
const transcriptURL = (((_k = this.config) === null || _k === void 0 ? void 0 : _k.lang)
|
|
210
|
+
? captions.captionTracks.find((track) => { var _a; return track.languageCode === ((_a = this.config) === null || _a === void 0 ? void 0 : _a.lang); })
|
|
211
211
|
: captions.captionTracks[0]).baseUrl;
|
|
212
212
|
// Fetch the transcript
|
|
213
213
|
const transcriptResponse = yield transcriptFetch({
|
|
214
214
|
url: transcriptURL,
|
|
215
|
-
lang: (_l = this.config) === null || _l ===
|
|
215
|
+
lang: (_l = this.config) === null || _l === void 0 ? void 0 : _l.lang,
|
|
216
216
|
userAgent,
|
|
217
217
|
});
|
|
218
218
|
if (!transcriptResponse.ok) {
|
|
@@ -226,11 +226,11 @@ class YoutubeTranscript {
|
|
|
226
226
|
text: result[3],
|
|
227
227
|
duration: parseFloat(result[2]),
|
|
228
228
|
offset: parseFloat(result[1]),
|
|
229
|
-
lang: (_b = (_a = this.config) === null || _a ===
|
|
229
|
+
lang: (_b = (_a = this.config) === null || _a === void 0 ? void 0 : _a.lang) !== null && _b !== void 0 ? _b : captions.captionTracks[0].languageCode,
|
|
230
230
|
});
|
|
231
231
|
});
|
|
232
232
|
// Store in cache if a strategy is provided
|
|
233
|
-
if ((_m = this.config) === null || _m ===
|
|
233
|
+
if ((_m = this.config) === null || _m === void 0 ? void 0 : _m.cache) {
|
|
234
234
|
yield this.config.cache.set(cacheKey, JSON.stringify(transcript), this.config.cacheTTL);
|
|
235
235
|
}
|
|
236
236
|
return transcript;
|
|
@@ -238,7 +238,7 @@ class YoutubeTranscript {
|
|
|
238
238
|
}
|
|
239
239
|
// Add static method for new usage pattern
|
|
240
240
|
static fetchTranscript(videoId, config) {
|
|
241
|
-
return __awaiter(this,
|
|
241
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
242
242
|
const instance = new YoutubeTranscript(config);
|
|
243
243
|
return instance.fetchTranscript(videoId);
|
|
244
244
|
});
|
|
@@ -247,4 +247,4 @@ class YoutubeTranscript {
|
|
|
247
247
|
// Export the static method directly for convenience
|
|
248
248
|
const fetchTranscript = YoutubeTranscript.fetchTranscript;
|
|
249
249
|
|
|
250
|
-
export { FsCache, InMemoryCache, YoutubeTranscript, fetchTranscript };
|
|
250
|
+
export { FsCache, InMemoryCache, YoutubeTranscript, YoutubeTranscriptDisabledError, YoutubeTranscriptInvalidVideoIdError, YoutubeTranscriptNotAvailableError, YoutubeTranscriptNotAvailableLanguageError, YoutubeTranscriptTooManyRequestError, YoutubeTranscriptVideoUnavailableError, fetchTranscript };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "youtube-transcript-plus",
|
|
3
|
-
"version": "1.0.
|
|
4
|
-
"description": "Fetch transcript from a
|
|
3
|
+
"version": "1.0.3",
|
|
4
|
+
"description": "Fetch transcript from a YouTube video",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/youtube-transcript-plus.js",
|
|
7
7
|
"module": "dist/youtube-transcript-plus.js",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"format": "prettier --write 'src/**/*.ts'",
|
|
12
12
|
"test": "jest",
|
|
13
13
|
"test:watch": "jest --watch",
|
|
14
|
-
"prepare": "husky
|
|
14
|
+
"prepare": "husky"
|
|
15
15
|
},
|
|
16
16
|
"author": "ericmmartin",
|
|
17
17
|
"keywords": [
|
|
@@ -33,23 +33,23 @@
|
|
|
33
33
|
"https-proxy-agent": "^7.0.6",
|
|
34
34
|
"husky": "^9.1.7",
|
|
35
35
|
"jest": "^29.7.0",
|
|
36
|
-
"lint-staged": "^15.
|
|
37
|
-
"prettier": "^3.
|
|
38
|
-
"rollup": "^4.
|
|
36
|
+
"lint-staged": "^15.5.0",
|
|
37
|
+
"prettier": "^3.5.3",
|
|
38
|
+
"rollup": "^4.37.0",
|
|
39
39
|
"rollup-plugin-typescript": "^1.0.1",
|
|
40
40
|
"rollup-plugin-typescript2": "^0.36.0",
|
|
41
|
-
"ts-jest": "^29.
|
|
41
|
+
"ts-jest": "^29.3.0",
|
|
42
42
|
"tslib": "^2.8.1",
|
|
43
|
-
"typescript": "^5.
|
|
43
|
+
"typescript": "^5.8.2"
|
|
44
44
|
},
|
|
45
45
|
"files": [
|
|
46
46
|
"dist/*"
|
|
47
47
|
],
|
|
48
|
-
"repository": "https://github.com/ericmmartin/youtube-transcript.git",
|
|
48
|
+
"repository": "https://github.com/ericmmartin/youtube-transcript-plus.git",
|
|
49
49
|
"publishConfig": {
|
|
50
50
|
"access": "public"
|
|
51
51
|
},
|
|
52
|
-
"homepage": "https://github.com/ericmmartin/youtube-transcript",
|
|
52
|
+
"homepage": "https://github.com/ericmmartin/youtube-transcript-plus",
|
|
53
53
|
"engines": {
|
|
54
54
|
"node": ">=18.0.0"
|
|
55
55
|
}
|