-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwatch.js
115 lines (92 loc) · 3.78 KB
/
watch.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
require("dotenv").config();
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const notifyPhoneNumber = process.env.NOTIFY_PHONE_NUMBER;
const twilioPhoneNumber = process.env.TWILIO_PHONE_NUMBER;
const client = require("twilio")(accountSid, authToken);
const fs = require("fs");
var contents = JSON.parse(fs.readFileSync("./config.json"));
const checkEveryXSeconds = contents.CheckEveryXSeconds;
const statusFileName = contents.StatusFileName;
const debugFileName = contents.DebugFileName;
const endDate = contents.EndDate;
const startDate = contents.StartDate;
console.log(statusFileName);
let pctPermitAvailabilityWebsite = "https://portal.permit.pcta.org/availability/mexican-border.php";
let messagesSent = [];
console.log("watching pct availability page on a constant loop every " + checkEveryXSeconds + " seconds");
const WriteStatus = (message) => {
fs.writeFile(statusFileName, message, (err) => {
if (err) throw err;
});
};
const WriteDebugLog = (log) => {
fs.writeFile(debugFileName, `${new Date()}:${log}\n`, { flag: "a" }, (err) => {
if (err) throw err;
});
};
const WriteLatestStatus = (date, responseCode, hasCalendar) => {
WriteStatus(`Latest Time Checked: ${date}\nLatest Response Code: ${responseCode}\nHas Calendar: ${hasCalendar}\n`);
};
const StringifyAvailablePermits = (availablePermits) => {
let message = "";
availablePermits.map((permit) => {
message += `(${permit.start_date}:${35 - permit.num}) `;
});
return message;
};
const GetNonSentMessages = (availablePermits) =>
availablePermits.filter((permit) => messagesSent.find((sentPermit) => sentPermit.start_date == permit.start_date) == undefined);
const CheckForPermitsAvailable = async (websiteSource, beginning_date) => {
let permitInfo = await JSON.parse(websiteSource.match(/(?<=data\s=\s){.*}/gm));
let availablePermits = [];
let datesChecked = 0;
console.log("checking permit info at " + new Date());
//console.log(permitInfo.calendar);
if (permitInfo == null) {
console.error("ruh roh, permit info is null");
console.error("here is the website source");
console.error(websiteSource);
} else {
permitInfo.calendar.map((date) => {
if (date.start_date >= startDate && date.start_date <= endDate) {
// only check dates in march/april/may
datesChecked++;
if (date.num != 50) {
console.log("we found " + (50 - date.num) + " permit(s) on " + date.start_date);
availablePermits = [...availablePermits, date];
}
}
});
WriteLatestStatus(new Date(), 200, datesChecked != 0);
if (availablePermits.length > 0) {
let nonSentMessages = GetNonSentMessages(availablePermits);
if (nonSentMessages.length > 0) {
console.log(`Open Permits ${StringifyAvailablePermits(availablePermits)}`);
console.log(`Sending message to ${notifyPhoneNumber} from ${twilioPhoneNumber}`);
let body = "Open Permits " + StringifyAvailablePermits(nonSentMessages);
client.messages.create({
body: body,
from: twilioPhoneNumber,
to: notifyPhoneNumber,
});
WriteDebugLog(`Sent message to ${notifyPhoneNumber} from ${twilioPhoneNumber} with body ${body}`);
messagesSent = [...messagesSent, ...nonSentMessages];
} else {
WriteDebugLog("Permits were available but they have all been sent");
}
} else {
WriteDebugLog("We did not find any available permits at " + new Date());
}
}
};
const CheckWebsite = () => {
try {
fetch(pctPermitAvailabilityWebsite)
.then((website) => website.text())
.then((text) => CheckForPermitsAvailable(text));
} catch (ex) {
console.error("failed to fetch at " + new Date());
}
};
setInterval(CheckWebsite, checkEveryXSeconds * 1000);