-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathworker.js
44 lines (38 loc) · 1.21 KB
/
worker.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
require('dotenv').config()
const Twitter = require('twitter')
const admin = require('firebase-admin')
const serviceAccount = process.env.FIREBASE_PRIVATE_KEY
? JSON.parse(process.env.FIREBASE_PRIVATE_KEY)
: require('./private-key')
const client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
})
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: 'https://reactbkk-tweets.firebaseio.com'
})
const db = admin.database()
const ref = db.ref('tweets/reactbkk')
console.log('Streaming started...')
const stream = client.stream('statuses/filter', {
track: 'reactbkk,reactbkk3,reactbnk,reactbnk3'
})
stream.on('data', function (event) {
if (event.delete && event.delete.status) {
ref.child(padId(event.delete.status.id_str)).remove()
return
}
if (!event.id_str) {
return
}
const id = padId(event.id_str)
console.log(`[${new Date()}] Received tweet ${event.id_str}`)
ref.child(id).set(event)
})
function padId (id) {
while (id.length < 20) id = `0${id}`
return id
}