-
Notifications
You must be signed in to change notification settings - Fork 8
/
crawl_github.js
215 lines (185 loc) · 8.46 KB
/
crawl_github.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
const Account = require('./configs/account');
const HolyShit = require('./helpers/top_coders');
const Utils = require('./helpers/my_utils');
const User = require('./crawl_user');
const Request = require('request');
const Promise = require('bluebird');
const Firebase = require('firebase');
var ref = new Firebase(Account.firebase_url);
var user_events_ref = ref.child("user_events");
var user_ranking_info_ref = ref.child("user_ranking_info");
var crawl_github_auth = function (production) {
ref.authWithCustomToken(Account.firebase_secrets, function (error, authData) {
if (error) {
console.log("Login Failed!", error);
terminate_app();
} else {
console.log("Login Succeeded!", authData);
crawl_github(production);
}
});
}
var crawl_github = function (production) {
var github_info = {
'members_list': [],
'repository_list': []
}
var current_time = Utils.get_current_timestamp();
var funcs = Promise.resolve(Utils.make_range(1, 10).map((n) => makeRequest(make_option(n), 'members_list')));
funcs
.mapSeries(iterator)
.catch(function (err) {
console.log(err);
})
.finally(function () {
var user_events = [];
// Crawl BitTiger members
var members_list = github_info['members_list'];
for (var i = 0; i < members_list.length; ++i) {
members_list[i]['organization'] = 'bittiger';
user_events.push(User.crawl_user(members_list[i]));
}
// Crawl top coders
for (var i = 0; i < HolyShit.top_coders.length; i++) {
user_events.push(User.crawl_user(HolyShit.top_coders[i]));
}
// Wait all the crawling to finish
Promise.all(user_events)
.catch(function (err) {
console.log(err);
})
.then(function (member_events) {
// sort the members based on events
member_events.sort(function (a, b) {
if (b['Total'] != a['Total']) {
return b['Total'] - a['Total'];
} else if (b['PushEvent'] != a['PushEvent']) {
return b['PushEvent'] - a['PushEvent'];
} else if (b['PullRequestEvent'] != a['PullRequestEvent']) {
return b['PullRequestEvent'] - a['PullRequestEvent'];
} else if (b['CreateEvent'] != a['CreateEvent']) {
return b['CreateEvent'] - a['CreateEvent'];
} else {
return b['ForkEvent'] - a['ForkEvent'];
}
});
// retrieve members' last ranking from Firebase
var previous_rankings_promise = retrieve_previous_user_ranking();
previous_rankings_promise
.catch(function (err) {
console.log(err);
})
.then(function (previous_rankings) {
for (var i = 0; i < member_events.length; i++) {
var user = member_events[i].login;
var current_ranking = i + 1;
if (previous_rankings[user]) {
var ranking_records = previous_rankings[user];
var last_ranking = ranking_records[ranking_records.length - 1].ranking;
member_events[i].ranking_change = current_ranking - last_ranking;
previous_rankings[user].push({
'timestamp': current_time,
'ranking': current_ranking
});
// Adjust the previous ranking range. If we have more than 100 records, we'll keep the latest 30 records.
if (previous_rankings[user].length > 100) {
var pre_range = previous_rankings[user];
var pre_range_len = pre_range.length;
var lastest_30_range = previous_range.slice(pre_range_len - 30, pre_range_len);
previous_rankings[user] = lastest_30_range;
}
} else {
// user not exists in last rankings
previous_rankings[user] = [{
'timestamp': current_time,
'ranking': current_ranking
}];
member_events[i].ranking_change = 'new';
}
var len = previous_rankings[user].length;
var max_num = 30;
if (len <= max_num) {
member_events[i].ranking_history = previous_rankings[user];
} else {
var last_few_records = previous_rankings[user].slice(len - max_num, len);
member_events[i].ranking_history = last_few_records;
}
}
// update members' ranking records to Firebase
if (production) {
update_previous_user_ranking(previous_rankings);
}
// retrieve the top 25 users' information
top25_members = member_events.slice(0, 25);
console.log('Updating the database...');
promise_events_update = user_events_ref.child('events').set(top25_members);
promise_time_udpate =
user_events_ref.child('created_time').set(current_time);
Promise.all([promise_events_update, promise_time_udpate])
.catch(function (err) {
console.log(err);
})
.finally(terminate_app);
});
});
})
function iterator(f) {
return f()
}
function makeRequest(option, key) {
return function () {
return new Promise(function (fulfill, reject) {
Request(option, function (error, response, body) {
if (error) {
reject(error);
} else if (body.length == 0) {
reject('page empty');
} else {
github_info[key] = github_info[key].concat(body);
fulfill(body);
}
})
});
};
}
function make_option(page_number) {
return {
url: 'https://api.github.com/orgs/bittigerInst/members', // URL to hit
qs: { // Query string data
page: page_number
},
method: 'GET', // Specify the method
headers: { // Define headers
'User-Agent': 'request'
},
auth: { // HTTP Authentication
user: Account.username,
pass: Account.password
},
json: true
};
}
}
function terminate_app() {
console.log("Finished!");
//process.exit();
}
function retrieve_previous_user_ranking() {
return new Promise(function (fulfill, reject) {
user_ranking_info_ref.once("value", function (user_ranking_info) {
var result;
if (user_ranking_info.val() == null) {
// Initialize the data
console.log("user_ranking_info == null");
result = {};
} else {
result = user_ranking_info.val();
}
fulfill(result);
});
});
}
function update_previous_user_ranking(new_records) {
user_ranking_info_ref.set(new_records);
}
exports.crawl_github = crawl_github_auth;