-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtwitlog.js
More file actions
201 lines (182 loc) · 7.08 KB
/
twitlog.js
File metadata and controls
201 lines (182 loc) · 7.08 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
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
188
189
190
191
192
193
194
195
196
197
198
var util = require('util');
var fs = require('fs');
var _ = require('underscore')._;
var winston = require('winston');
var Twitconnect = require('./lib/twitconnect.js').Twitconnect;
winston.add(winston.transports.File, {filename: 'logs/tweetlog.log', maxsize:20971520, maxfiles:5, colorize:true});
var argv = require('optimist')
.usage('Usage: $0 --track=hashtag --db=DBName')
//.demand(['track', 'db'])
// .default('db', 'tweetlog') //db to store tweets in
.default('mode', 'stream') // poll or stream. If stream becomes unreliable may fall back to poll during operation
.default('initializeBackfill', false) //fetch as far back as we can on the search term to start
// .default('dbtype', 'sqlite')
// .default('logtype', 'user')
.argv;
//example of db = 'hastactweetlog'
//example of track = 'hastac2011,hastac'
var dbinfo = {};
var logconfig = JSON.parse(fs.readFileSync('./config/logconfig.json', 'utf8'));
argv = _.extend({}, logconfig, argv);
var tweetstore;
if(argv.dbtype == 'sqlite'){
winston.info('using sqlite tweetstore');
tweetstore = require('./tweetstore_sqlite.js');
dbinfo.name = argv.db;
}
else if(argv.dbtype == 'mongodb'){
winston.info('using mongodb tweetstore');
tweetstore = require('./tweetstore_mongodb.js');
dbinfo.name = argv.db;
dbinfo.address = '127.0.0.1';
dbinfo.port = 27017;
}
else{
winston.error("invalid dbtype specified for logging");
process.exit();
}
//Stored app credentials - application credentials + oauth keys associated with user
var ntwitCredentials = JSON.parse(fs.readFileSync('./config/credentials.json'));
var credentials = {
"consumer_key": ntwitCredentials.ntwitlog_consumer_key,
"consumer_secret": ntwitCredentials.ntwitlog_consumer_secret,
"access_token_key": ntwitCredentials.oauthAccessToken,
"access_token_secret": ntwitCredentials.oauthAccessTokenSecret
};
winston.verbose(util.inspect(credentials));
var twitterConfig = _.extend({}, {credentials:credentials}, argv);
var tc = new Twitconnect(twitterConfig);
tc.tweetCallback = function(tweet){
winston.verbose("Received streamed tweet");
//store the tweet
tweetstore.storeTweet(tweet, function(err){
if(err){
winston.error("Error storing tweet");
winston.error(err);
}
});
//winston.info(util.inspect(tweet, false, null, true));
try{
if(tweet.from_user){
winston.info(tweet.created_at + ' : ' + tweet.from_user + ': ' + tweet.text);
}
else{
winston.info(tweet.created_at + ' : ' + tweet.user.screen_name + ': ' + tweet.text);
}
}
catch(e){
winston.error("Error showing streamed tweet");
winston.error(e);
winston.error(util.inspect(tweet));
}
};
var cleanexit = function(){
tweetstore.closeStore();
};
/*process.on('uncaughtException', function(err){
winston.info(err);
});
*/
//startListening();
if(argv.fillUserStream){
winston.info("FILLING USER STREAM");
tweetstore.init(dbinfo, {}, function(){
winston.verbose("tweetstore initiated");
tc.twit.verifyCredentials(function(err, data){
if(err){
winston.error("Error verifying twitter credentials. Have you fetched an oauth token yet?");
winston.error(err);
winston.verbose(data);
process.exit();
}
winston.info("Credentials verified okay");
winston.verbose(util.inspect(data));
winston.info("Pulling full user timeline");
var screen_name = '';
if(argv.screen_name) screen_name = argv.screen_name;
tc.pullFullUserTimeline(screen_name, {}, function(err, tweets){
_.each(tweets, function(tweet, ind){
tc.tweetCallback(tweet);
});
winston.info("Finished pulling full user timeline");
});
});
});
}
else if(argv.backlog){
winston.info("FILLING BACKLOG");
tweetstore.init(dbinfo, {}, function(){
winston.verbose("tweetstore initiated");
if(argv.username){
tc.fillTimelineFromRest(argv.username, {}, function(err, tweets){
_.each(tweets, function(tweet, ind){
tc.tweetCallback(tweet);
});
winston.info("Done filling user timeline backlog");
});
}
else if(argv.search){
tc.fillSearchFromRest(argv.search, {}, function(err, tweets){
_.each(tweets, function(tweet, ind){
tc.tweetCallback(tweet);
});
winston.info("Done filling search backlog");
});
}
else {
tc.fillTimelineFromRest(null, {}, function(err, tweets){
_.each(tweets, function(tweet, ind){
tc.tweetCallback(tweet);
});
winston.info("Done filling home timeline backlog");
});
}
});
}
else{
winston.info("Filling userstream after the most recent tweet, then streaming tweets");
//winston.info('Tracking ' + argv.track + ' logging into DB ' + argv.db);
tweetstore.init(dbinfo, {}, function(){
winston.verbose("tweetstore initiated, ");
//backfill log of tweets in home timeline after the most reent tweet we have record of
tweetstore.fetchRecent(1, function(err, tweets){
if(err){
winston.error("Error finding most recent tweet from storage");
}
var recentTweet;
if(argv.logtype === 'user'){
if(tweets.length){
recentTweet = tweets[0];
tc.fillTimelineFromRest(null, {since_id: recentTweet.id_str}, function(err, tweets){
_.each(tweets, function(tweet, ind){
tc.tweetCallback(tweet);
});
winston.info("Done filling user timeline backlog");
//Fill tracking backlog
_.each(argv.track, function(val, ind){
tc.fillSearchFromRest(val, {since_id: recentTweet.id_str}, function(err, tweets){
_.each(tweets, function(tweet, ind){
tc.tweetCallback(tweet);
});
});
});
});
}
}
else if(argv.logtype === 'track' || argv.logtype === 'search'){
if(tweets.length){
recentTweet = tweets[0];
_.each(argv.track, function(val, ind){
tc.fillSearchFromRest(val, {since_id: recentTweet.id_str}, function(err, tweets){
_.each(tweets, function(tweet, ind){
tc.tweetCallback(tweet);
});
});
});
}
}
//start streaming
tc.startListening();
});
});
}