From 865b6870a11babbccffcdaeed8743818b498c039 Mon Sep 17 00:00:00 2001 From: LuanRT Date: Fri, 27 May 2022 07:35:00 -0300 Subject: [PATCH] refactor!: change `getSearchSuggestions` response schema --- lib/core/Actions.js | 38 +++++++++++++------ .../youtube/search/SearchSuggestionItem.js | 10 ++--- .../search/MusicSearchSuggestionItem.js | 12 +++--- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/lib/core/Actions.js b/lib/core/Actions.js index 1493393a..36c10315 100644 --- a/lib/core/Actions.js +++ b/lib/core/Actions.js @@ -5,10 +5,15 @@ const Proto = require('../proto'); const Utils = require('../utils/Utils'); const Constants = require('../utils/Constants'); +/** namespace **/ class Actions { #session; #request; + /** + * @param {Innertube} session + * @constructor + */ constructor(session) { this.#session = session; this.#request = session.request; @@ -102,7 +107,7 @@ class Actions { throw new Utils.InnertubeError('Action not implemented', action); } - const response = await this.#request.post(action, data); + const response = await this.#request.post(`/${action}`, data); return response; } @@ -133,7 +138,7 @@ class Actions { throw new Utils.InnertubeError('Action not implemented', action); } - const response = await this.#request.post(action, data); + const response = await this.#request.post(`/${action}`, data); return response; } @@ -219,7 +224,7 @@ class Actions { throw new Utils.InnertubeError('Action not implemented', action); } - const response = await this.#request.post(action, data); + const response = await this.#request.post(`/${action}`, data); return response; } @@ -267,7 +272,7 @@ class Actions { throw new Utils.InnertubeError('Action not implemented', action); } - const response = await this.#request.post(action, data); + const response = await this.#request.post(`/${action}`, data); return response; } @@ -351,7 +356,7 @@ class Actions { throw new Utils.InnertubeError('Action not implemented', action); } - const response = await this.#request.post(action, data); + const response = await this.#request.post(`/${action}`, data); return response; } @@ -432,7 +437,7 @@ class Actions { throw new Utils.InnertubeError('Action not implemented', action); } - const response = await this.#request.post(action, data); + const response = await this.#request.post(`/${action}`, data); return response; } @@ -524,17 +529,25 @@ class Actions { * * @returns {Promise.<{ success: boolean; status_code: number; data: object; }>} */ - async getSearchSuggestions(client, input) { + async getSearchSuggestions(client, query) { if (!['YOUTUBE', 'YTMUSIC'].includes(client)) throw new Utils.InnertubeError('Invalid client', client); const response = await ({ YOUTUBE: () => this.#request({ - baseURL: Constants.URLS.YT_SUGGESTIONS + `search?client=firefox&ds=yt&q=${encodeURIComponent(input)}`, + url: 'search', + baseURL: Constants.URLS.YT_SUGGESTIONS, + params: { + q: query, + ds: 'yt', + client: 'youtube', + xssi: 't', + oe: 'UTF', + gl: this.#session.context.client.gl, + hl: this.#session.context.client.hl + } }), - YTMUSIC: () => this.music('get_search_suggestions', { - input - }) + YTMUSIC: () => this.music('get_search_suggestions', { input: query }) }[client])(); return response; @@ -565,7 +578,8 @@ class Actions { #needsLogin(id) { return [ 'FElibrary', 'FEhistory', 'FEsubscriptions', - 'SPaccount_notifications', 'SPaccount_privacy' + 'SPaccount_notifications', 'SPaccount_privacy', + 'SPtime_watched' ].includes(id); } } diff --git a/lib/parser/youtube/search/SearchSuggestionItem.js b/lib/parser/youtube/search/SearchSuggestionItem.js index 9083ccdc..fcdcc6e2 100644 --- a/lib/parser/youtube/search/SearchSuggestionItem.js +++ b/lib/parser/youtube/search/SearchSuggestionItem.js @@ -1,11 +1,11 @@ 'use strict'; class SearchSuggestionItem { - static parse(data, bold_text) { - return data.map((item) => ({ - text: item.trim(), - bold_text: bold_text.trim().toLowerCase() - })); + static parse(data) { + return { + query: data[0], + results: data[1].map((res) => res[0]) + } } } diff --git a/lib/parser/ytmusic/search/MusicSearchSuggestionItem.js b/lib/parser/ytmusic/search/MusicSearchSuggestionItem.js index 3740c654..d7029f6a 100644 --- a/lib/parser/ytmusic/search/MusicSearchSuggestionItem.js +++ b/lib/parser/ytmusic/search/MusicSearchSuggestionItem.js @@ -2,7 +2,10 @@ class MusicSearchSuggestionItem { static parse(data) { - return data.map((item) => this.parseItem(item)); + return { + query: this.parseItem(data[0]).runs[0].text.trim(), + results: data.map((item) => this.parseItem(item).runs.map((run) => run.text).join('').trim()) + } } static parseItem(item) { @@ -11,11 +14,8 @@ class MusicSearchSuggestionItem { item.historySuggestionRenderer && (suggestion = item.historySuggestionRenderer.suggestion) || (suggestion = item.searchSuggestionRenderer.suggestion); - - return { - text: suggestion.runs.map((run) => run.text).join('').trim(), - bold_text: suggestion.runs[0].text.trim() - }; + + return suggestion; } }