-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
178 lines (138 loc) · 5.15 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
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
var express = require('express');
var http = require("http");
var io = require('socket.io');
var Twit = require('twit');
var OAuth= require('oauth').OAuth;
var app = express();
var twitObj = new Twit({
consumer_key: 'Mq5nZDXjwtM5JTuvGRckOw'
, consumer_secret: 'BJKqFZaKFnI1ynLYixOXDBhGrEqLmb4IpPHSfUVFEA'
, access_token: '1351190376-o4q56bHU3391Bv3MDSGGu1bB6ni86IJnbvmEIP5'
, access_token_secret: 'cUpPQFU3VsjDUC7ylpaGkKwvvUiY3a4vJ8t6IaBig'
});
var oa = new OAuth(
"https://api.twitter.com/oauth/request_token",
"https://api.twitter.com/oauth/access_token",
"Mq5nZDXjwtM5JTuvGRckOw",
"BJKqFZaKFnI1ynLYixOXDBhGrEqLmb4IpPHSfUVFEA",
"1.0",
"http://admiostest.hugo506.c9.io/twitter_callback",
"HMAC-SHA1"
);
app.configure(function(){
app.set('ip', process.env.IP);
app.set('port', process.env.PORT || 8080);
app.set('views', __dirname + '/views');
app.set('stylesheets', __dirname + '/public/stylesheets');
app.set('view engine', 'jade');
app.use(express.static(__dirname + '/public'));
app.use(express.cookieParser());
app.use(express.session( { secret: 'whatever' } ));
app.use(express.bodyParser());
});
app.get('/', function(req, res){
res.render('index', {
title: 'Login'
});
});
app.get('/stock_check', function(req, res){
res.render('stock_check', {
title: 'Check Stock Symbol'
});
});
app.post('/get_stock_data', function(req, res){
var symbol = req.body.symbol;
var parseString = require('xml2js').parseString;
var options = {
host: 'www.google.com',
path: '/ig/api?stock='+symbol};
var request = http.get(options, function(response) {
var xmlData = "";
response.setEncoding('utf8');
response.on('data', function (chunk) {
//console.log(chunk);
xmlData += chunk;
});
response.on('end', function(){
parseString(xmlData, function (err, result) {
var symbolName = result.xml_api_reply.finance[0].exchange[0]['$']['data'];
if('UNKNOWN EXCHANGE' == symbolName){
res.send({value:'Error invalid stock symbol'});
}else{
res.send({value:'Symbol is valid', stock:result.xml_api_reply.finance[0]});
}
});
});
});
});
app.get('/twitter_auth', function(req, res){
oa.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results){
if (error) {
console.log(error);
//res.send("yeah no. didn't work.")
res.redirect("/");
}
else {
req.session.oauth = {};
req.session.oauth.token = oauth_token;
//console.log('oauth.token: ' + req.session.oauth.token);
req.session.oauth.token_secret = oauth_token_secret;
//console.log('oauth.token_secret: ' + req.session.oauth.token_secret);
res.redirect('https://twitter.com/oauth/authenticate?oauth_token='+oauth_token)
}
});
});
app.get('/twitter_callback', function(req, res, next){
if (req.session.oauth) {
req.session.oauth.verifier = req.query.oauth_verifier;
var oauth = req.session.oauth;
oa.getOAuthAccessToken(oauth.token,oauth.token_secret,oauth.verifier,
function(error, oauth_access_token, oauth_access_token_secret, results){
if (error){
console.log(error);
//res.send("yeah something broke.");
res.redirect("/");
} else {
req.session.oauth.access_token = oauth_access_token;
req.session.oauth.access_token_secret = oauth_access_token_secret;
console.log(results);
//res.send("worked. nice one.");
res.render('stock_check', {
title: 'Stock Check'
});
}
}
);
} else
next(new Error("you're not supposed to be here."))
});
// create a server
var server = http.createServer(app).listen(app.get('port'));
//create the socket object, use the main server
var socketIo = require('socket.io').listen(server);
socketIo.on('connection', function(socket) {
var stream;
socket.on('setSymbol', function (data) {
/*setInterval(function(){
socket.emit('symbolTweet',{name: Date.now()+data.symbol, tweetText: data.symbol});
}, 1000);*/
//create a twitter stream with tweets about the specified stock symbol
stream = twitObj.stream('statuses/filter', { track: data.symbol });
//listen for tweets and forward to the client socket
stream.on('tweet', function (tweet) {
//console.log(tweet.user.name+" says: "+tweet.text);
socket.emit('symbolTweet',{name: tweet.user.name, tweetText: tweet.text});
});
stream.on('disconnect', function (disconnectMessage) {
//Emitted when a disconnect message comes from Twitter.
//This occurs if you have multiple streams connected to Twitter's API.
//Upon receiving a disconnect message from Twitter, Twit will close the connection
//and emit this event with the message details received from twitter.
//console.log(disconnectMessage);
socket.emit('symbolTweet',{name: JSON.stringify(disconnectMessage), tweetText: JSON.stringify(disconnectMessage)});
});
});
socket.on('disconnect', function () {
//stream.stop();
});
});