-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathserver.js
65 lines (55 loc) · 1.79 KB
/
server.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
require('dotenv').config()
const util = require('util')
const express = require('express')
const logger = require('morgan')
const Twitter = require('twitter')
const cors = require('cors')
const socket = require('socket.io')
const { Server } = require('http')
const controllers = require('./controllers')
const { classifyTweetWithBayes } = require('./lib/classifier')
const classifyTweet = util.promisify(classifyTweetWithBayes)
const app = express()
const server = Server(app)
const io = socket(server)
// Express only serves static assets in production
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'))
}
app.use(cors())
app.use(logger('dev'))
app.get('/api/v1/tweets', controllers.tweets.get)
const client = new Twitter({
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token_key: process.env.ACCESS_TOKEN_KEY,
access_token_secret: process.env.ACCESS_TOKEN_SECRET
})
const searchOptions = {
track: 'we are hiring, looking for interns, vacancy, we have job available',
filter_level: 'low'
}
const stream = client.stream('statuses/filter', searchOptions)
stream.on('error', (error) => {
console.error('Twitter error occoured..', error)
})
io.on('connection', (socket) => {
socket.on('error', (error) => {
console.error('New socket Error', error)
})
})
stream.on('data', async (data) => {
const tweet = {}
if (data.lang === 'en') {
try {
const classifications = await classifyTweet(data.text)
tweet['id'] = data.id_str
tweet['first_class'] = classifications[0]
tweet['second_class'] = classifications[1]
await controllers.tweets.addTweet(tweet)
} catch (err) {
console.error(err)
}
}
})
server.listen(process.env.PORT || 8080, () => console.log('Server listening...'))