-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
154 lines (136 loc) · 4.79 KB
/
app.js
File metadata and controls
154 lines (136 loc) · 4.79 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
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
/**
* Copyright 2015. All Rights Reserved. Swaha Miller.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
var watson = require('watson-developer-cloud');
var config = require('./config');
var https = require('https');
var bloggerConfig = config.blogger;
var host = bloggerConfig.host;
var blogIdReadFrom = bloggerConfig.blog_id_read_from;
var postId = bloggerConfig.post_id;
var apiKey = bloggerConfig.api_key;
var blogIdWriteTo = bloggerConfig.blog_id_write_to;
var accessToken = bloggerConfig.access_token;
var fields = '&fields=url,title,content,labels';
var path = '/blogger/v3/blogs/' + blogIdReadFrom + '/posts/' + postId + '?key=' + apiKey;
var toneAnalyzer = watson.tone_analyzer({
version: 'v1',
username: 'e36a3037-b3f6-40d0-9c40-c5d4ee274277',
password: 'skzoNAmkClT6'
});
var addNewBlog = true;
var resultToConsole = true;
function getContent(host, path, process_content) {
var options = {
host: host,
path: path
};
console.log(bloggerConfig);
console.log(bloggerConfig[0]);
console.log('api_key : ', apiKey);
console.log('Making https request :', host + path);
https.get(options, function(res) {
res.setEncoding('utf-8');
var data = "";
res.on('data', function (next_chunk) {
data += next_chunk;
});
res.on('end', function() {
process_content(data);
});
}).on('error', function() {
process_content(null);
});
}
function postToBlog(host, blogIdWriteTo, title, content, accessToken) {
// TODO Implement OAuth2 to get access token.
// Request authorization
// If authorization granted, request access token
// Send post request with access token
// For now, obtain OAuth2 access token on Blogger and add to config
var options = {
host: host,
path: '/blogger/v3/blogs/'+ blogIdWriteTo + '/posts/',
method: 'POST',
headers: {
'Authorization' : 'Bearer ' + accessToken,
'Content-Type' : 'application/json'
}
};
var postJson = {
'kind' : 'blogger#post',
'blog' : {
'id' : blogIdWriteTo
},
'title' : title,
'content' : content,
'labels' : ['automated', 'tech', 'Watson']
};
var req = https.request(options, function(res) {
console.log('status code: ', res.statusCode);
console.log('headers: ', res.headers);
});
req.write(JSON.stringify(postJson));
req.end();
req.on('error', function(err) {
console.log('Error in POST response : ', err);
});
}
function processData (data) {
if (data) {
console.log(data);
// Convert json response to text
var dataObject = JSON.parse(data);
//console.log(dataObject.content);
var textToAnalyze = JSON.stringify(dataObject.content);
// console.log("textToAnalyze: ", textToAnalyze);
// Create request for tone analyzer
var text = {"scorecard" : "business", "text" : textToAnalyze};
// Prepare result text to post on blog
var title = "Tone Analysis by Watson : " + dataObject.title;
var toneOutput = "Original post at : <a href=\"" + dataObject.url + "\">" + dataObject.url + "</a><br><br>A break down of percentage (with actual number of words in parentheses) for each tone and tone trait<br>";
// Do tone analysis. Anonymous callback function updates result to
// post and adds new post to blogger
toneAnalyzer.tone(text, function(err, results) {
if (err)
console.log('Tone Analyzer error response : ', err);
else {
if (resultToConsole) {
console.log(results);
}
if (addNewBlog) {
// Total word count to calculate percentage
var total = 0.0;
for (var i = 0; i < results.children.length; i++) {
total = total + results.children[i].word_count;
}
for (var i = 0; i < results.children.length; i++) {
var child = results.children[i];
toneOutput = toneOutput + "<br>" + child.name + " : " + (100*child.word_count/total).toFixed(5) + "% (" + child.word_count + ")<br>";
for (var j = 0; j < child.children.length; j++) {
var childOfChild = child.children[j];
toneOutput = toneOutput + "    " + childOfChild.name + " : " + (100*childOfChild.word_count/total).toFixed(5) + "% (" + childOfChild.word_count + ")<br>";
}
}
postToBlog(host, blogId, title, toneOutput, accessToken);
}
}
});
} else {
console.log("Error in processData : data downloaded is null");
}
}
getContent(host, path, processData);