-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathstarsIndexer.js
86 lines (70 loc) · 2.82 KB
/
starsIndexer.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
/**
* GitHub stars indexer. Collects stars for a given user.
* Usage:
* node starsIndexer followers stars --tokens="A,B,C"
*
* Where:
* - followers: leveldb database name with indexed followers (produced by followersIndexer)
* - stars: leveldb database name where to save followers -> projects records.
* - tokens: Comma separated list of github access tokens (could be just one).
*
* Note: We treat users who gave more than 600 stars as users who gave 0 stars.
* Theortically the more stars someone gives to projects, the less is the value
* of a star, thus they can be ignored for similar projects constructions. In
* practice, there is approx. 2% of GitHub users who gave more than 600 stars.
*/
var Promise = require("bluebird");
var tokens = require('./lib/tokens')();
if (tokens.enabled === 0) {
printGenericHelp();
return -1;
}
var githubClient = require('./lib/ghclient')(tokens);
console.log('Loading databases...');
var followersDB = loadDB(process.argv[2], true);
var starsDB = loadDB(process.argv[3]);
Promise.all([getUniqueFollowers(followersDB), starsDB.getAllKeys()])
.spread(getRemainingFollowers)
.then(getStarsForRemainingFollowers);
function getRemainingFollowers(allFollowers, indexedFollowers) {
var indexed = {};
indexedFollowers.forEach(function (f) { indexed[f] = 1; });
var remaining = [];
allFollowers.forEach(function (f) { if (!indexed[f]) remaining.push(f); });
console.log('Total unique followers: ', allFollowers.length);
console.log('Processed followers so far:', indexedFollowers.length);
console.log('Remaining followers: ', remaining.length);
return remaining;
}
function getStarsForRemainingFollowers(remainingFollowers) {
var indexStars = require('./lib/indexStars');
indexStars(remainingFollowers, starsDB, githubClient);
}
function printGenericHelp() {
[
'GitHub stars indexer. Collects stars for a given user.',
'Usage:',
' node starsIndexer followers stars --tokens="A,B,C"',
'',
'Where:',
' - followers: leveldb database name with indexed followers (produced by followersIndexer)',
' - stars: leveldb database name where to save followers -> projects records.',
' - tokens: Comma separated list of github access tokens (could be just one).',
].forEach(function (line) { console.log(line); });
}
function loadDB(name, required) {
var fs = require('fs');
if (required && !fs.existsSync(name)) throw new Error('Cannot load required database ' + name);
return require('./lib/ldb')(name);
}
function getUniqueFollowers(followersDB) {
var uniqueFollowers = {};
return followersDB.forEach(recordProjectFollowers)
.then(function () { return Object.keys(uniqueFollowers); });
function recordProjectFollowers(projectName, followers) {
followers.forEach(addFollower);
}
function addFollower(name) {
uniqueFollowers[name] = 1;
}
}