mirror of
https://github.com/LuanRT/YouTube.js.git
synced 2026-06-19 04:21:35 +00:00
feat: add support for music search filters
This commit is contained in:
@@ -6,7 +6,7 @@ class Message {
|
||||
type = 'messageRenderer'
|
||||
|
||||
constructor(data) {
|
||||
this.text = new Text(data.text);
|
||||
this.text = new Text(data.text).toString();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -13,6 +13,7 @@ class AppendContinuationItemsAction {
|
||||
}
|
||||
}
|
||||
|
||||
/** @namespace */
|
||||
class ReloadContinuationItemsCommand {
|
||||
type = 'reloadContinuationItemsCommand';
|
||||
|
||||
@@ -108,7 +109,7 @@ class Parser {
|
||||
const keys = Object.keys(data);
|
||||
const classname = this.sanitizeClassName(keys[0]);
|
||||
|
||||
if (this.shouldIgnore(classname)) return;
|
||||
if (this.shouldIgnore(classname))return;
|
||||
|
||||
try {
|
||||
const TargetClass = require('./classes/' + classname);
|
||||
|
||||
@@ -63,7 +63,7 @@ class VideoInfo {
|
||||
/**
|
||||
* @type {import('../contents/classes/MerchandiseShelf')}
|
||||
*/
|
||||
this.merchandise = results?.get({ type: 'merchandiseShelfRenderer' }) || {};
|
||||
this.merchandise = results?.get({ type: 'merchandiseShelfRenderer' }) || null;
|
||||
|
||||
/**
|
||||
* @type {import('../contents/classes/ChipCloud')}
|
||||
@@ -152,7 +152,7 @@ class VideoInfo {
|
||||
return this.watch_next_feed;
|
||||
}
|
||||
|
||||
/** @type {string} */
|
||||
/** @type {string[]} */
|
||||
get filters() {
|
||||
return this.related_chip_cloud?.chips.map((chip) => chip.text) || [];
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ class Search {
|
||||
#actions;
|
||||
#continuation;
|
||||
#shelf_title;
|
||||
#header;
|
||||
|
||||
/**
|
||||
* @param {object} response - API response.
|
||||
@@ -27,6 +28,8 @@ class Search {
|
||||
const shelves = tab.content.contents;
|
||||
|
||||
const item_section = shelves.get({ type: 'itemSectionRenderer' });
|
||||
|
||||
this.#header = tab.content.header;
|
||||
|
||||
/**
|
||||
* @type {import('../contents/classes/DidYouMean')}
|
||||
@@ -38,7 +41,7 @@ class Search {
|
||||
*/
|
||||
this.showing_results_for = item_section?.contents.get({ type: 'showingResultsForRenderer' }) || null;
|
||||
|
||||
this.did_you_mean || this.showing_results_for && shelves.shift();
|
||||
(!!this.did_you_mean || !!this.showing_results_for) && shelves.shift();
|
||||
|
||||
if (is_continuation) {
|
||||
const shelf = shelves.get({ type: 'musicShelfRenderer' });
|
||||
@@ -78,9 +81,32 @@ class Search {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies given filter to the search.
|
||||
* @param {string} name
|
||||
* @returns {Promise.<Search>}
|
||||
*/
|
||||
async selectFilter(name) {
|
||||
if (!this.filters.includes(name))
|
||||
throw new InnertubeError('Invalid filter', { available_filters: this.filters });
|
||||
|
||||
const filter = this.#header.chips.get({ text: name });
|
||||
if (filter.is_selected) return this;
|
||||
|
||||
const response = await filter.endpoint.call(this.#actions, 'YTMUSIC');
|
||||
|
||||
return new Search(response, this.#actions, true);
|
||||
}
|
||||
|
||||
/** @type {boolean} */
|
||||
get has_continuation() {
|
||||
return !!this.#continuation;
|
||||
}
|
||||
|
||||
/** @type {string[]} */
|
||||
get filters() {
|
||||
return this.#header.chips.map((chip) => chip.text);
|
||||
}
|
||||
|
||||
/** @type {import('../contents/classes/MusicResponsiveListItem')[]} */
|
||||
get songs() {
|
||||
|
||||
Reference in New Issue
Block a user