-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
91 lines (70 loc) · 2.35 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
var colors = require('colors'); // pretty console colors
var dotenv = require('dotenv'); // Requires .env file with API keys
dotenv.load();
var request = require('request');
var sendgrid = require('sendgrid')(process.env.MAILKEY);
var http = require('http');
var currentDate = "test";
getLastUpdated();
// console.log(getLastUpdated());
var interval = setInterval(function() {
getLastUpdated();
}, 1000);
function getLastUpdated()
{
var dateLast;
var options = {
host: process.env.SCRAPE_HOST,
path: process.env.SCRAPE_URL
};
callback = function(response) {
var str = '';
//another chunk of data has been recieved, so append it to `str`
response.on('data', function (chunk) {
str += chunk;
});
//the whole response has been recieved, so we just print it out here
response.on('end', function () {
// console.log(str);
str = str.toString();
var location = str.search("updated:");
// console.log(typeof location);
dateLast = str.substring(location-2,str.length);
if(currentDate != dateLast && currentDate != "test")
{
// console.log("email");
alertUsers();
}
else {
// console.log("same");
// console.log(currentDate);
}
currentDate = dateLast;
});
};
http.request(options, callback).end();
}
// sends an email to all of the addresses registered in the firebase.
function alertUsers() {
// get user emails
request(process.env.DBHOST, function (err, res, body) {
var gform_data = JSON.parse(body).result;
var emails = [];
for (var i = 0; i < gform_data.length; i++) {
emails.push(gform_data[i]["What's your email?"]);
}
console.log("Sending to update to ".blue + emails.length.toString().magenta + " students...".blue);
for (var x = 0; x < emails.length; x++) {
sendgrid.send({
to: emails[x],
from: '[email protected]',
fromname: 'Chemistry H Updates',
subject: "O'Connell Grades Posted",
text: "Mr. O'Connell has updated the grades."
}, function(err, json) {
if (err) { return console.error(err); }
});
}
console.log(("Successfully sent emails to all " + emails.length.toString() + " students.").green);
});
}