-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (85 loc) · 2.77 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
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
import Twit from 'twit'
import twitterBot from 'node-twitterbot'
import { analyze } from './sentiment'
import twitterCredentials from './twitter-credentials.json'
import fs from 'fs'
const ohh = fs.readFileSync('./ohh.gif', { encoding: 'base64' })
let T = new Twit(twitterCredentials)
let stream = T.stream('statuses/filter', { track: ['fuck'] })
let reactions = []
console.log('Loading images...')
const reactionsNames = fs.readdirSync('./reactions');
const imageList = reactionsNames.reduce((reducer, name) => {
reducer[name] = fs.readFileSync(`./reactions/${name}`, { encoding: 'base64' })
return reducer
}, {})
uploadImages()
async function uploadImages() {
try {
reactions = await Promise.all(
Object.values(imageList).map((image) => uploadMedia(image))
)
console.log('🤘')
start()
} catch(error) {
console.error(error)
}
}
function uploadMedia(image) {
return new Promise((resolve, reject) => {
T.post('media/upload', { media_data: image }, function (err, data, response) {
if(err) {
reject(err)
}
const mediaIdStr = data.media_id_string
const altText = '😁'
const metaParams = { media_id: mediaIdStr, alt_text: { text: altText } }
resolve({
mediaIdStr,
altText,
metaParams
})
})
})
}
function start() {
stream.on('tweet', async function (tweet) {
if (!tweet.retweeted_status && tweet.in_reply_to_screen_name && (tweet.user.screen_name !== tweet.in_reply_to_screen_name)) {
const analysis = await analyze(tweet.text)
const isNegativeTweet = analysis.some((tweetAnalysis) => {
const { magnitude, score } = tweetAnalysis.documentSentiment
return isNegativeSentence(magnitude, score)
});
if(isNegativeTweet) {
console.log('😁')
sendResponse(tweet.id_str, tweet.user.screen_name, tweet.in_reply_to_screen_name)
}
}
})
}
function isNegativeSentence(magnitude, score) {
return magnitude > 1 && score < 0
}
function sendResponse(in_reply_to_status_id, screenName, inRepplyScreenName) {
if(in_reply_to_status_id) {
const { mediaIdStr, metaParams } = reactions[Math.floor(Math.random() * reactions.length)];
T.post('media/metadata/create', metaParams, function (err, data, response) {
if (!err) {
let replyMentions = inRepplyScreenName ? `@${inRepplyScreenName} @${screenName}` : `@${screenName}`
const params = {
in_reply_to_status_id,
status: `${replyMentions} 😁`,
media_ids: [mediaIdStr]
}
console.log(params)
T.post('statuses/update', params, function (err, data, response) {
if(!err) {
console.log('🔥')
} else {
console.log('🚨', err)
}
})
}
})
}
}