1// Asignar las funciones al objeto global 2 3 4obtenerPartidosCALENDARIO = async function (pass, idequipo, cantidad) { 5 let partidos = await obtenerFixtureCALENDARIO(pass, idequipo, cantidad); 6 return partidos; 7}; 8 9 10 11 12 13obtenerFixturetorneos = async function (torneo) { 14 let response = await fetch(`https://feed.datafactory.la/index.php?ppaass=C1u8T4113r35&canal=deportes.futbol.${torneo}.fixture`, { 15 headers: { 16 'Accept-Charset': 'ISO-8859-1', 17 'Content-Type': 'application/json' 18 } 19 }); 20 let buffer = await response.arrayBuffer(); // Obtener los datos binarios de la respuesta 21 let decoder = new TextDecoder('ISO-8859-1'); // Decodificar en UTF-8 22 let xmlText = decoder.decode(buffer); // Convertir los datos binarios a texto en UTF-8 23 let parser = new DOMParser(); 24 let xmlDoc = parser.parseFromString(xmlText, 'text/xml'); 25 let json = xmlToJson(xmlDoc); 26 return json; 27} 28const obtenerPartidosFiltrados = (ligaArgentina, copaArgentina,libertadores, idFiltro) => { 29 // Función auxiliar para procesar los partidos de cada torneo 30 const procesarPartidosTorneo = (datos, nombreTorneo) => { 31 return datos.fixture.fecha 32 .flatMap(item => item.partido) // Desenrollamos los partidos en un solo array 33 .filter(partido => { 34 const idLocal = partido.local?.['@attributes']?.id; 35 const idVisitante = partido.visitante?.['@attributes']?.id; 36 return idLocal === idFiltro || idVisitante === idFiltro; 37 }) 38 .map(partido => { 39 partido.campeonato = datos.fixture.campeonatoNombreAlternativo?.['#text'] || ''; 40 renombrarPropiedadesConflictuosasCALENDARIO(partido); 41 return partido; 42 }); 43 }; 44 45 // Procesar partidos de ambas competencias 46 const partidosLiga = procesarPartidosTorneo(ligaArgentina, 'Liga'); 47 const partidosCopa = procesarPartidosTorneo(copaArgentina, 'Copa'); 48 const partidoslibertadores = procesarPartidosTorneo(libertadores, 'Libertadores'); 49 50 // Combinar todos los partidos en un solo array 51 return [...partidosLiga, ...partidosCopa,...partidoslibertadores]; 52}; 53const ordenarPartidosPorFecha = (partidos) => { 54 return partidos.sort((a, b) => { 55 const fechaA = a._at_attributes.fecha; 56 const fechaB = b._at_attributes.fecha; 57 58 // Convertir el string de fecha a número para comparación directa 59 // Ya que el formato es YYYYMMDD, la comparación numérica funcionará correctamente 60 return Number(fechaA) - Number(fechaB); 61 }); 62}; 63obtenerFixtureCALENDARIO = async function (pass, idequipo, cantidad) { 64 let idFiltro = idequipo.toString(); 65 let ligaArgentina = await obtenerFixturetorneos('primeraa'); 66 let copaargentina = await obtenerFixturetorneos('copaargentina'); 67 let libertadores = await obtenerFixturetorneos('libertadores'); 68 const partidosFiltradosbyId = obtenerPartidosFiltrados(ligaArgentina, copaargentina,libertadores, idFiltro); 69 const partidosOrdenados = ordenarPartidosPorFecha(partidosFiltradosbyId); 70 const groupedMatches = partidosOrdenados.reduce((acc, match) => { 71 const monthNames = { 72 '01': 'Enero', 73 '02': 'Febrero', 74 '03': 'Marzo', 75 '04': 'Abril', 76 '05': 'Mayo', 77 '06': 'Junio', 78 '07': 'Julio', 79 '08': 'Agosto', 80 '09': 'Septiembre', 81 '10': 'Octubre', 82 '11': 'Noviembre', 83 '12': 'Diciembre' 84 }; 85 const date = match._at_attributes.fecha; 86 const year = date.substring(0, 4);
87 const month = date.substring(4, 6); 88 const day = date.substring(6, 8); 89 90 const key = `${year}-${month}`; 91 92 if (!acc[key]) { 93 acc[key] = { 94 year, 95 month, 96 month_text: monthNames[month], 97 matches: [] 98 }; 99 } 100 101 acc[key].matches.push({ 102 ...match, 103 formattedDate: `${year}-${month}-${day}` 104 }); 105 106 return acc; 107 }, {}); 108 109 let grupo = Object.values(groupedMatches).sort((a, b) => { 110 return `${a.year}${a.month}` - `${b.year}${b.month}`; 111 }); 112 113 return grupo; 114} 115function renombrarPropiedadesConflictuosasCALENDARIO(obj) { 116 for (let key in obj) { 117 if (obj.hasOwnProperty(key)) { 118 // Si la propiedad contiene un '@', lo reemplazamos por un guion bajo '_' 119 if (key.includes('@')) { 120 const newKey = key.replace('@', '_at_'); // Reemplaza el '@' por '_at_' 121 obj[newKey] = obj[key]; // Asigna el valor con el nuevo nombre 122 delete obj[key]; // Elimina la propiedad original 123 } 124 125 // Si la propiedad contiene un '#', lo reemplazamos por un guion bajo '_' 126 if (key.includes('#')) { 127 const newKey = key.replace('#', '_hash_'); // Reemplaza el '#' por '_hash_' 128 obj[newKey] = obj[key]; // Asigna el valor con el nuevo nombre 129 delete obj[key]; // Elimina la propiedad original 130 } 131 132 // Si el valor es un objeto o array, aplicar recursión 133 if (typeof obj[key] === "object" && obj[key] !== null) { 134 renombrarPropiedadesConflictuosasCALENDARIO(obj[key]); 135 } 136 } 137 } 138} 139 140 141function xmlToJson(xml) { 142 let obj = {}; 143 if (xml.nodeType === 1) { // Element 144 if (xml.attributes.length > 0) { 145 obj['@attributes'] = {}; 146 for (let j = 0; j < xml.attributes.length; j++) { 147 let attribute = xml.attributes.item(j); 148 obj['@attributes'][attribute.nodeName] = attribute.nodeValue; 149 } 150 } 151 } else if (xml.nodeType === 3) { // Text 152 obj = xml.nodeValue; 153 } 154 155 if (xml.hasChildNodes()) { 156 for (let i = 0; i < xml.childNodes.length; i++) { 157 let child = xml.childNodes.item(i); 158 let nodeName = child.nodeName; 159 if (typeof obj[nodeName] === 'undefined') { 160 obj[nodeName] = xmlToJson(child); 161 } else { 162 if (!Array.isArray(obj[nodeName])) { 163 obj[nodeName] = [obj[nodeName]]; 164 } 165 obj[nodeName].push(xmlToJson(child)); 166 } 167 } 168 } 169 170 return obj; 171}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.