-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·90 lines (74 loc) · 2.55 KB
/
app.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
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* 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 express = require('express'),
app = express(),
util = require('util'),
extend = util._extend,
watson = require('watson-developer-cloud'),
Q = require('q'),
isString = function (x) { return typeof x === 'string'; };
// Bootstrap application settings
require('./config/express')(app);
var personalityInsights = watson.personality_insights({
username: '374e6738-0a94-4501-b2b7-1829bfb36fd4',
password: 'PLUNjFWGH7rt',
version: 'v2'
});
// Creates a promise-returning function from a Node.js-style function
var getProfile = Q.denodeify(personalityInsights.profile.bind(personalityInsights));
function tweetToContentItem(tweet) {
return {
id: tweet.id_str,
userid: tweet.user.id_str,
sourceid: 'twitter',
language: 'en',
contenttype: 'text/plain',
content: tweet.text.replace('[^(\\x20-\\x7F)]*',''),
created: Date.parse(tweet.created_at)
};
}
app.get('/', function(req, res) {
res.render('index', { ct: req._csrfToken });
});
app.post('/sunburst', function (req, res) {
var jsonString = req.body.data;
res.render('sunburst',{profile: jsonString});
});
app.post('/api/profile/text', function(req, res, next) {
getProfile(extend(req.body, { text: req.body.text.replace(/[\s]+/g, ' ') }))
.then(function(response){
res.json(response[0]);
})
.catch(next)
.done();
});
app.post('/api/profile/twitter', function(req, res, next) {
if (!isString(req.body.userId))
next({code: 400, error: 'Missing required parameters: userId'});
var tweets = require('./public/data/twitter/'+ req.body.userId +'_tweets.json');
getProfile(extend(req.body, { contentItems: tweets.map(tweetToContentItem) }))
.then(function(response){
res.json(response[0]);
})
.catch(next)
.done();
});
// error-handler settings
require('./config/error-handler')(app);
var port = process.env.VCAP_APP_PORT || 3000;
app.listen(port);
console.log('listening at:', port);