-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtweetsArchiver.js
187 lines (166 loc) · 6.84 KB
/
tweetsArchiver.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
var config = require('./config');
var Twit = require('twit')
var fs = require('fs');
var path = require('path');
var moment = require('moment');
var zipper = require('./classes/Zipper');
var loggerFunc = require('./classes/Logger');
var generateMd5 = require('./classes/GenerateMD5');
var rimraf = require('rimraf');
// consts
var VERSION = 0.1;
var ENV = process.env.NODE_ENV || 'development';
var TWEETS_PATH = config.paths.tweets;
var ARCHVIES_PATH = config.paths.archives;
var ARCHIVE_MODE = config.archiveMode; // flag for archiving a day folder to the archives path
var DELETE_ARCHIVE_SOURCE_DIR = config.archiveDeleteSrcDir; // if true will delete the original folder after archiving
// helper functions
function getRandomArbitrary(min, max) {
return parseInt(Math.random() * (max - min) + min, 10);
}
// Twit Lib init
var T = new Twit({
consumer_key: config.twitterAPI.consumerKey,
consumer_secret: config.twitterAPI.consumerSecret,
access_token: config.twitterAPI.accessToken,
access_token_secret: config.twitterAPI.accessTokenSecret,
timeout_ms: config.twitterAPI.timeoutMs
});
//var stream = T.stream('statuses/filter', { track: 'mango' }) // Do this in the future release
var stream = T.stream('statuses/sample');
var logger = loggerFunc(config.paths.logs, config.streamLogsPrefix);
logger.verbose('Init Tweets Archiver v' + VERSION + ' ENV: ' + ENV);
var minuteTweetsArray = [];
var bufferStartMinuteTimestamp = moment();
var programStartTimestamp = moment();
var programCurrentTimestamp = moment();
var writeTwitterArrayToZipFile = function (tweets, fileTimestamp) {
if (tweets.length === 0) {
logger.warn('No tweets, no file to save');
return;
}
var monthPath = fileTimestamp.format("DD-MM-YYYY");
var hourPath = fileTimestamp.format("HH");
var fileHHmm = fileTimestamp.format("HH-mm");
var postFixFileName = fileHHmm + '_' + Date.now() + '_' + getRandomArbitrary(100, 1000000) + '_' + tweets.length;
var zipFileName = postFixFileName + '.zip';
var insideZipFileName = postFixFileName + '.json';
var zipPath = path.join(TWEETS_PATH, monthPath, hourPath);
var zipFullName = path.join(zipPath, zipFileName);
var zipperConfig = {
type: 'memzip',
insideZipFileName,
fileData: JSON.stringify(tweets),
testArchive: true
};
var zipFile = new zipper(zipPath, zipFileName, zipperConfig, function (err, bytes) {
if (err) {
logger.error(`Error saving zip [${zipFullName}]`, err);
} else {
logger.info(`Saved [${zipFullName}] (${bytes}Kb) with [${tweets.length}] tweets`);
}
});
}
var writeDailyArchiveZipFile = function (dailyTimestamp) {
// Archive directory and delete it's contents if required by config
var dayDirFormat = dailyTimestamp.format("DD-MM-YYYY");
var dayZipFileName = dayDirFormat + "-tweets.zip";
var daySourcePath = path.join(TWEETS_PATH, dayDirFormat);
var dayZipFullName = path.join(ARCHVIES_PATH, dayZipFileName);
var zipperConfig = {
type: 'dirzip',
sourcePath: daySourcePath,
insideZipFilePath: dayDirFormat,
testArchive: true
};
var zipFile = new zipper(ARCHVIES_PATH, dayZipFileName, zipperConfig, function (err, bytes) {
if (err) {
logger.error(`Error saving daily archive zip [${dayZipFullName}]`, err);
} else {
generateMd5(path.join(ARCHVIES_PATH, dayZipFileName), function (err) {
logger.info(`Daily Archive Saved [${dayZipFullName}] (${bytes}Kb)`);
if (err) {
logger.error(err);
if (DELETE_ARCHIVE_SOURCE_DIR) {
logger.error('MD5 error, skipping delete action of tweets source path');
}
} else {
if (DELETE_ARCHIVE_SOURCE_DIR) {
rimraf(daySourcePath, function(err) {
if (err) {
logger.error(`Error deleting tweets source path: ${daySourcePath}`);
} else {
logger.info(`Tweets source path: ${daySourcePath} - deleted`);
}
})
}
}
});
}
});
}
stream.on('tweet', function (tweet) {
// case this is the first tweet
if (minuteTweetsArray.length === 0) {
bufferStartMinuteTimestamp = moment();
}
var bufferCurrentMinuteTimeStamp = moment();
var minuteTimestampDiff = (+bufferCurrentMinuteTimeStamp.format("mm")) - (+bufferStartMinuteTimestamp.format("mm"));
if (minuteTimestampDiff !== 0) {
// save original array for write file and clear the old one and push current tweet
var minuteTweetsArrayCopywrite = Object.assign([], minuteTweetsArray);
minuteTweetsArray = [];
minuteTweetsArray.push(tweet);
// save writefile timestamp and change the global start timestamp to current
var fileTimestamp = bufferStartMinuteTimestamp.clone();
bufferStartMinuteTimestamp = bufferCurrentMinuteTimeStamp;
// write minute archive file
writeTwitterArrayToZipFile(minuteTweetsArrayCopywrite, fileTimestamp, TWEETS_PATH);
if (ARCHIVE_MODE) {
var currentTimestamp = moment();
var dateDiff = currentTimestamp.startOf('day')
.diff(programCurrentTimestamp.startOf('day'), 'days');
//var dateDiff = currentTimestamp.diff(programCurrentTimestamp, 'seconds');
//if (dateDiff > 120) {
if (dateDiff > 0) {
logger.info('New daily tweets archive start');
writeDailyArchiveZipFile(programCurrentTimestamp);
programCurrentTimestamp = currentTimestamp.clone();
}
}
} else {
// add tweet to tweets array
minuteTweetsArray.push(tweet);
//console.log(tweet.text);
process.stdout.write(".");
}
});
stream.on('disconnect', (disconnectMessage) => {
logger.warn('* Connection terminated');
reject(disconnectMessage);
});
stream.on('connect', () => {
logger.info('* Connection Attempted')
});
stream.on('connected', () => {
logger.info('* Connection Successful')
});
stream.on('reconnect', (req, res, interval) => {
logger.info(`* Reconnecting in ${interval / 1000.0} seconds`);
});
stream.on('warning', (warning) => {
logger.warn(warning);
});
stream.on('parser-error', (err) => {
// incase un-auth, happens when you change time for example
logger.error('Parser Error', err);
});
stream.on('error', (err) => {
logger.error('Stream Error', err);
});
process.on("SIGINT", function () {
logger.info('* stopping stream listen');
stream.stop();
// write last minute file
writeTwitterArrayToZipFile(minuteTweetsArray, bufferStartMinuteTimestamp, TWEETS_PATH);
});