refactor: use /sw.js_data to retrieve initial session data

Seems like the `/sw.js` service worker endpoint has a few peculiarities, see #55
This commit is contained in:
LuanRT
2022-05-19 05:02:22 -03:00
parent e0c7496e37
commit 6aaf9c70b9
2 changed files with 15 additions and 9 deletions

View File

@@ -14,6 +14,7 @@ class SessionBuilder {
#client_name;
#client_version;
#api_version;
#remote_host;
#context;
#player;
@@ -27,14 +28,15 @@ class SessionBuilder {
this.#getPlayerId()
]);
const ytcfg = data[0];
const ytcfg = data[0][0][2];
this.#key = ytcfg.INNERTUBE_API_KEY;
this.#client_name = ytcfg.INNERTUBE_CLIENT_NAME;
this.#client_version = ytcfg.INNERTUBE_CLIENT_VERSION;
this.#api_version = ytcfg.INNERTUBE_API_VERSION;
this.#key = ytcfg[1];
this.#api_version = `v${ytcfg[0][0][6]}`;
this.#client_name = Constants.CLIENTS.WEB.NAME;
this.#client_version = ytcfg[0][0][16];
this.#remote_host = ytcfg[0][0][3];
this.#player = await new Player(data[1]).init();
this.#context = this.#buildContext();
return this;
@@ -52,6 +54,7 @@ class SessionBuilder {
client: {
hl: 'en',
gl: this.#config.gl || 'US',
remoteHost: this.#remote_host,
deviceMake: user_agent.vendor,
deviceModel: user_agent.platform,
visitorData: visitor_data,
@@ -68,15 +71,15 @@ class SessionBuilder {
}
async #getYtConfig() {
const response = await Axios.get(`${Constants.URLS.YT_BASE}/sw.js`).catch((err) => err);
const response = await Axios.get(`${Constants.URLS.YT_BASE}/sw.js_data`).catch((err) => err);
if (response instanceof Error)
throw new Utils.InnertubeError('Could not retrieve session data', {
status_code: response?.response?.status || 0,
message: response.message
});
return JSON.parse(Utils.getStringBetweenStrings(response.data, 'ytcfg.set(', ')'));
return JSON.parse(response.data.replace(')]}\'', ''));
}
async #getPlayerId() {