Skip to content

Commit bcab037

Browse files
authored
Merge pull request #163 from Lemoncode/feature/integration-cuenca-segura
feature/integration-cuenca-segura
2 parents 9fca37c + 46cefde commit bcab037

14 files changed

Lines changed: 130 additions & 44 deletions

File tree

functions/src/functions/scraping-functions.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export async function scrapingsFunction(
3434

3535
const responseCuencaJucar = await embalsesRepository.actualizarCuencaJucar();
3636

37+
const responseCuencaSegura = await embalsesRepository.actualizarCuencaSegura();
38+
3739
if (responseCuencaMediterranea) {
3840
context.log(
3941
"scrapings-function: Se han actualizado los embalses de la cuenca Mediterránea",
@@ -82,6 +84,14 @@ export async function scrapingsFunction(
8284
);
8385
}
8486

87+
if (responseCuencaSegura) {
88+
context.log(`Se han actualizado los embalses de la cuenca Segura`);
89+
} else {
90+
context.log(
91+
"No se han podido actualizar los embalses de la cuenca Segura"
92+
);
93+
}
94+
8595
} catch (error) {
8696
context.error("scrapings-function: ERROR", error);
8797
throw error;
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
{
2-
"name": "scraping-cuenca-segura",
2+
"name": "scraping-cuenca-segura",
33
"version": "1.0.0",
44
"private": true,
55
"type": "module",
66
"exports": {
7-
".": "./src/index.ts"
7+
".": "./dist/index.js"
88
},
9+
"main": "./dist/index.js",
10+
"types": "./dist/index.d.ts",
911
"scripts": {
10-
"start": "tsx --watch src/console-runner.ts"
12+
"start": "tsx --watch ./src/console-runner.ts",
13+
"build": "run-p clean type-check build:scraping-cuenca-segura",
14+
"build:scraping-cuenca-segura": "tsc",
15+
"clean": "rimraf dist",
16+
"type-check": "tsc --noEmit --preserveWatchOutput"
1117
},
1218
"dependencies": {
19+
"axios": "^1.11.0",
20+
"cheerio": "^1.1.2",
1321
"db-model": "^1.0.0"
22+
},
23+
"devDependencies": {
24+
"ts-node": "^10.9.2",
25+
"typescript": "^5.9.2"
1426
}
1527
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// Barrel file for API exports
2-
export * from './cuenca.api';
3-
export * from './cuenca.model';
2+
export * from './cuenca.api.js';
3+
export * from './cuenca.model.js';
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import { scrapeCuencaSegura } from './integration';
2-
import { mapToEmbalseUpdateSAIH } from './scraper';
1+
import { scrapeCuencaSegura } from './integration.js';
32

4-
const URL = 'https://chsegura.es/es/cuenca/redes-de-control/estadisticas-hidrologicas/estado-de-embalses/';
53
console.log('Estado de la Cuenca Segura:');
6-
const scrapedCuencaSegura = await scrapeCuencaSegura(URL);
7-
const result = mapToEmbalseUpdateSAIH(scrapedCuencaSegura)
8-
console.log(result);
4+
const scrapedCuencaSegura = await scrapeCuencaSegura();
5+
console.log(scrapedCuencaSegura);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from "./integration";
1+
export * from "./integration.js";
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import * as cheerio from 'cheerio';
2-
import { getCuencaPageHTMLContent, EmbalsesSegura } from '@/api';
3-
import { extractReservoirsFromSeguraPage } from '@/scraper';
2+
import { getCuencaPageHTMLContent } from './api/index.js';
3+
import { extractReservoirsFromSeguraPage } from './scraper/index.js';
4+
import { mapToEmbalseUpdateSAIH } from "./scraper/mapper.js";
5+
import { EmbalseUpdateSAIHEntity } from 'db-model';
6+
7+
const URL = 'https://chsegura.es/es/cuenca/redes-de-control/estadisticas-hidrologicas/estado-de-embalses/';
48

59
/**
610
* Scrapes Segura reservoir data and returns it as an array.
711
* @param url - The URL to scrape the data from
812
*/
9-
export async function scrapeCuencaSegura(
10-
url: string
11-
): Promise<EmbalsesSegura[]> {
12-
const html = await getCuencaPageHTMLContent(url);
13+
export async function scrapeCuencaSegura(): Promise<EmbalseUpdateSAIHEntity[]> {
14+
const html = await getCuencaPageHTMLContent(URL);
1315
const $: cheerio.CheerioAPI = cheerio.load(html);
1416

1517
// Extract and map reservoir data
16-
return extractReservoirsFromSeguraPage($);
18+
const embalses = extractReservoirsFromSeguraPage($);
19+
return mapToEmbalseUpdateSAIH(embalses);
1720
}

integrations/scraping-cuenca-segura/src/scraper/business.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
import { CheerioAPI } from 'cheerio';
22
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'
55

66
// Function to extract capacity data from main table
77
function getReservoirCapacities($: CheerioAPI): Record<string, { capacity: number; percentage: number }> {
88
const capacityMap: Record<string, { capacity: number; percentage: number }> = {};
9-
9+
1010
$('#n0 tbody tr').each((_, row) => {
1111
const $row = $(row);
1212
const cols = $row.find('td');
1313
if (cols.length !== 4) return;
1414

1515
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')) {
1919
return;
2020
}
2121

2222
const capacidadTotalHm3 = Number($(cols[1]).text().trim());
2323
const porcentajeActual = Number($(cols[3]).text().trim());
24-
24+
2525
capacityMap[embalse] = {
2626
capacity: capacidadTotalHm3,
2727
percentage: porcentajeActual
@@ -67,11 +67,11 @@ function parseAnnualStatsRow(
6767
export function extractReservoirsFromSeguraPage($: CheerioAPI): EmbalsesSegura[] {
6868
// Get capacity data from main table (#n0)
6969
const capacityMap = getReservoirCapacities($);
70-
70+
7171
// Get most recent monthly data from annual table (#n1)
7272
const reservoirs: EmbalsesSegura[] = [];
7373
const annualRows = extractAnnualStatsRows($);
74-
74+
7575
// Take only the LAST row (most recent month)
7676
if (annualRows.length > 0) {
7777
const lastRow = annualRows[annualRows.length - 1];
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from './business';
2-
export * from './mapper';
1+
export * from './business.js';
2+
export * from './mapper.js';

integrations/scraping-cuenca-segura/src/scraper/mapper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { EmbalseUpdateSAIHEntity } from 'db-model';
2-
import { EmbalsesSegura } from '@/api';
2+
import { EmbalsesSegura } from '../api/index.js';
33

44
// Province lookup for each reservoir
55
const reservoirProvince: Record<string, string> = {
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
{
22
"compilerOptions": {
33
"target": "ESNext",
4-
"module": "ESNext",
5-
"moduleResolution": "bundler",
4+
"module": "nodenext",
5+
"moduleResolution": "nodenext",
6+
"outDir": "dist",
67
"skipLibCheck": true,
78
"isolatedModules": true,
89
"esModuleInterop": true,
9-
"baseUrl": "./",
10-
"paths": {
11-
"@/*": ["src/*"],
12-
"@/api/*": ["src/api/*"],
13-
"@/scraper/*": ["src/scraper/*"]
14-
}
10+
"verbatimModuleSyntax": false,
11+
"declaration": true,
12+
"baseUrl": "./"
1513
},
16-
"include": ["src"]
14+
"include": ["src/**/*"],
15+
"exclude": ["dist", "node_modules"]
1716
}

0 commit comments

Comments
 (0)