Skip to content

Commit

Permalink
fix: Add parameters to get_video_info endpoint to fix 404 errors (#938
Browse files Browse the repository at this point in the history
)

* fix: add parameters to `get_video_info` endpoint

fixes 404 errors

* fix: add fallback for lengthSeconds

* fix: correct lengthSeconds path

* style: fix linter errors

* Update info.js

* refactor: use cver for `x-youtube-client-version` header

cherry-picked from distubejs@e13c2a9

Co-authored-by: TimeForANinja <TimeForANinja@users.noreply.github.com>
  • Loading branch information
D3SOX and TimeForANinja committed Jun 26, 2021
1 parent bcd8719 commit 977e973
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/info-extras.js
Expand Up @@ -282,8 +282,9 @@ exports.cleanVideoDetails = (videoDetails, info) => {

// Use more reliable `lengthSeconds` from `playerMicroformatRenderer`.
videoDetails.lengthSeconds =
info.player_response.microformat &&
info.player_response.microformat.playerMicroformatRenderer.lengthSeconds;
(info.player_response.microformat &&
info.player_response.microformat.playerMicroformatRenderer.lengthSeconds) ||
info.player_response.videoDetails.lengthSeconds;
return videoDetails;
};

Expand Down
7 changes: 6 additions & 1 deletion lib/info.js
Expand Up @@ -18,6 +18,8 @@ const BASE_URL = 'https://www.youtube.com/watch?v=';
exports.cache = new Cache();
exports.cookieCache = new Cache(1000 * 60 * 60 * 24);
exports.watchPageCache = new Cache();
// Cache for cver used in getVideoInfoPage
let cver = '2.20210622.10.00';


// Special error class used to determine if an error is unrecoverable,
Expand Down Expand Up @@ -273,7 +275,7 @@ const getWatchJSONPage = async(id, options) => {
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-client-version': cver,
'x-youtube-identity-token': exports.cookieCache.get(cookie || 'browser') || '',
}, reqOptions.headers);

Expand Down Expand Up @@ -307,6 +309,7 @@ const getWatchHTMLPage = async(id, options) => {
let body = await getWatchHTMLPageBody(id, options);
let info = { page: 'watch' };
try {
cver = utils.between(body, '{"key":"cver","value":"', '"}');
info.player_response = findJSON('watch.html', 'player_response',
body, /\bytInitialPlayerResponse\s*=\s*\{/i, '</script>', '{');
} catch (err) {
Expand All @@ -325,6 +328,8 @@ 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('c', 'TVHTML5');
url.searchParams.set('cver', `7${cver.substr(1)}`);
url.searchParams.set('eurl', VIDEO_EURL + id);
url.searchParams.set('ps', 'default');
url.searchParams.set('gl', 'US');
Expand Down

0 comments on commit 977e973

Please sign in to comment.