refactor: remove some unnecessary code in the signIn method

This commit is contained in:
LuanRT
2021-10-12 19:11:35 -03:00
parent c4b1854bba
commit 6639687d76

View File

@@ -33,7 +33,7 @@ class Innertube extends EventEmitter {
this.sts = innertube_data.STS;
this.context.client.hl = 'en';
this.context.client.gl = 'US';
this.player = new Player(this);
await this.player.init();
@@ -48,53 +48,53 @@ class Innertube extends EventEmitter {
}
return this;
}
async signIn(credentials = {}) {
const oauth = new OAuth(credentials);
if (credentials.access_token && credentials.refresh_token) {
let token_validity = await oauth.checkTokenValidity(credentials.access_token, this);
if (token_validity === 'VALID') {
this.access_token = credentials.access_token;
this.refresh_token = credentials.refresh_token;
this.logged_in = true;
} else {
oauth.refreshAccessToken(credentials.refresh_token);
oauth.on('refresh-token', (data) => {
this.access_token = data.access_token;
signIn(credentials = {}) {
return new Promise(async (resolve, reject) => {
const oauth = new OAuth(credentials);
if (credentials.access_token && credentials.refresh_token) {
let token_validity = await oauth.checkTokenValidity(credentials.access_token, this);
if (token_validity === 'VALID') {
this.access_token = credentials.access_token;
this.refresh_token = credentials.refresh_token;
this.logged_in = true;
this.emit('update-credentials', {
access_token: data.access_token,
refresh_token: credentials.refresh_token,
status: data.status
});
});
}
return;
}
return new Promise((resolve, reject) => {
oauth.on('auth', (data) => {
if (data.status === 'SUCCESS') {
this.emit('auth', data);
this.access_token = data.access_token;
this.refresh_token = data.refresh_token;
this.logged_in = true;
resolve();
} else {
this.emit('auth', data);
oauth.refreshAccessToken(credentials.refresh_token);
oauth.on('refresh-token', (data) => {
this.access_token = data.access_token;
this.refresh_token = credentials.refresh_token;
this.logged_in = true;
this.emit('update-credentials', {
access_token: data.access_token,
refresh_token: credentials.refresh_token,
status: data.status
});
resolve();
});
}
});
} else {
oauth.on('auth', (data) => {
if (data.status === 'SUCCESS') {
this.emit('auth', data);
this.access_token = data.access_token;
this.refresh_token = data.refresh_token;
this.logged_in = true;
resolve();
} else {
this.emit('auth', data);
}
});
}
});
}
async search(query, options = { period: 'any', order: 'relevance', duration: 'any', quantity: 100 }) {
if (!this.initialized) throw new Error('Missing Innertube data.');
const yt_response = await Axios.post(`${Constants.urls.YT_BASE_URL}/youtubei/v1/search${this.logged_in && this.cookie.length < 1 ? '' : `?key=${this.key}`}`, JSON.stringify({ context: this.context, params: Constants.filters(options.period + ',' + options.duration + ',' + options.order), query }), Constants.innertube_request_opts({ session: this })).catch((error) => error);
if (yt_response instanceof Error) throw new Error('Could not search on YouTube: ' + yt_response.message);
let content = yt_response.data.contents.twoColumnSearchResultsRenderer.primaryContents.sectionListRenderer.contents[0].itemSectionRenderer.contents;
let search_response = {};
@@ -132,7 +132,7 @@ class Innertube extends EventEmitter {
}).filter((video_block) => video_block !== undefined);
return search_response;
}
async getDetails(id) {
const data = await this.requestVideoInfo(id, false);
const video_data = Constants.formatVideoData(data, this, false);
@@ -313,7 +313,7 @@ class Innertube extends EventEmitter {
response.data.on('end', () => {
chunk_start = chunk_end + 1;
chunk_end += chunk_size;
if (downloaded_size < selected_format.contentLength) {
downloadChunk();
} else {