forked from bastinat0r/Spaceapi_Visualisation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
putStatusToCouch.js
131 lines (120 loc) · 3.04 KB
/
putStatusToCouch.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
var http = require('http');
var https = require('https');
var util = require('util');
var crypto = require('crypto');
var couch = require('./couch.js');
var timers = require('timers');
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"
function getHttpAndHttps(url, cb) {
if(/^https/.test(url)) {
https.get(url, cb).on('error', function(e) {
util.puts(JSON.stringify(e));
util.puts(url);
cb(null);
}).on('error', function(e) {
util.puts(e);
});
} else {
http.get(url, cb).on('error', function(e) {
util.puts(JSON.stringify(e));
util.puts(url);
cb(null);
}).on('error', function(e) {
util.puts(e);
});
}
}
function putToCouch() {
var spaces = http.get('http://localhost:5984/spaces/_design/all/_view/json', function(res) {
var data = "";
res.on('data', function(chunk){
data = data + chunk;
});
res.on('end', function() {
spaces = JSON.parse(data).rows;
util.puts(JSON.stringify(spaces));
getListItems(spaces);
});
}).on('error', function(e) {
util.puts('Couch Response Error');
util.puts(e);
});
}
function getListItems(spaces) {
var current = spaces.pop();
if(!current)
return;
util.puts(current.key);
timers.setTimeout(getListItems, 3000, spaces);
getHttpAndHttps(current.key, function(res) {
if(res == null) {
return;
}
data = "";
res.on('data', function(chunk){
data = data + chunk;
});
res.on('end', function() {
try {
data = JSON.parse(data);
// util.puts(util.inspect(data));
var spacedate = {
space : data.space,
open : false,
lastchange : (new Date()).getTime() / 1000,
}
if(data.lastchange)
spacedate.lastchange = data.lastchange;
if(data.open) {
spacedate.open = true;
}
if(data.state) {
if(data.state.lastchange) {
spacedate.lastchange = data.state.lastchange;
}
if(data.state.open) {
spacedate.open = true;
}
}
util.puts(util.inspect(spacedate));
/* get last info and compare lastchange */
http.get('http://localhost:5984/' + current.id + '/_design/space/_view/all', function(res) {
var data = "";
res.on('data', function(chunk) {
data = data + chunk;
});
res.on('end', function() {
var answer = JSON.parse(data).rows;
if(answer && answer.length > 0) {
var last = answer.pop();
while(last.key > (new Date()).getTime()) {
util.puts('ERR: DB-Entry in the future');
util.puts(util.inspect(last));
last = answer.pop();
}
if(last.value.open == spacedate.open) {
util.puts("Already have this: ");
util.puts(util.inspect(last));
return;
}
}
couch.update(spacedate, "/" + current.id + "/", function(res) {
res.on('data', util.puts);
res.on('end', function() {
});
});
});
});
} catch (e) {
util.puts(current.key);
}
});
});
};
function hash(str) {
var hash = crypto.createHash('sha256');
hash.update("" + str);
return hash.digest('hex');
};
putToCouch();
timers.setInterval(putToCouch, 1000 * 60 * 10);