const urllib = require('url');
const querystring = require('querystring');
const sax = require('sax');
const request = require('miniget');
const util = require('./util');
const extras = require('./info-extras');
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 INFO_HOST = 'www.youtube.com';
const INFO_PATH = '/get_video_info';
const KEYS_TO_SPLIT = [
'fmt_list',
'fexp',
'watermark'
];
/**
* Gets info from a video without getting additional formats.
*
* @param {string} id
* @param {Object} options
* @param {Function(Error, Object)} callback
*/
exports.getBasicInfo = (id, options, callback) => {
// Try getting config from the video page first.
const params = 'hl=' + (options.lang || 'en');
let url = VIDEO_URL + id + '&' + params +
'&bpctr=' + Math.ceil(Date.now() / 1000);
// Remove header from watch page request.
// Otherwise, it'll use a different framework for rendering content.
const reqOptions = Object.assign({}, options.requestOptions);
reqOptions.headers = Object.assign({}, reqOptions.headers, {
'User-Agent': ''
});
request(url, reqOptions, (err, res, body) => {
if (err) return callback(err);
// Check if there are any errors with this video page.
const unavailableMsg = util.between(body, '
');
if (unavailableMsg &&
!/\bhid\b/.test(util.between(unavailableMsg, 'class="', '"'))) {
// Ignore error about age restriction.
if (!body.includes('
', '').trim()));
}
}
// Parse out additional metadata from this page.
const additional = {
// Get the author/uploader.
author: extras.getAuthor(body),
// Get the day the vid was published.
published: extras.getPublished(body),
// Get description.
description: extras.getVideoDescription(body),
// Get media info.
media: extras.getVideoMedia(body),
// Get related videos.
related_videos: extras.getRelatedVideos(body),
};
const jsonStr = util.between(body, 'ytplayer.config = ', '');
let 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 in to view, try the embed page.
url = EMBED_URL + id + '?' + params;
request(url, options.requestOptions, (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} info
* @return {Array.