-
Notifications
You must be signed in to change notification settings - Fork 1
/
sample_mirror.js
72 lines (54 loc) · 2.06 KB
/
sample_mirror.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
// Run this server so that client apps can connect to it and access data
bus = require('statebus').serve({
port: 9375
})
bus('*').on_save = function (obj) {
var key = obj.key
if (!(key in history))
history[key] = []
// If this doesn't match the latest item in history
if (history[key].length === 0
|| !matches(history[key][history[key].length-1], obj)) {
// Then clone it into the history
history[key].push(bus.clone(obj))
}
}
///////////////////////////////////////
// Synchronize with https://consider.it
/////////
// The {subdomain}.consider.it whose data you want to replicate
var subdomain = process.argv[2] || 'bitcoinclassic'
var request = require('request')
var history = {}
// Set to true if you want this node to poll the considerit server every 30 min
// for new data
var continuous = true
// The data that we want to fetch from considerit
keys = ['/proposals?all_points=true', '/subdomain', '/users']
// The all_points=true is so that the /proposals request includes all the published pro/con
// points for each returned proposal. In normal operation, the points are withheld, but
// accessible at fetch('/page/{proposal.slug}')
// Requests all the data we've specified from the considerit server
function refresh() {
for (idx in keys)
http_fetch(keys[idx])
// Note when the data was last replicated. Will be available in any client applications.
refreshed = bus.fetch('/refreshed')
if (!refreshed.refreshes)
refreshed.refreshes = []
refreshed.refreshes.unshift( new Date().getTime() )
bus.save(refreshed)
}
function http_fetch (key) {
request({ url: 'https://' + subdomain + '.consider.it' + key,
headers: {Accept:'application/json', 'X-Requested-With':'XMLHttpRequest'} },
function (err, response, body) {
bus.save.fire(JSON.parse(body))
})
}
function matches (a, b) { return false }
if (continuous)
setInterval(function () {
refresh()
}, 30 * 60 * 1000) // please keep this value respectful
refresh()