-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcron.js
51 lines (47 loc) · 1.74 KB
/
cron.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
var cronJob = require('cron').CronJob
, models = require('./models/models')
, users = require('./lib/users')
, request = require('request');
module.exports = function(app){
var User = app.set('db').model('user')
, githubApi = 'https://api.github.com/';
//drop records and update github data 5 seconds after launch
setTimeout(function(){
User.remove({}, updateGithubData);
}, 5*1000);
//update github data once per day
new cronJob('00 42 1 * * *', updateGithubData, null, true);
function updateGithubData () {
console.log('<<<<<<<< Updating github data');
users.forEach(function(batch){
batch.users.forEach(function(user){
user = user.toLowerCase();
//fetch user info from github
request.get({
url: githubApi + 'users/' + user
, headers: { 'User-Agent': 'Hacker-School-Github-Visualization' }
, qs: { access_token: app.set('githubToken') }
, json: true
}, function(e, response, body) {
if(!body.message) {
body.batch_id = batch.batch_id;
body.login_lower = user.toLowerCase();
User.update({login_lower: user}, body, {upsert: true}, function(e, count, item){});
request.get({
url: githubApi + 'users/' + user + '/repos'
, headers: { 'User-Agent': 'Hacker-School-Github-Visualization' }
, qs: {
access_token: app.set('githubToken')
}
, json: true
}, function(e, response, body) {
User.update({login_lower: user}, {$set: {repos: body}}, function(e, count, item){});
});
} else {
console.log(body.message);
}
});
});
});
}
}