forked from davidfekke/wallstreetbets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter_apple.js
49 lines (37 loc) · 1.41 KB
/
twitter_apple.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
import twitter from 'twitter';
import PG from 'pg';
import Sentiment from 'sentiment';
const sentiment = new Sentiment();
const symbolPredicate = 'AAPL';
const Pool = PG.Pool;
const pool = new Pool({
user: 'postgres',
host: 'localhost',
database: 'etfdb',
password: 'password',
port: 5432
});
const result = await pool.query('SELECT id, symbol FROM stock WHERE symbol = $1;', [symbolPredicate]);
const stock_id = result.rows[0].id;
const client = new twitter({
consumer_key: process.env.twitter_ck,
consumer_secret: process.env.twitter_cs,
access_token_key: process.env.twitter_atk,
access_token_secret: process.env.twitter_ats,
});
client.stream('statuses/filter', {track: '$' + symbolPredicate }, function(stream){
stream.on('data', async function(tweet) {
//console.log(tweet);
const sent = sentiment.analyze(tweet.text);
console.log(`Score: ${sent.score}; ${tweet.text}`);
await insertMention(pool, stock_id, tweet);
});
stream.on('error', function(error) {
console.log(error);
});
});
async function insertMention(pool, stock_id, tweet) {
const insertQuery = 'INSERT INTO mention (stock_id, dt, message, source, url) VALUES ($1, NOW(), $2, $3, $4);';
const full_link = `https://twitter.com/${tweet.user.screen_name}/status/${tweet.id_str}`;
await pool.query(insertQuery, [stock_id, tweet.text, 'twitter', full_link]);
}