Skip to content

Commit 977e973

Browse files
D3SOXTimeForANinja
andauthored
fix: Add parameters to get_video_info endpoint to fix 404 errors (#938)
* 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 <[email protected]>
1 parent bcd8719 commit 977e973

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

lib/info-extras.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,9 @@ exports.cleanVideoDetails = (videoDetails, info) => {
282282

283283
// Use more reliable `lengthSeconds` from `playerMicroformatRenderer`.
284284
videoDetails.lengthSeconds =
285-
info.player_response.microformat &&
286-
info.player_response.microformat.playerMicroformatRenderer.lengthSeconds;
285+
(info.player_response.microformat &&
286+
info.player_response.microformat.playerMicroformatRenderer.lengthSeconds) ||
287+
info.player_response.videoDetails.lengthSeconds;
287288
return videoDetails;
288289
};
289290

lib/info.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ const BASE_URL = 'https://www.youtube.com/watch?v=';
1818
exports.cache = new Cache();
1919
exports.cookieCache = new Cache(1000 * 60 * 60 * 24);
2020
exports.watchPageCache = new Cache();
21+
// Cache for cver used in getVideoInfoPage
22+
let cver = '2.20210622.10.00';
2123

2224

2325
// Special error class used to determine if an error is unrecoverable,
@@ -273,7 +275,7 @@ const getWatchJSONPage = async(id, options) => {
273275
let cookie = reqOptions.headers.Cookie || reqOptions.headers.cookie;
274276
reqOptions.headers = Object.assign({
275277
'x-youtube-client-name': '1',
276-
'x-youtube-client-version': '2.20201203.06.00',
278+
'x-youtube-client-version': cver,
277279
'x-youtube-identity-token': exports.cookieCache.get(cookie || 'browser') || '',
278280
}, reqOptions.headers);
279281

@@ -307,6 +309,7 @@ const getWatchHTMLPage = async(id, options) => {
307309
let body = await getWatchHTMLPageBody(id, options);
308310
let info = { page: 'watch' };
309311
try {
312+
cver = utils.between(body, '{"key":"cver","value":"', '"}');
310313
info.player_response = findJSON('watch.html', 'player_response',
311314
body, /\bytInitialPlayerResponse\s*=\s*\{/i, '</script>', '{');
312315
} catch (err) {
@@ -325,6 +328,8 @@ const VIDEO_EURL = 'https://youtube.googleapis.com/v/';
325328
const getVideoInfoPage = async(id, options) => {
326329
const url = new URL(`https://${INFO_HOST}${INFO_PATH}`);
327330
url.searchParams.set('video_id', id);
331+
url.searchParams.set('c', 'TVHTML5');
332+
url.searchParams.set('cver', `7${cver.substr(1)}`);
328333
url.searchParams.set('eurl', VIDEO_EURL + id);
329334
url.searchParams.set('ps', 'default');
330335
url.searchParams.set('gl', 'US');

0 commit comments

Comments
 (0)