* Get user from 'sessionid' cookie * @function getUser * @param {string} session User 'sessionid' cookie * @param {string} [signature] User 'sessionid_sign' cookie * @param {string} [location] Auth page location (For france: https://fr.tradingview.com/) * @returns {Promise } Toke
(session, signature = '', location = 'https://www.tradingview.com/')
| 430 | * @returns {Promise<User>} Token |
| 431 | */ |
| 432 | async getUser(session, signature = '', location = 'https://www.tradingview.com/') { |
| 433 | const { data, headers } = await axios.get(location, { |
| 434 | headers: { |
| 435 | cookie: genAuthCookies(session, signature), |
| 436 | }, |
| 437 | maxRedirects: 0, |
| 438 | validateStatus, |
| 439 | }); |
| 440 | |
| 441 | if (data.includes('auth_token')) { |
| 442 | return { |
| 443 | id: /"id":([0-9]{1,10}),/.exec(data)?.[1], |
| 444 | username: /"username":"(.*?)"/.exec(data)?.[1], |
| 445 | firstName: /"first_name":"(.*?)"/.exec(data)?.[1], |
| 446 | lastName: /"last_name":"(.*?)"/.exec(data)?.[1], |
| 447 | reputation: parseFloat(/"reputation":(.*?),/.exec(data)?.[1] || 0), |
| 448 | following: parseFloat(/,"following":([0-9]*?),/.exec(data)?.[1] || 0), |
| 449 | followers: parseFloat(/,"followers":([0-9]*?),/.exec(data)?.[1] || 0), |
| 450 | notifications: { |
| 451 | following: parseFloat(/"notification_count":\{"following":([0-9]*),/.exec(data)?.[1] ?? 0), |
| 452 | user: parseFloat(/"notification_count":\{"following":[0-9]*,"user":([0-9]*)/.exec(data)?.[1] ?? 0), |
| 453 | }, |
| 454 | session, |
| 455 | signature, |
| 456 | sessionHash: /"session_hash":"(.*?)"/.exec(data)?.[1], |
| 457 | privateChannel: /"private_channel":"(.*?)"/.exec(data)?.[1], |
| 458 | authToken: /"auth_token":"(.*?)"/.exec(data)?.[1], |
| 459 | joinDate: new Date(/"date_joined":"(.*?)"/.exec(data)?.[1] || 0), |
| 460 | }; |
| 461 | } |
| 462 | |
| 463 | if (headers.location !== location) { |
| 464 | return this.getUser(session, signature, headers.location); |
| 465 | } |
| 466 | |
| 467 | throw new Error('Wrong or expired sessionid/signature'); |
| 468 | }, |
| 469 | |
| 470 | /** |
| 471 | * Get user's private indicators from a 'sessionid' cookie |
nothing calls this directly
no test coverage detected