-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (58 loc) · 1.54 KB
/
index.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
#!/usr/bin/env node
"use strict";
var QIITA_URL = "http://qiita.com/";
var cheerio = require('cheerio');
var request = require('request');
var Promise = require('bluebird');
var argv = require('minimist')(process.argv.slice(2));
if (!argv.u && !argv.o) {
console.log("Usage: qiitarace -u user1,user2,...");
console.log("Usage: qiitarace -o organization");
return
}
new Promise(function(resolve, reject) {
if (argv.u) {
return resolve(argv.u.split(","));
}
if (argv.o) {
request(QIITA_URL + "organizations/" + argv.o, function(err, res, body) {
if (err) return reject(err);
var $ = cheerio.load(body);
return resolve($('img.organizationMemberIcons_icon').map(function(i, elem) {
return $(this).attr('title');
}).get());
});
}
})
.then(function(users) {
return Promise.settle(users.map(function(user) {
return new Promise(function(resolve, reject) {
request(QIITA_URL + user, function(err, res, body) {
if (err) return reject(err);
var $ = cheerio.load(body);
return resolve({
user: user,
score: parseInt($('span.userPage_contributionCount').text(), 10)
});
});
});
}))
})
.then(function(results) {
results
.filter(function(result) {
return result.isFulfilled();
})
.map(function(result) {
return result.value();
})
.sort(function(a, b) {
return a.score < b.score;
})
.forEach(function(result) {
console.log("%s\t%d", result.user, result.score);
});
})
.catch(function(err) {
console.log(err);
});