This repository has been archived by the owner on Apr 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.js
93 lines (74 loc) · 2.32 KB
/
scheduler.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
var got = require('got');
var gh_got = require('gh-got');
var cron_job = require('cron').CronJob;
var meta = require('metacran-node');
var last_key = "igraph";
var etags = { }
var crandb = process.env.CRANDB_URL || 'http://crandb.r-pkg.org';
var docsdb = process.env.DOCSDB_URL || 'http://docs.r-pkg.org:5984';
var gh_token = process.env.GH_TOKEN || process.env.GITHUB_TOKEN || null;
var nano = require('nano')(docsdb);
var db = nano.db.use('gh-stars');
// We run this every ten seconds. Get the next
// package, and update its star count.
var job = new cron_job('*/2 * * * * *', function() {
get_package(function(err, data) {
if (err) { console.log(err); return; }
var repo = meta.get_gh_repo(data);
if (repo) {
console.log("Updating ", data.Package, " repo: ", repo);
update_stars(data.Package, repo)
}
})
}, null, true, 'America/New_York');
function get_package(callback) {
get_package_name(function(err, data) {
if (err) { callback(err); return; }
var url = crandb + '/' + data;
got(url, function(err, data, res) {
if (err) { callback(err); return; }
callback(null, JSON.parse(data));
})
});
}
function get_package_name(callback) {
if (!last_key) {
// get first package
var url = crandb + '/-/desc?limit=1';
got(url, function(err, data, res) {
if (err) { callback(err); return; }
last_key = Object.keys(JSON.parse(data))[0];
callback(null, last_key);
})
} else {
// get next package
var url = crandb + '/-/desc?limit=2&startkey="' + last_key + '"';
got(url, function(err, data, res) {
if (err) { callback(err); return; }
last_key = Object.keys(JSON.parse(data))[1];
callback(null, last_key);
})
}
}
function update_stars(package, repo) {
gh_got('repos/' + repo, { token: gh_token }, function(err, data) {
if (err) {
console.log("Failed to update ", package, " ", repo);
return;
}
var no_stars = data.stargazers_count;
var doc = { 'date': new Date().toISOString(),
'stars': no_stars };
db.update = function(obj, key, callback) {
var db = this;
db.get(key, function (error, existing) {
if(!error) obj._rev = existing._rev;
db.insert(obj, key, callback);
});
}
db.update(doc, package, function(error, response) {
if (error) { console.log(error); return; }
});
})
}
module.exports = job;