forked from DeadPackets/CRNotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
executable file
·135 lines (112 loc) · 3.76 KB
/
client.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
//Config
const config = require('./config.json');
//Socket
const io = require('socket.io-client');
const socket = io('https://crnotify.cf')
// const socket = io('http://localhost:8080/')
//Requires
const random_useragent = require('random-useragent');
const chalk = require('chalk');
//On connection
socket.on('connect', function(){
console.log(chalk.green('Successfully connected!'))
})
socket.on('disconnect', function(){
console.log(chalk.yellow('Disconnected from socket!'))
});
//Functions
function checkCRN(termID, crn, cb) {
const Horseman = require('node-horseman');
const horseman = new Horseman({
// cookiesFile: './cookies.txt',
diskCache: true,
diskCachePath: './browsercache',
timeout: 15000,
loadImages: false,
// proxyType: 'socks5',
// proxy: '127.0.0.1:9050',
ignoreSSLErrors: true
});
horseman
.userAgent(random_useragent.getRandom())
.on('error', function(msg){
console.log(chalk.red(`Error crawling CRN!`))
cb(true, null)
})
.on('timeout', function(){
console.log(chalk.red(`Timeout crawling CRN!`))
cb(true, null)
})
.cookies([])
.open(`https://banner.aus.edu/axp3b21h/owa/bwckschd.p_disp_detail_sched?term_in=${termID}&crn_in=${crn}`)
.catch(function(e){
console.log(chalk.red(`Error crawling CRN!`))
cb(true, null)
})
.html()
.then(function(body) {
cb(false, body)
return horseman.close()
})
}
function fetchStatus(termID, subject, crns) {
return new Promise(function(resolve, reject) {
const Horseman = require('node-horseman');
const postData = `term_in=${termID}&sel_subj=dummy&sel_day=dummy&sel_schd=dummy&sel_insm=dummy&sel_camp=dummy&sel_levl=dummy&sel_sess=dummy&sel_instr=dummy&sel_ptrm=dummy&sel_attr=dummy&sel_subj=${subject}&sel_crse=&sel_title=&sel_from_cred=&sel_to_cred=&sel_levl=%25&sel_instr=%25&sel_attr=%25&begin_hh=0&begin_mi=0&begin_ap=a&end_hh=0&end_mi=0&end_ap=a`
const horseman = new Horseman({
// cookiesFile: './cookies.txt',
diskCache: true,
diskCachePath: './browsercache',
timeout: 15000,
loadImages: false,
// proxyType: 'socks5',
// proxy: '127.0.0.1:9050',
ignoreSSLErrors: true
});
horseman
.userAgent(random_useragent.getRandom())
.on('error', function(msg){
reject()
}, reject)
.on('timeout', function(){
console.log(chalk.red(`Timeout for ${subject}!`))
reject()
}, reject)
.post('https://banner.aus.edu/axp3b21h/owa/bwckschd.p_get_crse_unsec', postData)
.wait(5000)
.catch(function(e) {
reject()
}, reject)
.evaluate(function(data){
var array = []
data.crns.forEach(function(item, i){
var a = $('a[href="/axp3b21h/owa/bwckschd.p_disp_detail_sched?term_in='+data.termID+'&crn_in='+item.crn+'"]').closest('tr').next().find('td[colspan="1"]').text()
array.push({crn: item, status: a})
})
return array;
}, {termID, subject, crns})
.then(function(data){
resolve(data)
return horseman.close();
})
})
}
//Socket handlers
socket.on(`checkCRN_${config.misc.secret}`, function(termID, crn, cb) {
console.log(chalk.blue(`Checking CRN ${crn}...`))
checkCRN(termID, crn, cb)
console.log(chalk.blue(`Done checking ${crn}.`))
})
socket.on(`crawlCRN_${config.misc.secret}`, function(termID, subject, crns, cb){
console.log(chalk.blue(`Crawling subject ${subject}...`))
fetchStatus(termID, subject, crns).then(function(body){
cb(false, body)
console.log(chalk.blue(`Done crawling ${subject}.`))
}).catch(function(){
cb(true, null)
console.log(chalk.blue(`Done crawling ${subject}.`))
})
})
socket.on('auth', function(){
socket.emit(`${config.misc.secret}`)
})