-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
340 lines (281 loc) · 12.3 KB
/
index.js
File metadata and controls
340 lines (281 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import puppeteer from 'puppeteer';
async function scrapeOrderInfo(xmlId) {
if (!xmlId) {
throw new Error('XML ID is required');
}
const baseUrl = 'http://deliveryconsole/PDetail.asp';
const url = `${baseUrl}?paso=&SBO=1-12392468580&DKO=${xmlId}&UserDomain=&UserName=&USER_EMAIL=&back=&MAccountID=81-35Y302W&TaskEQID=0&USERID=0&SOMA_SIID=&base=&RoleName=Sales%20Engineer`;
let browser = null;
try {
console.log("leyendo Pagina XML")
browser = await puppeteer.launch({
headless: true,
args: ['--no-sandbox', '--disable-setuid-sandbox'],
});
const page = await browser.newPage();
await page.setDefaultNavigationTimeout(30000);
await page.goto(url, { waitUntil: 'load' });
await page.waitForSelector('#tabs-1 table[border="1"]');
const result = await page.evaluate(() => {
const contenedor = document.querySelector('#tabs-1');
const table = contenedor.querySelector('table[border="1"]');
// Extraer encabezados
const headers = Array.from(table.querySelectorAll('tr:first-child td'))
.map(td => td.textContent.replace(/\s+/g, ' ').trim());
// Extraer contenido del primer <font> en el segundo <td>
const secondTd = document.querySelectorAll('#tblCaberaPrincipal td')[1];
const fontContent = secondTd
? secondTd.querySelector('font')?.textContent.trim()
: null;
const tableC = document.querySelector('#tblCaberaPrincipal');
const siebelOrder = tableC.querySelector('tr:nth-of-type(2) tr:nth-of-type(1) td:nth-of-type(2)')?.innerText.trim();
const pais = tableC.querySelector('tr:nth-of-type(2) tr:nth-of-type(1) td:nth-of-type(4)')?.innerText.trim();
const razonSocial = tableC.querySelector('tr:nth-of-type(2) tr:nth-of-type(2) td:nth-of-type(2)')?.innerText.trim();
const coments2 = tableC.querySelector('tr:nth-of-type(2) tr:nth-of-type(9) td:nth-of-type(2)')?.innerText.trim();
const se = tableC.querySelector('tr:nth-of-type(2) tr:nth-of-type(10) td:nth-of-type(2)')?.innerText.trim();
const comentarios = document.querySelector(
'#tblCaberaPrincipal > tbody > tr:nth-of-type(2) table > tbody > tr:nth-of-type(11) > td:nth-of-type(2) textarea'
)?.value.trim();
// Extraer filas de datos
const rows = Array.from(table.querySelectorAll('tr:not(:first-child)'));
const dataRows = rows.map(row => {
const cells = Array.from(row.querySelectorAll('td'));
const values = cells.map((td, index) => {
// Si es la última columna, buscar el href del enlace
if (index === cells.length - 1) {
const link = td.querySelector('a');
if (link) {
// Extraer la URL del onclick
const onclickAttr = link.getAttribute('href') || link.getAttribute('onclick') || '';
const match = onclickAttr.match(/['"]([^'"]*guru[^'"]*)['"]/);
return match ? match[1] : null;
}
return null;
}
// Para las demás columnas, mantener el comportamiento original
return td.textContent.replace(/\s+/g, ' ').trim();
});
const obj = {};
headers.forEach((header, index) => {
if (values[index] !== undefined) {
obj[header] = values[index];
}
});
return obj;
});
return {
fontContent, // Incluye el contenido extraído del primer <font>
dataRows,
siebelOrder,
pais,
razonSocial,
se,
comentarios,
coments2
};
});
return {
success: true,
data: result.dataRows,
xml: {
siebelOrder: result.fontContent,
pais: result.pais,
razonSocial: result.razonSocial,
se: result.se,
comentarios: result.comentarios,
coments2: result.coments2
},// Devuelve el contenido del <font> extraído
timestamp: new Date().toISOString(),
xmlId: xmlId
};
} catch (error) {
console.log(`Error scraping data for XML ID ${xmlId}:`, error);
return {
success: false,
error: error.message,
xmlId: xmlId,
timestamp: new Date().toISOString()
};
} finally {
if (browser) {
await browser.close();
}
}
}
async function scrapePage(url, index) {
const httpUrl = url.replace(/^https:\/\//, 'http://');
const browser = await puppeteer.launch({
headless: true, // Cambiar a true para ejecución silenciosa
args: ['--no-sandbox', '--disable-setuid-sandbox'],
headless:true,
/* slowMo:500, */
});
const page = await browser.newPage();
try {
console.log(`Index: ${index}, scraping ${httpUrl}`);
await page.goto(httpUrl, { waitUntil: 'load' });
await page.waitForSelector('form.form-horizontal', { timeout: 7000 }); // Espera hasta 10s a que el formulario esté disponible
// Extraer información principal del formulario
const formData = await page.evaluate(() => {
// Seleccionar específicamente el tercer div.col-lg-12
const thirdSection = document.querySelectorAll('.col-lg-12')[2];
if (!thirdSection) {
throw new Error('No se encontró la sección específica');
}
// Seleccionar específicamente el primer form
const form = thirdSection.querySelector('.panel .panel-body form.form-horizontal');
if (!form) {
throw new Error('No se encontró el formulario');
}
const result = {
direccion: '',
latitud: '',
longitud: '',
ubicacion: '',
instancia: '',
estado: ''
};
// Buscar los elementos dentro del form específico
const addressInfo = form.querySelector('legend:first-child p');
if (addressInfo) {
const text = addressInfo.textContent;
console.log("Texto completo:", text);
const strongs = addressInfo.querySelectorAll('strong');
let direccion = '';
let latitud = '';
let longitud = '';
strongs.forEach((strong, index) => {
const strongText = strong.textContent.trim();
const nextText = strong.nextSibling ? strong.nextSibling.textContent.trim() : '';
console.log(`Strong ${index}:`, strongText);
console.log(`Texto siguiente ${index}:`, nextText);
if (strongText.includes('Address')) {
direccion = nextText;
} else if (strongText.includes('Latitud')) {
latitud = nextText;
} else if (strongText.includes('Longitud')) {
longitud = nextText;
}
});
result.direccion = direccion;
result.latitud = latitud;
result.longitud = longitud;
}
// Hacer lo mismo para la ubicación e instancia
const locationInfo = form.querySelector('legend:nth-child(2) p');
if (locationInfo) {
const strongs = locationInfo.querySelectorAll('strong');
strongs.forEach(strong => {
const strongText = strong.textContent.trim();
const nextText = strong.nextSibling ? strong.nextSibling.textContent.trim() : '';
if (strongText.includes('Ubication')) {
result.ubicacion = nextText;
} else if (strongText.includes('Instance')) {
result.instancia = nextText;
}
});
// Capturar el estado y el texto adicional
const estadoElement = locationInfo.querySelector('.pull-right .p-mr-auto');
const closeElement = locationInfo.querySelector('.pull-right [ng-if="(!form.isThirdParty || form.responseAmo==1)"]');
let estadoTexto = '';
if (estadoElement) {
estadoTexto = estadoElement.textContent.trim();
}
if (closeElement) {
// Obtener solo el texto "Close" sin incluir el contenido del span interno
const closeText = closeElement.childNodes[0].textContent.trim();
estadoTexto = estadoTexto + ' - ' + closeText;
}
result.estado = estadoTexto;
}
return result;
});
// Extrae datos de un conjunto de campos específicos
const extractFieldData = async (fields) => {
const data = {};
for (const field of fields) {
try {
// Esperar a que el selector esté disponible
await page.waitForSelector(`#${field}`, { timeout: 3000, visible: true });
// Obtener el valor del campo
const fieldValue = await page.evaluate((fieldId) => {
const element = document.getElementById(fieldId);
if (!element) return null;
if (element.tagName === 'SELECT') {
return element.options[element.selectedIndex]?.text || null;
}
return element.value || element.textContent.trim();
}, field);
data[field] = fieldValue;
} catch (error) {
console.warn(`Campo no encontrado: ${field}`, error.message);
data[field] = null;
}
}
return data;
};
// Campos adicionales para extraer
const additionalFields = [
"commitedBW", "accessBW", "portBW", "serviceType",
"consultationNCD", "cLLIPOP", "lastMile", "accessTSD",
"technologicalSolution", "quoteNumber", "contactPhone",
"contactName", "contactEmail", "clientInterface", "commentary",
"routing"
];
const additionalData = await extractFieldData(additionalFields);
// Campos de terceros
const thirdPartyFields = [
'thirdPartyDKO', 'sideAinterface', 'sideBinterface',
'accessType', 'provisioningInterval', 'qInq', 'mtu',
"quotedSubBandwidth", "thirdPartyInformation", "mrc", "nrc"
];
const thirdPartyData = await extractFieldData(thirdPartyFields);
// Retornar datos completos
return { formData, additionalData, thirdPartyData };
} catch (error) {
console.error(`Error scraping ${httpUrl}:`, error.message);
return null;
} finally {
try {
await browser.close();
} catch (closeError) {
console.error('Error closing browser:', closeError.message);
}
}
}
// Función principal
async function main() {
try {
const xmlId = '8875343';
const result = await scrapeOrderInfo(xmlId);
const allResults = [];
if (result.success) {
const dataArray = result.data;
for (const [index, entry] of dataArray.entries()) {
const url = entry["SOF/TDG Detail"];
try {
const pageResult = await scrapePage(url, index);
allResults.push({ TDG: pageResult });
} catch (error) {
console.error(`Error scraping URL at index ${index}: ${url} - ${error.message}`);
allResults.push({ TDG: null, error: error.message });
}
}
// Crear el objeto XML y asignar TDG a instancias
const XML = {
TDG: allResults,
instancias: result.data.map((instancia, index) => ({
...instancia,
TDG: allResults[index]?.TDG || null // Asigna el TDG correspondiente o null
})),
XML: result.xml
};
console.log("outputXML", XML.instancias);
} else {
console.log('Error:', result.error);
}
} catch (error) {
console.log('Fatal error:', error);
}
}
main();