diff --git a/src/Innertube.ts b/src/Innertube.ts index d2709884..a115c5f8 100644 --- a/src/Innertube.ts +++ b/src/Innertube.ts @@ -42,7 +42,7 @@ import { /** * Provides access to various services and modules in the YouTube API. - * + * * @example * ```ts * import { Innertube, UniversalCache } from 'youtubei.js'; @@ -75,7 +75,7 @@ export default class Innertube { const watch_endpoint = new NavigationEndpoint({ watchEndpoint: payload }); const watch_next_endpoint = new NavigationEndpoint({ watchNextEndpoint: payload }); - const watch_response = watch_endpoint.call(this.#session.actions, { + const extra_payload: Record = { playbackContext: { contentPlaybackContext: { vis: 0, @@ -84,12 +84,16 @@ export default class Innertube { signatureTimestamp: this.#session.player?.sts } }, - serviceIntegrityDimensions: { - poToken: this.#session.po_token - }, client - }); + }; + if (this.#session.po_token) { + extra_payload.serviceIntegrityDimensions = { + poToken: this.#session.po_token + }; + } + + const watch_response = watch_endpoint.call(this.#session.actions, extra_payload); const watch_next_response = watch_next_endpoint.call(this.#session.actions); const response = await Promise.all([ watch_response, watch_next_response ]); @@ -104,7 +108,7 @@ export default class Innertube { const watch_endpoint = new NavigationEndpoint({ watchEndpoint: { videoId: video_id } }); - const watch_response = await watch_endpoint.call(this.#session.actions, { + const extra_payload: Record = { playbackContext: { contentPlaybackContext: { vis: 0, @@ -113,11 +117,16 @@ export default class Innertube { signatureTimestamp: this.#session.player?.sts } }, - serviceIntegrityDimensions: { - poToken: this.#session.po_token - }, client - }); + }; + + if (this.#session.po_token) { + extra_payload.serviceIntegrityDimensions = { + poToken: this.#session.po_token + }; + } + + const watch_response = await watch_endpoint.call(this.#session.actions, extra_payload); const cpn = generateRandomString(16); @@ -136,7 +145,7 @@ export default class Innertube { }); const reel_watch_response = reel_watch_endpoint.call(this.#session.actions, { client }); - + const writer = ReelSequence.encode({ shortId: video_id, params: { @@ -222,7 +231,12 @@ export default class Innertube { } } - const search_endpoint = new NavigationEndpoint({ searchEndpoint: { query, params: filters ? encodeURIComponent(u8ToBase64(SearchFilter.encode(search_filter).finish())) : undefined } }); + const search_endpoint = new NavigationEndpoint({ + searchEndpoint: { + query, + params: filters ? encodeURIComponent(u8ToBase64(SearchFilter.encode(search_filter).finish())) : undefined + } + }); const response = await search_endpoint.call(this.#session.actions); return new Search(this.actions, response); @@ -239,10 +253,10 @@ export default class Innertube { url.searchParams.set('hl', this.#session.context.client.hl); url.searchParams.set('gl', this.#session.context.client.gl); url.searchParams.set('q', query); - + if (previous_query) url.searchParams.set('pq', previous_query); - + const response = await this.#session.http.fetch(url); const text = await response.text(); @@ -287,13 +301,13 @@ export default class Innertube { return new Comments(this.actions, response.data); } - + async getHomeFeed(): Promise { const browse_endpoint = new NavigationEndpoint({ browseEndpoint: { browseId: 'FEwhat_to_watch' } }); const response = await browse_endpoint.call(this.#session.actions); return new HomeFeed(this.actions, response); } - + async getGuide(): Promise { const response = await this.actions.execute('/guide'); return new Guide(response.data); diff --git a/src/core/clients/Kids.ts b/src/core/clients/Kids.ts index 70bb61f2..a0fdad92 100644 --- a/src/core/clients/Kids.ts +++ b/src/core/clients/Kids.ts @@ -24,7 +24,7 @@ export default class Kids { const watch_endpoint = new NavigationEndpoint({ watchEndpoint: payload }); const watch_next_endpoint = new NavigationEndpoint({ watchNextEndpoint: payload }); - const watch_response = watch_endpoint.call(this.#session.actions, { + const extra_payload: Record = { playbackContext: { contentPlaybackContext: { vis: 0, @@ -34,7 +34,15 @@ export default class Kids { } }, client: 'YTKIDS' - }); + }; + + if (this.#session.po_token) { + extra_payload.serviceIntegrityDimensions = { + poToken: this.#session.po_token + }; + } + + const watch_response = watch_endpoint.call(this.#session.actions, extra_payload); const watch_next_response = watch_next_endpoint.call(this.#session.actions, { client: 'YTKIDS' }); diff --git a/src/core/clients/Music.ts b/src/core/clients/Music.ts index 26ec392f..9d80f291 100644 --- a/src/core/clients/Music.ts +++ b/src/core/clients/Music.ts @@ -50,7 +50,7 @@ export default class Music { return this.#fetchInfoFromEndpoint(target.overlay?.content?.endpoint ?? target.endpoint); } else if (target instanceof NavigationEndpoint) { return this.#fetchInfoFromEndpoint(target); - } + } return this.#fetchInfoFromVideoId(target); } @@ -59,7 +59,7 @@ export default class Music { const watch_endpoint = new NavigationEndpoint({ watchEndpoint: payload }); const watch_next_endpoint = new NavigationEndpoint({ watchNextEndpoint: payload }); - const watch_response = watch_endpoint.call(this.#actions, { + const extra_payload: Record = { playbackContext: { contentPlaybackContext: { vis: 0, @@ -69,7 +69,15 @@ export default class Music { } }, client: 'YTMUSIC' - }); + }; + + if (this.#session.po_token) { + extra_payload.serviceIntegrityDimensions = { + poToken: this.#session.po_token + }; + } + + const watch_response = watch_endpoint.call(this.#actions, extra_payload); const watch_next_response = watch_next_endpoint.call(this.#actions, { client: 'YTMUSIC' }); @@ -83,16 +91,25 @@ export default class Music { if (!endpoint) throw new Error('This item does not have an endpoint.'); - const player_response = endpoint.call(this.#actions, { - client: 'YTMUSIC', + const extra_payload: Record = { playbackContext: { contentPlaybackContext: { - ...{ - signatureTimestamp: this.#session.player?.sts - } + vis: 0, + splay: false, + lactMilliseconds: '-1', + signatureTimestamp: this.#session.player?.sts } - } - }); + }, + client: 'YTMUSIC' + }; + + if (this.#session.po_token) { + extra_payload.serviceIntegrityDimensions = { + poToken: this.#session.po_token + }; + } + + const player_response = endpoint.call(this.#actions, extra_payload); const next_response = endpoint.call(this.#actions, { client: 'YTMUSIC', @@ -271,7 +288,11 @@ export default class Music { } async getSearchSuggestions(input: string): Promise> { - const response = await this.#actions.execute('/music/get_search_suggestions', { input, client: 'YTMUSIC', parse: true }); + const response = await this.#actions.execute('/music/get_search_suggestions', { + input, + client: 'YTMUSIC', + parse: true + }); if (!response.contents_memo) return [] as unknown as ObservedArray;