-
Notifications
You must be signed in to change notification settings - Fork 722
/
info.js
344 lines (308 loc) · 10.6 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
const urllib = require('url');
const querystring = require('querystring');
const sax = require('sax');
const miniget = require('miniget');
const util = require('./util');
const extras = require('./info-extras');
const sig = require('./sig');
const Cache = require('./cache');
const VIDEO_URL = 'https://www.youtube.com/watch?v=';
const EMBED_URL = 'https://www.youtube.com/embed/';
const VIDEO_EURL = 'https://youtube.googleapis.com/v/';
const INFO_HOST = 'www.youtube.com';
const INFO_PATH = '/get_video_info';
// Cached for storing basic/full info.
exports.cache = new Cache();
exports.cookieCache = new Cache(1000 * 60 * 60 * 24);
/**
* Gets info from a video without getting additional formats.
*
* @param {string} id
* @param {Object} options
* @returns {Promise<Object>}
*/
exports.getBasicInfo = async(id, options) => {
// Try getting config from the video page first.
const params = `hl=${options.lang || 'en'}`;
const watchPageURL = `${VIDEO_URL + id}&${params}&bpctr=${Math.ceil(Date.now() / 1000)}`;
const jsonEndpointURL = `${watchPageURL}&pbj=1`;
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.20200701.03.01',
'x-youtube-identity-token': exports.cookieCache.get(cookie || 'browser') || '',
}, reqOptions.headers);
const setIdentityToken = async(key, throwIfNotFound) => {
if (reqOptions.headers['x-youtube-identity-token']) { return; }
let token = await exports.cookieCache.getOrSet(key, async() => {
let watchPageBody = await miniget(watchPageURL, reqOptions).text();
let match = watchPageBody.match(/(["'])ID_TOKEN\1[:,]\s?"([^"]+)"/);
if (!match && throwIfNotFound) {
throw Error('Cookie header used in request, but nunable to find YouTube identity token');
}
return match && match[2];
});
reqOptions.headers['x-youtube-identity-token'] = token;
};
let getWatchPage = async(maxRetries = 1) => {
let body = await miniget(jsonEndpointURL, reqOptions).text();
let info;
try {
let jsonClosingChars = /^[)\]}'\s]+/;
if (jsonClosingChars.test(body)) {
body = body.replace(jsonClosingChars, '');
}
let parsedBody = JSON.parse(body);
if (parsedBody.reload === 'now' && maxRetries > 0) {
await setIdentityToken('browser', false);
return getWatchPage(maxRetries - 1);
}
if (!Array.isArray(parsedBody)) {
throw Error('Unable to retrieve video metadata');
}
info = parsedBody.reduce((part, curr) => Object.assign(curr, part), {});
} catch (err) {
throw Error(`Error parsing info: ${err.message}`);
}
return [info, body];
};
if (cookie) {
await setIdentityToken(cookie, true);
}
let [info, body] = await getWatchPage();
let playErr = util.playError(info, ['ERROR']);
if (playErr) {
throw playErr;
}
if (!info.player) {
// If the video page doesn't work, maybe because it has mature content.
// and requires an account logged in to view, try the embed page.
let embedUrl = `${EMBED_URL + id}?${params}`;
body = await miniget(embedUrl, options.requestOptions).text();
let jsonStr = util.between(body, /(['"])PLAYER_(CONFIG|VARS)\1:\s?/, '</script>');
let config;
if (!jsonStr) {
throw Error('Could not find player config');
}
try {
config = JSON.parse(util.cutAfterJSON(jsonStr));
} catch (err) {
throw Error(`Error parsing config: ${err.message}`);
}
playErr = util.playError(info, ['LOGIN_REQUIRED']);
if ((!config.args || (!config.args.player_response && !config.args.embedded_player_response)) &&
!config.embedded_player_response && playErr) {
throw playErr;
}
let html5playerRes = /<script\s+src="([^"]+)"\s+name="player_ias\/base"\s*>/.exec(body);
info.html5player = html5playerRes ? html5playerRes[1] : null;
info.player = config;
}
return gotConfig(id, options, info, body);
};
/**
* @param {Object} info
* @returns {Array.<Object>}
*/
const parseFormats = info => {
let formats = [];
if (info.player_response.streamingData) {
if (info.player_response.streamingData.formats) {
formats = formats.concat(info.player_response.streamingData.formats);
}
if (info.player_response.streamingData.adaptiveFormats) {
formats = formats.concat(info.player_response.streamingData.adaptiveFormats);
}
}
return formats;
};
/**
* @param {Object} id
* @param {Object} options
* @param {Object} info
* @param {string} body
* @returns {Promise<Object>}
*/
const gotConfig = async(id, options, info, body) => {
let player_response =
info.player && info.player.args && info.player.args.player_response;
if (!player_response) {
const url = urllib.format({
protocol: 'https',
host: INFO_HOST,
pathname: INFO_PATH,
query: {
video_id: id,
eurl: VIDEO_EURL + id,
ps: 'default',
gl: 'US',
hl: options.lang || 'en',
sts: info.sts,
},
});
let morebody = await miniget(url, options.requestOptions).text();
let moreinfo = querystring.parse(morebody);
player_response = moreinfo.player_response || info.playerResponse;
}
if (typeof player_response === 'object') {
info.player_response = player_response;
} else {
try {
info.player_response = JSON.parse(player_response);
} catch (err) {
throw Error(`Error parsing \`player_response\`: ${err.message}`);
}
}
info.formats = parseFormats(info);
// Add additional properties to info.
let additional = {
author: extras.getAuthor(info),
media: extras.getMedia(info),
likes: extras.getLikes(body),
dislikes: extras.getDislikes(body),
age_restricted: !!(info.player.args && info.player.args.is_embed),
// Give the standard link to the video.
video_url: VIDEO_URL + id,
};
info.videoDetails = Object.assign({},
info.player_response.microformat.playerMicroformatRenderer,
info.player_response.videoDetails, additional);
info.related_videos = extras.getRelatedVideos(info);
info.html5player = info.html5player || (info.player && info.player.assets && info.player.assets.js);
return info;
};
/**
* 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) {
const html5player = urllib.resolve(VIDEO_URL, info.html5player);
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(util.addFormatMeta);
info.formats.sort(util.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[itag] = {
itag, url,
bitrate: parseInt(node.attributes.BANDWIDTH),
mimeType: `${adaptationSet.MIMETYPE}; codecs="${node.attributes.CODECS}"`,
};
if (node.attributes.HEIGHT) {
Object.assign(formats[itag], {
width: parseInt(node.attributes.WIDTH),
height: parseInt(node.attributes.HEIGHT),
fps: parseInt(node.attributes.FRAMERATE),
});
} else {
Object.assign(formats[itag], {
audioSampleRate: node.attributes.AUDIOSAMPLINGRATE,
});
}
}
}
};
parser.onend = () => { resolve(formats); };
const req = miniget(urllib.resolve(VIDEO_URL, url), options.requestOptions);
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 = urllib.resolve(VIDEO_URL, url);
let body = await miniget(url, options.requestOptions).text();
let formats = {};
body
.split('\n')
.filter(line => /^https?:\/\//.test(line))
.forEach(line => {
const itag = parseInt(line.match(/\/itag\/(\d+)\//)[1]);
formats[itag] = { itag, url: line };
});
return formats;
};
// Cache get info functions.
// In case a user wants to get a video's info before downloading.
for (let fnName of ['getBasicInfo', 'getInfo']) {
/**
* @param {string} link
* @param {Object} options
* @param {Function(Error, Object)} callback
* @returns {Promise<Object>}
*/
const fn = exports[fnName];
exports[fnName] = (link, options, callback) => {
if (typeof options === 'function') {
callback = options;
options = {};
} else if (!options) {
options = {};
}
if (callback) {
// TODO: Fully remove callback support in a future release.
// eslint-disable-next-line no-console
console.warn(
`Calling \`ytdl.${fnName}\` with a callback will be removed in a near future release. ` +
`Use async/await.`);
return exports[fnName](link, options)
.then(info => callback(null, info), callback);
}
let id = util.getVideoID(link);
const key = [fnName, id, options.lang].join('-');
return exports.cache.getOrSet(key, () => fn(id, options));
};
}
// Export a few helpers.
exports.validateID = util.validateID;
exports.validateURL = util.validateURL;
exports.getURLVideoID = util.getURLVideoID;
exports.getVideoID = util.getVideoID;