-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
184 lines (160 loc) · 5.13 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
176
177
178
179
180
181
182
183
184
var sys = require('sys');
var http = require('http');
var json = JSON.stringify,
log = sys.puts;
//The Express framework
var express = require('express');
//This handles our websocket connections
var io = require('socket.io');
//The twitter client
var twitter = require('./lib/node-twitter');
//Connect middleware
var connect = require('connect');
var MemoryStore = require('connect/middleware/session/memory');
//Handles our twitter oauth on the browser
var auth= require('./lib/connect-auth/index');
var OAuth= require('./lib/oauth').OAuth;
//Loads in our twitter auth keys
try {
var example_keys= require('./lib/keys_file');
for(var key in example_keys) {
global[key]= example_keys[key];
}
}
catch(e) {
console.log('Unable to locate the keys_file.js file. Please copy and ammend the example_keys_file.js as appropriate');
return;
}
//Create our webserver
var app = module.exports = express.createServer(
//Register cookie handlers
connect.cookieDecoder(),
//Register session handler
connect.session({ store: new MemoryStore({ reapInterval: -1 }) }),
//Setup auth
auth( [
auth.Twitter({consumerKey: twitterConsumerKey, consumerSecret: twitterConsumerSecret})
])
);
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyDecoder());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.staticProvider(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
app.get ('/auth/twitter', function(req, res, params) {
req.authenticate(['twitter'], function(error, authenticated) {
if( authenticated ) {
//Render authenticated page
//Set our tokens to be used in the html
//These will be sent back to the browser via a websocket
res.render('auth', {
locals: {
title: 'Twitter media',
token: req.getAuthDetails()["twitter_oauth_token"],
token_secret: req.getAuthDetails()["twitter_oauth_token_secret"]
}
});
}
else {
res.writeHead(200, {'Content-Type': 'text/html'})
res.end("<html><h1>Twitter authentication failed :( </h1></html>")
}
});
})
//Index page
app.get('/', function(req, res){
res.render('index', {
locals: {
title: 'Twitter media'
}
});
});
//Attacking our socket to the webapp for incoming requests
var socket = io.listen(app);
socket.on('connection', function(client){
//We have a message
client.on('message', function(data){
//Dump the message
log(sys.inspect(data));
//Create a new twitter oAuth object
var twit = new twitter({
consumer_key: twitterConsumerKey,
consumer_secret: twitterConsumerSecret,
access_token_key: data.token,
access_token_secret: data.token_secret
});
//Setup the stream
twit.stream('user', function(stream) {
stream.on('data', function (data) {
if(data.entities)
{
//Have we got urls?
if(data.entities.urls.length>0)
{
//Send them to the client as a json string
for(var i=0; i<data.entities.urls.length; i++)
{
get_long_url(data.entities.urls[i], client);
}
//client.send(json(data.entities.urls));
sys.puts(sys.inspect(data.entities.urls));
}
}
else
{
sys.puts("Starting twitter connection");
}
});
//Store for use later
client.stream = stream;
});
});
//Disconnect sent
client.on('disconnect', function(){
client.stream.emit('end');
client.stream.emit('close');
});
});
var tweetmeme_client = http.createClient(80, "api.tweetmeme.com");
var EventEmitter = require('events').EventEmitter;
var tweetmeme_emmiter = new EventEmitter;
tweetmeme_emmiter.on('url', function(urldata, client){
client.send(json([urldata]));
sys.puts(sys.inspect(urldata));
});
function get_long_url(urldata, client)
{
var url = urldata.url;
var request = tweetmeme_client.request("GET", "/url_info.json?url="+url, {"host": "api.tweetmeme.com"});
request.addListener("response", function(response) {
var body = "";
response.addListener("data", function(data) {
body += data;
});
response.addListener("end", function() {
var jsondata = JSON.parse(body);
if(jsondata.status=='success')
{
urldata.url = jsondata.story.url;
}
tweetmeme_emmiter.emit("url", urldata, client);
});
});
request.end();
}
// Only listen on $ node app.js
if (!module.parent) {
app.listen(3010);
console.log("Express server listening on port %d", app.address().port)
}