-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
101 lines (94 loc) · 3.22 KB
/
index.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const https = require('https');
const RANGES = ['1h', '1d', '5d', '1mo', '1y', 'max'];
const get = url =>
new Promise((resolve, reject) => {
const req = https.get(url, res => {
let data = '';
res.on('data', d => {
data += d;
});
res.on('end', () => {
resolve(data);
});
});
req.on('error', e => {
reject(e);
});
});
const getJson = url =>
new Promise((resolve, reject) => {
get(url)
.then(resp => JSON.parse(resp))
.then(resolve)
.catch(reject);
});
const lookup = symbol =>
new Promise((resolve, reject) => {
Promise.all([
getJson(
`https://autoc.finance.yahoo.com/autoc?query=${symbol}®ion=1&lang=en`
),
getJson(
`https://query1.finance.yahoo.com/v10/finance/quoteSummary/${symbol}?&modules=summaryProfile,financialData`
),
])
.then(responses => {
if (!responses[0].ResultSet.Result.length) {
reject(true);
return;
}
const financialData =
responses[1].quoteSummary.result[0].financialData;
resolve({
symbol,
name: responses[0].ResultSet.Result[0].name,
exchange: responses[0].ResultSet.Result[0].exchDisp,
currentPrice: financialData.currentPrice.raw,
highPrice: financialData.targetHighPrice.raw,
lowPrice: financialData.targetLowPrice.raw,
meanPrice: financialData.targetMeanPrice.raw,
medianPrice: financialData.targetMedianPrice.raw,
});
})
.catch(reject);
});
const history = (symbol, args) =>
new Promise((resolve, reject) => {
let options = {
range: '1wk',
interval: '1d',
...args,
};
getJson(
`https://query2.finance.yahoo.com/v7/finance/chart/${symbol}?range=${
options.range
}&interval=${
options.interval
}&indicators=quote&includeTimestamps=true&includePrePost=true&events=div%7Csplit%7Cearn`
)
.then(response => {
const quote = response.chart.result[0].indicators.quote[0];
const meta = response.chart.result[0].meta;
const h = response.chart.result[0].timestamp.map(
(time, idx) => {
return {
time,
close: quote.close[idx],
open: quote.open[idx],
high: quote.high[idx],
low: quote.low[idx],
volume: quote.volume[idx],
};
}
);
resolve({
previousClose: meta.chartPreviousClose,
records: h,
});
})
.catch(reject);
});
module.exports = {
lookup,
history,
};