-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.js
38 lines (32 loc) · 1014 Bytes
/
scraper.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
var BROWSER_INSTANCES = 3;
var horseman = require('node-horseman');
var async = require('async');
var url = 'https://kortladdning3.chalmerskonferens.se/default.aspx';
var options = {
loadImages: false,
//switchToNewTab: true,
timeout: 10000
};
//var browser = new horseman(options);
var queue = async.queue(function(cardNumber, callback) {
var browser = new horseman(options);
browser.open(url)
.type('#txtCardNumber', cardNumber)
.click('#btnNext')
.waitForNextPage()
.text('#txtPTMCardValue')
.then((value) => {
if (!value)
return callback({status: 400, message: 'Invalid card number'});
callback(false, Math.round(parseInt(value)));
}).finally(function() {
browser.close();
}).on('error', function(e) {
return callback({status: 500, message: 'The scraper gave the following error: ' + e});
});
}, BROWSER_INSTANCES);
module.exports = {
queryCardAmount: (cardNumber, callback) => {
queue.push(cardNumber, callback);
}
};