Skip to content

πŸ“ˆ Get real-time stocks from TradingView

Add pine permission manager

Mathieu Colmond1a1cfd08f3f
diff --git a/examples/PinePermManage.js b/examples/PinePermManage.js
new file mode 100644
index 0000000..29db2aa
--- /dev/null
+++ b/examples/PinePermManage.js
@@ -0,0 +1,57 @@
+const { PinePermManager } = require('../main');
+
+/*
+ This example creates a pine
+ permission manager and tests
+ all the available functions
+*/
+
+const sessionid = process.argv[2];
+const pineid = process.argv[3];
+
+const manager = new PinePermManager(sessionid, pineid);
+
+(async () => {
+ console.log('Users:', await manager.getUsers());
+
+ console.log('Adding user \'TradingView\'...');
+
+ switch (await manager.addUser('TradingView')) {
+ case 'ok':
+ console.log('Done !');
+ break;
+ case 'exists':
+ console.log('This user is already authorized');
+ break;
+ default:
+ console.error('Unknown error...');
+ break;
+ }
+
+ console.log('Users:', await manager.getUsers());
+
+ console.log('Modifying expiration date...');
+
+ const newDate = new Date(Date.now() + 24 * 3600000); // Add one day
+ if (await manager.modifyExpiration('TradingView', newDate) === 'ok') {
+ console.log('Done !');
+ } else console.error('Unknown error...');
+
+ console.log('Users:', await manager.getUsers());
+
+ console.log('Removing expiration date...');
+
+ if (await manager.modifyExpiration('TradingView') === 'ok') {
+ console.log('Done !');
+ } else console.error('Unknown error...');
+
+ console.log('Users:', await manager.getUsers());
+
+ console.log('Removing user \'TradingView\'...');
+
+ if (await manager.removeUser('TradingView') === 'ok') {
+ console.log('Done !');
+ } else console.error('Unknown error...');
+
+ console.log('Users:', await manager.getUsers());
+})();
diff --git a/main.js b/main.js
index 2f0397e..721ab7a 100644
--- a/main.js
+++ b/main.js
@@ -2,8 +2,10 @@ const miscRequests = require('./src/miscRequests');
const Client = require('./src/client');
const BuiltInIndicator = require('./src/classes/BuiltInIndicator');
const PineIndicator = require('./src/classes/PineIndicator');
+const PinePermManager = require('./src/classes/PinePermManager');
module.exports = { ...miscRequests };
module.exports.Client = Client;
module.exports.BuiltInIndicator = BuiltInIndicator;
module.exports.PineIndicator = PineIndicator;
+module.exports.PinePermManager = PinePermManager;
diff --git a/src/FormData.js b/src/FormData.js
new file mode 100644
index 0000000..560792c
--- /dev/null
+++ b/src/FormData.js
@@ -0,0 +1,35 @@
+class FormData {
+ #string = '';
+
+ #boundary = '';
+
+ get boundary() {
+ return this.#boundary;
+ }
+
+ constructor() {
+ const random = (Math.random() * 10 ** 20).toString(36);
+ this.#boundary = `${random}`;
+ this.#string = `--${this.boundary}`;
+ }
+
+ /**
+ * Adds a property to the FormData object
+ * @param {string} key Property key
+ * @param {string} value Property value
+ */
+ append(key, value) {
+ this.#string += `\r\nContent-Disposition: form-data; name="${key}"`;
+ this.#string += `\r\n\r\n${value}`;
+ this.#string += `\r\n--${this.boundary}`;
+ }
+
+ /**
+ * @returns {string}
+ */
+ toString() {
+ return `${this.#string}--`;
+ }
+}
+
+module.exports = FormData;
diff --git a/src/classes/PinePermManager.js b/src/classes/PinePermManager.js
new file mode 100644
index 0000000..ac7bf07
--- /dev/null
+++ b/src/classes/PinePermManager.js
@@ -0,0 +1,142 @@
+const request = require('../request');
+const FormData = require('../FormData');
+
+/**
+ * @typedef {Object} AuthorizationUser
+ * @prop {id} id User id
+ * @prop {string} username User's username
+ * @prop {string} userpic User's profile picture URL
+ * @prop {string} expiration Authorization expiration date
+ * @prop {string} created Authorization creation date
+ */
+
+class PinePermManager {
+ sessionId;
+
+ pineId;
+
+ /**
+ * Creates a PinePermManager instance
+ * @param {string} sessionId Token from sessionid cookie
+ * @param {string} pineId Indicator ID (Like: PUB;XXXXXXXXXXXXXXXXXXXXX)
+ */
+ constructor(sessionId, pineId) {
+ if (!sessionId) throw new Error('Please provide a SessionID');
+ if (!pineId) throw new Error('Please provide a PineID');
+ this.pineId = pineId;
+ this.sessionId = sessionId;
+ }
+
+ /**
+ * Get list of authorized users
+ * @param {number} limit Fetching limit
+ * @param {'user__username'
+ * | '-user__username'
+ * | 'created' | 'created'
+ * | 'expiration,user__username'
+ * | '-expiration,user__username'
+ * } order Fetching order
+ * @returns {AuthorizationUser[]}
+ */
+ async getUsers(limit = 10, order = '-created') {
+ const data = await request({
+ method: 'POST',
+ host: 'www.tradingview.com',
+ path: `/pine_perm/list_users/?limit=${limit}&order_by=${order}`,
+ headers: {
+ origin: 'https://www.tradingview.com',
+ 'Content-Type': 'application/x-www-form-urlencoded',
+ cookie: `sessionid=${this.sessionId}`,
+ },
+ }, false, `pine_id=${this.pineId.replace(/;/g, '%3B')}`);
+
+ if (!data.results) throw new Error('Wrong sessionId or pineId');
+
+ return data.results;
+ }
+
+ /**
+ * Adds an user to the authorized list
+ * @param {string} username User's username
+ * @param {Date} [expiration] Expiration date
+ * @returns {'ok' | 'exists' | null}
+ */
+ async addUser(username, expiration = null) {
+ const formData = new FormData();
+ formData.append('pine_id', this.pineId);
+ formData.append('username_recip', username);
+ if (expiration && expiration instanceof Date) {
+ formData.append('expiration', expiration.toString());
+ }
+
+ const data = await request({
+ method: 'POST',
+ host: 'www.tradingview.com',
+ path: '/pine_perm/add/',
+ headers: {
+ origin: 'https://www.tradingview.com',
+ 'Content-Type': `multipart/form-data; boundary=${formData.boundary}`,
+ cookie: `sessionid=${this.sessionId}`,
+ },
+ }, false, formData.toString());
+
+ if (!data.status) throw new Error('Wrong sessionId or pineId');
+ return data.status;
+ }
+
+ /**
+ * Modify an authorization expiration date
+ * @param {string} username User's username
+ * @param {Date} [expiration] New expiration date
+ * @returns {'ok' | null}
+ */
+ async modifyExpiration(username, expiration = null) {
+ const formData = new FormData();
+ formData.append('pine_id', this.pineId);
+ formData.append('username_recip', username);
+ if (expiration && expiration instanceof Date) {
+ formData.append('expiration', expiration.toISOString());
+ }
+
+ const data = await request({
+ method: 'POST',
+ host: 'www.tradingview.com',
+ path: '/pine_perm/modify_user_expiration/',
+ headers: {
+ origin: 'https://www.tradingview.com',
+ 'Content-Type': `multipart/form-data; boundary=${formData.boundary}`,
+ cookie: `sessionid=${this.sessionId}`,
+ },
+ }, false, formData.toString());
+
+ if (!data.status) throw new Error('Wrong sessionId or pineId');
+ return data.status;
+ }
+
+ /**
+ * Removes an user to the authorized list
+ * @param {string} username User's username
+ * @returns {'ok' | null}
+ */
+ async removeUser(username) {
+ const formData = new FormData();
+ formData.append('pine_id', this.pineId);
+ formData.append('username_recip', username);
+
+ const data = await request({
+ method: 'POST',
+ host: 'www.tradingview.com',
+ path: '/pine_perm/remove/',
+ headers: {
+ origin: 'https://www.tradingview.com',
+ 'Content-Type': `multipart/form-data; boundary=${formData.boundary}`,
+ cookie: `sessionid=${this.sessionId}`,
+ },
+ }, false, formData.toString());
+
+ if (!data.status) throw new Error('Wrong sessionId or pineId');
+ return data.status;
+ }
+}
+
+module.exports = PinePermManager;
diff --git a/src/miscRequests.js b/src/miscRequests.js
index 5f18c2c..e0a7885 100644
--- a/src/miscRequests.js
+++ b/src/miscRequests.js
@@ -1,43 +1,12 @@
const https = require('https');
+const request = require('./request');
+const PinePermManager = require('./classes/PinePermManager');
const PineIndicator = require('./classes/PineIndicator');
const indicators = ['Recommend.Other', 'Recommend.All', 'Recommend.MA'];
const builtInIndicList = [];
-/**
- * @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
- */
-function request(options = {}, raw = false, content = '') {
- return new Promise((cb, err) => {
- const req = https.request(options, (res) => {
- let data = '';
- res.on('data', (c) => { data += c; });
- res.on('end', () => {
- if (raw) {
- cb(data);
- return;
- }
-
- try {
- data = JSON.parse(data);
- } catch (error) {
- err(new Error('Can\'t parse server response'));
- return;
- }
-
- cb(data);
- });
- });
-
- req.on('error', err);
- req.end(content);
- });
-}
-
async function fetchScanData(tickers = [], type = '', columns = []) {
let data = await request({
method: 'POST',
@@ -437,6 +406,9 @@ module.exports = {
get() {
return module.exports.getIndicator(ind.scriptIdPart, ind.version);
},
+ getManager() {
+ return new PinePermManager(ind.scriptIdPart);
+ },
})));
});
diff --git a/src/request.js b/src/request.js
new file mode 100644
index 0000000..9452090
--- /dev/null
+++ b/src/request.js
@@ -0,0 +1,37 @@
+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
+ */
+function request(options = {}, raw = false, content = '') {
+ return new Promise((cb, err) => {
+ const req = https.request(options, (res) => {
+ let data = '';
+ res.on('data', (c) => { data += c; });
+ res.on('end', () => {
+ if (raw) {
+ cb(data);
+ return;
+ }
+
+ try {
+ data = JSON.parse(data);
+ } catch (error) {
+ console.log(data);
+ err(new Error('Can\'t parse server response'));
+ return;
+ }
+
+ cb(data);
+ });
+ });
+
+ req.on('error', err);
+ req.end(content);
+ });
+}
+
+module.exports = request;