1- import { EmbalseDuero } from ' ../api/cuenca.model' ;
2- import { load } from ' cheerio' ;
1+ import { EmbalseDuero } from " ../api/cuenca.model" ;
2+ import { load } from " cheerio" ;
33
44// Función auxiliar para parsear string a number o null
55function _parseToNumberOrNull ( value : string ) : number | null {
66 const trimmed = value . trim ( ) ;
7- if ( trimmed === '-' || trimmed === '' ) return null ;
7+ if ( trimmed === "-" || trimmed === "" ) return null ;
88 // Quitar puntos de miles y cambiar coma decimal por punto
9- const normalized = trimmed . replace ( / \. / g, '' ) . replace ( ',' , '.' ) ;
9+ const normalized = trimmed . replace ( / \. / g, "" ) . replace ( "," , "." ) ;
1010 const num = Number ( normalized ) ;
1111 return isNaN ( num ) ? null : num ;
1212}
@@ -16,21 +16,21 @@ export function parseReservoirsFromHtml(html: string): EmbalseDuero[] {
1616 const $ = load ( html ) ;
1717 const reservoirs : EmbalseDuero [ ] = [ ] ;
1818
19- $ ( ' tbody > tr' ) . each ( ( index , element ) => {
20- const tds = $ ( element ) . find ( 'td' ) ;
19+ $ ( " tbody > tr" ) . each ( ( index , element ) => {
20+ const tds = $ ( element ) . find ( "td" ) ;
2121 const embalse = $ ( tds [ 0 ] ) . text ( ) . trim ( ) ;
2222 const capacityRaw = $ ( tds [ 1 ] ) . text ( ) . trim ( ) ;
2323 const currentVolumeRaw = $ ( tds [ 2 ] ) . text ( ) . trim ( ) ;
2424 const normalizedName = embalse . toLowerCase ( ) ;
2525 const provinceHeader = $ ( element ) . find ( 'td[colspan="11"]' ) ;
26- const detectedProvince = provinceHeader . text ( ) . trim ( )
26+ const detectedProvince = provinceHeader . text ( ) . trim ( ) ;
2727 const capacity = _parseToNumberOrNull ( capacityRaw ) ;
2828 const currentVolume = _parseToNumberOrNull ( currentVolumeRaw ) ;
2929 if (
3030 ! detectedProvince &&
3131 embalse &&
32- ! normalizedName . startsWith ( ' total' ) &&
33- ! normalizedName . startsWith ( ' % del total' )
32+ ! normalizedName . startsWith ( " total" ) &&
33+ ! normalizedName . startsWith ( " % del total" )
3434 ) {
3535 reservoirs . push ( {
3636 id : index ,
@@ -47,13 +47,30 @@ export function parseReservoirsFromHtml(html: string): EmbalseDuero[] {
4747export const getCurrentDate = ( html : string ) => {
4848 const $ = load ( html ) ;
4949
50- const titleElement = $ ( 'div .title-table' ) . text ( ) ;
51- const currentValue = titleElement . split ( 'Duero a día' ) [ 1 ] . split ( 'de' ) . join ( " " ) . trim ( ) ;
50+ const titleElement = $ ( "div .title-table" ) . text ( ) ;
51+
52+ if ( ! titleElement . includes ( "Duero a día" ) ) {
53+ throw new Error (
54+ 'El formato del título no contiene "Duero a día". Verifica el HTML proporcionado.'
55+ ) ;
56+ }
57+
58+ const parts = titleElement . split ( "Duero a día" ) ;
59+ if ( parts . length < 2 ) {
60+ throw new Error (
61+ "No se pudo extraer la fecha del título. Verifica el formato del HTML."
62+ ) ;
63+ }
64+
65+ const currentValue = parts [ 1 ] . split ( "de" ) . join ( " " ) . trim ( ) ;
5266
5367 const currentDate = new Date ( currentValue ) ;
68+ if ( isNaN ( currentDate . getTime ( ) ) ) {
69+ throw new Error ( `La fecha extraída no es válida: ${ currentValue } ` ) ;
70+ }
5471
5572 return formatApiDate ( currentDate ) ;
56- }
73+ } ;
5774
5875const formatApiDate = ( date : Date ) : string => {
5976 const year = date . getFullYear ( ) ;
0 commit comments