/
server.js
466 lines (324 loc) · 10.2 KB
/
server.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//////// This is the Server-side code and app
/////// **** All files and componenets relevant to the React app that runs in tandem with this node server are in another folder
///// navigate '/client/src/-' for react stuff
/// '/client/src/App.js' has all the routes and components (All components are in that root directory atm, need to be sorted to folders)
require('dotenv').config()
const express = require("express");
const path = require("path");
const PORT = process.env.PORT || 3001;
const app = express();
const mongoose = require('mongoose')
const db = require('./Models')
const encoder = require('./decoder.js')
const axios = require('axios')
const cheerio = require('cheerio')
const nodemailer = require('nodemailer');
const E0 = process.env.E0
const E1 = process.env.E1
const faker = require("faker");
const AccessToken = require("twilio").jwt.AccessToken;
const VideoGrant = AccessToken.VideoGrant;
// Define middleware here
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
if(process.env.NODE_ENV === "development") { // Configuration for development environment
var webpackDevMiddleware = require("webpack-dev-middleware");
var webpackConfig = require("./webpack.config.js");
const webpackCompiler = webpack(webpackConfig);
app.use(webpackDevMiddleware(webpackCompiler));
app.use(express.static(path.join(__dirname, "app")));
} else if(process.env.NODE_ENV === "production") { // Configuration for production environment
app.use(express.static(path.join(__dirname, "client/build")));
}
let transport = {
host: 'smtp.gmail.com',
auth: {
user: E0,
pass: E1
}
}
let transporter = nodemailer.createTransport(transport)
transporter.verify((error, success) => {
if (error) {
console.log(error);
} else {
console.log('Server is ready to take messages');
}
});
const mm = mongoose.connect(process.env.MONGODB_URI || "mongodb://testaccount:fakepassword1@ds241493.mlab.com:41493/deploytest", { useNewUrlParser : true});
app.post('/sendmail', (req, res, next) => {
var name = req.body.name
var email = req.body.email
var message = req.body.message
var content = `name: ${name} \n email: ${email} \n message: ${message} `
console.log(req.body.message)
var mail = {
from: name,
to: process.env.E0 || 'happylizardman22@gmail.com', //Change to email address that you want to receive messages on
subject: 'New Message from Contact Form',
text: content
}
transporter.sendMail(mail, (err, data) => {
if (err) {
res.json({
msg: 'fail'
})
} else {
res.json({
msg: 'success'
})
}
}
);
});
app.get('/token', function(req, res) {
var identity = faker.name.findName();
let token = new AccessToken(
proccess.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_API_KEY,
process.env.TWILIO_API_SECRET
);
token.identity = identity;
const grant = new VideoGrant();
token.addGrant(grant);
console.log(token)
console.log(token.toJwt())
res.send({
identity:identity,
token: token.toJwt()
});
})
app.get("/get/friends", function(req, res) {
let energytype = "";
let orgtype = "";
let dectype = "";
let inftype = ""
if (req.body.En === 'E') {
energytype = "Extrovert"
} else {
energytype = "Introvert"
}
if (req.body.In === 'N') {
inftype = "Intuition"
} else {
inftype = "Sense"
}
if (req.body.Org === 'J') {
orgtype = "Judging"
} else {
orgtype = "Perceiving"
}
if (req.body.Dec === 'E') {
dectype = "Thinking"
} else {
dectype = "Feeling"
}
db.friendFinder
.find({ Energy : [energytype],
Decision : [dectype],
Information : [inftype],
Organization : [orgtype]})
.then(function(dbModel) {
res.json(dbModel)
});
})
app.get("/scrape/current", function(req, res) {
// First, we grab the body of the html with axios
db.Article
.find({'time' : 'current'})
.sort({ date: -1 }).then(function(dbModel) {
res.json(dbModel)
})
axios.get("https://www.gosugamers.net/events/list?type=current&").then(function(response) {
// Then, we load that into cheerio and save it to $ for a shorthand selector
var $ = cheerio.load(response.data);
// Now, we grab every h2 within an article tag, and do the following:
$("div.event.columns").each(function(i, element) {
// Save an empty result object
let result = [];
console.log(i)
result.title = $(this).children('div.right')
.children('h3')
.children('a')
.text();
result.link = $(this).children('div.left')
.children('a')
.attr("href");
result.banner = $(this).children('div.left')
.children('a')
.children('img')
.attr("src");
result.icon = $(this).children('div.right')
.children('p')
.children('img')
.attr('title');
if (true) {
// Insert the data in the scrapedData db
db.Article.create(
{
title: result.title,
link: result.link,
banner : result.banner,
icon : result.icon,
time : 'current'
},
function(err, inserted) {
if (err) {
// Log the error if one is encountered during the query
console.log(err);
}
else {
console.log(inserted)
}
});
}
});
dbModel = db.Article
.find()
.sort({ date: -1 })
}).catch(err => console.log(err));
})
app.get("/scrape/upcoming", function(req, res) {
// First, we grab the body of the html with axios
db.Article
.find({ 'time' : 'upcoming'})
.sort({ date: -1 }).then(function(dbModel) {
res.json(dbModel)
})
axios.get("https://www.gosugamers.net/events/list?type=upcoming&").then(function(response) {
// Then, we load that into cheerio and save it to $ for a shorthand selector
var $ = cheerio.load(response.data);
// Now, we grab every h2 within an article tag, and do the following:
$("div.event.columns").each(function(i, element) {
// Save an empty result object
let result = [];
console.log(i)
// Save the text and href of each link enclosed in the current element
result.title = $(this).children('div.right')
.children('h3')
.children('a')
.text();
result.link = $(this).children('div.left')
.children('a')
.attr("href");
result.banner = $(this).children('div.left')
.children('a')
.children('img')
.attr("src");
result.icon = $(this).children('div.right')
.children('p')
.children('img')
.attr('title');
// If this found element had both a title and a link
if (true) {
// Insert the data in the scrapedData db
db.Article.create(
{
title: result.title,
link: result.link,
banner : result.banner,
icon : result.icon,
time : 'upcoming'
},
function(err, inserted) {
if (err) {
// Log the error if one is encountered during the query
console.log(err);
}
else {
}
});
}
});
dbModel = db.Article
.find()
.sort({ date: -1 })
}).catch(err => console.log(err));
})
app.post("/post/friendfinder", function(req, res) {
console.log(req.body)
db.friendFinder
.create(
{
name: req.body.name,
Extrovert: req.body.E,
Introvert : req.body.I,
Intuitive : req.body.N,
Sensor : req.body.S,
Thinker : req.body.T,
Feeler : req.body.F,
Judger : req.body.J,
Perceiver : req.body.P,
},
function(err, inserted) {
if (err) {
// Log the error if one is encountered during the query
console.log(err);
}
else {
}
});
console.log(req.body)
});
app.post("/scrape/NYT", function(topic, startYear, endYear) {
axios.get("https://api.nytimes.com/svc/search/v2/articlesearch.json?api-key=8f4f6746cba3492392355e72c21ed34d&q=" + topic + "&begin_date=" + startYear + "0101&end_date=" + endYear + "0101").then(function(req, res) {
console.log("Adding saved artice to the db");
console.log("req.body: ", req.body);
db.NYT.create(req.body).then(function(doc) {
res.json(doc);
console.log("doc: ", doc);
}).catch(function(err) {
res.json(err);
});
})
console.log("Gathering saved articles from the db");
db.NYT.find().then(function(doc) {
res.json(doc);
}).catch(function(err) {
res.json(err);
});
})
app.post("/post/tournamentresults", function(req, res) {
console.log(req.body)
model
.create(
{
tournamentName: req.body.tournamentName,
tournamentDate: req.body.tournamentDate,
players : req.body.players
},
function(err, inserted) {
if (err) {
// Log the error if one is encountered during the query
console.log(err);
}
else {
console.log(inserted)
}
});
console.log(req.body)
});
app.get("/get/tournamentresults", function(req, res) {
db.Report
.find({ }).sort({"date" : -1})
.then(function(dbModel) {
res.json(dbModel)
});
console.log(res.data)
});
app.get("/get/analysis", function(req, res) {
db.Report
.find({ }).sort({"date" : -1})
.then(function(dbModel) {
res.json(dbModel)
});
console.log(res.data)
});
// Send every request to the React app
// Define any API routes before this runs
app.get("*", function(req, res) {
res.sendFile(path.join(__dirname, "./client/build/index.html"));
});
app.listen(PORT, function() {
console.log(`🌎 ==> Server now on port ${PORT}!`);
});