-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfinance.js
184 lines (173 loc) · 5.15 KB
/
finance.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import axios from 'axios';
// import rssParser from 'react-native-rss-parser';
let Parser = require('rss-parser');
let rssParser = new Parser();
let cancelToken;
// below will move to api folder
export const getStock = (opts, type) => {
const defs = {
baseURL: 'https://query.yahooapis.com/v1/public/yql?q=',
query: {
quotes: 'select * from yahoo.finance.quotes where symbol in ("{stock}")',
historicaldata: 'select * from yahoo.finance.historicaldata where symbol = "{stock}" and startDate = "{startDate}" and endDate = "{endDate}"',
},
suffixURL: {
quotes: '&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys',
historicaldata: '&format=json&diagnostics=true&env=store://datatables.org/alltableswithkeys',
},
};
opts = opts || {};
if (!opts.stock) {
console.log('No stock defined');
return;
}
if (opts.stock instanceof Array) {
opts.stock = opts.stock.join("', '");
}
console.log(opts.stock);
const query = defs.query[type]
.replace('{stock}', opts.stock)
.replace('{startDate}', opts.startDate)
.replace('{endDate}', opts.endDate);
const url = defs.baseURL + query + (defs.suffixURL[type] || '');
console.log(url);
return fetch(url); // eslint-disable-line no-undef
};
export const properties = [
'AfterHoursChangeRealtime',
'AnnualizedGain',
'Ask',
'AskRealtime',
'AverageDailyVolume',
'Bid',
'BidRealtime',
'BookValue',
'Change',
'ChangeFromFiftydayMovingAverage',
'ChangeFromTwoHundreddayMovingAverage',
'ChangeFromYearHigh',
'ChangeFromYearLow',
'ChangePercentRealtime',
'ChangeRealtime',
'Change_PercentChange',
'ChangeinPercent',
'Commission',
'Currency',
'DaysHigh',
'DaysLow',
'DaysRange',
'DaysRangeRealtime',
'DaysValueChange',
'DaysValueChangeRealtime',
'DividendPayDate',
'DividendShare',
'DividendYield',
'EBITDA',
'EPSEstimateCurrentYear',
'EPSEstimateNextQuarter',
'EPSEstimateNextYear',
'EarningsShare',
'ErrorIndicationreturnedforsymbolchangedinvalid',
'ExDividendDate',
'FiftydayMovingAverage',
'HighLimit',
'HoldingsGain',
'HoldingsGainPercent',
'HoldingsGainPercentRealtime',
'HoldingsGainRealtime',
'HoldingsValue',
'HoldingsValueRealtime',
'LastTradeDate',
'LastTradePriceOnly',
'LastTradeRealtimeWithTime',
'LastTradeTime',
'LastTradeWithTime',
'LowLimit',
'MarketCapRealtime',
'MarketCapitalization',
'MoreInfo',
'Name',
'Notes',
'OneyrTargetPrice',
'Open',
'OrderBookRealtime',
'PEGRatio',
'PERatio',
'PERatioRealtime',
'PercebtChangeFromYearHigh',
'PercentChange',
'PercentChangeFromFiftydayMovingAverage',
'PercentChangeFromTwoHundreddayMovingAverage',
'PercentChangeFromYearLow',
'PreviousClose',
'PriceBook',
'PriceEPSEstimateCurrentYear',
'PriceEPSEstimateNextYear',
'PricePaid',
'PriceSales',
'SharesOwned',
'ShortRatio',
'StockExchange',
'Symbol',
'TickerTrend',
'TradeDate',
'TwoHundreddayMovingAverage',
'Volume',
'YearHigh',
'YearLow',
'YearRange',
];
export const getNews = async (symbol) => {
if (typeof cancelToken != typeof undefined) {
cancelToken.cancel("Operation canceled due to new request.");
}
cancelToken = axios.CancelToken.source();
console.log('symbol => ',symbol);
const url = `https://feeds.finance.yahoo.com/rss/2.0/headline?s=${symbol}®ion=US&lang=en-US`;
console.log(url);
try {
const res = await axios.get(url, {
cancelToken: cancelToken.token,
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,HEAD,OPTIONS,POST,PUT"
},
});
if(res?.data) {
console.log(res?.data)
return [];
// const news = await rssParser.parse(res.data);
// return news?.items;
}
return []; // eslint-disable-line no-undef
} catch (error) {
console.log(error);
return [];
}
};
export const getNews = async (query) => {
//Check if there are any previous pending requests
if (typeof cancelToken != typeof undefined) {
cancelToken.cancel("Operation canceled due to new request.");
}
//Save the cancel token for the current request
cancelToken = axios.CancelToken.source();
const url = `http://d.yimg.com/aq/autoc?query=${query}®ion=US&lang=en-US&callback=YAHOO.util.ScriptNodeDataSource.callbacks`;
// console.log(url);
try {
const res = await axios.get(url, { cancelToken: cancelToken.token });
if(res?.data) {
const results = res?.data?.replace(
/(YAHOO\.util\.ScriptNodeDataSource\.callbacks\()(.*)(\);)/g,
'$2',
);
return JSON.parse(results)?.ResultSet?.Result;
}
return []; // eslint-disable-line no-undef
} catch (error) {
console.log(error);
return [];
}
};