-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcarsharing.js
60 lines (48 loc) · 1.35 KB
/
carsharing.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
var mongo = require('mongodb');
var request = require('request');
var db = new mongo.Db('carsharing', new mongo.Server('localhost', 27017), {safe:false});
var optionsCar2Go = {
url: 'https://www.car2go.com/api/v2.1/vehicles',
qs: {'loc':'milano', 'oauth_consumer_key':'car2gowebsite', 'format':'json'},
json: 'body'
};
var optionsEnjoy = {
url: 'https://enjoy.eni.com/get_vetture',
method: 'POST',
strictSSL: false,
json: 'body'
};
var minutes = 2,
the_interval = minutes * 60 * 1000;
db.open(function(){
//insert Car2Go data
db.collection('car2go', function(err, collection){
setInterval(function() {
request(optionsCar2Go, function(err, res, body) {
if(err){console.log(err)}
else{
var data = {
'date' : new Date(),
'values' : body['placemarks']
};
collection.insert(data, {safe:true}, function(){});
}
});
}, the_interval);
});
//insert Enjoy data
db.collection('enjoy', function(err, collection){
setInterval(function() {
request(optionsEnjoy, function(err, res, body) {
if(err){console.log(err)}
else{
var data = {
'date' : new Date(),
'values' : body
};
collection.insert(data, {safe:true}, function(){});
}
});
}, the_interval);
});
});