Skip to content

Commit

Permalink
[fix] dividends and utc dates (#140)
Browse files Browse the repository at this point in the history
* filtering out Dividend rows

* dates are now UTC at 00:00 instead of subject to timezone

* minor version bump as dates are now UTC - so API change
  • Loading branch information
colthreepv authored Aug 21, 2023
1 parent 5230b73 commit 5661ff6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yahoo-stock-api",
"version": "2.1.2",
"version": "2.2.0",
"description": "Simple package to get stock price from yahoo finance",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
26 changes: 16 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,16 @@ class YahooStockAPI {
currency = currency ? currency.split('.')[1].replace('Currency in', '').trim() : undefined;
const name: string = this.getTidyName($('h1').text());
const response = $('#Col1-1-HistoricalDataTable-Proxy > section > div:nth-child(2) > table > tbody > tr').map(this.getHistoricalPricesMapRows).get();
// filter out null values (dividend pays)
const columns = ['date', 'open', 'high', 'low', 'close', 'adjClose', 'volume'];
const filteredResponse = response.filter((obj) => {
return !columns.some((column) => obj[column] == null);
});
return {
error: false,
currency: currency || undefined,
name: name || undefined,
response,
response: filteredResponse,
};
}
catch(err) {
Expand Down Expand Up @@ -76,18 +81,19 @@ class YahooStockAPI {
private getHistoricalPricesMapRows(_, row): HistoricalPricesResponse {
const obj = { date: null, open: null, high: null, low: null, close: null, adjClose: null, volume: null };
const columns = ['date', 'open', 'high', 'low', 'close', 'adjClose', 'volume'];
cheerio.load(row)('td').map((index, cell: any) => {
cell = cheerio.load(cell);
const $row = cheerio.load(row);

$row('td').each((index, cell: any) => {
const text = $row(cell).text();
const selector = columns[index];
switch(index) {
case 0:
obj[selector] = new Date(cell.text()).getTime() / 1000;
break;
default:
obj[selector] = numeral(cell.text()).value();
break;

if (index === 0) {
obj[selector] = new Date(`${text} 00:00:00 +0000`).getTime() / 1000;
} else {
obj[selector] = numeral(text).value();
}
});

return obj;
}

Expand Down

0 comments on commit 5661ff6

Please sign in to comment.