-
Notifications
You must be signed in to change notification settings - Fork 315
/
scrape.js
61 lines (52 loc) · 1.89 KB
/
scrape.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
var _ = require('lodash');
var config = require('config');
var fbCookie = config.get('fbCookie');
var fbSleep = require('fb-sleep');
var userService = require('./src/server/services/user');
var TEN_MINUTES = 1000 * 60 * 10;
var pollingInterval = (config.pollingInterval * 1000) || TEN_MINUTES;
function getRandomDelay() {
return _.random(pollingInterval * 0.9, pollingInterval);
}
function getFormattedUsers(users) {
return _(users)
.map(function(timestamp, userId) {
return {
userId: userId,
timestamp: timestamp * 1000
};
})
.value();
}
function getRecentlyActiveUsers(users, since) {
users = getFormattedUsers(users);
var timestampDiff = getTimestampDiff(users);
return users.filter(function(user) {
return user.timestamp >= (since - timestampDiff);
});
}
function getTimestampDiff(users) {
var mostRecentTimestamp = _(users)
.orderBy('timestamp')
.map('timestamp')
.last();
return Date.now() - mostRecentTimestamp;
}
function getAndSaveActiveUsers(config, since) {
fbSleep.getUsers(config)
.then(function(users) {
var activeUsers = getRecentlyActiveUsers(users, since);
console.log(new Date().toLocaleString(), ' - Active users: ', activeUsers.length, '/', _.size(users));
return userService.saveUsers(activeUsers);
})
.catch(function(err) {
console.error(new Date().toLocaleString(), 'An error occured while scraping. Please check to make sure your development.json config is correct', err);
})
.then(function() {
var since = Date.now();
setTimeout(getAndSaveActiveUsers, getRandomDelay(), config, since);
})
.done();
}
console.log('Polling every', pollingInterval/1000, 'seconds');
getAndSaveActiveUsers(fbCookie, Date.now() - pollingInterval);