-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
194 lines (170 loc) · 5.79 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
185
186
187
188
189
190
191
192
193
194
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var app = express();
var twitterAPI = require('node-twitter-api');
var twitter = new twitterAPI({
consumerKey: 'CFRzw4QBDkxDua8SFmbKuKqlh',
consumerSecret: '2b5ZBt5tRRysTPQpLxG78u6wTjFRog1H68AyZNFyaiyMj5TsHn',
callback: 'http://127.0.0.1:3000/results'
});
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({
secret: 'dev',
resave: true,
saveUninitialized: true
}));
var rToken = '';
var rTokenSecret = '';
var aToken = '';
var aTokenSecret = '';
var username = '';
var requestToken = function(req, res, next) {
twitter.getRequestToken(function(error, requestToken, requestTokenSecret, results){
if (error) {
error = JSON.stringify(error);
console.log("Error getting OAuth request token : " + error);
} else {
//store token and tokenSecret somewhere, you'll need them later; redirect user
rToken = requestToken;
rTokenSecret = requestTokenSecret;
console.log("Access Token & Secret:\n"+rToken+ '\n' + rTokenSecret);
res.redirect("https://twitter.com/oauth/authenticate?oauth_token=" + rToken);
}
});
};
function List(id,slug) {
this._id = id;
this.slug = slug;
this.tweets = [];
}
function Tweet(name,text,date,imageURL){
this.name = name;
this.text = text;
this.date = date;
this.imageURL = imageURL;
}
var myLists = [];
var accessToken = function(req,res,next,callback) {
if(!req.query.oauth_token || !req.query.oauth_verifier) {
res.sendFile(__dirname + '/views/error.html');
}
console.log("REQUEST TOKEN IS " + rToken);
var requestToken = rToken;
var requestTokenSecret = rTokenSecret;
var oauth_verifier = req.query.oauth_verifier;
twitter.getAccessToken(requestToken, requestTokenSecret, oauth_verifier, function(error, accessToken, accessTokenSecret, results) {
if (error){
error = JSON.stringify(error);
console.log("Error getting Access token : " + error);
} else {
aToken = accessToken;
aTokenSecret = accessTokenSecret;
console.log("Authorized");
twitter.verifyCredentials(accessToken, accessTokenSecret, function(error, data, results) {
if(error) {
error = JSON.stringify(error);
console.log("Error getting User info : " + error);
} else {
console.log("Logged in as " + data.screen_name)
username = data.screen_name;
}
});
callback();
}
});
};
var getLists = function(req,res,next,callback){
//API call to get the ids and names of a user's lists
twitter.lists("list",{
screen_name: username
},
aToken,
aTokenSecret,
function(error,data,response) {
if(error) {
error = JSON.stringify(error);
console.log("Error getting List info : " + error);
} else {
for (var i=0;i<data.length;i++) {
var l = new List(data[i].id,data[i].slug);
myLists.push(l);
console.log(myLists[i]);
}
callback();
}
}
);
};
var getTweets = function(req,res,next,callback) {
if (!results.length === 0) {
for(var i=0;i<results.length;i++) {
twitter.lists("statuses",{
list_id: results[i]._id,
slug: results[i].slug
},
aToken,
aTokenSecret,
function(error,data,response) {
if(error) {
error = JSON.stringify(error);
console.log("Error getting Tweet info : " + error);
} else {
console.log(JSON.stringify(data));
/*console.log("Tweet: " data[0].text +
"\n Tweeted by: " + data[0].user.name +
"\n Tweeted at: " + data[0].created_at +
"\n Image URL: " + data[0].user.profile_image_url
);*/
}
}
);
}
callback();
} else { console.log("lol");}
}
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', function (req, res, next) {
requestToken(req,res, next);
//res.sendFile(__dirname + '/views/index.html');
});
app.get('/results', function (req, res, next) {
accessToken(req,res,next, function(){
getLists(req,res, next,function(){
getTweets(req,res,next,function(){
res.sendFile(__dirname + '/views/results.html');
});
});
});
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.sendFile(__dirname + '/views/error.html');
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.sendFile(__dirname + '/views/error.html');
});
module.exports = app;