-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
163 lines (136 loc) · 3.98 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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
var express = require('express')
var app = express()
var https = require('https')
var http = require('http')
var fs = require('fs')
var qs = require('qs')
var config = require('./config.json')
var request = require('sync-request')
const Markov = require('libmarkov')
var unescape = require('unescape')
var bodyParser = require('body-parser');
function generateMarkov(comments_json_path) {
var comments_json = JSON.parse(fs.readFileSync(comments_json_path, 'utf8'))
var text = ""
for (var i = 0; i < comments_json.length; i++) {
text += comments_json[i].text + " "
}
var generator = new Markov(text);
var markovset = new Set([]);
var logger = fs.createWriteStream('markov.js', {
flags: 'w'
})
while (markovset.size < 11) {
markovset.add(generator.generate(1).concat("\n"))
}
logger.write('var markovlist = [')
markovset.forEach(function(value) {
logger.write('`'.concat(value).concat('`'))
logger.write(',')
})
logger.write(']')
logger.end()
}
function positive(s) {
if (s == undefined || s == "") return -1;
//return Math.random(0,1);
var comment = {
"documents": [
{
"language": "en",
"id": "0",
"text": s
}
]
}
var options = {
"host": "westus.api.cognitive.microsoft.com",
"port": 443,
"path": "/text/analytics/v2.0/sentiment",
"headers": {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(comment),
"Ocp-Apim-Subscription-Key": config.microsoftAPIKey
},
"method": "POST"
}
var request = require('sync-request')
var res = request('POST', 'https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment', {
json: comment,
headers: {"Ocp-Apim-Subscription-Key": config.microsoftAPIKey}
})
data = JSON.parse(res.getBody('utf8'))
score = data['documents'][0]['score']
return score
}
function getComments(videoId) {
var get_params = qs.stringify({
"part": "id,replies,snippet",
"videoId": videoId,
"key": config.apiKey
})
var url = "https://www.googleapis.com/youtube/v3/commentThreads?".concat(get_params)
var commentList = []
var get_req = https.request(url, function (res) {
var rawData = ""
res.setEncoding('utf8')
res.on('data', (chunk) => rawData += chunk)
res.on('end', function() {
var data = JSON.parse(rawData)
for (key in data['items']) {
if (key > 250) break;
var comment = {}
var string = data['items'][key]['snippet']['topLevelComment']['snippet']['textDisplay']
comment.text = unescape(string)
comment.sentiment = positive(string)
if (comment.sentiment == -1) continue; // error from MS api
comment.replies = []
if (data['items'][key].hasOwnProperty('replies')) {
for (key_replies in data['items'][key]['replies']['comments']) {
var text_stuff = unescape(data['items'][key]['replies']['comments'][key_replies]['textDisplay'])
var sentiment_score = positive(text_stuff)
if (sentiment_score == -1) continue; // error from MS api
comment.replies.push([
{
'text': text_stuff,
'sentiment': sentiment_score
}
])
}
}
commentList.push(comment)
}
var str = JSON.stringify(commentList)
fs.writeFile("comments.json", str, function(err) {
if (err) {
console.log("error writing file ", err)
}
})
})
})
get_req.write(get_params)
get_req.end()
}
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
app.use(express.static(__dirname));
app.use(express.static("public"));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/force_directed.html')
});
app.get('/force_directed.html', function (req, res) {
res.sendFile(__dirname + '/force_directed.html')
});
app.get('/comments.json', function (req, res) {
res.sendFile(__dirname + '/comments.json')
});
app.post('/', function (req, res) {
getComments(req.body.videoId)
generateMarkov("comments.json")
setTimeout(function() {
res.sendFile(__dirname + '/force_directed.html')
}, 5000); // pretend CMU wifi is slow
});
app.listen(8000, function() {
console.log('Listening on port 8000')
});