start working on submitting course without user-provided course code

This commit is contained in:
Vineet K 2024-01-11 18:36:22 -05:00
parent e9c76998dc
commit de8ed98b4c
3 changed files with 39 additions and 8 deletions

View File

@ -11,17 +11,27 @@ import (
"strings" "strings"
) )
func submit_code(course_code int, attendance_code string) { func submit_code(course_code int, attendance_code string) bool {
cur_body := launch_aplus(course_code) cur_body := launch_aplus(course_code)
if verbose { if verbose {
fmt.Printf("%s", cur_body) 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 false
}
current_url := fmt.Sprintf("%s/student/", base_aplus_link) current_url := fmt.Sprintf("%s/student/", base_aplus_link)
// the links to submit the code for a class is under the dayPanel div // the links to submit the code for a class is under the dayPanel div
daypanel_start := strings.Index(cur_body, "<div class=\"dayPanel\"") daypanel_start := strings.Index(cur_body, "<div class=\"dayPanel\"")
if !strings.Contains(cur_body[daypanel_start:], "<a href=\"./?module") {
fmt.Println("No code submitted. Did the professor generate the code or is the class in session?")
return false
}
// link_start - link_end is to extract the URL // link_start - link_end is to extract the URL
// TODO: make this more elegant to allow multiple of these links // TODO: make this more elegant to allow multiple of these links
// (maybe use an html parser) // (maybe use an html parser)
@ -62,6 +72,8 @@ func submit_code(course_code int, attendance_code string) {
} else if strings.Index(string(body), "ctl02_codeSuccessMessage") != -1 { } else if strings.Index(string(body), "ctl02_codeSuccessMessage") != -1 {
fmt.Println("Code successfully recorded") fmt.Println("Code successfully recorded")
} }
return true
} }
func launch_aplus(course_code int) string { func launch_aplus(course_code int) string {
@ -148,3 +160,17 @@ func extract_attribute(input string, attribute string) string {
return input[start : start+end] return input[start : start+end]
} }
func submit_code_sans_course(attendance_code string) {
courses := list_all_courses(true)
for _, course := range courses {
fmt.Println("Checking with course", course.ID%10000)
if submit_code(int(course.ID%10000), attendance_code) == true {
fmt.Println("Submitted code to course", course.ID%10000)
return
}
}
fmt.Println("Could not submit code %s to any course\n", attendance_code)
}

View File

@ -83,9 +83,15 @@ func list_all_courses() {
canvas_courses := get_courses(token, all_courses, client) canvas_courses := get_courses(token, all_courses, client)
for _, course := range canvas_courses { if !onlyreturn {
fmt.Printf("%d, %s\n", course.ID%10000, course.Name)
for _, course := range canvas_courses {
fmt.Printf("%d, %s\n", course.ID%10000, course.Name)
}
} }
return canvas_courses
} }
func list_favorite_courses() { func list_favorite_courses() {

View File

@ -54,7 +54,7 @@ func main() {
} }
if listall { if listall {
list_all_courses() _ = list_all_courses(false)
os.Exit(0) os.Exit(0)
} }
@ -71,10 +71,6 @@ func main() {
if len(code) == 5 && course != -1 { if len(code) == 5 && course != -1 {
fmt.Printf("%d %s\n", course, code) fmt.Printf("%d %s\n", course, code)
submit_code(course, code) submit_code(course, code)
} else if code != "" && course == -1 {
fmt.Println("Course code not provided")
usage()
os.Exit(1)
} else if code == "" && course != -1 { } else if code == "" && course != -1 {
fmt.Println("Attendance code not provided") fmt.Println("Attendance code not provided")
usage() usage()
@ -83,5 +79,8 @@ func main() {
fmt.Println("Attendance code must be 5 characters long") fmt.Println("Attendance code must be 5 characters long")
usage() usage()
os.Exit(1) os.Exit(1)
} else if len(code) == 5 && course == -1 {
fmt.Println("Checking which course to send the code to")
submit_code_sans_course(code)
} }
} }