-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdgt3.js
156 lines (112 loc) · 6.41 KB
/
dgt3.js
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
function dgt3(globalVar, eventEmitter) {
(async () => {
const constants = require('./constants.js');
const puppeteer = require('puppeteer');
const downloads = require('./download.js');
downloads.download(globalVar, eventEmitter);
const fs = require('fs')
const keyboardMapping = require('./USKeyboardLayout.js');
const browser = await puppeteer.launch(constants.PUPPETEER_OPTS)
const page = await browser.newPage()
const navigationPromise = page.waitForNavigation()
await page.goto("https://sedeapl.dgt.gob.es/WEB_TTRA_CONSULTA/Todos.faces?idioma=es")
//await page.goto("https://sedeapl.dgt.gob.es/WEB_TTRA_CONSULTA/TablonEdictosPublico.faces")
await page.setViewport({ width: 1600, height: 803 })
await page.keyboard.down(keyboardMapping.keyCodeLayout[118].code)
//Abre el buscador avanzado
await page.waitForSelector("body > DIV:nth-child(1) > DIV:nth-child(1) > DIV:nth-child(1) > DIV:nth-child(2) > DIV:nth-child(1) > DIV:nth-child(7) > DIV:nth-child(1) > DIV:nth-child(2) > FORM:nth-child(3) > INPUT:nth-child(2)");
await page.click("body > DIV:nth-child(1) > DIV:nth-child(1) > DIV:nth-child(1) > DIV:nth-child(2) > DIV:nth-child(1) > DIV:nth-child(7) > DIV:nth-child(1) > DIV:nth-child(2) > FORM:nth-child(3) > INPUT:nth-child(2)");
await navigationPromise
//Selecciona fecha inicio
await page.waitForSelector("body > DIV:nth-child(1) > DIV:nth-child(1) > DIV:nth-child(1) > DIV:nth-child(2) > DIV:nth-child(1) > DIV:nth-child(7) > DIV:nth-child(1) > DIV:nth-child(2) > FORM:nth-child(5) > DIV:nth-child(4) > DIV:nth-child(3) > DIV:nth-child(1) > INPUT:nth-child(2)");
await page.click("body > DIV:nth-child(1) > DIV:nth-child(1) > DIV:nth-child(1) > DIV:nth-child(2) > DIV:nth-child(1) > DIV:nth-child(7) > DIV:nth-child(1) > DIV:nth-child(2) > FORM:nth-child(5) > DIV:nth-child(4) > DIV:nth-child(3) > DIV:nth-child(1) > INPUT:nth-child(2)");
//Borrar el campo situando el puntero
for(let i =0;i<10;i++)
await page.keyboard.press(keyboardMapping.keyCodeLayout[37].code)
for(let i =0;i<10;i++)
await page.keyboard.press(keyboardMapping.keyCodeLayout[46].code)
//await page.keyboard.type("10-03-2020")
await page.keyboard.type(globalVar.dateIni)
//Presionamobs enter para lanzar
await page.keyboard.press(keyboardMapping.keyCodeLayout[13].code)
await navigationPromise
//Selecciona fecha fin
await page.waitForSelector("body > DIV:nth-child(1) > DIV:nth-child(1) > DIV:nth-child(1) > DIV:nth-child(2) > DIV:nth-child(1) > DIV:nth-child(7) > DIV:nth-child(1) > DIV:nth-child(2) > FORM:nth-child(5) > DIV:nth-child(4) > DIV:nth-child(3) > DIV:nth-child(2) > INPUT:nth-child(2)");
await page.click("body > DIV:nth-child(1) > DIV:nth-child(1) > DIV:nth-child(1) > DIV:nth-child(2) > DIV:nth-child(1) > DIV:nth-child(7) > DIV:nth-child(1) > DIV:nth-child(2) > FORM:nth-child(5) > DIV:nth-child(4) > DIV:nth-child(3) > DIV:nth-child(2) > INPUT:nth-child(2)");
//Borrar el campo situando el puntero
for(let i =0;i<10;i++)
await page.keyboard.press(keyboardMapping.keyCodeLayout[37].code)
for(let i =0;i<10;i++)
await page.keyboard.press(keyboardMapping.keyCodeLayout[46].code)
//await page.keyboard.type("10-04-2020")
await page.keyboard.type(globalVar.dateFin)
//Presionamobs enter para lanzar formulario
await page.keyboard.press(keyboardMapping.keyCodeLayout[13].code)
await navigationPromise
/** Setea descarga (no se si funciona)*/
await page._client.send('Page.setDownloadBehavior', {
behavior: 'allow',
// This path must match the WORKSPACE_DIR in Step 1
downloadPath: './downloads',
});
try{
while(true){
// for(let i=0;i<1;i++){
let data;
try{
await page.waitForNavigation();
//page.exposeFunction('downloadPDF', function (elem) { return elem; });
let script=fs.readFileSync('./TemplateGenerator.js', 'utf8');
console.log(script);
try{
await page.evaluate(script);
}catch(e2){
console.log("Falla evaluate: "+e2)
}
data = await page.evaluate(element => {
debugger;
window.tg = (new TemplateGenerator({ "customFunction": "downloadPDF", "selector": "A:nth-child(1)", "selectorDom": [{ "tag": "A", "child": 0 }], "nodes": [{ "node": {}, "value": "Ayuntamiento de Pamplona\n\nEdicto de notificación de denuncias.", "fixed": true }, { "node": {}, "value": "\n\t\t\t\t\t\t\t\t\t" }] }));
window.tg.staticData();
/***************** */
window.tg.calculatedItems.forEach(element => {
// debugger;
element.href=element.selected.href;
/*
Nos ncesario hacr este click
*/
//element.selected.click();
});
/************************ */
return window.tg.calculatedItems;
})
}catch(e){
console.log("Se acabó lo que se daba 1"+e)
}
// console.log(data);
//Evento generado con lo datos para tratarlos en otro punto
eventEmitter.emit("scrapedDataEvent",data);
try{
//Busco el segundo boton de paginacion
console.log("Páginas dos y siguientes")
await page.waitForSelector("body > DIV:nth-child(1) > DIV:nth-child(1) > DIV:nth-child(1) > DIV:nth-child(2) > DIV:nth-child(1) > FORM:nth-child(10) > DIV:nth-child(2) > DIV:nth-child(11) > DIV:nth-child(4) > INPUT:nth-child(1)");
await page.click("body > DIV:nth-child(1) > DIV:nth-child(1) > DIV:nth-child(1) > DIV:nth-child(2) > DIV:nth-child(1) > FORM:nth-child(10) > DIV:nth-child(2) > DIV:nth-child(11) > DIV:nth-child(4) > INPUT:nth-child(1)");
}catch(e){
//Estoy en la primera pagina (Si se hace este try / cath al reves no pasa de la pagina 1,2,1,2,1,2,...)
console.log("Primera pagina")
await page.waitForSelector("body > DIV:nth-child(1) > DIV:nth-child(1) > DIV:nth-child(1) > DIV:nth-child(2) > DIV:nth-child(1) > FORM:nth-child(10) > DIV:nth-child(2) > DIV:nth-child(11) > DIV:nth-child(2) > INPUT:nth-child(1)");
await page.click("body > DIV:nth-child(1) > DIV:nth-child(1) > DIV:nth-child(1) > DIV:nth-child(2) > DIV:nth-child(1) > FORM:nth-child(10) > DIV:nth-child(2) > DIV:nth-child(11) > DIV:nth-child(2) > INPUT:nth-child(1)");
}
await navigationPromise
await page.setViewport({ width: 1600, height: 803 })
console.log("Espera 90 segundos")
await page.waitFor(90000)
console.log("Fin Espera");
}
}catch(e){
console.log("Se acabó lo que se daba")
}
})()
}
module.exports = {
dgt3: dgt3
}