Skip to content

Commit bf06869

Browse files
authored
feat: add richThumbnails (animated thumbnails) to related videos (#850)
* Add rich thumbnail (animated thumbnail) to related videos * Optimizations, corrected tests, added richThumbnail to refresh.js * Properly check for richThumbnails
1 parent 7b2efa3 commit bf06869

File tree

8 files changed

+14345
-2
lines changed

8 files changed

+14345
-2
lines changed

lib/info-extras.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ exports.getRelatedVideos = info => {
193193
Math.floor(parseTimestamp(getText(details.lengthText)) / 1000) :
194194
rvsParams && `${rvsParams.length_seconds}`,
195195
thumbnails: details.thumbnail.thumbnails,
196+
richThumbnails:
197+
details.richThumbnail ?
198+
details.richThumbnail.movingThumbnailRenderer.movingThumbnailDetails.thumbnails : [],
196199
isLive: !!(details.badges && details.badges.find(b => b.metadataBadgeRenderer.label === 'LIVE NOW')),
197200
};
198201

lib/info.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ const AGE_RESTRICTED_URLS = [
4343
*/
4444
exports.getBasicInfo = async(id, options) => {
4545
const retryOptions = Object.assign({}, miniget.defaultOptions, options.requestOptions);
46+
options.requestOptions = Object.assign({}, options.requestOptions, {});
47+
options.requestOptions.headers = Object.assign({},
48+
{
49+
// eslint-disable-next-line max-len
50+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.101 Safari/537.36',
51+
}, options.requestOptions.headers);
4652
const validate = info => {
4753
let playErr = utils.playError(info.player_response, ['ERROR'], UnrecoverableError);
4854
let privateErr = privateVideoError(info.player_response);

test/basic-info-test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ const assert = require('assert-diff');
33
const nock = require('./nock');
44
const miniget = require('miniget');
55

6+
const DEFAULT_USER_AGENT =
7+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.101 Safari/537.36';
68

79
describe('ytdl.getBasicInfo()', () => {
810
let minigetDefaults = miniget.defaultOptions;
@@ -83,6 +85,40 @@ describe('ytdl.getBasicInfo()', () => {
8385
assert.strictEqual(info2, info1);
8486
});
8587
});
88+
89+
describe('With user-agent header', () => {
90+
it('Uses default user-agent if no user-agent is provided', async() => {
91+
const id = '_HSylqgVYQI';
92+
const scope = nock(id, 'rich-thumbnails', {
93+
headers: {
94+
'User-Agent': DEFAULT_USER_AGENT,
95+
},
96+
watchHtml: false,
97+
player: false,
98+
});
99+
await ytdl.getBasicInfo(id);
100+
scope.done();
101+
});
102+
103+
it('Uses provided user-agent instead of default', async() => {
104+
const id = '_HSylqgVYQI';
105+
const scope = nock(id, 'regular', {
106+
headers: {
107+
'User-Agent': 'yay',
108+
},
109+
watchHtml: false,
110+
player: false,
111+
});
112+
await ytdl.getBasicInfo(id, {
113+
requestOptions: {
114+
headers: {
115+
'User-Agent': 'yay',
116+
},
117+
},
118+
});
119+
scope.done();
120+
});
121+
});
86122
});
87123

88124
describe('From a live video', () => {

test/files/refresh.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ const videos = [
8989
type: 'no-likes-or-dislikes',
9090
skip: ['get_video_info', /player/],
9191
},
92+
{
93+
id: '_HSylqgVYQI',
94+
type: 'rich-thumbnails',
95+
basicInfo: true,
96+
saveInfo: true,
97+
skip: ['watch.html', /player/],
98+
},
9299
{
93100
id: 'z2jeHsa0UG0',
94101
type: 'private',

0 commit comments

Comments
 (0)