Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use dynamic YouTube Music params #722

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions src/services/youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class YouTubeMusic {
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36',
},
}),
apiKey: null,
apiConfig: null,
};

#request = async function request(url, opts) {
Expand All @@ -103,13 +103,15 @@ export class YouTubeMusic {
return response.body;
};

#getApiKey = async function getApiKey(force = false) {
if (this.#store.apiKey && !force) return this.#store.apiKey;
#deriveConfig = async function deriveConfig(force = false) {
if (this.#store.apiConfig && !force) return this.#store.apiConfig;
const body = await this.#request('https://music.youtube.com/', {method: 'get'});
let match;
if ((match = (body || '').match(/(?="INNERTUBE_API_KEY":"(.+?)")/)))
return ([, this.#store.apiKey] = match), this.#store.apiKey;
throw new YouTubeSearchError('Failed to extract `INNERTUBE_API_KEY`');
if ((match = (body || '').match(/ytcfg\.set\s*\(\s*({.+})\s*\)\s*;/))) {
this.#store.apiConfig = JSON.parse(match[1]);
return this.#store.apiConfig;
}
throw new YouTubeSearchError('Failed to extract YouTube Music Configuration');
};

#YTM_PATHS = {
Expand All @@ -130,16 +132,19 @@ export class YouTubeMusic {
*/
if (typeof queryObject !== 'object') throw new Error('<queryObject> must be an object');
if (params && typeof params !== 'object') throw new Error('<params>, if defined must be an object');

let {INNERTUBE_API_KEY, INNERTUBE_CLIENT_NAME, INNERTUBE_CLIENT_VERSION} = await this.#deriveConfig();

const response = await this.#request('https://music.youtube.com/youtubei/v1/search', {
timeout: {request: 10000},
method: 'post',
searchParams: {alt: 'json', key: await this.#getApiKey(), ...params},
searchParams: {alt: 'json', key: INNERTUBE_API_KEY, ...params},
responseType: 'json',
json: {
context: {
client: {
clientName: 'WEB_REMIX',
clientVersion: '0.1',
clientName: INNERTUBE_CLIENT_NAME,
clientVersion: INNERTUBE_CLIENT_VERSION,
hl: 'en',
gl: 'US',
},
Expand Down
Loading