From e88537f6b68d251c69533b9cd99b7079c25eb3b5 Mon Sep 17 00:00:00 2001 From: fent <933490+fent@users.noreply.github.com> Date: Wed, 21 Oct 2020 19:58:56 -0700 Subject: [PATCH] fix: use `get_video_info` endpoint if watch and embed pages fail fixes #735 --- lib/info.js | 9 ++++++--- .../videos/age-restricted/embed-player-vars.html | 16 ++++++++++++++++ test/info-test.js | 14 ++++++++++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 test/files/videos/age-restricted/embed-player-vars.html diff --git a/lib/info.js b/lib/info.js index dd63faff..429d77a3 100644 --- a/lib/info.js +++ b/lib/info.js @@ -88,7 +88,7 @@ exports.getBasicInfo = async(id, options) => { // 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['"]:\s?/, ''); + let jsonStr = util.between(body, /(['"])PLAYER_(CONFIG|VARS)\1:\s?/, ''); let config; if (!jsonStr) { throw Error('Could not find player config'); @@ -99,9 +99,12 @@ exports.getBasicInfo = async(id, options) => { throw Error(`Error parsing config: ${err.message}`); } playErr = util.playError(info, 'LOGIN_REQUIRED'); - if (!config.args.player_response && !config.args.embedded_player_response && playErr) { + if ((!config.args || (!config.args.player_response && !config.args.embedded_player_response)) && + !config.embedded_player_response && playErr) { throw playErr; } + let html5playerRes = //.exec(body); + info.html5player = html5playerRes ? html5playerRes[1] : null; info.player = config; } return gotConfig(id, options, info, body); @@ -184,7 +187,7 @@ const gotConfig = async(id, options, info, body) => { info.player_response.microformat.playerMicroformatRenderer, info.player_response.videoDetails, additional); info.related_videos = extras.getRelatedVideos(info); - info.html5player = info.player && info.player.assets && info.player.assets.js; + info.html5player = info.html5player || (info.player && info.player.assets && info.player.assets.js); // TODO: Remove these warnings later and remove the properties. // Remember to remove from typings too. diff --git a/test/files/videos/age-restricted/embed-player-vars.html b/test/files/videos/age-restricted/embed-player-vars.html new file mode 100644 index 00000000..b50515ba --- /dev/null +++ b/test/files/videos/age-restricted/embed-player-vars.html @@ -0,0 +1,16 @@ + + + + + + NASA Live: Official Stream of NASA TV - YouTube + + + + + + +
+ \ No newline at end of file diff --git a/test/info-test.js b/test/info-test.js index 463e9f9f..98904683 100644 --- a/test/info-test.js +++ b/test/info-test.js @@ -180,6 +180,20 @@ describe('ytdl.getInfo()', () => { await assert.rejects(ytdl.getInfo(id), /Could not find player config/); scope.done(); }); + + describe('When embed page returns limited `player_response`', () => { + it('Uses get_vide_info as backup', async() => { + const scope = nock(id, { + type: 'age-restricted', + embed: [true, 200, 'player-vars'], + get_video_info: true, + player: true, + }); + let info = await ytdl.getInfo(id); + scope.done(); + assert.strictEqual(info.formats.length, expectedInfo.formats.length); + }); + }); }); describe('From a video that was live streamed but not currently live', () => {