-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtwc.js
More file actions
61 lines (46 loc) · 1.16 KB
/
twc.js
File metadata and controls
61 lines (46 loc) · 1.16 KB
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
var https = require('https');
module.exports = function() {
var DEFAULT_RANKING = 'unknown';
var currentRank = DEFAULT_RANKING;
var twcComicId = "18931";
function pullRank(cb) {
https.get("https://www.topwebcomics.com/rankimages/plaintext.aspx?comicid=" + twcComicId, function(res) {
var wData = '';
res.on('data', function(chunk) {
wData += chunk;
});
res.on('end', function() {
try {
var rank = wData;
cb(null, rank);
} catch (e) {
cb(e);
}
});
}).on('error', function(e) {
cb(e);
});
}
function rankTimer() {
pullRank(function(err, data) {
if (err) {
console.log(err);
} else {
try {
currentRank = data || (function() { console.log('no ranking??'); return DEFAULT_RANKING; }());
console.log((new Date()) + ": twc ranking is " + currentRank);
} catch (e) {
console.error(e.stack);
console.log((new Date()) + ": setting twc ranking to " + DEFAULT_RANKING);
}
}
setTimeout(rankTimer, 3600000); // call again in 60 minutes
});
}
rankTimer(); // run now, and then every 60 minutes
return {
rank: function() {
return currentRank;
}
};
};