diff options
| author | vin <git@vineetk.net> | 2024-01-16 14:50:19 -0500 |
|---|---|---|
| committer | vin <git@vineetk.net> | 2024-01-16 14:50:19 -0500 |
| commit | 627120b31f22f2f286fc2552f4586f29e7e1762c (patch) | |
| tree | d5fb12c10aaf44044f239c44fa99724908e5d5ab | |
| parent | 4d0051f5165c5455f8780a163cfb225754aaa7d9 (diff) | |
add timetable function
| -rw-r--r-- | aplus.go | 54 | ||||
| -rw-r--r-- | main.go | 15 |
2 files changed, 68 insertions, 1 deletions
| @@ -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 | ||
| 13 | func submit_code(course_code int, attendance_code string) bool { | 16 | func 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 | |||
| 183 | func 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 | } | ||
| @@ -16,11 +16,12 @@ var client http.Client | |||
| 16 | var verbose bool | 16 | var verbose bool |
| 17 | 17 | ||
| 18 | func usage() { | 18 | func usage() { |
| 19 | fmt.Println("Usage: aplus --code attendance_code --course course_code [--list] [--listfav] [--help]") | 19 | fmt.Println("Usage: aplus --code attendance_code --course course_code [--list] [--listfav] [--timetable] [--help]") |
| 20 | fmt.Println(" -c, --code\tfive character attendance code") | 20 | fmt.Println(" -c, --code\tfive character attendance code") |
| 21 | fmt.Println(" -C, --course\tcanvas course ID") | 21 | fmt.Println(" -C, --course\tcanvas course ID") |
| 22 | fmt.Println(" -L, --list\tlists canvas courses with name and ID") | 22 | fmt.Println(" -L, --list\tlists canvas courses with name and ID") |
| 23 | fmt.Println(" -F, --listfav\tlists favourited canvas courses with name and ID") | 23 | fmt.Println(" -F, --listfav\tlists favourited canvas courses with name and ID") |
| 24 | fmt.Println(" -t, --timetable\tlists the class sessions in a course") | ||
| 24 | fmt.Println(" -v, --verbose\tprints with verbose output") | 25 | fmt.Println(" -v, --verbose\tprints with verbose output") |
| 25 | fmt.Println(" -h, --help\tdisplays this help message") | 26 | fmt.Println(" -h, --help\tdisplays this help message") |
| 26 | fmt.Println(" -p, --profile\twrites cpu profile to a file") | 27 | fmt.Println(" -p, --profile\twrites cpu profile to a file") |
| @@ -33,6 +34,7 @@ func main() { | |||
| 33 | var course int | 34 | var course int |
| 34 | var listall bool | 35 | var listall bool |
| 35 | var listfav bool | 36 | var listfav bool |
| 37 | var timetable_ bool | ||
| 36 | var help bool | 38 | var help bool |
| 37 | var cpuprofile string | 39 | var cpuprofile string |
| 38 | 40 | ||
| @@ -44,6 +46,8 @@ func main() { | |||
| 44 | flag.BoolVar(&listall, "L", false, "list all canvas courses") | 46 | flag.BoolVar(&listall, "L", false, "list all canvas courses") |
| 45 | flag.BoolVar(&listfav, "listfav", false, "list favorited canvas courses") | 47 | flag.BoolVar(&listfav, "listfav", false, "list favorited canvas courses") |
| 46 | flag.BoolVar(&listfav, "F", false, "list favorited canvas courses") | 48 | flag.BoolVar(&listfav, "F", false, "list favorited canvas courses") |
| 49 | flag.BoolVar(&timetable_, "t", false, "lists the class sessions in a course") | ||
| 50 | flag.BoolVar(&timetable_, "timetable", false, "lists the class sessions in a course") | ||
| 47 | flag.BoolVar(&verbose, "verbose", false, "verbose output") | 51 | flag.BoolVar(&verbose, "verbose", false, "verbose output") |
| 48 | flag.BoolVar(&verbose, "v", false, "verbose output") | 52 | flag.BoolVar(&verbose, "v", false, "verbose output") |
| 49 | flag.BoolVar(&help, "help", false, "view usage message") | 53 | flag.BoolVar(&help, "help", false, "view usage message") |
| @@ -84,6 +88,15 @@ func main() { | |||
| 84 | os.Exit(0) | 88 | os.Exit(0) |
| 85 | } | 89 | } |
| 86 | 90 | ||
| 91 | if course != -1 && timetable_ { | ||
| 92 | timetable(course) | ||
| 93 | os.Exit(0) | ||
| 94 | } else if course == -1 && timetable_ { | ||
| 95 | fmt.Println("Course code required for timetable") | ||
| 96 | usage() | ||
| 97 | os.Exit(1) | ||
| 98 | } | ||
| 99 | |||
| 87 | if help { | 100 | if help { |
| 88 | usage() | 101 | usage() |
| 89 | os.Exit(0) | 102 | os.Exit(0) |
