From 9f3f8ad820792951dbc43660af8b8d6f0a02b82e Mon Sep 17 00:00:00 2001 From: LuanRT Date: Wed, 18 May 2022 05:56:28 -0300 Subject: [PATCH] style: format code --- lib/Innertube.js | 20 +++++++++----- lib/core/Actions.js | 31 ++++++++-------------- lib/core/Livechat.js | 5 ++-- lib/core/Player.js | 4 +-- lib/parser/index.js | 10 +++---- lib/parser/youtube/others/CommentThread.js | 1 - lib/proto/index.js | 2 +- lib/utils/Constants.js | 6 ++--- lib/utils/Utils.js | 18 ++++++------- 9 files changed, 47 insertions(+), 50 deletions(-) diff --git a/lib/Innertube.js b/lib/Innertube.js index 1d7164ff..dfb424b6 100644 --- a/lib/Innertube.js +++ b/lib/Innertube.js @@ -22,7 +22,6 @@ const Signature = require('./deciphers/Signature'); class Innertube { #oauth; #player; - #actions; /** * ```js @@ -274,7 +273,7 @@ class Innertube { return { success: response.success, status_code: response.status_code, - playlist_id: playlistId, + playlist_id, data: response.data } }, @@ -297,7 +296,7 @@ class Innertube { return { success: response.success, status_code: response.status_code, - playlist_id: playlistId, + playlist_id, data: response.data } }, @@ -329,7 +328,7 @@ class Innertube { return { success: response.success, status_code: response.status_code, - playlist_id: playlist_id, + playlist_id, data: response.data } } @@ -430,7 +429,16 @@ class Innertube { language: menu.sections[1].multiPageMenuSectionRenderer.items[1].compactLinkRenderer.subtitle.simpleText } } - + + + // TEMPORARY: This is only here for testing purposes + // eslint-disable-next-line no-unused-private-class-members + async #getBasicChannelAnalytics() { + const params = Proto.encodeChannelAnalyticsParams('UCM9VCokI0KanBqPwq6QBv3g'); + const action = await this.actions.browse('FEanalytics_screen', { params, is_yta: true }); + return action; + } + /** * Searches on YouTube. * @@ -789,7 +797,7 @@ class Innertube { const streaming_data = this.#chooseFormat(options, data); if (!streaming_data.selected_format) - throw new Utils.NoStreamingDataError('Could not find any suitable format.', { id, options }); + throw new Utils.NoStreamingDataError('Could not find any suitable format.', { video_id, options }); return streaming_data; } diff --git a/lib/core/Actions.js b/lib/core/Actions.js index 58aa22db..9fd2517b 100644 --- a/lib/core/Actions.js +++ b/lib/core/Actions.js @@ -1,7 +1,6 @@ 'use strict'; const Uuid = require('uuid'); -const Axios = require('axios'); const Proto = require('../proto'); const Utils = require('../utils/Utils'); const Constants = require('../utils/Constants'); @@ -41,12 +40,13 @@ class Actions { (data.continuation = id) || (data.browseId = id); - if (args.is_ytm) { - const context = JSON.parse(JSON.stringify(this.#session.context)); // deep copy the context obj so we don't accidentally change it - - context.client.originalUrl = Constants.URLS.YT_MUSIC; - context.client.clientVersion = Constants.CLIENTS.YTMUSIC.VERSION; - context.client.clientName = Constants.CLIENTS.YTMUSIC.NAME; + // TODO: maybe refactor this? + if (args.is_ytm || args.is_yta) { + const context = JSON.parse(JSON.stringify(this.#session.context)); + + context.client.originalUrl = args.is_ytm && Constants.URLS.YT_MUSIC || Constants.URLS.YT_BASE; + context.client.clientVersion = args.is_ytm && Constants.CLIENTS.YTMUSIC.VERSION || Constants.CLIENTS.ANDROID.VERSION; + context.client.clientName = args.is_ytm && Constants.CLIENTS.YTMUSIC.NAME || Constants.CLIENTS.ANDROID.NAME; data.context = context; } @@ -96,19 +96,10 @@ class Actions { data.commentText = args.text; break; case 'comment/perform_comment_action': - const params = { - video_id: args.video_id, - comment_id: args.comment_id - }; - const target_action = ({ - like: () => Proto.encodeCommentActionParams(5, params), - dislike: () => Proto.encodeCommentActionParams(4, params), - translate: () => { - params.text = args.text; - params.target_language = args.target_language; - return Proto.encodeCommentActionParams(22, params); - } + like: () => Proto.encodeCommentActionParams(5, args), + dislike: () => Proto.encodeCommentActionParams(4, args), + translate: () => Proto.encodeCommentActionParams(22, args) })[args.comment_action](); data.actions = [ target_action ]; @@ -490,4 +481,4 @@ class Actions { } } -module.exports = Actions; +module.exports = Actions; \ No newline at end of file diff --git a/lib/core/Livechat.js b/lib/core/Livechat.js index ce31cb06..c1871afa 100644 --- a/lib/core/Livechat.js +++ b/lib/core/Livechat.js @@ -122,10 +122,9 @@ class Livechat extends EventEmitter { /** * Blocks a user. - * @todo Implement this method. - * @param {object} msg_params + * @todo Implement this method */ - async blockUser(msg_params) { + async blockUser() { throw new Error('Not implemented'); } diff --git a/lib/core/Player.js b/lib/core/Player.js index 354230d5..dacd3571 100644 --- a/lib/core/Player.js +++ b/lib/core/Player.js @@ -32,7 +32,7 @@ class Player { this.#ntoken_decipher_sc = this.#extractNTokenSc(player_data); } else { const response = await Axios.get(this.#player_url, { headers: { 'content-type': 'text/javascript', 'user-agent': Utils.getRandomUserAgent('desktop').userAgent } }).catch((error) => error); - if (response instanceof Error) throw new Utils.InnertubeError('Could not download js player', { player_id }); + if (response instanceof Error) throw new Utils.InnertubeError('Could not download js player', { player_id: this.#player_id }); this.#signature_timestamp = this.#extractSigTimestamp(response.data); this.#signature_decipher_sc = this.#extractSigDecipherSc(response.data); @@ -46,7 +46,7 @@ class Player { // Cache the current player Fs.mkdirSync(this.#cache_dir, { recursive: true }); Fs.writeFileSync(this.#player_path, response.data); - } catch (err) {} + } finally { /* do nothing */ } } return this; diff --git a/lib/parser/index.js b/lib/parser/index.js index 47ab6464..97a35415 100644 --- a/lib/parser/index.js +++ b/lib/parser/index.js @@ -289,8 +289,8 @@ class Parser { response.items = items.map((item) => { const comment = YTDataItems.CommentThread.parseItem(item); if (comment) { - comment.like = () => this.session.actions.engage('comment/perform_comment_action', { comment_action: 'like', comment_id: comment.metadata.id, video_id: this.args.video_id }), - comment.dislike = () => this.session.actions.engage('comment/perform_comment_action', { comment_action: 'dislike', comment_id: comment.metadata.id, video_id: this.args.video_id }), + comment.like = () => this.session.actions.engage('comment/perform_comment_action', { comment_action: 'like', comment_id: comment.metadata.id, video_id: this.args.video_id }); + comment.dislike = () => this.session.actions.engage('comment/perform_comment_action', { comment_action: 'dislike', comment_id: comment.metadata.id, video_id: this.args.video_id }); comment.reply = (text) => this.session.actions.engage('comment/create_comment_reply', { text, comment_id: comment.metadata.id, video_id: this.args.video_id }); comment.report = async () => { @@ -391,7 +391,7 @@ class Parser { stats } - const content = Utils.findNode(this.data, 'contents', 'content', 8, false); + // const content = Utils.findNode(this.data, 'contents', 'content', 8, false); // console.info(content[0].itemSectionRenderer.contents[0].shelfRenderer); return { @@ -489,7 +489,7 @@ class Parser { #processNotifications() { const contents = this.data.actions[0].openPopupAction.popup.multiPageMenuRenderer.sections[0]; - if (!contents.multiPageMenuNotificationSectionRenderer) throw new Utils.InnertubeError('No notifications', response); + if (!contents.multiPageMenuNotificationSectionRenderer) throw new Utils.InnertubeError('No notifications'); const parseItems = (items) => { const parsed_items = YTDataItems.NotificationItem.parse(items); @@ -513,7 +513,7 @@ class Parser { const tabs = Utils.findNode(this.data, 'contents', 'tabRenderer', 4, false); const categories = {}; - const trending = tabs.map((tab) => { + tabs.forEach((tab) => { const tab_renderer = tab.tabRenderer; const tab_content = tab_renderer?.content; const category_title = tab_renderer.title.toLowerCase(); diff --git a/lib/parser/youtube/others/CommentThread.js b/lib/parser/youtube/others/CommentThread.js index 6d2edb9f..175b2f09 100644 --- a/lib/parser/youtube/others/CommentThread.js +++ b/lib/parser/youtube/others/CommentThread.js @@ -6,7 +6,6 @@ class CommentThread { static parseItem(item) { if (item.commentThreadRenderer || item.commentRenderer) { const comment = item?.commentThreadRenderer?.comment || item; - const replies = item?.commentThreadRenderer?.replies; const like_btn = comment.commentRenderer?.actionButtons?.commentActionButtonsRenderer.likeButton; const dislike_btn = comment.commentRenderer?.actionButtons?.commentActionButtonsRenderer.dislikeButton; diff --git a/lib/proto/index.js b/lib/proto/index.js index b76f5ab6..0d4ce18f 100644 --- a/lib/proto/index.js +++ b/lib/proto/index.js @@ -13,7 +13,7 @@ class Proto { */ static encodeVisitorData(id, timestamp) { const buf = messages.VisitorData.encode({ id, timestamp }); - return encodeURIComponent(Buffer.from(buf).toString('base64').replace(/\//g, '_')); + return encodeURIComponent(Buffer.from(buf).toString('base64').replace(/\/|\+/g, '_')); } /** diff --git a/lib/utils/Constants.js b/lib/utils/Constants.js index 06fe33c0..ec540dc9 100644 --- a/lib/utils/Constants.js +++ b/lib/utils/Constants.js @@ -21,13 +21,13 @@ module.exports = { 'origin': 'https://www.youtube.com', 'user-agent': 'Mozilla/5.0 (ChromiumStylePlatform) Cobalt/Version', 'content-type': 'application/json', - 'referer': `https://www.youtube.com/tv`, + 'referer': 'https://www.youtube.com/tv', 'accept-language': 'en-US' } }, REGEX: { - AUTH_SCRIPT: /