diff --git a/src/client.js b/src/client.js index c38aa6a..feb41e2 100644 --- a/src/client.js +++ b/src/client.js @@ -228,8 +228,14 @@ module.exports = class Client { if (clientOptions.DEBUG) global.TW_DEBUG = clientOptions.DEBUG; const server = clientOptions.server || 'data'; - this.#ws = new WebSocket(`wss://${server}.tradingview.com/socket.io/websocket?type=chart`, { + this.#ws = new WebSocket(`wss://${server}.tradingview.com/socket.io/websocket?from=chart&type=chart`, { origin: 'https://www.tradingview.com', + headers: { + 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', + 'Accept-Language': 'en-US,en;q=0.9', + 'Cache-Control': 'no-cache', + Pragma: 'no-cache', + }, }); if (clientOptions.token) { diff --git a/src/miscRequests.js b/src/miscRequests.js index a5d5048..ea2c5ba 100644 --- a/src/miscRequests.js +++ b/src/miscRequests.js @@ -429,7 +429,11 @@ module.exports = { * @param {string} [location] Auth page location (For france: https://fr.tradingview.com/) * @returns {Promise} Token */ - async getUser(session, signature = '', location = 'https://www.tradingview.com/') { + async getUser(session, signature = '', location = 'https://www.tradingview.com/', redirectCount = 0) { + if (redirectCount > 5) { + throw new Error('Too many redirects - invalid session/signature'); + } + const { data, headers } = await axios.get(location, { headers: { cookie: genAuthCookies(session, signature), @@ -461,7 +465,7 @@ module.exports = { } if (headers.location !== location) { - return this.getUser(session, signature, headers.location); + return this.getUser(session, signature, headers.location, redirectCount + 1); } throw new Error('Wrong or expired sessionid/signature');