Skip to content

Commit

Permalink
fix: fix null likes and dislikes on other languages (#761)
Browse files Browse the repository at this point in the history
* fix: `null` likes and dislikes on other languages

* linting

* Update test for new getLikes() and getDislikes()

* Linting
  • Loading branch information
skick1234 committed Nov 1, 2020
1 parent 969c4ff commit 9c885fc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
38 changes: 26 additions & 12 deletions lib/info-extras.js
Expand Up @@ -172,23 +172,37 @@ exports.getRelatedVideos = info => {
/**
* Get like count.
*
* @param {string} body
* @return {number}
* @param {string} info
* @returns {number}
*/
const getLikesRegex = /"label":"([\d,]+?) likes"/;
exports.getLikes = body => {
const likes = body.match(getLikesRegex);
return likes ? parseInt(likes[1].replace(/,/g, '')) : null;
exports.getLikes = info => {
try {
let contents = info.response.contents.twoColumnWatchNextResults.results.results.contents;
let video = contents.find(r => r.videoPrimaryInfoRenderer);
let buttons = video.videoPrimaryInfoRenderer.videoActions.menuRenderer.topLevelButtons;
let like = buttons.find(b => b.toggleButtonRenderer &&
b.toggleButtonRenderer.defaultIcon.iconType === 'LIKE');
return parseInt(like.toggleButtonRenderer.defaultText.accessibility.accessibilityData.label.replace(/\D+/g, ''));
} catch (err) {
return null;
}
};

/**
* Get dislike count.
*
* @param {string} body
* @return {number}
* @param {string} info
* @returns {number}
*/
const getDislikesRegex = /"label":"([\d,]+?) dislikes"/;
exports.getDislikes = body => {
const dislikes = body.match(getDislikesRegex);
return dislikes ? parseInt(dislikes[1].replace(/,/g, '')) : null;
exports.getDislikes = info => {
try {
let contents = info.response.contents.twoColumnWatchNextResults.results.results.contents;
let video = contents.find(r => r.videoPrimaryInfoRenderer);
let buttons = video.videoPrimaryInfoRenderer.videoActions.menuRenderer.topLevelButtons;
let dislike = buttons.find(b => b.toggleButtonRenderer &&
b.toggleButtonRenderer.defaultIcon.iconType === 'DISLIKE');
return parseInt(dislike.toggleButtonRenderer.defaultText.accessibility.accessibilityData.label.replace(/\D+/g, ''));
} catch (err) {
return null;
}
};
8 changes: 4 additions & 4 deletions lib/info.js
Expand Up @@ -31,7 +31,7 @@ exports.watchPageCache = new Cache();
* @returns {Promise<Object>}
*/
exports.getBasicInfo = async(id, options) => {
let [info, body] = await getJSONWatchPage(id, options);
let info = await getJSONWatchPage(id, options);
let player_response =
(info.player && info.player.args && info.player.args.player_response) ||
info.player_response || info.playerResponse;
Expand Down Expand Up @@ -74,8 +74,8 @@ exports.getBasicInfo = async(id, options) => {
let additional = {
author: extras.getAuthor(info),
media: extras.getMedia(info),
likes: extras.getLikes(body),
dislikes: extras.getDislikes(body),
likes: extras.getLikes(info),
dislikes: extras.getDislikes(info),
age_restricted,

// Give the standard link to the video.
Expand Down Expand Up @@ -184,7 +184,7 @@ const getJSONWatchPage = async(id, options, maxRetries = 1) => {
throw Error('Unable to retrieve video metadata');
}
let info = parsedBody.reduce((part, curr) => Object.assign(curr, part), {});
return [info, body];
return info;
};

const getEmbedURL = (id, options) => `${EMBED_URL + id}?hl=${options.lang || 'en'}`;
Expand Down
22 changes: 8 additions & 14 deletions test/info-extras-test.js
@@ -1,5 +1,3 @@
const fs = require('fs');
const path = require('path');
const assert = require('assert-diff');
const extras = require('../lib/info-extras');

Expand Down Expand Up @@ -151,35 +149,31 @@ describe('extras.getRelatedVideos()', () => {

describe('extras.getLikes()', () => {
it('Returns like count', () => {
let html = fs.readFileSync(path.resolve(__dirname,
'files/videos/regular/watch.json'), 'utf8');
const likes = extras.getLikes(html);
const info = require('./files/videos/regular/watch.json')[3];
const likes = extras.getLikes(info);
assert.strictEqual(typeof likes, 'number');
});

describe('With no likes', () => {
it('Does not return likes', () => {
let html = fs.readFileSync(path.resolve(__dirname,
'files/videos/no-likes-or-dislikes/watch.json'), 'utf8');
const likes = extras.getLikes(html);
const info = require('./files/videos/no-likes-or-dislikes/watch.json')[3];
const likes = extras.getLikes(info);
assert.strictEqual(likes, null);
});
});
});

describe('extras.getDislikes()', () => {
it('Returns dislike count', () => {
let html = fs.readFileSync(path.resolve(__dirname,
'files/videos/regular/watch.json'), 'utf8');
const dislikes = extras.getDislikes(html);
const info = require('./files/videos/regular/watch.json')[3];
const dislikes = extras.getDislikes(info);
assert.strictEqual(typeof dislikes, 'number');
});

describe('With no dislikes', () => {
it('Does not return dislikes', () => {
let html = fs.readFileSync(path.resolve(__dirname,
'files/videos/no-likes-or-dislikes/watch.json'), 'utf8');
const dislikes = extras.getDislikes(html);
const info = require('./files/videos/no-likes-or-dislikes/watch.json')[3];
const dislikes = extras.getDislikes(info);
assert.strictEqual(dislikes, null);
});
});
Expand Down

0 comments on commit 9c885fc

Please sign in to comment.