-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (58 loc) · 1.64 KB
/
Copy pathindex.js
File metadata and controls
62 lines (58 loc) · 1.64 KB
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
const twitter = require('./twitter');
require('dotenv').config();
const language = require('@google-cloud/language');
const client = new language.LanguageServiceClient();
const axios = require('axios');
getSentiment = async () => {
const allSentiments = [];
await twitter.getTweets()
.then(async tweets => {
for (status of tweets.statuses) {
await callSentimentApi(status.text)
.then(async sentiment => {
const score = sentiment.sentiment;
const magnitude = sentiment.magnitude;
// const id = await getTwitterJson(status.id_str).then(json => { return json; })
allSentiments.push({
sentiment: score,
magnitude,
x: Math.round(score * 100),
y: Math.round(magnitude * 100),
text: status.text,
id: status.id_str
});
return allSentiments;
});
}
});
return allSentiments;
}
callSentimentApi = (status) => {
const data = axios.get(`${process.env.API_URL}?tweet=${encodeURIComponent(status)}`)
.then(response => {
const data = response.data;
return data;
})
.catch(error => {
console.log(error);
});
return new Promise((resolve, reject) => {
data ? resolve(data) : reject('no data');
});
}
getTwitterJson = (id) => {
const json = axios.get(`https://publish.twitter.com/oembed?url=https://twitter.com/Interior/status/${encodeURIComponent(id)}`)
.then(response => {
const data = response.data.html;
return data;
})
.catch(error => {
console.log(error);
})
return new Promise((resolve, reject) => {
json ? resolve(json) : reject('no data');
});
}
module.exports = {
getSentiment
}