add timetable function

This commit is contained in:
Vineet K 2024-01-16 14:50:19 -05:00
parent 4d0051f516
commit 627120b31f
2 changed files with 68 additions and 1 deletions

View File

@ -7,7 +7,10 @@ import (
"io"
"net/http"
"net/url"
"regexp"
"strings"
"strconv"
"time"
)
func submit_code(course_code int, attendance_code string) bool {
@ -176,3 +179,54 @@ func submit_code_sans_course(attendance_code string) {
fmt.Println("Could not submit code", attendance_code, "to any course")
}
func timetable(course_code int) {
cur_body := launch_aplus(course_code)
if cur_body == "" {
return
}
if verbose {
fmt.Printf("%s", cur_body)
}
if strings.Contains(cur_body, "Student not known") {
fmt.Println("Student not known. This course must first be visited by an instructor for students to have access.")
return
}
current_url := fmt.Sprintf("%s/student/", base_aplus_link)
tt_start := strings.Index(cur_body, "<div class=\"stv_tt_container\"")
link_start := tt_start
link_start += strings.Index(cur_body[link_start:], "<a href=\"./?canvasCourse")
link_start += len("<a href=\"")
link_end := link_start
link_end += strings.Index(cur_body[link_end:], "\"")
link_url := current_url + cur_body[link_start:link_end]
link_url = html.UnescapeString(link_url)
resp, _ := client.Get(link_url)
body, _ := io.ReadAll(resp.Body)
tt_full := string(body)[(strings.Index(string(body), "<div class=\"stv_tt_container\"")):]
tt_full = tt_full[:strings.Index(tt_full, "\n")]
re_date := regexp.MustCompile("Date\\.UTC\\([^\\)]+")
re_status := regexp.MustCompile("title=\"\">[^<]*")
dates, statuses := re_date.FindAllString(tt_full, -1), re_status.FindAllString(tt_full, -1)
for i := range dates {
jsdate := strings.Split(dates[i][9:], ",")
year, _ := strconv.Atoi(jsdate[0])
month, _ := strconv.Atoi(jsdate[1])
day, _ := strconv.Atoi(jsdate[2])
hour, _ := strconv.Atoi(jsdate[3])
min, _ := strconv.Atoi(jsdate[4])
sec, _ := strconv.Atoi(jsdate[5])
date := time.Date(year, time.Month(month + 1), day, hour, min, sec, 0, time.UTC).Local()
fmt.Println(date.Format("Mon Jan 02 15:04:05 MST 2006") + ": " + strings.Replace(statuses[i], "title=\"\">", "", 1))
}
}

15
main.go
View File

@ -16,11 +16,12 @@ var client http.Client
var verbose bool
func usage() {
fmt.Println("Usage: aplus --code attendance_code --course course_code [--list] [--listfav] [--help]")
fmt.Println("Usage: aplus --code attendance_code --course course_code [--list] [--listfav] [--timetable] [--help]")
fmt.Println(" -c, --code\tfive character attendance code")
fmt.Println(" -C, --course\tcanvas course ID")
fmt.Println(" -L, --list\tlists canvas courses with name and ID")
fmt.Println(" -F, --listfav\tlists favourited canvas courses with name and ID")
fmt.Println(" -t, --timetable\tlists the class sessions in a course")
fmt.Println(" -v, --verbose\tprints with verbose output")
fmt.Println(" -h, --help\tdisplays this help message")
fmt.Println(" -p, --profile\twrites cpu profile to a file")
@ -33,6 +34,7 @@ func main() {
var course int
var listall bool
var listfav bool
var timetable_ bool
var help bool
var cpuprofile string
@ -44,6 +46,8 @@ func main() {
flag.BoolVar(&listall, "L", false, "list all canvas courses")
flag.BoolVar(&listfav, "listfav", false, "list favorited canvas courses")
flag.BoolVar(&listfav, "F", false, "list favorited canvas courses")
flag.BoolVar(&timetable_, "t", false, "lists the class sessions in a course")
flag.BoolVar(&timetable_, "timetable", false, "lists the class sessions in a course")
flag.BoolVar(&verbose, "verbose", false, "verbose output")
flag.BoolVar(&verbose, "v", false, "verbose output")
flag.BoolVar(&help, "help", false, "view usage message")
@ -84,6 +88,15 @@ func main() {
os.Exit(0)
}
if course != -1 && timetable_ {
timetable(course)
os.Exit(0)
} else if course == -1 && timetable_ {
fmt.Println("Course code required for timetable")
usage()
os.Exit(1)
}
if help {
usage()
os.Exit(0)