-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
331 lines (303 loc) · 15.8 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Third party stuff
var bodyParser = require('body-parser'); // Helps get stuff back from form inputs
var express = require('express'); // Handles page routing
var mysql = require('mysql'); // Library for interacting with mysql DB
var app = express();
var session = require('express-session'); //Unused right now, don't worry about it
// var cors = require('cors');
var axios = require('axios');
var util = require('util');
// Our stuff
// File that handles database stuff
const database = require('./node/database');
// File that handles twitch api requests
const twitchRequests = require('./node/twitch-requests');
// File that handles user authentication
const authenticator = require('./node/authentication');
const youtubeRequests = require('./node/youtube-requests');
const twitterRequests = require('./node/twitter-requests');
const redditRequests = require('./node/reddit-requests');
// Just setup, ignore
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.static(__dirname + "/public"));
app.use(session({
resave: true,
saveUninitialized: true,
secret: 'fdsaafddfdfsfdsfddfs'
}));
// GET /
// This code runs when you either go to / (aka the homepage) or are redirected to /
// app.get('/', function(req, res, next)) is a function that is called when a get request is sent to /
// req is basically what was requested
// res is the response to give after get is done basically what to do next
// next don't worry about
app.get('/', function(req, res, next){
database.startConnection();
// This 'if' happens when you go through signing up and linking your twitch because the twitch api puts a 'code' attribute in the query string.
// After signing up and linking your twitch, look at the url and you will see a 'code' in the url.
// This code is a code for the given user in order to access that user's data on twitch's side.
if(req.query.code){
// Make a post request to the twitch api for an access token for that user to get the user's data
return axios
.post('https://id.twitch.tv/oauth2/token?client_id=jjkfx0rdgvk3ddm2y2fd4doq5m1mba&client_secret=73c55kpq9xvtiy4qdi4mgrg5qtnbna&grant_type=authorization_code&redirect_uri=https://twitch-social.herokuapp.com/&code=' + req.query.code)
.then(response => {
console.log(response.data);
database.insertUserTwitchTokens(req.session.username, response.data.access_token, response.data.refresh_token, function(result){
twitchRequests.getUserFollowersID(response.data.access_token).then(function(followers){
twitchRequests.getLiveFollowers(followers).then(function(stuff){
for(let i = 0; i < followers.length; i++){
if(stuff.includes(followers[i].id)){
followers[i].stream_status = 'live';
}
}
// res.render('index', {
// followers: followers
// });
res.redirect('/streamers');
});
});
});
})
.catch(error => {
console.log('<app GET /: getting tokens>' + error);
if(error.response.status == 400){
console.log('error401');
}
});
}else if(req.session.username){
res.redirect('/streamers');
}else{
// This happens when you first go to the page and do nothing, we just load the landing.ejs file
// console.log('hello');
res.render('landing');
// res.redirect('https://id.twitch.tv/oauth2/authorize?client_id=jjkfx0rdgvk3ddm2y2fd4doq5m1mba&redirect_uri=https://twitch-social.herokuapp.com/&response_type=code&scope=user:edit');
}
// res.render('landing');
});
app.get('/streamers', function(req, res, next){
if(req.session.username){
// This should be a pull from their followers in our database instead of the same api call of getting followers every time
// api call should only be done when they login, in case they followed someone new
database.getUserAccessToken(req.session.username, function(token){
twitchRequests.getUserFollowersID(token.access_token).then(function(followers){
// If we get an http 401 error, the user's access token expired, we need a new one
if(followers == 'error401'){
twitchRequests.getNewToken(token.refresh_token).then(function(newToken){
database.insertUserTwitchTokens(req.session.username, newToken.access_token, newToken.refresh_token, function(result){
twitchRequests.getUserFollowersID(newToken.access_token).then(function(followers){
twitchRequests.getLiveFollowers(followers).then(function(stuff){
for(let i = 0; i < followers.length; i++){
if(stuff.includes(followers[i].id)){
followers[i].stream_status = 'live';
}
}
res.render('index', {
followers: followers
});
});
});
});
});
}else{
// If the user's access token is still valid, just get their followers
twitchRequests.getLiveFollowers(followers).then(function(stuff){
for(let i = 0; i < followers.length; i++){
if(stuff.includes(followers[i].id)){
followers[i].stream_status = 'live';
}
}
res.render('index', {
followers: followers
});
});
}
}).catch(error => {
// console.log('errrrrr');
});
});
}
});
// POST /streamers
// This code runs when you are directed to /streamers
// FOLLOWERS NEED TO BE STORED IN DB SO THAT WE DON'T NEED TO KEEP SENDING REQUESTS TO TWITCH, ONLY GET THEM ONCE ON INITIAL LOGIN
app.post('/streamers', function(req, res, next){
if(req.body.login){
authenticator.userExists(req.body.username, req.body.password, function(result){
if(result){
if(!req.session.username){
req.session.username = req.body.username;
}
database.getUserAccessToken(req.session.username, function(token){
twitchRequests.getUserFollowersID(token.access_token).then(function(followers){
// If we get an http 401 error, the user's access token expired, we need a new one
if(followers == 'error401'){
twitchRequests.getNewToken(token.refresh_token).then(function(newToken){
database.insertUserTwitchTokens(req.session.username, newToken.access_token, newToken.refresh_token, function(result){
twitchRequests.getUserFollowersID(newToken.access_token).then(function(followers){
twitchRequests.getLiveFollowers(followers).then(function(stuff){
for(let i = 0; i < followers.length; i++){
if(stuff.includes(followers[i].id)){
followers[i].stream_status = 'live';
}
}
res.render('index', {
followers: followers
});
});
});
});
});
}else{
// If the user's access token is still valid, just get their followers
twitchRequests.getLiveFollowers(followers).then(function(stuff){
for(let i = 0; i < followers.length; i++){
if(stuff.includes(followers[i].id)){
followers[i].stream_status = 'live';
}
}
res.render('index', {
followers: followers
});
});
}
}).catch(error => {
// console.log('errrrrr');
});
});
}else{
res.send('nope');
}
});
}else if(req.body.signup){
// This code gets run when you hit the sign up button on landing (look at landing.ejs for the form where action='/streamers')
// Use authenticator file to store the user in the db
authenticator.storeUser(req.body.username, req.body.password);
req.session.username = req.body.username;
console.log('running');
// After storing the new user in the DB, redirect him to the oauth thing to link twitch account using twitch credentials
res.redirect('https://id.twitch.tv/oauth2/authorize?client_id=jjkfx0rdgvk3ddm2y2fd4doq5m1mba&redirect_uri=https://twitch-social.herokuapp.com/&response_type=code&scope=user:edit');
}
// req.body holds what was submitted in the form in json format, in this case it'll be a username and password (see landing.ejs) i.e. use req.body.username for the username
// the body attributes such as 'username' are defined in the landing.ejs form
// console.log(JSON.stringify(req.body));
// authenticator.userExists(req.body.username, req.body.password);
});
// GET /scheduler
// This code runs when you go to /scheduler or are redirected to /scheduler
app.get('/scheduler', function(req, res, next){
// res.redirect('https://id.twitch.tv/oauth2/authorize?client_id=jjkfx0rdgvk3ddm2y2fd4doq5m1mba&redirect_uri=https://twitch-social.herokuapp.com/&response_type=code&scope=user:edit');
res.render('scheduler');
})
app.get('/login', function(req, res, next){
res.render('login');
});
app.get('/link_stuff', function(req, res, next){
if(req.query.code){
// console.log(req.query);
youtubeRequests.getTokens(req.query.code).then(function(tokens){
// console.log(tokens.tokens);
database.insertUserYoutubeTokens(req.session.username, tokens.tokens.access_token, tokens.tokens.refresh_token, function(result){
youtubeRequests.getChannelID(tokens.tokens.access_token).then(function(response){
database.insertUserYoutubeChannelID(req.session.username, response.items[0].id, function(result){
youtubeRequests.getChannelVideos(response.items[0].id).then(function(vids){
// console.log(vids.items[0].id.videoId);
res.render('link_stuff');
});
});
});
});
}).catch(error => {
console.log(error);
});
}else{
res.render('link_stuff');
}
});
app.get('/link_stuff/youtube', function(req, res, next){
// console.log('running');
youtubeRequests.getAuthURL(function(url){
res.redirect(url);
});
});
app.get('/link_stuff/twitter', function(req, res, next){
});
app.post('/streamers/:streamer', function(req, res, next){
if(req.body.username){
database.getYoutubeChannelID(req.body.username, function(channelID){
if(channelID != -1){
youtubeRequests.getChannelVideos(channelID).then(function(vids){
var videos = [];
for (let i = 0; i < 3; i++) {
videos.push(vids.items[i].id.videoId);
}
twitchRequests.getUserIDFromUsername(req.body.username).then(function(twitchInfo){
// console.log(twitchID);
var clips = [];
twitchRequests.getTwitchClips(twitchInfo.id).then(function(rawClips){
// console.log(rawClips);
for(let i = 0; i < 3; i++){
clips.push(rawClips[i].embed_url);
}
database.getTwitterHandle(req.body.username, function(handle){
twitterRequests.createTwitterClient();
twitterRequests.getTweetsByUser(handle, 10, true, false, function(tweetURLs){
twitterRequests.getEmbedTweets(tweetURLs, 400, false, function(tweetEmbeds){
redditRequests.createRedditClient();
database.getSubreddit(req.body.username, function(subreddit){
redditRequests.getSubredditPosts(subreddit, 3).then(function(posts){
res.render('main', {
twitchName: twitchInfo.login,
twitchDisplayName: twitchInfo.display_name,
twitchImage: twitchInfo.profile_image_url,
youtubeID: channelID,
ytvideos: videos,
clips: clips,
handle: handle,
tweetHTMLs: tweetEmbeds,
subreddit: subreddit,
redditPosts: posts
});
});
});
});
});
});
});
});
});
}else{
twitchRequests.getUserIDFromUsername(req.body.username).then(function(twitchInfo){
res.render('main', {
twitchName: twitchInfo.login,
twitchDisplayName: twitchInfo.display_name,
twitchImage: twitchInfo.profile_image_url,
youtubeID: null,
ytvideos: null,
clips: null,
handle: null,
tweetHTMLs: null,
subreddit: null,
redditPosts: null
});
});
}
});
}else{
// console.log('wat');
}
});
app.get('/test', function(req, res, next){
redditRequests.createRedditClient();
redditRequests.getSubredditPosts('loltyler1', 3).then(function(posts){
redditRequests.getAllPostEmbeds(posts, function(embeds){
// console.log(embeds);
res.send('hy');
});
});
});
// This is the server "listening" for a connection
app.listen(process.env.PORT || 3000, function() {
console.log("Server running on 3000!");
});