-
Notifications
You must be signed in to change notification settings - Fork 81
/
nse_lib.js
61 lines (55 loc) · 2.06 KB
/
nse_lib.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const {
exec,
spawn
} = require('child_process');
function execute(command, callback) {
exec(command, {
maxBuffer: 1024 * 1000 * 10
}, function (error, stdout, stderr) {
callback(stdout);
});
};
function isJson(text) {
try {
return JSON.parse(text);
} catch (err) {
return false;
}
}
function loadCookies() {
return new Promise((resolve, reject) => {
execute(`curl 'https://www.nseindia.com/' \
-H 'authority: www.nseindia.com' \
-H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' \
-H 'accept-encoding: gzip, deflate, br' \
-H 'accept-language: en-IN,en;q=0.9,en-GB;q=0.8,en-US;q=0.7,hi;q=0.6,mr;q=0.5' \
-H 'cache-control: no-cache' \
-H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36' \
--compressed --silent --output /dev/null --cookie-jar cookie.txt`, function (resp) {
// console.log(resp);
resolve();
});
});
}
function getOptionChain(instrument) {
return new Promise((resolve, reject) => {
execute(`curl 'https://www.nseindia.com/api/option-chain-indices?symbol=${instrument}' \
-H 'authority: www.nseindia.com' \
-H 'dnt: 1' \
-H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' \
-H 'accept-encoding: gzip, deflate, br' \
-H 'accept-language: en-IN,en;q=0.9,en-GB;q=0.8,en-US;q=0.7,hi;q=0.6,mr;q=0.5' \
-H 'cache-control: no-cache' \
-H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36' \
--compressed --cookie cookie.txt --cookie-jar cookie.txt`, function (resp) {
// console.log(resp);
let isValidData = isJson(resp);
if (isValidData) {
resolve(isValidData);
} else {
resolve();
}
});
});
}
module.exports = {getOptionChain, loadCookies};