-
Notifications
You must be signed in to change notification settings - Fork 1
/
piQuery.js
65 lines (58 loc) · 1.68 KB
/
piQuery.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
var request = require('request');
var discoveryQuery = require('./discoveryQuery');
var queryObject = {
uri: process.env.PERSONALITY_URL,
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
auth: {
user: process.env.PERSONALITY_USERNAME,
pass: process.env.PERSONALITY_PASSWORD
},
qs:{
version: process.env.PERSONALITY_VERSION,
}
};
/**
* Query Personality Insights service to get results
* @param {String[]} names - The names a player goes by (e.g. ['Rafael Nadal', 'nadal', 'raffa']).
* @param {requestCallback} callback - Callback.
*/
function analysePersonality(author, callback){
// Create variables to hold results
var contentList = [];
var contentObject = {};
// Call discoveryQuery.getQuotes to query Watson Discovery Service(WDS) to get quotes
discoveryQuery.getTextsByAuthor(author, getPersonalityInsight);
// Callback from discoveryQuery.getQuotes to organise results from WDS
function getPersonalityInsight( quoteErr, quoteData){
if(quoteErr){
console.log(quoteErr);
return callback(quoteErr);
}
else{
var contentList = [];
for(var i=0; i < quoteData.length; i++){
contentList.push(quoteData[i].text);
}
// Personality Insights json object containing quotes
queryObject.json = {
contentItems: contentList
};
request(queryObject, handleResponse);
// Callback from request to handle HTTP response
function handleResponse(err, httpResponse, body){
if(err){
return callback(err);
}
else{
callback(false,body);
}
}
}
}
}
module.exports = {
analysePersonality:analysePersonality
};