-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
166 lines (132 loc) · 6 KB
/
Main.java
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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Scanner;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Main class
*
* Make sure to include the JSON library in the lib folder as a project dependency.
* Only works if Chrome is installed, works on both Windows and Mac OS (hopefully).
*
* @author Jatin Kohli
* @since 3/14/20
*/
public class Main {
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSS'Z'"); //Java date time sucks :(
private static final int OFFSET = 120; // Time (seconds) to join a meeting before the scheduled time
private static final int ALLOWABLE_DELAY = 380; // Time after the meeting starts where you will join automatically
private static final String WINDOWS_CHROME_CMD = "cmd /c start chrome ";
private static final String MAC_CHROME_CMD = "bash -c open -a \"Google Chrome\" ";
private static Map<String, String> periodLinks;
private static Queue<QueueElement> linkQueue;
/**
* Fills periodLinks with Zoom links for each corresponding period key.
* Reads periods (keys) and links (values) from links.txt in the same directory.
*/
private static void instantiatePeriodLinks() {
periodLinks = new HashMap<String, String>();
try {
Scanner linkScanner = new Scanner(new File("links.txt"));
while (linkScanner.hasNextLine()) {
String key = linkScanner.next();
String value = linkScanner.next();
periodLinks.put(key, value);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* Fills linkQueue with Zoom links and times.
* Gets the schedule from the HarkerDev API using getScheduleJson(), gets the link corresponding to each period, and
* creates a Queue representing the day's schedule.
*/
private static void instantiateLinkQueue() {
linkQueue = new LinkedList<QueueElement>();
try {
Iterator<Object> scheduleIterator = (new JSONObject(getScheduleJson())).getJSONArray("schedule").iterator();
while (scheduleIterator.hasNext()) {
JSONObject period = (JSONObject) scheduleIterator.next();
String time = period.getString("start");
String link = periodLinks.get(period.getString("name"));
if (link != null) //Could be null if certain period has no associated link, like for office hours
linkQueue.add(new QueueElement(time, link));
}
} catch (JSONException e) {
System.out.println("No Meetings or Schedule found for today");
System.exit(-1);
} catch (NullPointerException e1) { //Harker Dev Machine broke
System.out.println("HarkerDev servers broke again :(");
System.exit(-2);
}
}
/**
* Gets the JSON object representing today's schedule from the HarkerDev API.
* Executes a curl command and parses the result.
*/
private static String getScheduleJson() {
LocalDate date = LocalDate.now();
System.out.println("Current Date: " + date);
System.out.println("Current Time: " + LocalTime.now() + "\r\n");
String getScheduleCommand = String.format(
"curl --request GET --url https://bell.dev.harker.org/api/schedule --header \"Content-Type: application/x-www-form-urlencoded\" --data month=%s --data day=%s --data year=%s",
date.getMonthValue(), date.getDayOfMonth(), date.getYear());
String schedule = null;
try {
Process process = (new ProcessBuilder()).command(getScheduleCommand.split(" ")).start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
schedule = reader.readLine();
process.waitFor();
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("API result: " + schedule + "\r\n");
return schedule;
}
/**
* Main method
*/
public static void main(String[] args) {
instantiatePeriodLinks();
instantiateLinkQueue();
int currentTime = -1;
int nextMeetingTime = -1;
boolean isWindowsOS = System.getProperty("os.name").toLowerCase().indexOf("win") != -1;
System.out.println("OS: " + System.getProperty("os.name"));
System.out.println(linkQueue.size() + " meetings today!\r\n");
while (!linkQueue.isEmpty()) {
currentTime = LocalTime.now().toSecondOfDay();
nextMeetingTime = LocalDateTime.parse(linkQueue.element().time, TIME_FORMATTER).toLocalTime().toSecondOfDay();
int timeDiff = nextMeetingTime - currentTime - OFFSET;
try {
if (timeDiff > 0) {
System.out.println("Waiting for meeting to start...");
Thread.sleep((long)(timeDiff * 1000));
}
String link = linkQueue.remove().link;
if (timeDiff >= -ALLOWABLE_DELAY) { //Don't join meeting if a certain amount of time after the meeting start time has passed
System.out.println("Opened link for next meeting.\r\n");
String command = (isWindowsOS ? WINDOWS_CHROME_CMD : MAC_CHROME_CMD) + link;
(new ProcessBuilder()).command(command.split(" ")).start().waitFor();
}
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("No more meetings today :)");
System.exit(0);
}
}