phase-2: extract shared utils
This commit is contained in:
committed by
saikiranvella
parent
5a4b4aa6d1
commit
d5cf3fc31f
@@ -0,0 +1,40 @@
|
||||
import YahooFinance from 'yahoo-finance2';
|
||||
|
||||
export class YahooClient {
|
||||
constructor() {
|
||||
// Instantiate the client as required by v3
|
||||
this.yf = new YahooFinance({
|
||||
suppressNotices: ['yahooSurvey'],
|
||||
});
|
||||
}
|
||||
|
||||
async fetchSummary(ticker, retries = 3, backoff = 1000) {
|
||||
for (let i = 0; i < retries; i++) {
|
||||
try {
|
||||
return await this.yf.quoteSummary(ticker, {
|
||||
modules: [
|
||||
'assetProfile',
|
||||
'financialData',
|
||||
'defaultKeyStatistics',
|
||||
'price',
|
||||
'summaryDetail',
|
||||
],
|
||||
});
|
||||
} catch (error) {
|
||||
if (i === retries - 1) throw error;
|
||||
await new Promise((res) => setTimeout(res, backoff * (i + 1)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fetches upcoming earnings dates, ex-dividend date, and dividend date for a ticker.
|
||||
// Returns null on failure so callers can skip gracefully.
|
||||
async fetchCalendarEvents(ticker) {
|
||||
try {
|
||||
const r = await this.yf.quoteSummary(ticker, { modules: ['calendarEvents'] });
|
||||
return r.calendarEvents ?? null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user