43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
import YahooFinance from 'yahoo-finance2';
|
|
|
|
export class YahooClient {
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
private yf: any;
|
|
|
|
constructor() {
|
|
this.yf = new (YahooFinance as unknown as new (opts: object) => unknown)({
|
|
suppressNotices: ['yahooSurvey'],
|
|
});
|
|
}
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
async fetchSummary(ticker: string, retries = 3, backoff = 1000): Promise<any> {
|
|
for (let i = 0; i < retries; i++) {
|
|
try {
|
|
return await (this.yf as any).quoteSummary(ticker, {
|
|
modules: [
|
|
'assetProfile',
|
|
'financialData',
|
|
'defaultKeyStatistics',
|
|
'price',
|
|
'summaryDetail',
|
|
],
|
|
});
|
|
} catch (error) {
|
|
if (i === retries - 1) throw error;
|
|
await new Promise<void>((res) => setTimeout(res, backoff * (i + 1)));
|
|
}
|
|
}
|
|
}
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
async fetchCalendarEvents(ticker: string): Promise<any | null> {
|
|
try {
|
|
const r = await (this.yf as any).quoteSummary(ticker, { modules: ['calendarEvents'] });
|
|
return r.calendarEvents ?? null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
}
|