-
Notifications
You must be signed in to change notification settings - Fork 0
/
precosTimNovo.js
269 lines (251 loc) · 8.55 KB
/
precosTimNovo.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
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
const puppeteer = require('puppeteer');
const fs = require('fs');
const sql = require('mssql');
var links = recuperarLinks();
const prepararParaTestes = async (page) => {
// Pass the User-Agent Test.
const userAgent = 'Mozilla/5.0 (X11; Linux x86_64)' +
'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.39 Safari/537.36';
await page.setUserAgent(userAgent);
// Pass the Webdriver Test.
await page.evaluateOnNewDocument(() => {
Object.defineProperty(navigator, 'webdriver', {
get: () => false,
});
});
// Pass the Chrome Test.
await page.evaluateOnNewDocument(() => {
// We can mock this in as much depth as we need for the test.
window.navigator.chrome = {
runtime: {},
// etc.
};
});
// Pass the Permissions Test.
await page.evaluateOnNewDocument(() => {
const originalQuery = window.navigator.permissions.query;
return window.navigator.permissions.query = (parameters) => (
parameters.name === 'notifications' ?
Promise.resolve({ state: Notification.permission }) :
originalQuery(parameters)
);
});
// Pass the Plugins Length Test.
await page.evaluateOnNewDocument(() => {
// Overwrite the `plugins` property to use a custom getter.
Object.defineProperty(navigator, 'plugins', {
// This just needs to have `length > 0` for the current test,
// but we could mock the plugins too if necessary.
get: () => [1, 2, 3, 4, 5],
});
});
// Pass the Languages Test.
await page.evaluateOnNewDocument(() => {
// Overwrite the `plugins` property to use a custom getter.
Object.defineProperty(navigator, 'languages', {
get: () => ['en-US', 'en'],
});
});
}
const opts = {
headless: false,
defaultViewport: null,
args: [
'--start-maximized',
'--no-sandbox',
]
};
function recuperarLinks(){
var links = fs.readFileSync('linksTim.txt', 'utf-8').split('\r\n');
var indices = [];
var objs = [];
for(var i = 0; i < links.length; i++){
if(links[i].length == 9 || links[i].length == 8){
indices.push(i);
}
}
for(var i = 0; i < indices.length; i++){
var obj = {}
obj.cep = links[indices[i]];
obj.links = [];
if(i == indices.length - 1){
for(var j = indices[i] + 1; j < links.length; j++){
if(links[j] != ''){
obj.links.push(links[j]);
}
}
}else{
for(var j = indices[i] + 1; j < indices[i + 1]; j++){
obj.links.push(links[j]);
}
}
objs.push(obj);
}
return objs;
}
puppeteer.launch(opts).then(async browser => {
const page = await browser.newPage();
//tirando o timeout
await page.setDefaultNavigationTimeout(0);
var list = [];
await prepararParaTestes(page);
for(let link of links){
for(let url of link.links){
try{
if(url != '' && url != '\r'){
console.log("Cep: " + link.cep);
console.log("Link: " + url);
var dadosAjustados;
if(url.includes('apenas-aparelho')){
await page.goto(url);
await page.waitFor(15000);
await page.click('a[class="regional"]')
await page.waitFor('input[id="zipcode"]')
await page.type('input[id="zipcode"]', link.cep)
await page.click('label[for="client_check_2"]')
await page.click('input[data-ng-click="submitInfos()"]')
await page.waitFor(15000)
let aparelho = await page.evaluate(function () {
return Array.from(document.querySelectorAll('.offer-title .subtitle')).map(element => element.innerText);
});
let precoAparelho = await page.evaluate(function () {
return Array.from(document.querySelectorAll('.price-value')).map(element => element.innerText);
});
dadosAjustados = await montarDadosTimSomenteAparelho(aparelho[0], precoAparelho[0], link.cep);
//console.log(dadosAjustados)
await Promise.all(dadosAjustados.map(obj => salvarBanco(obj)));
}else{
await page.goto(url);
await page.waitFor(15000);
await page.click('a[class="regional"]')
await page.waitFor('input[id="zipcode"]')
await page.type('input[id="zipcode"]', link.cep)
await page.click('label[for="client_check_2"]')
await page.click('input[data-ng-click="submitInfos()"]')
await page.waitFor(15000)
let aparelho = await page.evaluate(function () {
return Array.from(document.querySelectorAll('.offer-title .subtitle')).map(element => element.innerText);
});
let precoAparelho = await page.evaluate(function () {
return Array.from(document.querySelectorAll('.price-value')).map(element => element.innerText);
});
let plano = await page.evaluate(function () {
return Array.from(document.querySelectorAll(".plan-widget .subtitle")).map(element => element.innerText);
});
let precoPlano = await page.evaluate(function(){
return Array.from(document.querySelectorAll('.plan-price .price')).map(element => element.innerText)
});
dadosAjustados = await montarObj(aparelho[0], precoAparelho[0], plano[0], precoPlano[0], link.cep);
//console.log(dadosAjustados)
await Promise.all(dadosAjustados.map(obj => salvarBanco(obj)));
}
console.log('\r\n');
}
}catch(e){
console.log(e);
}
}
}
//por fim, vai fechar o navegador
await browser.close();
});
async function salvarBanco(dados){
const pool = new sql.ConnectionPool({
user: 'RegionalNE',
password: 'RegionalNEvivo2019',
server: '10.238.176.136',
database: 'SOL'
});
pool.connect().then(function(){
const request = new sql.Request(pool);
const insert = "insert into [SOL].[dbo].[PRECOS_CONCORRENCIA_TIM_AUX] ([APARELHO], [PRECO_APARELHO], [PLANO], [PRECO_PLANO], [UF], [CEP], [DATA_CARGA]) " +
" values ('" + dados.aparelho + "', '" + dados.precoAparelho + "', '" + dados.plano + "', '" + dados.precoPlano + "', '" + dados.uf + "','" + dados.cep + "', convert(date, getdate(),101))" ;
request.query(insert).then(function(recordset){
console.log('Dado inserido');
pool.close();
}).catch(function(err){
console.log(err);
pool.close();
})
}).catch(function(err){
console.log(err);
});
}
async function excluirBanco(){
const pool = new sql.ConnectionPool({
user: 'RegionalNE',
password: 'RegionalNEvivo2019',
server: '10.238.176.136',
database: 'SOL'
});
pool.connect().then(function(){
const request = new sql.Request(pool);
const del = "DELETE FROM [SOL].[dbo].[PRECOS_CONCORRENCIA_TIM_AUX]";
request.query(del).then(function(recordset){
console.log('Dados removidos');
pool.close();
}).catch(function(err){
console.log(err);
pool.close();
})
}).catch(function(err){
console.log(err);
});
}
function montarObj(aparelho, precoAparelho, plano, precoPlano, cep) {
var obj = {};
var retorno = [];
obj.aparelho = aparelho.replace("NOTE9", "NOTE 9").replace("Note9", "Note 9").replace("TIM", "");
if(precoAparelho.includes("De")){
let precosplit = precoAparelho.split("\n");
obj.precoAparelho = precosplit[1].replace(' ', '').trim();
}else{
obj.precoAparelho = precoAparelho.replace(' ', '').trim();
}
obj.plano = plano;
obj.precoPlano = "R$" + precoPlano + "/Mensal";
obj.cep = cep.trim().replace(' ', '').replace('\r\n', '').replace('\r', '').replace('\n', '');
console.log(obj);
if (cep.includes('60170250')) {
obj.uf = 'CE';
} else if (cep.includes('45600760')) {
obj.uf = 'BA';
} else if (cep.includes('51011051')){
obj.uf = 'PE'
}else{
obj.uf = 'SP';
}
console.log(obj);
if(obj.plano != ''){
retorno.push(obj);
}
return retorno;
}
function montarDadosTimSomenteAparelho(aparelho, preco, cep) {
var dadosAjustados = [];
var obj = {};
console.log('Aparelho: ' + aparelho);
console.log('Preço: ' + preco);
obj.aparelho = aparelho.trim().replace('\r\n', '').replace('\r', '').replace('\n', '').replace("NOTE9", "NOTE 9").replace("Note9", "Note 9");
obj.plano = "Somente Aparelho"
obj.precoPlano = 'R$0,00';
if(preco.includes("De")){
let precosplit = preco.split("\n");
obj.precoAparelho = precosplit[1].replace(' ', '').trim();
}else{
obj.precoAparelho = preco.trim().replace(' ', '').replace('\r\n', '').replace('\r', '').replace('\n', '');
}
obj.cep = cep.trim().replace('\r\n', '').replace('\r', '').replace('\n', '');
if (cep.includes('60170250')) {
obj.uf = 'CE';
} else if (cep.includes('45600760')) {
obj.uf = 'BA';
} else if (cep.includes('51011051')){
obj.uf = 'PE'
}else{
obj.uf = 'SP';
}
console.log(obj);
dadosAjustados.push(obj);
return dadosAjustados;
}