|
| 1 | +import * as cheerio from "cheerio"; |
| 2 | +import { EmbalsesMinoSil } from '../api/index.js'; |
| 3 | +import { mapStringToApiDate } from './helpers.js'; |
| 4 | + |
| 5 | +export function parseEuropeanNumber(value: string): number { |
| 6 | + if (!value || value.trim() === "" || value === "*" || value === "n/d") { |
| 7 | + return NaN; |
| 8 | + } |
| 9 | + |
| 10 | + // Replace comma with dot for decimal separator |
| 11 | + const normalizedValue = value.replace(",", "."); |
| 12 | + return parseFloat(normalizedValue); |
| 13 | +} |
| 14 | + |
| 15 | +export function extractProvinceTables( |
| 16 | + $: cheerio.CheerioAPI |
| 17 | +): EmbalsesMinoSil[] { |
| 18 | + |
| 19 | + let embalses: EmbalsesMinoSil[] = []; |
| 20 | + |
| 21 | + $("table.tabla tr").each((_index, row) => { |
| 22 | + const cells = $(row).find("td"); |
| 23 | + if (cells.length === 0) return; // Skip header rows with <th> |
| 24 | + |
| 25 | + const nombre = $(cells[1]).text().trim(); |
| 26 | + if (!nombre || !nombre.includes(" - ")) return; // Skip totals row |
| 27 | + const id = nombre.split(" - ")[0].trim().match(/^([A-Z])(\d+)$/)[2]; // Extract ID from name |
| 28 | + const capacidadTotal = $(cells[4]).text().trim(); |
| 29 | + const fecha = $(cells[8]).text().trim(); |
| 30 | + const volumenActual = $(cells[6]).text().trim(); |
| 31 | + |
| 32 | + const embalse: EmbalsesMinoSil = { |
| 33 | + id: Number(id), |
| 34 | + embalse: nombre.split(" - ")[1].trim(), |
| 35 | + capacidadTotalHm3: parseEuropeanNumber(capacidadTotal), |
| 36 | + volumenActualHm3: parseEuropeanNumber(volumenActual), |
| 37 | + fecha: mapStringToApiDate(fecha) |
| 38 | + }; |
| 39 | + |
| 40 | + embalses = [...embalses, embalse]; |
| 41 | + }); |
| 42 | + |
| 43 | + return embalses; |
| 44 | +} |
0 commit comments