@pratiksha90/financial-data-extractors
Version:
Utilities for extracting financial data from various economic calendar websites
202 lines (201 loc) • 8.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isFXStreetCalendar = isFXStreetCalendar;
exports.extractFXStreetData = extractFXStreetData;
/**
* Check if the current page is FXStreet calendar
*/
function isFXStreetCalendar(url) {
return url.includes('fxstreet.com/economic-calendar');
}
/**
* Extract FXStreet calendar data
*/
function extractFXStreetData(options) {
const { document } = options;
const url = document.URL || '';
const events = [];
try {
// Get the calendar container
const calendarContainer = document.getElementById('economic-calendar-events');
if (!calendarContainer) {
console.error("Calendar container not found");
return {
metadata: {
title: "FXStreet Economic Calendar",
source: url,
extractedAt: new Date().toISOString(),
error: "Calendar container not found"
},
events: []
};
}
// Find the main calendar table
const calendarTable = calendarContainer.querySelector('table.fxs_c_table');
if (!calendarTable) {
console.error("Calendar table element not found");
return {
metadata: {
title: "FXStreet Economic Calendar",
source: url,
extractedAt: new Date().toISOString(),
error: "Calendar table not found"
},
events: []
};
}
// Track current date as we process events
let currentDate = '';
// Process all rows
const rows = calendarTable.querySelectorAll('tbody tr');
rows.forEach((row) => {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
try {
// Check if this is a date header row
const datePeriod = row.querySelector('td.fxs_c_period');
if (datePeriod) {
// Update current date
currentDate = ((_a = datePeriod.textContent) === null || _a === void 0 ? void 0 : _a.trim().replace(/^\\s*/, '')) || '';
return; // Skip processing this row as it's just a date header
}
// Check if this is a data row with event-date-id attribute
if (!row.hasAttribute('data-event-date-id')) {
return; // Skip rows without event data
}
// Extract event data
const cells = row.querySelectorAll('td.fxs_c_item');
if (cells.length < 5) {
return; // Skip rows with insufficient data
}
// Process cells to extract data
// Time
const timeCell = cells[0];
const timeText = timeCell ? ((_b = timeCell.textContent) === null || _b === void 0 ? void 0 : _b.trim()) || '' : '';
// Country
const countryCell = cells.length > 1 ? cells[1] : null;
let country = '';
if (countryCell) {
// Try to extract country from flag
const flagSpan = countryCell.querySelector('.fxs_flag');
if (flagSpan) {
country = flagSpan.getAttribute('title') || '';
}
}
// Currency
const currencyCell = cells.length > 2 ? cells[2] : null;
const currency = currencyCell ? ((_c = currencyCell.textContent) === null || _c === void 0 ? void 0 : _c.trim()) || '' : '';
// Event name
const nameCell = cells.length > 3 ? cells[3] : null;
let eventName = '';
let period = '';
if (nameCell) {
// Get event name, which might contain period in parentheses
const fullText = ((_d = nameCell.textContent) === null || _d === void 0 ? void 0 : _d.trim()) || '';
// Try to extract period from parentheses
const periodRegex = /\s*\((.*?)\)$/;
const periodMatch = fullText.match(periodRegex);
if (periodMatch && periodMatch[1]) {
period = periodMatch[1];
// Remove period from event name
eventName = fullText.replace(periodRegex, '').trim();
}
else {
eventName = fullText;
}
}
// Impact (importance)
const impactCell = cells.length > 4 ? cells[4] : null;
let impact = '';
if (impactCell) {
const impactIcon = impactCell.querySelector('.fxs_c_impact-icon');
if (impactIcon) {
if (impactIcon.classList.contains('fxs_c_impact-high')) {
impact = 'High';
}
else if (impactIcon.classList.contains('fxs_c_impact-medium')) {
impact = 'Medium';
}
else if (impactIcon.classList.contains('fxs_c_impact-low')) {
impact = 'Low';
}
}
}
// Values (Actual, Deviation, Consensus, Previous)
const actualCell = cells.length > 5 ? cells[5] : null;
const deviationCell = cells.length > 6 ? cells[6] : null;
const consensusCell = cells.length > 7 ? cells[7] : null;
const previousCell = cells.length > 8 ? cells[8] : null;
let actual = '';
let deviation = '';
let consensus = '';
let previous = '';
// Check if we have a "Speech" or similar type instead of values
const typeCell = actualCell && actualCell.getAttribute('colspan') === '4' ? actualCell : null;
let eventType = '';
if (typeCell) {
const typeSpan = typeCell.querySelector('.fxs_c_label');
if (typeSpan) {
eventType = ((_e = typeSpan.textContent) === null || _e === void 0 ? void 0 : _e.trim()) || '';
}
}
else {
// Extract numeric values
if (actualCell) {
actual = ((_f = actualCell.textContent) === null || _f === void 0 ? void 0 : _f.trim()) || '';
}
if (deviationCell) {
deviation = ((_g = deviationCell.textContent) === null || _g === void 0 ? void 0 : _g.trim()) || '';
}
if (consensusCell) {
consensus = ((_h = consensusCell.textContent) === null || _h === void 0 ? void 0 : _h.trim()) || '';
}
if (previousCell) {
previous = ((_j = previousCell.textContent) === null || _j === void 0 ? void 0 : _j.trim()) || '';
}
}
// Create event object
const event = {
id: row.getAttribute('data-event-date-id') || '',
date: currentDate,
time: timeText,
country: country,
currency: currency,
event: eventName,
period: period,
impact: impact,
type: eventType,
actual: actual,
deviation: deviation,
forecast: consensus,
previous: previous
};
// Add to events array
events.push(event);
}
catch (err) {
console.error(`Error processing row:`, err);
}
});
return {
metadata: {
title: "FXStreet Economic Calendar",
source: url,
extractedAt: new Date().toISOString(),
totalEvents: events.length
},
events: events
};
}
catch (error) {
console.error("Error extracting FXStreet data:", error);
return {
metadata: {
title: "FXStreet Economic Calendar",
source: url,
extractedAt: new Date().toISOString(),
error: "Failed to extract calendar data"
},
events: []
};
}
}