-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
77 lines (72 loc) · 2.01 KB
/
app.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
var http = require('http'), //Requerendo módulos.
fs = require('fs'),
querystring = require('querystring'),
cheerio = require('cheerio'); //Modulo para tratar DOM html
var contentPOST = querystring.stringify({ //Conteudo do POST
'edtIdUs': '2014390221',
'edtIdSen': '150796',
'btnIdOk': 'Ok'
});
var headerPOST = { //header da requisição POST
host: 'academico.ufrrj.br',
path: '/quiosque/aluno/quiosque.php',
method: 'POST',
headers: {
'Content-Length': contentPOST.length,
'Content-Type': 'application/x-www-form-urlencoded'
}
};
var cookie = '';
var cliente = http.request(headerPOST, function(resp){
resp.setEncoding('utf8');
cookie = resp.headers['set-cookie'][0];
cookie = cookie.substring(0, cookie.indexOf(';'))
var html = '';
resp.on('data', function(data){
html += data;
});
resp.on('end', function(){
var $ = cheerio.load(html);
$('a').each(function(index, elem) {
if(index<15){
console.log(index+' : '+elem.attribs['href']);
buscalink(cookie, elem.attribs['href']);
};
});
});
});
cliente.setMaxListeners(0);
cliente.on('error', function(e) {
console.log('DEU ZEBU NA REQUISIçÃO: ' + e.message);
});
cliente.write(contentPOST);
cliente.end();
var headerGET = {
host: 'academico.ufrrj.br',
path: '/quiosque/aluno/quiosque.php?pag=horario',
method: 'GET',
headers: {
'Cookie': '',
'Connection': 'keep-alive'
}
}
function buscalink(cookie, link){
headerGET['headers']['Cookie'] = cookie;
headerGET['path'] = '/quiosque/aluno/'+link;
var cli = http.request(headerGET, function(resp){
resp.setEncoding('utf8');
var html = '';
resp.on('data', function(data){
html += data;
});
resp.on('end', function(){
fs.writeFile(link.substring(link.indexOf('=')+'.html'), html,function(){
console.log(link+' salvo.');
});
});
});
cliente.on('error', function(e) {
console.log('DEU ZEBU NA REQUISIçÃO: '+ e.message);
});
cli.end();
};