summaryrefslogtreecommitdiff
path: root/aplus.go
diff options
context:
space:
mode:
authorvin <git@vineetk.net>2024-01-16 14:50:19 -0500
committervin <git@vineetk.net>2024-01-16 14:50:19 -0500
commit627120b31f22f2f286fc2552f4586f29e7e1762c (patch)
treed5fb12c10aaf44044f239c44fa99724908e5d5ab /aplus.go
parent4d0051f5165c5455f8780a163cfb225754aaa7d9 (diff)
add timetable function
Diffstat (limited to 'aplus.go')
-rw-r--r--aplus.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/aplus.go b/aplus.go
index 2e2ab0a..e546b6c 100644
--- a/aplus.go
+++ b/aplus.go
@@ -7,7 +7,10 @@ import (
7 "io" 7 "io"
8 "net/http" 8 "net/http"
9 "net/url" 9 "net/url"
10 "regexp"
10 "strings" 11 "strings"
12 "strconv"
13 "time"
11) 14)
12 15
13func submit_code(course_code int, attendance_code string) bool { 16func submit_code(course_code int, attendance_code string) bool {
@@ -176,3 +179,54 @@ func submit_code_sans_course(attendance_code string) {
176 179
177 fmt.Println("Could not submit code", attendance_code, "to any course") 180 fmt.Println("Could not submit code", attendance_code, "to any course")
178} 181}
182
183func timetable(course_code int) {
184 cur_body := launch_aplus(course_code)
185 if cur_body == "" {
186 return
187 }
188
189 if verbose {
190 fmt.Printf("%s", cur_body)
191 }
192
193 if strings.Contains(cur_body, "Student not known") {
194 fmt.Println("Student not known. This course must first be visited by an instructor for students to have access.")
195 return
196 }
197
198 current_url := fmt.Sprintf("%s/student/", base_aplus_link)
199 tt_start := strings.Index(cur_body, "<div class=\"stv_tt_container\"")
200
201 link_start := tt_start
202 link_start += strings.Index(cur_body[link_start:], "<a href=\"./?canvasCourse")
203 link_start += len("<a href=\"")
204 link_end := link_start
205 link_end += strings.Index(cur_body[link_end:], "\"")
206
207 link_url := current_url + cur_body[link_start:link_end]
208 link_url = html.UnescapeString(link_url)
209
210 resp, _ := client.Get(link_url)
211 body, _ := io.ReadAll(resp.Body)
212
213 tt_full := string(body)[(strings.Index(string(body), "<div class=\"stv_tt_container\"")):]
214 tt_full = tt_full[:strings.Index(tt_full, "\n")]
215
216 re_date := regexp.MustCompile("Date\\.UTC\\([^\\)]+")
217 re_status := regexp.MustCompile("title=\"\">[^<]*")
218
219 dates, statuses := re_date.FindAllString(tt_full, -1), re_status.FindAllString(tt_full, -1)
220 for i := range dates {
221 jsdate := strings.Split(dates[i][9:], ",")
222 year, _ := strconv.Atoi(jsdate[0])
223 month, _ := strconv.Atoi(jsdate[1])
224 day, _ := strconv.Atoi(jsdate[2])
225 hour, _ := strconv.Atoi(jsdate[3])
226 min, _ := strconv.Atoi(jsdate[4])
227 sec, _ := strconv.Atoi(jsdate[5])
228 date := time.Date(year, time.Month(month + 1), day, hour, min, sec, 0, time.UTC).Local()
229
230 fmt.Println(date.Format("Mon Jan 02 15:04:05 MST 2006") + ": " + strings.Replace(statuses[i], "title=\"\">", "", 1))
231 }
232}