UNPKG

@pratiksha90/financial-data-extractors

Version:

Utilities for extracting financial data from various economic calendar websites

97 lines (96 loc) 4.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.isMarketWatchCalendar = isMarketWatchCalendar; exports.extractMarketWatchData = extractMarketWatchData; /** * Check if the current page is MarketWatch calendar */ function isMarketWatchCalendar(url) { return url.includes('marketwatch.com/economy-politics/calendar'); } /** * Extract MarketWatch calendar data */ function extractMarketWatchData(options) { const { document } = options; const url = document.URL || ''; const events = []; try { // MarketWatch has multiple tables, one for each date const calendarTables = document.querySelectorAll('.element--tableblock table'); // Process each table calendarTables.forEach(table => { var _a, _b; // Get the date from the table caption or nearest heading let tableDate = ''; const caption = table.querySelector('caption'); if (caption) { tableDate = ((_a = caption.textContent) === null || _a === void 0 ? void 0 : _a.trim()) || ''; } else { // Look for a heading before the table let prevElement = table.previousElementSibling; while (prevElement) { if (prevElement.tagName.match(/^H[1-6]$/)) { tableDate = ((_b = prevElement.textContent) === null || _b === void 0 ? void 0 : _b.trim()) || ''; break; } prevElement = prevElement.previousElementSibling; } } // Get the rows const rows = table.querySelectorAll('tbody tr'); // Process each row rows.forEach(row => { var _a, _b, _c, _d, _e; const cells = row.querySelectorAll('td'); if (cells.length < 3) return; // Skip rows with few cells // Extract data const timeCell = cells[0]; const time = timeCell ? ((_a = timeCell.textContent) === null || _a === void 0 ? void 0 : _a.trim()) || '' : ''; const eventCell = cells[1]; const eventName = eventCell ? ((_b = eventCell.textContent) === null || _b === void 0 ? void 0 : _b.trim()) || '' : ''; // The rest of the cells depend on the table structure // For MarketWatch, the columns could be Actual, Forecast, Prior let actual = '', forecast = '', previous = ''; if (cells.length >= 3) actual = ((_c = cells[2].textContent) === null || _c === void 0 ? void 0 : _c.trim()) || ''; if (cells.length >= 4) forecast = ((_d = cells[3].textContent) === null || _d === void 0 ? void 0 : _d.trim()) || ''; if (cells.length >= 5) previous = ((_e = cells[4].textContent) === null || _e === void 0 ? void 0 : _e.trim()) || ''; if (eventName) { events.push({ date: tableDate, time: time, event: eventName, actual: actual, forecast: forecast, previous: previous }); } }); }); return { metadata: { title: "MarketWatch Economic Calendar", source: url, extractedAt: new Date().toISOString(), totalEvents: events.length }, events: events }; } catch (error) { return { metadata: { title: "MarketWatch Economic Calendar", source: url, extractedAt: new Date().toISOString(), error: "Failed to extract calendar data" }, events: [] }; } }