refactor!: change getSearchSuggestions response schema

This commit is contained in:
LuanRT
2022-05-27 07:35:00 -03:00
parent 7284425618
commit 865b6870a1
3 changed files with 37 additions and 23 deletions

View File

@@ -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);
}
}

View File

@@ -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])
}
}
}

View File

@@ -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;
}
}