import 'package:http/http.dart' as http; import 'package:html/parser.dart' as htmlParser; class DataService { Future>> obtenerDatos() async { final response = await http.get(Uri.parse('https://crcivan.asociacionador.es/contenedores.html')); if (response.statusCode == 200) { final document = htmlParser.parse(response.body); final filaTabla = document.querySelectorAll('html > body > div > center > table > tbody > tr > td > div > center > table > tbody > tr > td > table > tbody > tr > td > font > font > table > tbody > tr'); List> embalsesData = []; for (var fila in filaTabla) { var nombreElemento = fila.querySelector('td > p > span'); var elementos = fila.querySelectorAll('td > span'); if (nombreElemento != null && elementos.isNotEmpty) { var nombre = nombreElemento.text; var listaElementos = elementos.map((elemento) => elemento.text).toList(); var embalseMap = {'nombre': nombre, 'elementos': listaElementos}; embalsesData.add(embalseMap); } } return embalsesData; } else { throw Exception('Error al obtener datos'); } } Future obtenerFechaComunicacionEmbalses() async { final response = await http .get(Uri.parse('https://crcivan.asociacionador.es/contenedores.html')); if (response.statusCode == 200) { final document = htmlParser.parse(response.body); final dateElement = document.querySelector('span[style="font-size: 12pt;"]'); final communicationDate = dateElement != null ? dateElement.text.replaceAll(':', '').trim() : 'Fecha no encontrada'; return communicationDate; } else { throw Exception('Error al obtener la fecha de comunicación'); } } Future> obtenerDatosSecciones() async { final response = await http.get(Uri.parse('https://crcivan.asociacionador.es/secciones.html')); if (response.statusCode == 200) { final document = htmlParser.parse(response.body); final rows = document.querySelectorAll('table[style="height: 1202px; font-size: 13px;"] tbody tr'); List> seccionesData = []; String? currentSection; String? currentGuardInfo; List> currentSectionData = []; // Extraer la fecha de la comunicación final dateRegex = RegExp(r'\d{2}/\d{2}/\d{4}'); final dateMatch = dateRegex.firstMatch(document.body!.text); final communicationDate = dateMatch != null ? dateMatch.group(0) : 'Fecha no encontrada'; for (var row in rows) { var cells = row.querySelectorAll('td'); if (cells.length == 2) { var sectionElement = cells[1].querySelector('strong'); if (sectionElement != null && sectionElement.text.contains('SECCIÓN')) { if (currentSection != null) { seccionesData.add({ 'section': currentSection, 'guardInfo': currentGuardInfo, 'data': currentSectionData, }); } currentSection = sectionElement.text.trim(); currentGuardInfo = cells[1].querySelector('span[style="text-decoration: underline;"]')?.text.trim(); currentSectionData = []; } else { currentSectionData.add({ 'date': cells[0].text.trim(), 'location': cells[1].text.trim(), }); } } } if (currentSection != null) { seccionesData.add({ 'section': currentSection, 'guardInfo': currentGuardInfo, 'data': currentSectionData, }); } return { 'communicationDate': communicationDate, 'seccionesData': seccionesData, }; } else { throw Exception('Error al cargar los datos de secciones'); } } }