-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
82 lines (72 loc) · 2.33 KB
/
index.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
var request = require('request');
var parseString = require('xml2js').parseString;
var express = require('express')
var uuid = require('uuid/v1');
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: 'search-cs499a4-utgechbh7gsxzcsg5gonutnntq.us-west-1.es.amazonaws.com',
log: 'info'
});
var healthCheck = function () {
client.ping({
requestTimeout: 5000
}, function (error) {
if (error) {
console.trace('Elasticsearch cluster is down!');
} else {
console.log('All is well');
}
});
}
var uploadToElastic = function (items) {
for (var i = 0; i < items.length; ++i) {
client.create({
index: 'universal',
type: 'waitTime',
id: uuid(),
body: items[i]
}, function (error, response) {
if (error) {
console.error(error);
}
});
}
console.log("Upload finished");
};
function fetchWaitingtimes() {
request('http://www.universalstudioshollywood.com/waittimes/?type=all&site=USH', function (error, response, body) {
if (!error && response.statusCode == 200) {
var upload = [];
parseString(body, function (err, result) {
var items = result.rss.channel[0].item;
for (var i = 0; i < items.length; ++i) {
var ride = JSON.stringify(items[i].description[0]);
//if (items[i].title[0] !== 'Last updated' && ride.match(/[APap][mM]/)) {
if(ride.indexOf('min') > -1){
upload.push({
'time': Date.now(),
'title': items[i].title[0],
'waittime': parseInt(ride.match(/\d/g).join(""))
});
}
}
});
uploadToElastic(upload);
}
})
}
var wait = 600000 // Every 10 mins
var waitTimer = function () {
console.log('Waiting 10 mins before fetching wait times.')
setInterval(function () {
fetchWaitingtimes();
}, wait)
};
var healthTimer = function () {
console.log('Waiting 11 mins before performing health check.')
setInterval(function () {
healthCheck();
}, wait + 60000)
};
waitTimer();
healthTimer();