-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
260 lines (245 loc) · 7 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
const express = require("express");
const port = 8032;
const axios = require("axios");
const db = require("./config/mongoose");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const passport = require("passport");
const passportLocal = require("./config/passport-local-strategy");
const mongoStore = require("connect-mongo")(session);
const passportGoogle = require("./config/passport-google-oauth2-strategy");
const flash = require("connect-flash");
const customMware = require("./config/middleware");
const { google } = require("googleapis");
//const request = require('request-promise');
const cheerio = require("cheerio");
const https = require("https");
//const professorsList = require('./config/professors');
const fs = require("fs");
const Admin = require("./models/admin");
const expressLayouts = require("express-ejs-layouts");
const app = express();
app.set("view engine", "ejs");
app.set("views", "./views");
app.use("/assets", express.static("./assets"));
app.use(expressLayouts);
app.use(express.urlencoded());
app.use(cookieParser());
app.use(
session({
name: "nodues",
secret: "blahsomething",
saveUninitialized: false,
resave: false,
cookie: {
maxAge: 1000 * 60 * 100,
},
store: new mongoStore(
{
mongooseConnection: db,
autoRemove: "disabled",
},
(err) => {
if (err) {
console.log("Error in setting up connect-mongo");
}
}
),
})
);
app.use(passport.initialize());
app.use(passport.session());
app.use(passport.setAuthenticatedUser);
app.use(flash());
app.use(customMware.setFlash);
app.use("/", require("./routes/index"));
app.listen(port, async (err) => {
if (err) {
console.log("Error in running the server", err);
return;
}
console.log("Server is running perfectly fine on port: ", port);
//Admin Details
var spreadsheetId = "1hXUEUHWGt3TyrhvWPh25U8KlmutxcTVx2qfAdPHyCEY";
var auth = new google.auth.GoogleAuth({
keyFile: "credentials.json",
scopes: "https://www.googleapis.com/auth/spreadsheets",
});
var client = await auth.getClient();
var googleSheets = google.sheets({ version: "v4", auth: client });
var metadata = await googleSheets.spreadsheets.get({
auth: auth,
spreadsheetId: spreadsheetId,
});
var data = await googleSheets.spreadsheets.values.get({
auth: auth,
spreadsheetId: spreadsheetId,
range: "Sheet1",
});
global.admins_data = data.data.values;
fs.readFile("./data/admins.json", (err, data) => {
if (err) {
console.log("Error in writing to admins file: ", err);
return;
}
if (data != JSON.stringify(global.admins_data)) {
fs.writeFile(
"./data/admins.json",
JSON.stringify(global.admins_data),
(err) => {
if (err) {
console.log("Error in writing to admins file: ", err);
return;
}
}
);
}
});
Admin.db.collection.drop();
Admin.addAdmins(global.admins_data);
//Professors List
var spreadsheetId = "1L-mCmog-GlNVKQlV_6Jvo9WyQKZpr-S346ijUlPj0gM";
var auth = new google.auth.GoogleAuth({
keyFile: "credentials.json",
scopes: "https://www.googleapis.com/auth/spreadsheets",
});
var client = await auth.getClient();
var googleSheets = google.sheets({ version: "v4", auth: client });
var metadata = await googleSheets.spreadsheets.get({
auth: auth,
spreadsheetId: spreadsheetId,
});
var data = await googleSheets.spreadsheets.values.get({
auth: auth,
spreadsheetId: spreadsheetId,
range: "Sheet1",
});
professors_data = data.data.values;
professors_data.shift();
fs.readFile("./data/professors.json", (err, data) => {
if (err) {
console.log("Error in writing to admins file: ", err);
return;
}
if (data != JSON.stringify(professors_data)) {
fs.writeFile(
"./data/professors.json",
JSON.stringify(professors_data),
(err) => {
if (err) {
console.log("Error in writing to admins file: ", err);
return;
}
}
);
}
});
var proffNames = {};
for (var i in professors_data) {
if(i==0){
continue;
}
proffNames[professors_data[i][1]] = professors_data[i][0];
}
var text = `
const getProffName = (email) => {
return proffNames[email];
}
module.exports.proffNames = proffNames;
const isProff = (email) => {
if (email in proffNames) {
return true;
} else {
return false;
}
}
module.exports={getProffName,isProff};
`;
fs.writeFile(
"./data/getProffName.js",
"var proffNames = " + JSON.stringify(proffNames) + "\n" + text,
(err) => {
if (err) {
console.log("Error in writing to admins file: ", err);
return;
}
}
);
//Students List
var spreadsheetId = "1NfsIc8CO7n4CvqkmtmGhoOQgL7lGKAmlbk3konSuCxY";
var auth = new google.auth.GoogleAuth({
keyFile: "credentials.json",
scopes: "https://www.googleapis.com/auth/spreadsheets",
});
var client = await auth.getClient();
var googleSheets = google.sheets({ version: "v4", auth: client });
var metadata = await googleSheets.spreadsheets.get({
auth: auth,
spreadsheetId: spreadsheetId,
});
var data = await googleSheets.spreadsheets.values.get({
auth: auth,
spreadsheetId: spreadsheetId,
range: "Sheet1",
});
students_data = data.data.values;
fs.readFile("./data/students.json", (err, data) => {
if (err) {
console.log("Error in writing to studnts file: ", err);
return;
}
if (data != JSON.stringify(students_data)) {
fs.writeFile(
"./data/students.json",
JSON.stringify(students_data),
(err) => {
if (err) {
console.log("Error in writing to students file: ", err);
return;
}
}
);
}
});
function changeNameFormat(name) {
if (name.substring(0, 9) == "Academics") {
return "academics";
}
var arr = name.split(" ");
var newName = arr[0].toLowerCase();
for (var i = 1; i < arr.length; i++) {
if (arr[i] == "&") {
arr[i] = "and";
}
newName = newName + arr[i][0].toUpperCase() + arr[i].substring(1);
}
return newName;
}
var adminNames = {};
var names = [];
for (var i in global.admins_data) {
adminNames[global.admins_data[i][1]] = changeNameFormat(
global.admins_data[i][0]
);
names.push(changeNameFormat(global.admins_data[i][0]));
}
var text = `module.exports.getAdminName = (email) => {
if (email in adminNames) {
return adminNames[email];
} else {
return 'student';
}
};
module.exports.adminNames = adminNames;`;
global.admins_data = adminNames;
fs.writeFile(
"./data/getAdminName.js",
"var adminNames = " + JSON.stringify(adminNames) + ";\n" + text,
(err) => {
if (err) {
console.log("Error in writing to admins file: ", err);
return;
}
}
);
});