|
1 | 1 | import { CheerioAPI } from 'cheerio'; |
2 | 2 | import type { Element } from 'domhandler'; |
3 | | -import { EmbalsesSegura } from '@/api'; |
4 | | -import { mapEmbalsesToEntities } from '@/scraper' |
| 3 | +import { EmbalsesSegura } from '../api/index.js'; |
| 4 | +import { mapEmbalsesToEntities } from './mapper.js' |
5 | 5 |
|
6 | 6 | // Function to extract capacity data from main table |
7 | 7 | function getReservoirCapacities($: CheerioAPI): Record<string, { capacity: number; percentage: number }> { |
8 | 8 | const capacityMap: Record<string, { capacity: number; percentage: number }> = {}; |
9 | | - |
| 9 | + |
10 | 10 | $('#n0 tbody tr').each((_, row) => { |
11 | 11 | const $row = $(row); |
12 | 12 | const cols = $row.find('td'); |
13 | 13 | if (cols.length !== 4) return; |
14 | 14 |
|
15 | 15 | const embalse = $(cols[0]).text().trim(); |
16 | | - if (!embalse || |
17 | | - embalse.toLowerCase().includes('total') || |
18 | | - embalse.toLowerCase().includes('resto')) { |
| 16 | + if (!embalse || |
| 17 | + embalse.toLowerCase().includes('total') || |
| 18 | + embalse.toLowerCase().includes('resto')) { |
19 | 19 | return; |
20 | 20 | } |
21 | 21 |
|
22 | 22 | const capacidadTotalHm3 = Number($(cols[1]).text().trim()); |
23 | 23 | const porcentajeActual = Number($(cols[3]).text().trim()); |
24 | | - |
| 24 | + |
25 | 25 | capacityMap[embalse] = { |
26 | 26 | capacity: capacidadTotalHm3, |
27 | 27 | percentage: porcentajeActual |
@@ -67,11 +67,11 @@ function parseAnnualStatsRow( |
67 | 67 | export function extractReservoirsFromSeguraPage($: CheerioAPI): EmbalsesSegura[] { |
68 | 68 | // Get capacity data from main table (#n0) |
69 | 69 | const capacityMap = getReservoirCapacities($); |
70 | | - |
| 70 | + |
71 | 71 | // Get most recent monthly data from annual table (#n1) |
72 | 72 | const reservoirs: EmbalsesSegura[] = []; |
73 | 73 | const annualRows = extractAnnualStatsRows($); |
74 | | - |
| 74 | + |
75 | 75 | // Take only the LAST row (most recent month) |
76 | 76 | if (annualRows.length > 0) { |
77 | 77 | const lastRow = annualRows[annualRows.length - 1]; |
|
0 commit comments