Skip to content

πŸ“ˆ Get real-time stocks from TradingView

Add loginUser function

Mathieu Colmon671950747b8b
diff --git a/examples/BuiltInIndicator.js b/examples/BuiltInIndicator.js
index 8ac9a13..ede156b 100644
--- a/examples/BuiltInIndicator.js
+++ b/examples/BuiltInIndicator.js
@@ -7,7 +7,7 @@ const TradingView = require('../main');
const volumeProfile = new TradingView.BuiltInIndicator('VbPFixed@tv-basicstudies-139!');
-if (!process.argv[2] && volumeProfile.type !== 'VbPFixed@tv-basicstudies-139!') {
+if (!process.argv[2] && !['VbPFixed@tv-basicstudies-139!', 'Volume@tv-basicstudies-144'].includes(volumeProfile.type)) {
throw Error('Please specify your \'sessionid\' cookie');
}
diff --git a/examples/UserLogin.js b/examples/UserLogin.js
new file mode 100644
index 0000000..e9b2f19
--- /dev/null
+++ b/examples/UserLogin.js
@@ -0,0 +1,16 @@
+const TradingView = require('../main');
+
+/*
+ This example tests the
+ user login function
+*/
+
+if (!process.argv[2]) throw Error('Please specify your username/email');
+if (!process.argv[3]) throw Error('Please specify your password');
+
+TradingView.loginUser(process.argv[2], process.argv[3], false).then((user) => {
+ console.log('User:', user);
+ console.log('Sessionid:', user.session);
+}).catch((err) => {
+ console.error('Login error:', err.message);
+});
diff --git a/src/classes/PinePermManager.js b/src/classes/PinePermManager.js
index 892f46c..02d03c8 100644
--- a/src/classes/PinePermManager.js
+++ b/src/classes/PinePermManager.js
@@ -40,7 +40,7 @@ class PinePermManager {
* @returns {AuthorizationUser[]}
*/
async getUsers(limit = 10, order = '-created') {
- const data = await request({
+ const { data } = await request({
method: 'POST',
host: 'www.tradingview.com',
path: `/pine_perm/list_users/?limit=${limit}&order_by=${order}`,
@@ -70,7 +70,7 @@ class PinePermManager {
formData.append('expiration', expiration.toString());
}
- const data = await request({
+ const { data } = await request({
method: 'POST',
host: 'www.tradingview.com',
path: '/pine_perm/add/',
@@ -99,7 +99,7 @@ class PinePermManager {
formData.append('expiration', expiration.toISOString());
}
- const data = await request({
+ const { data } = await request({
method: 'POST',
host: 'www.tradingview.com',
path: '/pine_perm/modify_user_expiration/',
@@ -124,7 +124,7 @@ class PinePermManager {
formData.append('pine_id', this.pineId);
formData.append('username_recip', username);
- const data = await request({
+ const { data } = await request({
method: 'POST',
host: 'www.tradingview.com',
path: '/pine_perm/remove/',
diff --git a/src/miscRequests.js b/src/miscRequests.js
index e2ad899..06b2c14 100644
--- a/src/miscRequests.js
+++ b/src/miscRequests.js
@@ -1,5 +1,7 @@
+const os = require('os');
const https = require('https');
const request = require('./request');
+const FormData = require('./FormData');
const PinePermManager = require('./classes/PinePermManager');
const PineIndicator = require('./classes/PineIndicator');
@@ -8,7 +10,7 @@ const indicators = ['Recommend.Other', 'Recommend.All', 'Recommend.MA'];
const builtInIndicList = [];
async function fetchScanData(tickers = [], type = '', columns = []) {
- let data = await request({
+ let { data } = await request({
method: 'POST',
hostname: 'scanner.tradingview.com',
path: `/${type}/scan`,
@@ -136,7 +138,7 @@ module.exports = {
* @returns {Promise<SearchMarketResult[]>} Search results
*/
async searchMarket(search, filter = '') {
- const data = await request({
+ const { data } = await request({
host: 'symbol-search.tradingview.com',
path: `/symbol_search/?text=${search.replace(/ /g, '%20')}&type=${filter}`,
origin: 'https://www.tradingview.com',
@@ -187,14 +189,14 @@ module.exports = {
async searchIndicator(search = '') {
if (!builtInIndicList.length) {
await Promise.all(['standard', 'candlestick', 'fundamental'].map(async (type) => {
- builtInIndicList.push(...await request({
+ builtInIndicList.push(...(await request({
host: 'pine-facade.tradingview.com',
path: `/pine-facade/list/?filter=${type}`,
- }));
+ })).data);
}));
}
- const data = await request({
+ const { data } = await request({
host: 'www.tradingview.com',
path: `/pubscripts-suggest-json/?search=${search.replace(/ /g, '%20')}`,
});
@@ -253,7 +255,7 @@ module.exports = {
async getIndicator(id, version = 'last') {
const indicID = id.replace(/ |%/g, '%25');
- let data = await request({
+ let { data } = await request({
host: 'pine-facade.tradingview.com',
path: `/pine-facade/translate/${indicID}/${version}`,
}, true);
@@ -344,7 +346,55 @@ module.exports = {
*/
/**
- * Get an user from a 'sessionid' cookie
+ * Get user and sessionid from username/email and password
+ * @function loginUser
+ * @param {string} username User username/email
+ * @param {string} password User password
+ * @param {boolean} [remember] Remember the session (default: false)
+ * @param {string} [UA] Custom UserAgent
+ * @returns {Promise<User>} Token
+ */
+ async loginUser(username, password, remember = true, UA = 'TWAPI/3.0') {
+ const formData = new FormData();
+ formData.append('username', username);
+ formData.append('password', password);
+ if (remember) formData.append('remember', 'on');
+
+ const { data, cookies } = await request({
+ method: 'POST',
+ host: 'www.tradingview.com',
+ path: '/accounts/signin/',
+ headers: {
+ referer: 'https://www.tradingview.com',
+ 'Content-Type': `multipart/form-data; boundary=${formData.boundary}`,
+ 'User-agent': `${UA} (${os.version()}; ${os.platform()}; ${os.arch()})`,
+ },
+ }, false, formData.toString());
+
+ if (data.error) throw new Error(data.error);
+
+ const cookie = cookies.find((c) => c.includes('sessionid='));
+ const session = (cookie.match(/sessionid=(.*?);/) ?? [])[1];
+
+ return {
+ id: data.user.id,
+ username: data.user.username,
+ firstName: data.user.first_name,
+ lastName: data.user.last_name,
+ reputation: data.user.reputation,
+ following: data.user.following,
+ followers: data.user.followers,
+ notifications: data.user.notification_count,
+ session,
+ sessionHash: data.user.session_hash,
+ privateChannel: data.user.private_channel,
+ authToken: data.user.auth_token,
+ joinDate: new Date(data.user.date_joined),
+ };
+ },
+
+ /**
+ * Get user from 'sessionid' cookie
* @function getUser
* @param {string} session User 'sessionid' cookie
* @param {string} [location] Auth page location (For france: https://fr.tradingview.com/)
@@ -455,7 +505,7 @@ module.exports = {
const userID = creds ? credentials.id : -1;
const session = creds ? credentials.session : null;
- const data = await request({
+ const { data } = await request({
host: 'www.tradingview.com',
path: `/chart-token/?image_url=${layout}&user_id=${userID}`,
headers: { cookie: session ? `sessionid=${session}` : '' },
@@ -502,7 +552,7 @@ module.exports = {
const creds = credentials.id && credentials.session;
const session = creds ? credentials.session : null;
- const data = await request({
+ const { data } = await request({
host: 'charts-storage.tradingview.com',
path: `/charts-storage/layout/${layout}/sources?chart_id=${chartID
}&jwt=${chartToken}${symbol ? `&symbol=${symbol}` : ''}`,
diff --git a/src/request.js b/src/request.js
index 9452090..edadd0a 100644
--- a/src/request.js
+++ b/src/request.js
@@ -4,7 +4,7 @@ const https = require('https');
* @param {https.RequestOptions} options HTTPS Request options
* @param {boolean} [raw] Get raw or JSON data
* @param {string} [content] Request body content
- * @returns {Promise<string | object | array>} Result
+ * @returns {Promise<{ data: (string | object | array), cookies: string[] }>} Result
*/
function request(options = {}, raw = false, content = '') {
return new Promise((cb, err) => {
@@ -13,7 +13,7 @@ function request(options = {}, raw = false, content = '') {
res.on('data', (c) => { data += c; });
res.on('end', () => {
if (raw) {
- cb(data);
+ cb({ data, cookies: res.headers.cookie });
return;
}
@@ -25,7 +25,7 @@ function request(options = {}, raw = false, content = '') {
return;
}
- cb(data);
+ cb({ data, cookies: res.headers['set-cookie'] });
});
});