-
Notifications
You must be signed in to change notification settings - Fork 2
/
data-sync.js
127 lines (111 loc) · 4.02 KB
/
data-sync.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const fs = require('fs'),
helpers = require(__dirname + '/helpers/general.js'),
Twit = require('twit'),
dataFilePath = __dirname + '/data/data.json',
botListFilePath = __dirname + '/data/bots.json';
let dataSync = {
reload: function(cb){
console.log('reloading data...');
let twit = new Twit({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
app_only_auth: true
});
twit.get('lists/members', {
owner_screen_name: process.env.TWITTER_USERNAME,
slug: process.env.TWITTER_LIST_NAME,
count: 5000,
skip_status: true
}, function(err, data, response) {
if (err){
console.log('error:', err);
}
else if (data && data.users){
let bots = [];
data.users.forEach( function (user){
bots.push(user.screen_name);
});
console.log(bots);
if (bots && bots.length){
twit.get('users/lookup', {
screen_name: bots.join(',')
}, function(err, data, response) {
if (err){
console.log(err);
} else {
let updatedBotData = [];
data.forEach(function(bot){
updatedBotData.push({
id_str: bot.id_str,
name: bot.name,
screen_name: bot.screen_name,
description: bot.description,
followers_count: bot.followers_count,
friends_count: bot.friends_count,
created_at: bot.created_at,
last_tweet_date: bot.status ? bot.status.created_at : null,
favourites_count: bot.favourites_count,
verified: bot.verified,
statuses_count: bot.statuses_count,
profile_background_image_url_https: bot.profile_background_image_url_https,
profile_image_url_https: bot.profile_image_url_https
});
});
// console.log(updatedBotData);
fs.writeFile(dataFilePath, JSON.stringify({
last_update: Date.now(),
data: updatedBotData.sort(helpers.sortByFollowersCount)
}), function (err) {
if (err){
console.log(err)
} else {
console.log('data file updated');
}
});
if (cb){
cb(null, updatedBotData);
}
}
});
}
}
});
},
sync: function(cb){
let botData = fs.readFileSync(dataFilePath),
dataSync = this;
try{
botData = JSON.parse(botData);
} catch (err){ console.log(err) }
if (botData){
// console.log(botData);
botData.data = botData.data.sort(helpers.sortByFollowersCount);
let syncIntervalMinutes = 60;
if (process.env.SYNC_INTERVAL_MINUTES && process.env.SYNC_INTERVAL_MINUTES > 15){
syncIntervalMinutes = parseInt(process.env.SYNC_INTERVAL_MINUTES);
}
var todayDate = new Date(),
lastCheckDate = new Date(botData.last_update),
expirationDate = new Date(lastCheckDate.getTime() + (syncIntervalMinutes * 60000));
console.log({
'lastCheckDate': lastCheckDate,
'syncIntervalMinutes' : `${syncIntervalMinutes} (${ (syncIntervalMinutes/60).toFixed(3) } hours)`,
'expirationDate': expirationDate,
'todayDate': todayDate,
'dataExpired': todayDate > expirationDate,
// 'botData': botData
});
if (!botData || !botData.data || !botData.data.length || todayDate > expirationDate){
/* Update data */
dataSync.reload(function(err, data){
cb(null, data);
});
} else {
if (cb){
cb(null, botData);
}
}
}
}
}
module.exports = dataSync;