/
app.js
315 lines (293 loc) · 11.4 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
var express = require('express');
var mustacheExpress = require('mustache-express');
var request = require('request')
var bodyParser = require('body-parser');
var app = express();
app.engine('html', mustacheExpress());
app.set('view engine', 'mustache');
app.set('views', __dirname + '/views');
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/static', express.static(__dirname + '/static'));
/**
* Takes in the `seasonsDict` dictionary and creates a string of HTML
* to display the seasons and their corresponding data.
* @param {Object} seasonsDict - A dictionary where the key is the season number, &
* the value is a list with the episode count and season API URL
* @return {String} - The string of HTML to inject dynamically to display
* season options.
*/
function makeSeasonsHTML(seasonsDict) {
HTML = '<form class="input-field col s10" id="optionsForm" action="/tunefind_get_show_episodes" method="POST">';
HTML += '<ul>';
for (seasonNumber in seasonsDict) {
seasonList = seasonsDict[seasonNumber];
HTML += '<li>';
HTML += '<button type="submit" id ="tvresult" class="validate btn waves-effect waves-light" name="selectedSeason" value="';
HTML += seasonList[1];
HTML += '">Season ';
HTML += seasonNumber;
HTML += '<br/>';
HTML += 'Episode Count: ';
HTML += seasonList[0];
HTML += '</button>';
HTML += '</li>';
HTML += '</br>';
}
HTML += '</ul>';
HTML += '</form>';
return HTML;
};
/**
* Takes in the `episodesDict` dictionary and creates a string of HTML
* to display the episodes and their corresponding data.
* @param {Object} episodesDict - A dictionary where the key is the episode number, &
* the value is a list with the episode number and episode API URL
* @return {String} - The string of HTML to inject dynamically to display
* episode options.
*/
function makeEpisodesHTML(episodesDict) {
HTML = '<form class="input-field col s10" id="optionsForm" action="/tunefind_get_show_songs" method="POST">';
HTML += '<ul>';
for (episodeNumber in episodesDict) {
episodeList = episodesDict[episodeNumber];
HTML += '<li>';
HTML += '<button type="submit" id ="tvresult" class="validate btn waves-effect waves-light" name="selectedEpisode" value="';
HTML += episodeList[1];
HTML += '">Episode ';
HTML += episodeNumber;
HTML += '<br/>';
HTML += 'Episode Name: ';
HTML += episodeList[0];
HTML += '</button>';
HTML += '</li>';
HTML += '</br>';
}
HTML += '</ul>';
HTML += '</form>';
return HTML;
};
/**
* Takes in the `songsDict` dictionary and creates a string of HTML
* to display the songs and their corresponding data for a movie.
* @param {Object} songsDict - A dictionary where the key is the song name, &
* the value is a list with the artist name, and scene if applicable
* @return {String} - The string of HTML to inject dynamically to display
* song options.
*/
function makeSongsMovieHTML(songsDict) {
if (Object.keys(songsDict).length == 0) {
HTML = '<p>Woops! Sorry no songs found for this movie!';
return HTML;
}
HTML = '<form class="input-field col s10" id="optionsForm" action="/youtube_search_movie" method="POST">';
HTML += '<ul>';
for (songName in songsDict) {
songList = songsDict[songName];
youtubeSearch = songList[0] + ' ' + songName;
HTML += '<li>';
HTML += '<button id ="tvresult" class="validate btn waves-effect waves-light" type="submit" name="selectedSong" value="';
HTML += youtubeSearch;
HTML += '">Song: ';
HTML += songName;
HTML += '<br/>';
HTML += 'Artist: ';
HTML += songList[0];
HTML += '<br/>';
HTML += 'Scene: ';
HTML += songList[1];
HTML += '</button>';
HTML += '</li>';
HTML += '</br>';
}
HTML += '</ul>';
HTML += '</form>';
return HTML;
};
/**
* Takes in the `songsDict` dictionary and creates a string of HTML
* to display the songs and their corresponding data for a TV show.
* @param {Object} songsDict - A dictionary where the key is the song name, &
* the value is a list with the artist name, and scene if applicable
* @return {String} - The string of HTML to inject dynamically to display
* song options.
*/
function makeSongsShowHTML(songsDict) {
if (Object.keys(songsDict).length == 0) {
HTML = '<p>Woops! Sorry no songs found for this episode!';
return HTML;
}
HTML = '<form class="input-field col s10" id="optionsForm" action="/youtube_search_show" method="POST">';
HTML += '<ul>';
for (songName in songsDict) {
songList = songsDict[songName];
youtubeSearch = songList[0] + ' ' + songName;
HTML += '<li>';
HTML += '<button id ="tvresult" class="validate btn waves-effect waves-light" type="submit" name="selectedSong" value="';
HTML += youtubeSearch;
HTML += '">Song: ';
HTML += songName;
HTML += '<br/>';
HTML += 'Artist: ';
HTML += songList[0];
HTML += '<br/>';
HTML += 'Scene: ';
HTML += songList[1];
HTML += '</button>';
HTML += '</li>';
HTML += '</br>';
}
HTML += '</ul>';
HTML += '</form>';
return HTML;
};
// Define your routes here
app.get('/', function (req, res, next) {
res.render('main.html', {'searchAction' : '"/no_button_selected"', 'searchPlaceHolder' : '"Select Movie or TV Show"'});
});
app.post('/no_button_selected', function (req, res, next) {
noSearchHTML = '<p>Woops! Please select either Movie or TV Show!</p>';
res.render('main.html', {'search_error' : noSearchHTML, 'searchAction' : '"/no_button_selected"', 'searchPlaceHolder' : '"Select Movie or TV Show"'});
});
app.post('/tunefind_get_movie_songs', function(req, res) {
movieName = req.body.show_or_movie_name;
if (!movieName) {
noSearchHTML = '<p>Woops! Please enter a Movie title!</p>';
res.render('main.html', {'search_error' : noSearchHTML, 'searchAction' : '"/tunefind_get_movie_songs"', 'searchPlaceHolder' : '"Select by Movie Title"'});
} else {
// url = 'http://localhost:3001/tunefind_get_movie_songs';
url = 'https://trackstream-api.herokuapp.com/tunefind_get_movie_songs';
request(
{
url: url,
method: 'POST',
json: {'movieName' : movieName}
},
function(error, response, body) {
if (error || response.statusCode == 404) {
invalidSearchHTML = '<p>Woops! No results found! Try another search term!</p>';
res.render('main.html', {'search_error' : invalidSearchHTML, 'searchAction' : '"/tunefind_get_movie_songs"', 'searchPlaceHolder' : '"Select by Movie Title"'});
} else {
var songDict = body;
songsHTML = makeSongsMovieHTML(songDict);
res.render('main.html', {'optionsForm' : songsHTML, 'searchAction' : '"/tunefind_get_movie_songs"', 'searchPlaceHolder' : '"Select by Movie Title"'});
}
}
)
};
});
app.post('/tunefind_get_show_seasons', function (req, res, next) {
showName = req.body.show_or_movie_name;
if (!showName) {
noSearchHTML = '<p>Woops! Please enter a TV Show title!</p>';
res.render('main.html', {'search_error' : noSearchHTML, 'searchAction' : '"/tunefind_get_show_seasons"', 'searchPlaceHolder' : '"Select by TV Show Title"'});
} else {
// url = 'http://localhost:3001/tunefind_get_show_seasons';
url = 'https://trackstream-api.herokuapp.com/tunefind_get_show_seasons';
request(
{
url: url,
method: 'POST',
json: {'showName' : showName}
},
function(error, response, body) {
if (error || response.statusCode == 404) {
invalidSearchHTML = '<p>Woops! No results found! Try another search term!</p>';
res.render('main.html', {'search_error' : invalidSearchHTML, 'searchAction' : '"/tunefind_get_show_seasons"', 'searchPlaceHolder' : '"Select by TV Show Title"'});
} else {
var seasonsDict = body;
seasonsHTML = makeSeasonsHTML(seasonsDict);
res.render('main.html', {'optionsForm' : seasonsHTML, 'searchAction' : '"/tunefind_get_show_seasons"', 'searchPlaceHolder' : '"Select by TV Show Title"'});
}
}
)
};
});
app.post('/tunefind_get_show_episodes', function (req, res, next) {
selectedSeasonURL = req.body.selectedSeason;
if (!selectedSeasonURL) {
noSearchHTML = '<p>Woops! Please enter a Season!</p>';
res.render('main.html', {'search_error' : noSearchHTML, 'searchAction' : '"/tunefind_get_show_seasons"', 'searchPlaceHolder' : '"Select by TV Show Title"'});
} else {
// url = 'http://localhost:3001/tunefind_get_show_episodes';
url = 'https://trackstream-api.herokuapp.com/tunefind_get_show_episodes';
request(
{
url: url,
method: 'POST',
json: {'selectedSeason' : selectedSeasonURL}
},
function(error, response, body) {
var episodesDict = body;
episodesHTML = makeEpisodesHTML(episodesDict);
res.render('main.html', {'optionsForm' : episodesHTML, 'searchAction' : '"/tunefind_get_show_seasons"', 'searchPlaceHolder' : '"Select by TV Show Title"'});
}
)
};
});
app.post('/tunefind_get_show_songs', function (req, res, next) {
selectedEpisodeURL = req.body.selectedEpisode;
if (!selectedEpisodeURL) {
noSearchHTML = '<p>Woops! Please enter an Episode number!</p>';
res.render('main.html', {'search_error' : noSearchHTML, 'searchAction' : '"/tunefind_get_show_seasons"', 'searchPlaceHolder' : '"Select by TV Show Title"'});
} else {
// url = 'http://localhost:3001/tunefind_get_show_songs';
url = 'https://trackstream-api.herokuapp.com/tunefind_get_show_songs';
request(
{
url: url,
method: 'POST',
json: {'selectedEpisode' : selectedEpisodeURL}
},
function(error, response, body) {
var songsDict = body;
songsHTML = makeSongsShowHTML(songsDict);
res.render('main.html', {'optionsForm' : songsHTML, 'searchAction' : '"/tunefind_get_show_seasons"', 'searchPlaceHolder' : '"Select by TV Show Title"'});
}
)
};
});
app.post('/youtube_search_movie', function (req, res, next) {
youtubeSearch = req.body.selectedSong;
// url = 'http://localhost:3001/youtube_search';
url = 'https://trackstream-api.herokuapp.com/youtube_search';
request(
{
url: url,
method: 'POST',
json: {'youtubeSearch' : youtubeSearch}
},
function(error, response, body) {
youtubeURL = body.youtubeURL;
youtubeHTML = '<iframe width="560" height="315" src="' + youtubeURL;
youtubeHTML += '" frameborder="0" allowfullscreen></iframe>';
res.render('main.html', {'optionsForm' : youtubeHTML, 'searchAction' : '"/tunefind_get_movie_songs"', 'searchPlaceHolder' : '"Select by Movie Title"'});
}
)
});
app.post('/youtube_search_show', function (req, res, next) {
youtubeSearch = req.body.selectedSong;
// url = 'http://localhost:3001/youtube_search';
url = 'https://trackstream-api.herokuapp.com/youtube_search';
request(
{
url: url,
method: 'POST',
json: {'youtubeSearch' : youtubeSearch}
},
function(error, response, body) {
youtubeURL = body.youtubeURL;
youtubeHTML = '<iframe width="560" height="315" src="' + youtubeURL;
youtubeHTML += '" frameborder="0" allowfullscreen></iframe>';
res.render('main.html', {'optionsForm' : youtubeHTML, 'searchAction' : '"/tunefind_get_show_seasons"', 'searchPlaceHolder' : '"Select by TV Show Title"'});
}
)
});
app.get('*', function(req, res){
res.status(404).send('So sorry, not found, idk love u lots - trackstream team');
});
// Start up server on port 3000 on host localhost
var server = app.listen(process.env.PORT || 3000, function () {
var port = server.address().port;
console.log('Trackstream server on localhost listening on port ' + port + '!');
//console.log('Open up your browser (within your VM) and enter the URL "http://localhost:' + port + '" to view your website!');
});