Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add storyboards to info.videoDetails #851

Merged
merged 5 commits into from
Jan 3, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions lib/info-extras.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,3 +268,54 @@ exports.cleanVideoDetails = videoDetails => {
'videoDetails.shortDescription', 'videoDetails.description');
return videoDetails;
};

/**
* Get storyboards info.
*
* @param {Object} info
* @returns {Array.<Object>}
*/
exports.getStoryboards = info => {
const parts = info.player_response.storyboards &&
info.player_response.storyboards.playerStoryboardSpecRenderer &&
info.player_response.storyboards.playerStoryboardSpecRenderer.spec &&
info.player_response.storyboards.playerStoryboardSpecRenderer.spec.split('|');

if (!parts) return [];

const url = new URL(parts.shift());

return parts.map((part, i) => {
let [
width,
height,
count,
storyboardWidth,
storyboardHeight,
Aasim-A marked this conversation as resolved.
Show resolved Hide resolved
interval,
Aasim-A marked this conversation as resolved.
Show resolved Hide resolved
nameReplacement,
Aasim-A marked this conversation as resolved.
Show resolved Hide resolved
sigh,
] = part.split('#');

url.searchParams.set('sigh', sigh);

count = parseInt(count, 10);
storyboardWidth = parseInt(storyboardWidth, 10);
storyboardHeight = parseInt(storyboardHeight, 10);

const storyboardCount = Math.ceil(
count / (storyboardWidth * storyboardHeight),
);

return {
templateUrl: url.toString().replace('$L', i).replace('$N', nameReplacement),
width: parseInt(width, 10),
height: parseInt(height, 10),
count,
interval: parseInt(interval, 10),
storyboardWidth,
storyboardHeight,
storyboardCount,
};
});
};
1 change: 1 addition & 0 deletions lib/info.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ exports.getBasicInfo = async(id, options) => {

// Give the standard link to the video.
video_url: VIDEO_URL + id,
storyboards: extras.getStoryboards(info),
};

info.videoDetails = extras.cleanVideoDetails(Object.assign({},
Expand Down
30 changes: 30 additions & 0 deletions test/info-extras-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,3 +244,33 @@ describe('extras.getDislikes()', () => {
});
});
});

describe('extras.getStoryboards()', () => {
it('Returns storyboards', () => {
const info = infoFromWatchJSON('no-likes-or-dislikes');
const storyboards = extras.getStoryboards(info);

assert.ok(Array.isArray(storyboards));
assert.ok(storyboards.length > 0);

for (let storyboard of storyboards) {
assert.ok(storyboard.templateUrl);
Aasim-A marked this conversation as resolved.
Show resolved Hide resolved
assert.strictEqual(typeof storyboard.width, 'number');
assert.strictEqual(typeof storyboard.height, 'number');
assert.strictEqual(typeof storyboard.count, 'number');
assert.strictEqual(typeof storyboard.interval, 'number');
assert.strictEqual(typeof storyboard.storyboardWidth, 'number');
assert.strictEqual(typeof storyboard.storyboardHeight, 'number');
assert.strictEqual(typeof storyboard.storyboardCount, 'number');
}
});

describe('With no storyboards', () => {
it('Returns empty array', () => {
const info = infoFromWatchJSON('regular');
const storyboards = extras.getStoryboards(info);
assert.ok(Array.isArray(storyboards));
assert.ok(storyboards.length === 0);
});
});
});
12 changes: 12 additions & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,17 @@ declare module 'ytdl-core' {
uploadDate: string;
}

interface storyboard {
templateUrl: string;
width: number;
height: number;
count: number;
interval: number;
storyboardWidth: number;
storyboardHeight: number;
storyboardCount: number;
}

interface MoreVideoDetails extends Omit<VideoDetails, 'author' | 'thumbnail' | 'shortDescription'>, Omit<MicroformatRenderer, 'title' | 'description'> {
published: number;
video_url: string;
Expand All @@ -200,6 +211,7 @@ declare module 'ytdl-core' {
media: Media;
author: Author;
thumbnails: thumbnail[];
storyboards: storyboard[];
}

interface videoInfo {
Expand Down