This repository has been archived by the owner on May 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpiQuery.js
206 lines (189 loc) · 7.84 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const request = require('request');
let 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
}
};
let processedResults = {
emotionalRange: 0,
agreeableness: 0,
conscientiousness: 0,
openness: 0,
extraversion: 0
};
// Callback from twitterQuery.getTweets to organise results from twitter
function getPersonalityInsight(twitterErr, name, position, tweets, callback){
if(twitterErr){
console.log(twitterErr);
return callback(twitterErr);
}
// Personality Insights json object containing tweets
queryObject.json = {
contentItems: tweets
};
request(queryObject, handleResponse);
// Callback from request to handle HTTP response
function handleResponse(err, httpResponse, body){
if(err){
console.log(err);
return callback(err);
}
else{
if(body){
processedResults = findPersonality(body, name, position);
}
return callback(false, processedResults);
}
}
}
function findPersonality(data, name, position){
if(data.error){
console.log('Error in personality for: ' + name + ' ' + data.error);
}
else{
if(data.personality){
// Assign personality Attributes to results object
for(let i=0; i < data.personality.length; i++){
let personalityAttribute = data.personality[i];
// Neuroticism
if(personalityAttribute.trait_id === 'big5_neuroticism'){
processedResults.emotionalRange = personalityAttribute.percentile;
for(let j=0; j < personalityAttribute.children.length; j++){
let personalityFacet = personalityAttribute.children[j]
if(personalityFacet.trait_id == 'facet_anger'){
processedResults.anger = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_anxiety'){
processedResults.anxiety = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_depression'){
processedResults.depression = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_immoderation'){
processedResults.immoderation = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_self_consciousness'){
processedResults.self_consciousness = personalityFacet.percentile
} if(personalityFacet.trait_id == 'facet_vulnerability'){
processedResults.vulnerability = personalityFacet.percentile
}
}
}
// Agreeableness
if(personalityAttribute.trait_id === 'big5_agreeableness'){
processedResults.agreeableness = personalityAttribute.percentile;
for(let j=0; j < personalityAttribute.children.length; j++){
let personalityFacet = personalityAttribute.children[j]
if(personalityFacet.trait_id == 'facet_altruism'){
processedResults.altruism = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_cooperation'){
processedResults.cooperation = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_modesty'){
processedResults.modesty = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_morality'){
processedResults.morality = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_sympathy'){
processedResults.sympathy = personalityFacet.percentile
} if(personalityFacet.trait_id == 'facet_trust'){
processedResults.trust = personalityFacet.percentile
}
}
}
// Conscientiousness
if(personalityAttribute.trait_id === 'big5_conscientiousness'){
processedResults.conscientiousness = personalityAttribute.percentile;
for(let j=0; j < personalityAttribute.children.length; j++){
let personalityFacet = personalityAttribute.children[j]
if(personalityFacet.trait_id == 'facet_achievement_striving'){
processedResults.achievement_striving = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_cautiousness'){
processedResults.cautiousness = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_dutifulness'){
processedResults.dutifulness = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_orderliness'){
processedResults.orderliness = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_self_discipline'){
processedResults.self_discipline = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_self_efficacy'){
processedResults.self_efficacy = personalityFacet.percentile
}
}
}
// Openness
if(personalityAttribute.trait_id === 'big5_openness'){
processedResults.openness = personalityAttribute.percentile;
for(let j=0; j < personalityAttribute.children.length; j++){
let personalityFacet = personalityAttribute.children[j]
if(personalityFacet.trait_id == 'facet_adventurousness'){
processedResults.adventurousness = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_artistic_interests'){
processedResults.artistic_interests = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_emotionality'){
processedResults.emotionality = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_imagination'){
processedResults.imagination = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_intellect'){
processedResults.intellect = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_liberalism'){
processedResults.liberalism = personalityFacet.percentile
}
}
}
if(personalityAttribute.trait_id === 'big5_extraversion'){
processedResults.extraversion = personalityAttribute.percentile;
for(let j=0; j < personalityAttribute.children.length; j++){
let personalityFacet = personalityAttribute.children[j]
console.log('Extraversion', personalityFacet)
if(personalityFacet.trait_id == 'facet_activity_level'){
processedResults.activity_level = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_assertiveness'){
processedResults.assertiveness = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_cheerfulness'){
processedResults.cheerfulness = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_excitement_seeking'){
processedResults.excitement_seeking = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_friendliness'){
processedResults.friendliness = personalityFacet.percentile
}
if(personalityFacet.trait_id == 'facet_gregariousness'){
processedResults.gregariousness = personalityFacet.percentile
}
}
}
}
}
processedResults.Name = name;
processedResults.Position = position;
return processedResults;
}
}
module.exports = {
getPersonalityInsight:getPersonalityInsight
};