-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmain.js
299 lines (256 loc) · 8.03 KB
/
main.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
'use strict';
var fs = require('fs');
var StockerBot = require('./index.js');
var config = require('./config'); // update to point to your Twit credentials
var parse = require('csv-parse');
var async = require('async');
var csv = require('csv');
var admin = require("firebase-admin");
/*
*
* Parse command line arguments & set flags
*
*/
var symbol, count;
var firebase = false;
var verified = false;
var search = false;
var poll = false;
var search_poll = false;
var csvExport = false;
process.argv.forEach(function (val, index, array) {
// console.log(index + ': ' + val);
if (val == '--symbol' || val == '-s') { symbol = process.argv[index+1]; }
if (val == '--count' || val == '-c') { count = parseInt(process.argv[index+1]); }
if (val == '--firebase') { firebase = true; }
if (val == '--verified') { verified = true; }
if (val == '--search') { search = true; }
if (val == '--poll') { poll = true; }
if (val == '--search_poll') { search_poll = true; }
if (val == '--csv') { csvExport = true; }
});
/*
*
* Initalize Firebase account (update to point to your paths & credentials -- different for each user)
*
*/
var serviceAccount, db, ref;
if (firebase) {
console.log('Firebase enabled...Attempting to verify credentials.')
serviceAccount = require("./stockerbot-firebase-adminsdk-1yhwz-6e9672bd0a.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://stockerbot.firebaseio.com"
});
db = admin.database();
ref = db.ref('/');
console.log('Firebase account connected \x1b[42m successfully! \x1b[0m');
} else { console.log('Firebase disabled.'); }
/*
*
* Declare Global vars
*
*
*/
const ONE_MINUTE = 60*1000;
const ONE_HOUR = ONE_MINUTE*60;
const ONE_DAY = ONE_HOUR*24;
const WAIT_PERIOD = ONE_MINUTE*3;
const MAX_COUNT = 100;
const WATCHLIST_PATH = 'data/stocks.json';
const CSV_PATH = 'data/tweets.csv';
var stockerBot = new StockerBot(config);
var watchlist = []
if (poll || search_poll) { get_user_watchlist(); }
var influencers = ['MarketWatch', 'business', 'YahooFinance', 'TechCrunch',
'WSJ', 'Forbes', 'FT', 'TheEconomist', 'nytimes', 'Reuters', 'GerberKawasaki',
'jimcramer', 'TheStreet', 'TheStalwart', 'TruthGundlach',
'Carl_C_Icahn', 'ReformedBroker', 'benbernanke', 'bespokeinvest', 'BespokeCrypto',
'stlouisfed', 'federalreserve', 'GoldmanSachs', 'ianbremmer', 'MorganStanley', 'AswathDamodaran',
'mcuban', 'muddywatersre', 'StockTwits', 'SeanaNSmith'];
function get_user_watchlist() {
/*
*
* This function would get a user's watch list, but for testing & developing reasons
* it will just load NYSE top 100, SNP500, and a list of other stocks that I am
* curious about
*
*/
var obj = JSON.parse(fs.readFileSync(WATCHLIST_PATH, 'utf8'));
var symbol, name;
for (var key in obj) {
symbol = key;
name = obj[key];
watchlist.push([symbol, name]);
}
console.log("Number of Companies on watchlist: ", watchlist.length);
}
function write_to_firebase(tweet, child) {
/*
*
* Writes the tweet to the user's firebase realtime DB at the root
* of the DB using the tweet's ID as the key
*
*/
var id = tweet.id;
delete tweet.id;
if (typeof(tweet.url) == 'undefined') {tweet.url = ''}
ref.child(child).child(id).set({
text: tweet.text,
timestamp: tweet.created_at,
source: tweet.source,
symbols: tweet.symbols,
company_names: tweet.company_names,
url: tweet.url,
verified: tweet.verified
});
console.log('updated Firebase w new tweet ', '(', id ,') ', '\x1b[42m successfully!\x1b[0m');
}
function save(tweet, child) {
/*
*
* This function saves the tweet to a file called tweets.csv which is
* located at the path stored in the DATA_WRITE_PATH variable. If this file does
* not exist, then this function will create it at the designated path.
*
*/
if (csvExport) {
if (!fs.existsSync(CSV_PATH)) {
var header = 'id,text,timestamp,source,symbols,company_names,url,verified\n'
fs.writeFile(CSV_PATH, header, function(err) {
if(err) {
return console.log(err);
}
console.log(CSV_PATH, " file was created!");
});
}
}
var words = tweet.text.replace(/,/g , '').split(' ');
for (var i = 0; i < words.length; i++) {
words[i] = words[i].replace(/^\s+|\s+$/g, '');
}
var text = words.join(' ');
text = text.replace(/\r?\n|\r/g, ' ');
tweet.text = text;
if (csvExport) {
var line = tweet.id + ',' + text + ',' + tweet.created_at + ',' + tweet.source + ',' + tweet.symbols + ',' + tweet.company_names + ','+ tweet.url + tweet.verified +'\n';
fs.appendFile(CSV_PATH, line, function (err) {
if (err) { console.log('error saving data (append)', err); }
console.log('Tweet ', tweet.id, ' was saved \x1b[42m successfully! \x1b[0m');
});
}
if (firebase) { write_to_firebase(tweet, child); }
}
function uniqueify(a) {
/*
* Taken from https://stackoverflow.com/questions/1960473/get-all-unique-values-in-an-array-remove-duplicates
*
*/
var seen = {};
return a.filter(function(item) {
return seen.hasOwnProperty(item) ? false : (seen[item] = true);
});
}
function find_companies(screen_name, text) {
/*
*
* Aims to see if the tweet is pertaining to any one of the
* stocks in the user's watch list -- if so, then save to disk and tag them
* with associated symbols
*
*/
var companies = [];
var symbol, name, $symbol, $S, S, N, words
if (watchlist) {
for (var i=0; i < watchlist.length; i++) {
symbol = ' ' + watchlist[i][0] + ' ';
$symbol = ' ' + '$' + watchlist[i][0] + ' ';
name = watchlist[i][1];
$S = text.toLowerCase().includes($symbol.toLowerCase());
N = text.toLowerCase().includes(name.toLowerCase());
S = text.toLowerCase().split(' ').indexOf(symbol) > -1;
if ($S || S || N) { companies.push(watchlist[i]); }
}
}
return uniqueify(companies);
}
/*
* Attach event emitters
*
*/
stockerBot.on('newTweet', function(screen_name, tweet) {
/*
* Twitter's Tweet API (https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/intro-to-tweet-json)
*
*/
console.log('tweet from: ', screen_name, '|', tweet.id, '|', '(', tweet.created_at, ')', ' ---> ', tweet.text);
var urls
if (tweet.entities.urls) {
urls = tweet.entities.urls.map(url => url.expanded_url);
tweet.url = urls[0];
} else {
tweet.url = '';
}
var companies = find_companies(screen_name, tweet.text);
var classification = classify(screen_name, tweet.text);
tweet.source = screen_name;
if (companies.length > 0) {
var symbols = companies.map(c => c[0]);
var names = companies.map(c => c[1]);
tweet.symbols = symbols.join('-');
tweet.company_names = names.join('*');
tweet.verified = tweet.user.verified;
save(tweet, 'poll');
}
});
stockerBot.on('symbolTweets', function(symbol, tweets) {
console.log('Found ', tweets.length, ' tweets');
if (verified) {
tweets = tweets.filter(t => t.user.verified);
console.log('Found ', tweets.length, ' verified tweets');
}
var tweet;
for (var i=0; i<tweets.length; i++) {
tweet = tweets[i];
var urls;
if (tweet.entities.urls) {
urls = tweet.entities.urls.map(url => url.expanded_url);
tweet.url = urls[0];
} else {
tweet.url = '';
}
tweet.source = tweet.user.screen_name;
tweet.symbols = symbol;
tweet.company_names = '';
tweet.verified = tweet.user.verified;
save(tweet, 'search');
}
});
/*
*
* StockerBot Methods
*
*/
if (search) {
if (!symbol) { throw new Error('Must provide a publicly traded stock ticker for the search API\n EXAMPLE: > `npm run search -- -s AAPL`'); }
count = typeof(count) == "undefined" ? 10 : count;
stockerBot.searchSymbol(symbol, count);
}
if (poll) {
stockerBot.pollAccounts(influencers, WAIT_PERIOD);
}
if (search_poll) {
console.log('search_poll starting..');
/*
* Run once immediately, then start the interval to run every day
* after
*/
for (var i=0; i < watchlist.length; i++) {
stockerBot.searchSymbol(watchlist[i][0], MAX_COUNT);
}
setInterval(function() {
for (var i=0; i < watchlist.length; i++) {
stockerBot.searchSymbol(watchlist[i][0], MAX_COUNT);
}
}, ONE_DAY);
}