-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkstimnovo.js
153 lines (137 loc) · 4.75 KB
/
linkstimnovo.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
const puppeteer = require('puppeteer');
const fs = require('fs');
var ceps = fs.readFileSync('cepsTim.txt', 'utf-8').split('\n');
fs.writeFileSync('linksTim.txt', '', function () { console.log('Arquivo limpo.') });
const url = 'https://lojaonline.tim.com.br/';
const url2 = 'https://lojaonline.tim.com.br/celulares';
var tempoEspera = 60000;
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',
]
};
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 cep of ceps){
try {
console.log('cep: ' + cep);
await page.goto(url);
await page.waitFor(20000);
await page.click('a[class="regional"]')
await page.waitFor('input[id="zipcode"]')
await page.type('input[id="zipcode"]', cep)
await page.click('label[for="client_check_2"]')
await page.click('input[data-ng-click="submitInfos()"]')
await page.waitFor(20000)
await page.evaluate(async () => {
await new Promise((resolve, reject) => {
let totalHeight = 0;
let distance = 100;
let timer = setInterval(() => {
let scrollHeight = document.body.scrollHeight
window.scrollBy(0, distance)
totalHeight += distance;
if(totalHeight >= scrollHeight){
clearInterval(timer);
resolve();
}
}, 500);
})
})
var links = await page.evaluate(function(){
return Array.from(document.querySelectorAll('a.offer')).map(element => element.href);
});
var obj = {};
obj.cep = cep;
obj.urls = [];
obj.urls = links
list.push(obj);
var file = fs.createWriteStream('linksTim.txt', {flags: 'a'});
if(obj.urls.length > 0){
file.write(obj.cep + '\r\n');
for(let url of obj.urls){
var aparelho = url.split('/')[5].slice(0, -8).replace(/-/g, ' ');
file.write(apenasAparelho(url) + '\r\n') ;
for(var i = 1; i <= 6; i++){
file.write(formatarLink(url, i) + '\r\n');
}
};
}
file.end();
} catch (err) {
console.log('Erro ao navegar para a página: ' + err);
}
}
//por fim, vai fechar o navegador
await browser.close();
});
function apenasAparelho(link){
return link.substring(0, link.lastIndexOf('/') + 1) + 'apenas-aparelho';
}
function formatarLink(link, indice){
if(indice == 1){
return link.substring(0, link.lastIndexOf('/') + 1) + 'controle-2.0gb-fid-novalinha';
}else if(indice == 2){
return link.substring(0, link.lastIndexOf('/') + 1) + 'pos_7gb_social_fid-novalinha';
}else if(indice == 3){
return link.substring(0, link.lastIndexOf('/') + 1) + 'tim-pos-10gb-plus-fid-novalinha';
}else if(indice == 4){
return link.substring(0, link.lastIndexOf('/') + 1) + 'pos_15gb_plus_fid-novalinha';
}else if(indice == 5){
return link.substring(0, link.lastIndexOf('/') + 1) + 'tim-pos-20gb-plus-fid-novalinha';
}else if(indice == 6){
return link.substring(0, link.lastIndexOf('/') + 1) + 'tim-controle-2,0gb-novalinha';
}
}