forked from conube/juce-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
84 lines (64 loc) · 1.94 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
78
79
80
81
82
83
84
/**
* Main server API application
*
*
*/
var
url = require('url'),
http = require('http'),
fs = require('fs'),
Promise = require('bluebird');
Promise.promisifyAll(fs);
var server = http.createServer(
function(request, response) {
// check if the request is GET
if (request.method !== 'GET'
|| request.url === '/'
|| request.url === '/favicon.ico') {
statusResponse(404, response);
return;
}
// check if the request contains at least 2 parts to match the /{scraperName}/{protocolNumber}
var path = url.parse(request.url, true).pathname;
var parts = path.substring(1, path.length).split('/');
if (parts.length < 2) {
statusResponse(400, response);
return;
}
var scrapperName = parts[0].toLowerCase(); // to avoid issues
var firstParam = parts[1].toLowerCase();
var modulePath = './scrapers/' + scrapperName + '.js';
// try find scraper module based on the url
var findModule = fs.statAsync(modulePath);
findModule.then(function() {
// load module
var scraperModule = require(modulePath);
// create promise for scrap
var scraperPromise = scraperModule.scrap(firstParam);
scraperPromise.then(function(data) {
jsonResponse(response, data);
});
}).catch(function(err) {
// scraper load module error
console.error(scrapperName, 'load module error', err);
statusResponse(400, response);
});
}
);
var statusResponse = function(status, response) {
response.writeHead(status);
response.end();
};
var jsonResponse = function(response, data) {
response.writeHead(200, {
'Content-Type': 'application/json'
});
response.write(JSON.stringify(data));
response.end();
};
// flag for open ssl
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
var port = process.env.PORT || 9080;
server.listen(port, function() {
console.log('server started on port ' + port);
});