forked from sheffieldjordan/moocSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
313 lines (260 loc) · 11.4 KB
/
app.js
File metadata and controls
313 lines (260 loc) · 11.4 KB
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
/******NO NEED TO MODIFY ****/
var express = require('express'); // Adding the express library
var mustacheExpress = require('mustache-express'); // Adding mustache templating system and connecting it to
var request = require('request'); // Adding the request library (to make HTTP reqeusts from the server);
var tools = require('./tools.js'); // Custom module providing additional functionality to the server
var bodyParser = require('body-parser');
var app = express(); // initializing applicaiton
var YouTube = require('youtube-node');
var youTube = new YouTube();
youTube.setKey('AIzaSyASBTPnt-OJlLc3ey0SMhiU38a2THSsnP4');
app.engine('html', mustacheExpress());
app.set('view engine', 'mustache');
app.set('views', __dirname + '/views');
app.use(bodyParser.urlencoded({ extended: false }));
// For each request to this server, run the function "logger" in tools.js
// app.use(tools.logger);
// Set up /static path to host css/js/image files directly
app.use('/static', express.static(__dirname + '/static'));
/****END OF NO NEED TO MODIFY SECTION ******/
// Get homepage
app.get('/', function (req, res, next) {
res.render("home.html");
});
//Upon submitting form, send get request to MOOCS APIs
app.post('/', function(req,res,next){
var name = req.body.name;
var courses = req.body.courses;
var options = req.body.searchoptions;
var results = [];
var results_image = [];
console.log("Variables");
console.log(name);
console.log(courses);
console.log(options);
console.log(results);
//Ensure that options always remains an array.
if (! Array.isArray(options)) {
options = [];
options.push(req.body.searchoptions);
}
var async_tokens = options.length; //Number of search tasks expected to complete, "countdown" approach -- e.g.from 6 to 1
// Add requests for each API that is checked
for(var i = 0; i < options.length; i++) {
//Youtube
//Check if this MOOC provider is checked
if(options[i] === 'youtube'){
console.log("Search YouTube");
console.log(i);
youTube.search(courses, 5, function(error, result, body) {
var youTubeResp = result;
if (error) {
console.log(error);
} else {
//console.log(JSON.stringify(result, null, 5));
json_body = JSON.stringify(youTubeResp);
json_parsed = JSON.parse(json_body);
if (json_parsed.pageInfo.totalResults == 0){
console.log("Youtube Returned No Results!");
} else {
var max_len = 0; //initialize the max_len for cappping the results
if (json_parsed.pageInfo.totalResults >= 5) {
max_len = 5;
} else {
max_len = json_parsed.pageInfo.totalResults;
}
for(var i = 0; i < max_len; i++) {
var youTubeUrl = "https://www.youtube.com/embed/" + json_parsed.items[i].id.videoId;
var youTubeTitle = json_parsed.items[i].snippet.title;
var youTubeDescr = json_parsed.items[i].snippet.description;
results.push(["YouTube: " + youTubeTitle, youTubeUrl, youTubeDescr]);
}
}
}
//Check if this is the last search task before rendering all the results?
if (async_tokens === 1) {
res.render("home.html", {'results': results,'results_image': results_image});
}
async_tokens--; //One Task done: decrements the token.
});
}
//Udemy
//Check if this MOOC provider is checked
if(options[i] === 'udemy'){
console.log("Search Udemy");
request({
url: 'https://www.udemy.com/api-2.0/courses/?search=' + courses,
method: 'GET',
headers: {
"Authorization": "Basic U2k5aWt2amNPNmtwSHlQTWpMSzhvZDZ2Tml3TlRLQkQzdW83MGdMTzpaUGI1VkRXV2Z2T1F0QUlqbXNIaVUxMFlrMVJSN0xaMGh3R1QwZnFRRzM0VGRVV1VTb245SVJyUGRsbVpiNDdETjRmb0ZXQjVreFpaaVY3Q25nMUJ3S3NjU2JYMjBqRW5sRXVoOEdtMjZlakJRZzVvcHpjeVhIbmIyVEo2bThXQg==",
"Accept": "application/json, text/plain, */*" },
}, function(error, response, body){
if(error) {
console.log(error);
} else {
//console.log(response.statusCode);
json_body = JSON.parse(body);
// console.log(json_body);
if (json_body.results.length == 0) {
console.log('Udemy Returned No Results!');
} else {
var max_len = 0; //initialize the max_len for cappping the results
if (json_body.results.length >= 5) {
max_len = 5;
} else {
max_len = json_body.results.length;
}
for(var i =0 ; i < max_len; i++) {
var name = json_body.results[i].title;
var courseUrl = 'https://www.udemy.com' + json_body.results[i].url;
var imageUrl = json_body.results[i].image_480x270;
var description = json_body.results[i].description;
//console.log(name, courseUrl, imageUrl);
results_image.push(["Udemy: " + name, courseUrl, imageUrl, description]);
}
//console.log(results);
}
}
//Check if this is the last search task before rendering all the results?
if (async_tokens === 1) {
res.render("home.html", {'results': results,'results_image': results_image});
}
async_tokens--; //One Task done: decrements the token.
});
}
//Khan Academy
//Check if this MOOC provider is checked
if(options[i] === 'khanAcademy'){
console.log("Search Khan Academy");
console.log(i);
youTube.khanSearch(courses, 5, function(error, result, body) {
var khanResp = result;
if (error) {
console.log(error);
} else {
//console.log(JSON.stringify(result, null, 5));
json_body = JSON.stringify(khanResp);
json_parsed = JSON.parse(json_body);
//console.log(json_body);
if (json_parsed.pageInfo.totalResults == 0){
console.log('Khan Academy Returned No Results!');
} else {
var max_len = 0; //initialize the max_len for cappping the results
if (json_parsed.pageInfo.totalResults >= 5) {
max_len = 5;
} else {
max_len = json_parsed.pageInfo.totalResults;
}
for (var i = 0; i < max_len; i++) {
var khanUrl = "https://www.youtube.com/embed/" + json_parsed.items[i].id.videoId;
var khanTitle = json_parsed.items[i].snippet.title;
var khanDescr = json_parsed.items[i].snippet.description;
results.push(["Khan Academy: " + khanTitle, khanUrl, khanDescr]);
}
}
}
//Check if this is the last search task before rendering all the results?
if (async_tokens === 1) {
res.render("home.html", {'results': results,'results_image': results_image});
}
async_tokens--; //One Task done: decrements the token.
});
}
// Coursera
// Check if this MOOC provider is checked
if(options[i] === 'coursera'){
console.log("Search Coursera");
console.log(i);
var coursera_results = [];
var query_url = 'https://api.coursera.org/api/courses.v1?q=search&query='+courses+'&includes=name,description,photoUrl,previewLink&fields=name,description,photoUrl,previewLink';
request({
url: query_url,
method: 'GET',
}, function(error, response, body){
if(error) {
console.log(error);
} else {
//console.log(response.statusCode);
json_body = JSON.parse(body);
// console.log(json_body);
if (json_body.elements.length == 0) {
console.log('Coursera Returned No Results!');
} else {
var max_len = 0; //initialize the max_len for cappping the results
if (json_body.elements.length >= 5) {
max_len = 5;
} else {
max_len = json_body.elements.length;
}
for (var i=0; i<max_len; i++) {
var name = json_body.elements[i].name;
var courseUrl = json_body.elements[i].previewLink;
var imageUrl = json_body.elements[i].photoUrl;
var description = json_body.elements[i].description;
//console.log(name, courseUrl, imageUrl, description);
results_image.push(["Coursera: " + name, courseUrl, imageUrl, description]);
}
// console.log(results);
// console.log(results_image);
}
}
//Check if this is the last search task before rendering all the results?
if (async_tokens === 1) {
res.render("home.html", {'results': results,'results_image': results_image});
}
async_tokens--; //One Task done: decrements the token.
});
}
//Udacity
//Check if this MOOC provider is checked
if(options[i] === 'udacity'){
console.log("Search Udacity");
console.log(i);
request({
url: 'http://peaceful-springs-13283.herokuapp.com/udacity?title=' + courses,
method: 'GET',
}, function(error, response, body){
if(error) {
console.log(error);
} else {
//console.log(response.statusCode);
json_body = JSON.parse(body);
console.log("THIS IS THE UDATICYT RESULT!!!!!!!");
if (json_body.length == 0){
console.log('Udacity Returned No Results!');
} else {
var max_len = 0; //initialize the max_len for cappping the results
if (json_body.length >= 5) {
max_len = 5;
} else {
max_len = json_body.length;
}
for(var i=0; i<max_len; i++) {
var name = json_body[i].title;
var courseUrl = json_body[i].homepage;
// var imageUrl = json_body.image;
var description = json_body[i].short_summary;
//console.log(name, courseUrl, imageUrl);
results_image.push(["Udacity: " + name, courseUrl,'https://s3.amazonaws.com/accredible-cdn/udacity/udacity_seo.jpg',description]);
}
console.log(results);
}
}
//Check if this is the last search task before rendering all the results?
if (async_tokens === 1) {
res.render("home.html", {'results': results,'results_image': results_image});
}
async_tokens--; //One Task done: decrements the token.
});
}
}
//End For-loop
//???The codes below probably won't render anything, because Nodejs is asynchronous, so it renders empty results before the steps above get results back.
//Render page with all results
// res.render("home.html", {'results': results});
});
// Start up server on heroku host or localhost 3000
var server = app.listen(process.env.PORT || 3000, function () {
var port = server.address().port;
console.log('Group project webserver listening on port ' + port + '!');
});