forked from fent/node-ytdl-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
info.js
328 lines (286 loc) · 9.36 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
const urllib = require('url');
const querystring = require('querystring');
const sax = require('sax');
const request = require('miniget');
const util = require('./util');
const sig = require('./sig');
const FORMATS = require('./formats');
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 THUMBNAIL_URL = 'https://i.ytimg.com/vi/';
const INFO_HOST = 'www.youtube.com';
const INFO_PATH = '/get_video_info';
const KEYS_TO_SPLIT = [
'keywords',
'fmt_list',
'fexp',
'watermark'
];
/**
* Gets info from a video.
*
* @param {String} link
* @param {Object} options
* @param {Function(Error, Object)} callback
*/
module.exports = function getInfo(link, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
} else if (!options) {
options = {};
}
if (!callback) {
return new Promise(function(resolve, reject) {
getInfo(link, options, function(err, info) {
if (err) return reject(err);
resolve(info);
});
});
}
var id = util.getVideoID(link);
if (id instanceof Error) return callback(id);
// Try getting config from the video page first.
var url = VIDEO_URL + id + '&hl=' + (options.lang || 'en');
request(url, options.requestOptions, function(err, res, body) {
if (err) return callback(err);
// Check if there are any errors with this video page.
var unavailableMsg = util.between(body, '<div id="player-unavailable"', '>');
if (unavailableMsg && !/\bhid\b/.test(util.between(unavailableMsg, 'class="', '"'))) {
// Ignore error about age restriction.
if (body.indexOf('<div id="watch7-player-age-gate-content"') < 0) {
return callback(new Error(util.between(body,
'<h1 id="unavailable-message" class="message">', '</h1>').trim()));
}
}
// Parse out some additional informations since we already load that page.
var additional = {
// Get informations about the author/uploader.
author: util.getAuthor(body),
// Get the day the vid was published.
published: util.getPublished(body),
// Get description from #eow-description.
description: util.getVideoDescription(body),
// Get related videos.
related_videos: util.getRelatedVideos(body),
// Give the canonical link to the video.
video_url: url,
// Thumbnails.
iurlsd : THUMBNAIL_URL + id + '/sddefault.jpg',
iurlmq : THUMBNAIL_URL + id + '/mqdefault.jpg',
iurlhq : THUMBNAIL_URL + id + '/hqdefault.jpg',
iurlmaxres : THUMBNAIL_URL + id + '/maxresdefault.jpg',
};
var jsonStr = util.between(body, 'ytplayer.config = ', '</script>');
var config;
if (jsonStr) {
config = jsonStr.slice(0, jsonStr.lastIndexOf(';ytplayer.load'));
gotConfig(id, options, additional, config, false, callback);
} else {
// If the video page doesn't work, maybe because it has mature content.
// and requires an account logged into view, try the embed page.
url = EMBED_URL + id + '?hl=' + (options.lang || 'en');
request(url, options.requestOptions, function(err, res, body) {
if (err) return callback(err);
config = util.between(body, 't.setConfig({\'PLAYER_CONFIG\': ', '},\'');
gotConfig(id, options, additional, config, true, callback);
});
}
});
};
/**
* @param {Object} id
* @param {Object} options
* @param {Object} additional
* @param {Object} config
* @param {Boolean} appendConfig
* @param {Function(Error, Object)} callback
*/
function gotConfig(id, options, additional, config, appendConfig, callback) {
if (!config) {
return callback(new Error('Could not find player config'));
}
try {
config = JSON.parse(config + (appendConfig ? '}' : ''));
} catch (err) {
return callback(new Error('Error parsing config: ' + err.message));
}
var 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: config.sts,
},
});
request(url, options.requestOptions, function(err, res, body) {
if (err) return callback(err);
var info = querystring.parse(body);
if (info.status === 'fail') {
info = config.args;
} else if (info.requires_purchase === '1') {
return callback(new Error(info.ypc_video_rental_bar_text));
}
// Split some keys by commas.
KEYS_TO_SPLIT.forEach(function(key) {
if (!info[key]) return;
info[key] = info[key]
.split(',')
.filter(function(v) { return v !== ''; });
});
if (info.player_response) {
info.player_response = JSON.parse(info.player_response);
}
info.fmt_list = info.fmt_list ?
info.fmt_list.map(function(format) {
return format.split('/');
}) : [];
info.formats = util.parseFormats(info);
// Add additional properties to info.
info = util.objectAssign(info, additional, false);
if (info.formats.some(function(f) { return !!f.s; }) ||
config.args.dashmpd || info.dashmpd || info.hlsvp) {
var html5playerfile = urllib.resolve(VIDEO_URL, config.assets.js);
sig.getTokens(html5playerfile, options, function(err, tokens) {
if (err) return callback(err);
sig.decipherFormats(info.formats, tokens, options.debug);
var funcs = [];
var dashmpd;
if (config.args.dashmpd) {
dashmpd = decipherURL(config.args.dashmpd, tokens);
funcs.push(getDashManifest.bind(null, dashmpd, options));
}
if (info.dashmpd && info.dashmpd !== config.args.dashmpd) {
dashmpd = decipherURL(info.dashmpd, tokens);
funcs.push(getDashManifest.bind(null, dashmpd, options));
}
if (info.hlsvp) {
info.hlsvp = decipherURL(info.hlsvp, tokens);
funcs.push(getM3U8.bind(null, info.hlsvp, options));
}
util.parallel(funcs, function(err, results) {
if (err) return callback(err);
if (results[0]) { mergeFormats(info, results[0]); }
if (results[1]) { mergeFormats(info, results[1]); }
if (results[2]) { mergeFormats(info, results[2]); }
if (!info.formats.length) {
callback(new Error('No formats found'));
return;
}
gotFormats();
});
});
} else {
if (!info.formats.length) {
callback(new Error('This video is unavailable'));
return;
}
sig.decipherFormats(info.formats, null, options.debug);
gotFormats();
}
function gotFormats() {
if (options.debug) {
info.formats.forEach(function(format) {
var itag = format.itag;
if (!FORMATS[itag]) {
console.warn('No format metadata for itag ' + itag + ' found');
}
});
}
info.formats.forEach(util.addFormatMeta);
info.formats.sort(util.sortFormats);
callback(null, info);
}
});
}
/**
* @param {String} url
* @param {Array.<String>} tokens
*/
function decipherURL(url, tokens) {
return url.replace(/\/s\/([a-fA-F0-9\.]+)/, function(_, s) {
return '/signature/' + sig.decipher(tokens, s);
});
}
/**
* Merges formats from DASH or M3U8 with formats from video info page.
*
* @param {Object} info
* @param {Object} formatsMap
*/
function mergeFormats(info, formatsMap) {
info.formats.forEach(function(f) {
var cf = formatsMap[f.itag];
if (cf) {
for (var key in f) { cf[key] = f[key]; }
} else {
formatsMap[f.itag] = f;
}
});
info.formats = [];
for (var itag in formatsMap) { info.formats.push(formatsMap[itag]); }
}
/**
* Gets additional DASH formats.
*
* @param {String} url
* @param {Object} options
* @param {Function(!Error, Array.<Object>)} callback
*/
function getDashManifest(url, options, callback) {
var formats = {};
var currentFormat = null;
var expectUrl = false;
var parser = sax.parser(false);
parser.onerror = callback;
parser.onopentag = function(node) {
if (node.name === 'REPRESENTATION') {
var itag = node.attributes.ID;
currentFormat = { itag: itag };
formats[itag] = currentFormat;
}
expectUrl = node.name === 'BASEURL';
};
parser.ontext = function(text) {
if (expectUrl) {
currentFormat.url = text;
}
};
parser.onend = function() { callback(null, formats); };
var req = request(urllib.resolve(VIDEO_URL, url), options.requestOptions);
req.on('error', callback);
req.setEncoding('utf8');
req.on('error', callback);
req.on('data', function(chunk) { parser.write(chunk); });
req.on('end', parser.close.bind(parser));
}
/**
* Gets additional formats.
*
* @param {String} url
* @param {Object} options
* @param {Function(!Error, Array.<Object>)} callback
*/
function getM3U8(url, options, callback) {
url = urllib.resolve(VIDEO_URL, url);
request(url, options.requestOptions, function(err, res, body) {
if (err) return callback(err);
var formats = {};
body
.split('\n')
.filter(function(line) {
return /https?:\/\//.test(line);
})
.forEach(function(line) {
var itag = line.match(/\/itag\/(\d+)\//)[1];
formats[itag] = { itag: itag, url: line };
});
callback(null, formats);
});
}