-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
292 lines (253 loc) · 7.99 KB
/
index.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
/* This file contains the lightweight Express/NodeJS backend for https://eecs101.org/. */
const express = require('express');
const app = express();
/* Add support for fetch. */
const fetch = require("node-fetch");
/* Heroku uses process.env.PORT */
const port = process.env.PORT || 8080;
/* TODO: Implement SSL/HTTPS redirection. */
// var sslRedirect = require('heroku-ssl-redirect');
// app.use(sslRedirect());
/* Add CORS. */
var cors = require('cors')
app.use(cors())
/* Add support for JSON payloads in POST requests. */
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
/* Import metadata.json for the course map (it's used for preprocessing the search query). */
const metadata = require('./metadata.json');
const coursemap = metadata.coursemap;
/* Import dump.json for the cached Piazza dataset. Filter to only posts with at least one response. */
var dataset = require('./dump.json');
dataset = dataset.filter((row) => { return (row.children.length > 0); })
/* Fuse is our fuzzy searching API. */
const Fuse = require('fuse.js')
const fuse = new Fuse(dataset, {
keys: ['searchContent']
})
/* -------------- SECTION: Filtering Methods ----------------- */
/* A helper function to filter on data. */
function filterData(data, question, courses, professors, tags, isAuto) {
var results = [];
/* For 'auto' search mode, we perform query preprocessing to extract classes and keywords
from the search query. We add a special case for when we see the keyword "workload" */
var filterOnWorkload = false;
if (isAuto) {
var parts = question.split(' ');
/* Iterate through each word in the search query, looking for courses. */
for (var i = 0; i < parts.length; i++) {
var part = parts[i].toUpperCase();
var entry = coursemap[part];
if (entry != null && !courses.includes(entry)) courses.push(entry);
}
/* Special flag for when we're searching for 'workload' */
if (parts.includes('workload')) filterOnWorkload = true;
}
/* Perform strict filter matching. */
for (var i = 0; i < data.length; i++) {
var row = data[i];
var skip = false;
/* Filter on courses. */
for (var j = 0; j < courses.length; j++) {
var course = courses[j];
if (row.courses == null || !row.courses.includes(course)) {
skip = true;
break;
}
}
if (skip) continue;
/* Filter on workload. */
if (filterOnWorkload && !row.searchContent.includes('workload')) {
continue;
}
/* Filter on tags. */
for (var j = 0; j < tags.length; j++) {
var tag = tags[j];
if (row.tags == null || !row.tags.includes(tag)) {
skip = true;
break;
};
}
if (skip) continue;
/* Filter on professors. */
for (var j = 0; j < professors.length; j++) {
var professor = professors[j];
if (row.professors == null || !row.professors.includes(professor)) {
skip = true;
break;
};
}
if (skip) continue;
/* Our filtering is complete; push a stripped-down version of the row. */
results.push(rowMetadata(row));
}
return results;
}
function rowMetadata(row) {
return {
id: row.id,
title: row.title,
date: row.date,
upvotes: row.upvotes,
source: 'piazza'
}
}
/* -------------- SECTION: Search Methods ----------------- */
function searchFuzzy(question) {
return fuse.search(question).map((row) => row.item);
}
function searchRegex(question) {
/* TODO: Improve regex search (this is buggy and not currently implemented) */
var regex = new RegExp(question);
return dataset.filter((row) => {
return (row.searchContent.toLowerCase().search(regex) != -1);
});
}
function searchExact(question) {
return dataset.filter((row) => {
return row.searchContent.toLowerCase().includes(question);
})
}
function searchTitle(question) {
return dataset.filter((row) => {
return row.title.toLowerCase().includes(question);
})
}
function queryPiazza(question, courses, professors, tags, mode) {
/* First, apply our search method. */
var data = [];
var isAuto = false;
if (question != '') {
switch (mode) {
case 'auto':
data = searchFuzzy(question);
isAuto = true;
break;
case 'fuzzy':
data = searchFuzzy(question);
break;
case 'regex':
data = searchRegex(question);
break;
case 'exact':
data = searchExact(question);
break;
case 'title':
data = searchTitle(question);
break;
}
} else {
data = dataset;
}
/* Then, filter data on courses, professors, and tags. */
data = filterData(data, question, courses, professors, tags, isAuto);
return data;
}
async function queryReddit(question, courses, professors, tags) {
var data = [];
const response = await fetch('https://www.reddit.com/r/berkeley/search.json?q=' + question + '&restrict_sr=true')
const record = await response.json();
if (record == null) return [];
record.data.children.forEach((row) => {
var date = new Date(0);
date.setUTCSeconds(row.data.created_utc);
data.push({
id: row.data.url,
title: row.data.title,
date: date,
upvotes: row.data.ups,
permalink: row.data.permalink,
source: 'reddit'
});
})
return data;
}
function sortData(data, sort) {
/* Then, apply our sort. */
switch(sort) {
case 'auto':
break;
case 'upvotes':
data.sort(function(a, b) {
return (a.upvotes || 0) > (b.upvotes || 0);
})
break;
case 'date':
data.sort(function(a, b) {
return (a.date > b.date) ? -1 : 1
})
break;
case 'default':
break;
}
return data;
}
/* -------------- SECTION: Routing ----------------- */
function returnRedditHits(url, title, res) {
res.json({
title: title,
tags: ['reddit'],
content: 'To open this post on Reddit, click ' + '<a href="' + url + '" target="_blank">here.</a>',
upvotes: 0,
id: url,
professors: [],
children: [],
courses: [],
date: new Date().toString()
})
}
/* Returns post content for a given post. */
app.post('/content', (req, res) => {
var id = req.body.id || '';
var source = req.body.source || '';
var title = req.body.title || '';
if (id != '' && source != '') {
if (source == 'reddit') {
returnRedditHits(id, title, res);
return;
} else {
for (var i = 0; i < dataset.length; i++) {
if (dataset[i].id == id) {
res.json(dataset[i]);
return;
}
}
}
}
})
/* Returns a list of applicable posts' metadata. */
app.post('/query', (req, res) => {
/* First, we unwrap and null-check all parameters, setting default values as we go. */
var question = req.body.question || '';
if (question == null) question = '';
question = question.toLowerCase();
var source = req.body.source || 'piazza';
var mode = req.body.mode || 'auto';
var sort = req.body.sort || 'auto';
var courses = req.body.courses || [];
if (typeof courses == 'string' && courses != '') courses = [courses];
var professors = req.body.professors || [];
if (typeof professors == 'string' && professors != '') professors = [professors];
var tags = req.body.tags || [];
if (typeof tags == 'string' && tags != '') tags = [tags];
/* If no filters were applied and the search query is empty, return an empty dictionary. */
if (question == '' && (courses.length + professors.length + tags.length == 0)) {
res.json([]);
return;
}
var data = [];
if (source == 'piazza') {
data = queryPiazza(question, courses, professors, tags, mode);
data = sortData(data, sort);
res.json(data);
} else if (source == 'reddit') {
queryReddit(question, courses, professors, tags).then(records => {
data = records;
sortData(data, sort);
res.json(data);
});
}
})
app.use(express.static('client/build'))
app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`) })