forked from godgunman/fanschecker
-
Notifications
You must be signed in to change notification settings - Fork 1
/
clock.js
43 lines (37 loc) · 1.25 KB
/
clock.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
var moment = require('moment-timezone');
var request = require('request');
var mongoose = require('mongoose');
var Fans = require('./store/models').Fans;
process.env.TZ = 'Asia/Taipei';
// Here we find an appropriate database to connect to, defaulting to
// localhost if we don't find one.
var uristring =
process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost/fanschecker';
// Makes connection asynchronously. Mongoose will queue up database
// operations and release them when the connection is complete.
mongoose.connect(uristring, function (err, res) {
if (err) {
console.log ('ERROR connecting to: ' + uristring + '. ' + err);
} else {
console.log ('Succeeded connected to: ' + uristring);
}
});
var pageId = '310212962461242';
var url = 'http://graph.facebook.com/' + pageId;
var sampleRate = 60 * 1000;
setInterval(function(){
var r = request.get(url, function(err, res, body) {
var body = JSON.parse(body);
console.log(new Date(), 'likes:', body.likes);
var fans = new Fans({
likes: parseInt(body.likes)
});
fans.save(function (err) {
if (err) {
console.log('err:', err);
}
});
});
}, sampleRate);