-
Notifications
You must be signed in to change notification settings - Fork 812
/
Copy pathinfo.js
482 lines (419 loc) · 15.9 KB
/
info.js
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
const { URL } = require('url');
const querystring = require('querystring');
const sax = require('sax');
const miniget = require('miniget');
const utils = require('./utils');
// Forces Node JS version of setTimeout for Electron based applications
const { setTimeout } = require('timers');
const formatUtils = require('./format-utils');
const urlUtils = require('./url-utils');
const extras = require('./info-extras');
const sig = require('./sig');
const Cache = require('./cache');
const BASE_URL = 'https://www.youtube.com/watch?v=';
// Cached for storing basic/full info.
exports.cache = new Cache();
exports.cookieCache = new Cache(1000 * 60 * 60 * 24);
exports.watchPageCache = new Cache();
// Special error class used to determine if an error is unrecoverable,
// as in, ytdl-core should not try again to fetch the video metadata.
// In this case, the video is usually unavailable in some way.
class UnrecoverableError extends Error {}
// List of URLs that show up in `notice_url` for age restricted videos.
const AGE_RESTRICTED_URLS = [
'support.google.com/youtube/?p=age_restrictions',
'youtube.com/t/community_guidelines',
];
/**
* Gets info from a video without getting additional formats.
*
* @param {string} id
* @param {Object} options
* @returns {Promise<Object>}
*/
exports.getBasicInfo = async(id, options) => {
const retryOptions = Object.assign({}, miniget.defaultOptions, options.requestOptions);
options.requestOptions = Object.assign({}, options.requestOptions, {});
options.requestOptions.headers = Object.assign({},
{
// eslint-disable-next-line max-len
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.101 Safari/537.36',
}, options.requestOptions.headers);
const validate = info => {
let playErr = utils.playError(info.player_response, ['ERROR'], UnrecoverableError);
let privateErr = privateVideoError(info.player_response);
if (playErr || privateErr) {
throw playErr || privateErr;
}
return info && info.player_response && (
info.player_response.streamingData || isRental(info.player_response) || isNotYetBroadcasted(info.player_response)
);
};
let info = await pipeline([id, options], validate, retryOptions, [
getWatchHTMLPage,
getWatchJSONPage,
getVideoInfoPage,
]);
Object.assign(info, {
formats: parseFormats(info.player_response),
related_videos: extras.getRelatedVideos(info),
});
// Add additional properties to info.
const media = extras.getMedia(info);
const additional = {
author: extras.getAuthor(info),
media,
likes: extras.getLikes(info),
dislikes: extras.getDislikes(info),
age_restricted: !!(media && media.notice_url && AGE_RESTRICTED_URLS.some(url => media.notice_url.includes(url))),
// Give the standard link to the video.
video_url: BASE_URL + id,
storyboards: extras.getStoryboards(info),
chapters: extras.getChapters(info),
};
info.videoDetails = extras.cleanVideoDetails(Object.assign({},
info.player_response && info.player_response.microformat &&
info.player_response.microformat.playerMicroformatRenderer,
info.player_response && info.player_response.videoDetails, additional), info);
return info;
};
const privateVideoError = player_response => {
let playability = player_response && player_response.playabilityStatus;
if (playability && playability.status === 'LOGIN_REQUIRED' && playability.messages &&
playability.messages.filter(m => /This is a private video/.test(m)).length) {
return new UnrecoverableError(playability.reason || (playability.messages && playability.messages[0]));
} else {
return null;
}
};
const isRental = player_response => {
let playability = player_response.playabilityStatus;
return playability && playability.status === 'UNPLAYABLE' &&
playability.errorScreen && playability.errorScreen.playerLegacyDesktopYpcOfferRenderer;
};
const isNotYetBroadcasted = player_response => {
let playability = player_response.playabilityStatus;
return playability && playability.status === 'LIVE_STREAM_OFFLINE';
};
const getWatchHTMLURL = (id, options) => `${BASE_URL + id}&hl=${options.lang || 'en'}`;
const getWatchHTMLPageBody = (id, options) => {
const url = getWatchHTMLURL(id, options);
return exports.watchPageCache.getOrSet(url, () => utils.exposedMiniget(url, options).text());
};
const EMBED_URL = 'https://www.youtube.com/embed/';
const getEmbedPageBody = (id, options) => {
const embedUrl = `${EMBED_URL + id}?hl=${options.lang || 'en'}`;
return utils.exposedMiniget(embedUrl, options).text();
};
const getHTML5player = body => {
let html5playerRes =
/<script\s+src="([^"]+)"(?:\s+type="text\/javascript")?\s+name="player_ias\/base"\s*>|"jsUrl":"([^"]+)"/
.exec(body);
return html5playerRes ? html5playerRes[1] || html5playerRes[2] : null;
};
const getIdentityToken = (id, options, key, throwIfNotFound) =>
exports.cookieCache.getOrSet(key, async() => {
let page = await getWatchHTMLPageBody(id, options);
let match = page.match(/(["'])ID_TOKEN\1[:,]\s?"([^"]+)"/);
if (!match && throwIfNotFound) {
throw new UnrecoverableError('Cookie header used in request, but unable to find YouTube identity token');
}
return match && match[2];
});
/**
* Goes through each endpoint in the pipeline, retrying on failure if the error is recoverable.
* If unable to succeed with one endpoint, moves onto the next one.
*
* @param {Array.<Object>} args
* @param {Function} validate
* @param {Object} retryOptions
* @param {Array.<Function>} endpoints
* @returns {[Object, Object, Object]}
*/
const pipeline = async(args, validate, retryOptions, endpoints) => {
let info;
for (let func of endpoints) {
try {
const newInfo = await retryFunc(func, args.concat([info]), retryOptions);
if (newInfo.player_response) {
newInfo.player_response.videoDetails = assign(
info && info.player_response && info.player_response.videoDetails,
newInfo.player_response.videoDetails);
newInfo.player_response = assign(info && info.player_response, newInfo.player_response);
}
info = assign(info, newInfo);
if (validate(info, false)) {
break;
}
} catch (err) {
if (err instanceof UnrecoverableError || func === endpoints[endpoints.length - 1]) {
throw err;
}
// Unable to find video metadata... so try next endpoint.
}
}
return info;
};
/**
* Like Object.assign(), but ignores `null` and `undefined` from `source`.
*
* @param {Object} target
* @param {Object} source
* @returns {Object}
*/
const assign = (target, source) => {
if (!target || !source) { return target || source; }
for (let [key, value] of Object.entries(source)) {
if (value !== null && value !== undefined) {
target[key] = value;
}
}
return target;
};
/**
* Given a function, calls it with `args` until it's successful,
* or until it encounters an unrecoverable error.
* Currently, any error from miniget is considered unrecoverable. Errors such as
* too many redirects, invalid URL, status code 404, status code 502.
*
* @param {Function} func
* @param {Array.<Object>} args
* @param {Object} options
* @param {number} options.maxRetries
* @param {Object} options.backoff
* @param {number} options.backoff.inc
*/
const retryFunc = async(func, args, options) => {
let currentTry = 0, result;
while (currentTry <= options.maxRetries) {
try {
result = await func(...args);
break;
} catch (err) {
if (err instanceof UnrecoverableError ||
(err instanceof miniget.MinigetError && err.statusCode < 500) || currentTry >= options.maxRetries) {
throw err;
}
let wait = Math.min(++currentTry * options.backoff.inc, options.backoff.max);
await new Promise(resolve => setTimeout(resolve, wait));
}
}
return result;
};
const jsonClosingChars = /^[)\]}'\s]+/;
const parseJSON = (source, varName, json) => {
if (!json || typeof json === 'object') {
return json;
} else {
try {
json = json.replace(jsonClosingChars, '');
return JSON.parse(json);
} catch (err) {
throw Error(`Error parsing ${varName} in ${source}: ${err.message}`);
}
}
};
const findJSON = (source, varName, body, left, right, prependJSON) => {
let jsonStr = utils.between(body, left, right);
if (!jsonStr) {
throw Error(`Could not find ${varName} in ${source}`);
}
return parseJSON(source, varName, utils.cutAfterJSON(`${prependJSON}${jsonStr}`));
};
const findPlayerResponse = (source, info) => {
const player_response = info && (
(info.args && info.args.player_response) ||
info.player_response || info.playerResponse || info.embedded_player_response);
return parseJSON(source, 'player_response', player_response);
};
const getWatchJSONURL = (id, options) => `${getWatchHTMLURL(id, options)}&pbj=1`;
const getWatchJSONPage = async(id, options) => {
const reqOptions = Object.assign({ headers: {} }, options.requestOptions);
let cookie = reqOptions.headers.Cookie || reqOptions.headers.cookie;
reqOptions.headers = Object.assign({
'x-youtube-client-name': '1',
'x-youtube-client-version': '2.20201203.06.00',
'x-youtube-identity-token': exports.cookieCache.get(cookie || 'browser') || '',
}, reqOptions.headers);
const setIdentityToken = async(key, throwIfNotFound) => {
if (reqOptions.headers['x-youtube-identity-token']) { return; }
reqOptions.headers['x-youtube-identity-token'] = await getIdentityToken(id, options, key, throwIfNotFound);
};
if (cookie) {
await setIdentityToken(cookie, true);
}
const jsonUrl = getWatchJSONURL(id, options);
const body = await utils.exposedMiniget(jsonUrl, options, reqOptions).text();
let parsedBody = parseJSON('watch.json', 'body', body);
if (parsedBody.reload === 'now') {
await setIdentityToken('browser', false);
}
if (parsedBody.reload === 'now' || !Array.isArray(parsedBody)) {
throw Error('Unable to retrieve video metadata in watch.json');
}
let info = parsedBody.reduce((part, curr) => Object.assign(curr, part), {});
info.player_response = findPlayerResponse('watch.json', info);
info.html5player = info.player && info.player.assets && info.player.assets.js;
return info;
};
const getWatchHTMLPage = async(id, options) => {
let body = await getWatchHTMLPageBody(id, options);
let info = { page: 'watch' };
try {
info.player_response = findJSON('watch.html', 'player_response',
body, /\bytInitialPlayerResponse\s*=\s*\{/i, '\n', '{');
} catch (err) {
let args = findJSON('watch.html', 'player_response', body, /\bytplayer\.config\s*=\s*{/, '</script>', '{');
info.player_response = findPlayerResponse('watch.html', args);
}
info.response = findJSON('watch.html', 'response', body, /\bytInitialData("\])?\s*=\s*\{/i, '\n', '{');
info.html5player = getHTML5player(body);
return info;
};
const INFO_HOST = 'www.youtube.com';
const INFO_PATH = '/get_video_info';
const VIDEO_EURL = 'https://youtube.googleapis.com/v/';
const getVideoInfoPage = async(id, options) => {
const url = new URL(`https://${INFO_HOST}${INFO_PATH}`);
url.searchParams.set('video_id', id);
url.searchParams.set('eurl', VIDEO_EURL + id);
url.searchParams.set('ps', 'default');
url.searchParams.set('gl', 'US');
url.searchParams.set('hl', options.lang || 'en');
const body = await utils.exposedMiniget(url.toString(), options).text();
let info = querystring.parse(body);
info.player_response = findPlayerResponse('get_video_info', info);
return info;
};
/**
* @param {Object} player_response
* @returns {Array.<Object>}
*/
const parseFormats = player_response => {
let formats = [];
if (player_response && player_response.streamingData) {
formats = formats
.concat(player_response.streamingData.formats || [])
.concat(player_response.streamingData.adaptiveFormats || []);
}
return formats;
};
/**
* Gets info from a video additional formats and deciphered URLs.
*
* @param {string} id
* @param {Object} options
* @returns {Promise<Object>}
*/
exports.getInfo = async(id, options) => {
let info = await exports.getBasicInfo(id, options);
const hasManifest =
info.player_response && info.player_response.streamingData && (
info.player_response.streamingData.dashManifestUrl ||
info.player_response.streamingData.hlsManifestUrl
);
let funcs = [];
if (info.formats.length) {
info.html5player = info.html5player ||
getHTML5player(await getWatchHTMLPageBody(id, options)) || getHTML5player(await getEmbedPageBody(id, options));
if (!info.html5player) {
throw Error('Unable to find html5player file');
}
const html5player = new URL(info.html5player, BASE_URL).toString();
funcs.push(sig.decipherFormats(info.formats, html5player, options));
}
if (hasManifest && info.player_response.streamingData.dashManifestUrl) {
let url = info.player_response.streamingData.dashManifestUrl;
funcs.push(getDashManifest(url, options));
}
if (hasManifest && info.player_response.streamingData.hlsManifestUrl) {
let url = info.player_response.streamingData.hlsManifestUrl;
funcs.push(getM3U8(url, options));
}
let results = await Promise.all(funcs);
info.formats = Object.values(Object.assign({}, ...results));
info.formats = info.formats.map(formatUtils.addFormatMeta);
info.formats.sort(formatUtils.sortFormats);
info.full = true;
return info;
};
/**
* Gets additional DASH formats.
*
* @param {string} url
* @param {Object} options
* @returns {Promise<Array.<Object>>}
*/
const getDashManifest = (url, options) => new Promise((resolve, reject) => {
let formats = {};
const parser = sax.parser(false);
parser.onerror = reject;
let adaptationSet;
parser.onopentag = node => {
if (node.name === 'ADAPTATIONSET') {
adaptationSet = node.attributes;
} else if (node.name === 'REPRESENTATION') {
const itag = parseInt(node.attributes.ID);
if (!isNaN(itag)) {
formats[url] = Object.assign({
itag, url,
bitrate: parseInt(node.attributes.BANDWIDTH),
mimeType: `${adaptationSet.MIMETYPE}; codecs="${node.attributes.CODECS}"`,
}, node.attributes.HEIGHT ? {
width: parseInt(node.attributes.WIDTH),
height: parseInt(node.attributes.HEIGHT),
fps: parseInt(node.attributes.FRAMERATE),
} : {
audioSampleRate: node.attributes.AUDIOSAMPLINGRATE,
});
}
}
};
parser.onend = () => { resolve(formats); };
const req = utils.exposedMiniget(new URL(url, BASE_URL).toString(), options);
req.setEncoding('utf8');
req.on('error', reject);
req.on('data', chunk => { parser.write(chunk); });
req.on('end', parser.close.bind(parser));
});
/**
* Gets additional formats.
*
* @param {string} url
* @param {Object} options
* @returns {Promise<Array.<Object>>}
*/
const getM3U8 = async(url, options) => {
url = new URL(url, BASE_URL);
const body = await utils.exposedMiniget(url.toString(), options).text();
let formats = {};
body
.split('\n')
.filter(line => /^https?:\/\//.test(line))
.forEach(line => {
const itag = parseInt(line.match(/\/itag\/(\d+)\//)[1]);
formats[line] = { itag, url: line };
});
return formats;
};
// Cache get info functions.
// In case a user wants to get a video's info before downloading.
for (let funcName of ['getBasicInfo', 'getInfo']) {
/**
* @param {string} link
* @param {Object} options
* @returns {Promise<Object>}
*/
const func = exports[funcName];
exports[funcName] = async(link, options = {}) => {
utils.checkForUpdates();
let id = await urlUtils.getVideoID(link);
const key = [funcName, id, options.lang].join('-');
return exports.cache.getOrSet(key, () => func(id, options));
};
}
// Export a few helpers.
exports.validateID = urlUtils.validateID;
exports.validateURL = urlUtils.validateURL;
exports.getURLVideoID = urlUtils.getURLVideoID;
exports.getVideoID = urlUtils.getVideoID;