-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
56 lines (43 loc) · 1.34 KB
/
server.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
const http = require("http")
const rp = require('request-promise');
const JSSoup = require('jssoup').default
const config = require('./config.json')
const parse = require('./parse')
const url = "https://recyclingservices.bromley.gov.uk/property/" + config.propertyid
const updateIntervalHours = 24
const updateIntervalms = updateIntervalHours * 60 * 60 * 1000
function Info() {
var theInfo = {"status": "not run yet"}
var doUpdate = function() {
console.log("Running update")
rp(url)
.then(function(html){
//success!
console.log("success: " + html)
theInfo = parse.parse(html)
})
.catch(function(err){
//handle error
console.log("error retrieving document: " + err)
theInfo = {"status": "error"}
});
}
var runUpdate = function() {
setTimeout(function() {
doUpdate() // update the data from source site
runUpdate() // set another timeout
}, updateIntervalms)
}
runUpdate() // set up timeout
doUpdate() // run initial update from source
return {
get: function() {
return JSON.stringify(theInfo)
}
}
}
var dataInfo = new Info()
http.createServer(function(req, res) {
res.write(dataInfo.get())
res.end()
}).listen(8080)