Skip to content

Commit 3c4c1c2

Browse files
committed
Implements genAuthCookies
1 parent ff56269 commit 3c4c1c2

3 files changed

Lines changed: 16 additions & 10 deletions

File tree

src/classes/PinePermManager.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const axios = require('axios');
2+
const { genAuthCookies } = require('../utils');
23

34
/**
45
* @typedef {Object} AuthorizationUser
@@ -50,7 +51,7 @@ class PinePermManager {
5051
headers: {
5152
origin: 'https://www.tradingview.com',
5253
'Content-Type': 'application/x-www-form-urlencoded',
53-
cookie: `sessionid=${this.sessionId};sessionid_sign=${this.signature};`,
54+
cookie: genAuthCookies(this.sessionId, this.signature),
5455
},
5556
},
5657
);
@@ -84,7 +85,7 @@ class PinePermManager {
8485
headers: {
8586
origin: 'https://www.tradingview.com',
8687
'Content-Type': 'application/x-www-form-urlencoded',
87-
cookie: `sessionid=${this.sessionId};sessionid_sign=${this.signature};`,
88+
cookie: genAuthCookies(this.sessionId, this.signature),
8889
},
8990
},
9091
);
@@ -118,7 +119,7 @@ class PinePermManager {
118119
headers: {
119120
origin: 'https://www.tradingview.com',
120121
'Content-Type': 'application/x-www-form-urlencoded',
121-
cookie: `sessionid=${this.sessionId};sessionid_sign=${this.signature};`,
122+
cookie: genAuthCookies(this.sessionId, this.signature),
122123
},
123124
},
124125
);
@@ -143,7 +144,7 @@ class PinePermManager {
143144
headers: {
144145
origin: 'https://www.tradingview.com',
145146
'Content-Type': 'application/x-www-form-urlencoded',
146-
cookie: `sessionid=${this.sessionId};sessionid_sign=${this.signature};`,
147+
cookie: genAuthCookies(this.sessionId, this.signature),
147148
},
148149
},
149150
);

src/miscRequests.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const os = require('os');
22
const axios = require('axios');
33

44
const PineIndicator = require('./classes/PineIndicator');
5+
const { genAuthCookies } = require('./utils');
56

67
const validateStatus = (status) => status < 500;
78

@@ -279,7 +280,7 @@ module.exports = {
279280
`https://pine-facade.tradingview.com/pine-facade/translate/${indicID}/${version}`,
280281
{
281282
headers: {
282-
cookie: `${session ? `sessionid=${session};` : ''}${signature ? `sessionid_sign=${signature};` : ''}`,
283+
cookie: genAuthCookies(session, signature),
283284
},
284285
validateStatus,
285286
},
@@ -429,7 +430,7 @@ module.exports = {
429430
async getUser(session, signature = '', location = 'https://www.tradingview.com/') {
430431
const { data } = await axios.get(location, {
431432
headers: {
432-
cookie: `sessionid=${session}${signature ? `;sessionid_sign=${signature};` : ''}`,
433+
cookie: genAuthCookies(session, signature),
433434
},
434435
validateStatus,
435436
});
@@ -471,7 +472,7 @@ module.exports = {
471472
'https://pine-facade.tradingview.com/pine-facade/list',
472473
{
473474
headers: {
474-
cookie: `sessionid=${session}${signature ? `;sessionid_sign=${signature};` : ''}`,
475+
cookie: genAuthCookies(session, signature),
475476
},
476477
params: {
477478
filter: 'saved',
@@ -529,9 +530,7 @@ module.exports = {
529530
'https://www.tradingview.com/chart-token',
530531
{
531532
headers: {
532-
cookie: session
533-
? `sessionid=${session}${signature ? `;sessionid_sign=${signature};` : ''}`
534-
: '',
533+
cookie: genAuthCookies(session, signature),
535534
},
536535
params: {
537536
image_url: layout,

src/utils.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ module.exports = {
1111
for (let i = 0; i < 12; i += 1) r += c.charAt(Math.floor(Math.random() * c.length));
1212
return `${type}_${r}`;
1313
},
14+
15+
genAuthCookies(sessionId = '', signature = '') {
16+
if (!sessionId) return '';
17+
if (!signature) return `sessionid=${sessionId}`;
18+
return `sessionid=${sessionId};sessionid_sign=${signature}`;
19+
},
1420
};

0 commit comments

Comments
 (0)