-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (42 loc) · 1.17 KB
/
index.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
const Stream = require('user-stream')
const GmailSend = require('gmail-send')
const config = require('./config.json')
const stream = new Stream(config.twitter)
const sender = new GmailSend({
user: config.gmail.user,
pass: config.gmail.pass,
to: config.gmail.to,
subject: 'NO SUBJECT',
})
stream.stream()
stream.on('connected', () => {
console.log('locked and dialed in to the stream...')
})
stream.on('data', data => {
if (data.user && data.user.screen_name === config.twitter.username) {
sendEmail(data)
}
})
stream.on('error', err => {
console.log(err)
})
function sendEmail (tweet) {
let url = 'UNKNOWN_ARTICLE'
if (data.entities && data.entities.urls.length > 0) {
let obj = data.entities.urls[0]
url = obj.expanded_url || obj.display_url || obj.url
}
const subject = config.subjects[Math.floor(Math.random() * config.subjects.length)]
const html = config.bodyMaker(url, tweet)
try {
console.log('sending email for...', data.text)
sender({ subject, html }, err => {
if (err) {
console.log('send error', err)
}
})
} catch (err) {
console.log('send error', err)
}
}
module.exports = () => `Hello :)`