-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.js
118 lines (98 loc) · 3 KB
/
cache.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
var https = require('https');
var redis = require('redis'),
rpub = redis.createClient(),
rsub = redis.createClient(),
docstore = redis.createClient();
var config = require('./codelove-community.json');
rsub.subscribe('codelove-community');
rsub.on('message', function(channel, message) {
if (channel == 'codelove-community') {
switch(message) {
case 'cacheGists':
cacheGists();
break;
case 'cacheUsers':
cacheUsers();
break;
}
}
});
function scheduleCacheUsers() {
console.log('scheduling cacheUsers');
rpub.publish('codelove-community', 'cacheUsers');
};
function scheduleCacheGists() {
console.log('scheduling cacheGists');
rpub.publish('codelove-community', 'cacheGists');
};
module.exports.scheduleCacheGists = scheduleCacheGists;
module.exports.scheduleCacheUsers = scheduleCacheUsers;
function cacheUsers() {
console.log('executing cacheUsers');
var options = {
headers: {
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'codelove'
},
hostname: 'api.github.com',
method: 'GET',
path: '/orgs/codelove-org/public_members?client_id=' + config.client_id + '&client_secret=' + config.client_secret,
port: '443',
rejectUnauthorized: 'false',
requestCert: 'true'
}
var members_req = https.request(options, function (response) {
var datastring = '';
response.on('data', function(chunk) {
datastring += chunk;
});
response.on('end', function() {
docstore.set('codelove-community.members', datastring, redis.print);
});
});
members_req.end();
};
function cacheGists() {
gists = [ ];
docstore.get('codelove-community.members', function(err, data) {
if (!data) {
docstore.set('codelove-community.gists', '', redis.print);
return;
}
members = JSON.parse(data);
function nextMember(i) {
var member = members[i];
if (!member) {
gists = gists.sort(function(a,b) {
return new Date(a.updated_at) - new Date(b.updated_at);
}).reverse();
docstore.set('codelove-community.gists', JSON.stringify(gists), redis.print);
return;
}
var options = {
headers: {
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'codelove-community'
},
hostname: 'api.github.com',
method: 'GET',
path: '/users/' + member.login + '/gists?client_id=' + config.client_id + '&client_secret=' + config.client_secret,
port: '443',
rejectUnauthorized: 'false',
requestCert: 'true'
}
var gists_req = https.request(options, function (response) {
var datastring = '';
response.on('data', function(chunk) {
datastring += chunk;
});
response.on('end', function getPublicGists_end() {
gists = gists.concat(JSON.parse(datastring));
nextMember(i + 1);
});
});
gists_req.end();
};
nextMember(0);
});
};