aplus

A+Attendance CLI utility via Instructure Canvas
Log | Files | Refs | README | LICENSE

commit 7b613b030da2432830a9f00bfc6aeb9e59bcd728
parent aa16b741d54325527eb6bea451cb24526551ab52
Author: vin <git@vineetk.net>
Date:   Tue, 14 Nov 2023 12:05:20 -0500

add rudimentary messy function for submitting aplus code

It is very messy, but later using golang's HTML parser should help make it more
readable.

Diffstat:
Maplus.go | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Mmain.go | 2+-
2 files changed, 57 insertions(+), 3 deletions(-)

diff --git a/aplus.go b/aplus.go @@ -10,7 +10,7 @@ import ( "strings" ) -func launch_aplus(course_code int) { +func launch_aplus(course_code int, attendance_code string) { aplus_link := fmt.Sprintf("%s/courses/%d/external_tools/sessionless_launch?id=%d&access_token=%s", base_link, course_code, external_tools_code, token) aplus := get_aplus(token, aplus_link, client) @@ -24,7 +24,9 @@ func launch_aplus(course_code int) { resp, _ = client.PostForm("https://floridapoly.aplusattendance.com/canvas", form_values) body, _ = io.ReadAll(resp.Body) - fmt.Println(string(body)) + submit_code(string(body), attendance_code, "https://floridapoly.aplusattendance.com/canvas/student/") + + // fmt.Println(string(body)) } func get_aplus(token string, link string, client http.Client) string { @@ -38,6 +40,58 @@ func get_aplus(token string, link string, client http.Client) string { return aplus.URL } +func submit_code(cur_body string, attendance_code string, current_url string) { + // the links to submit the code for a class is under the dayPanel div + daypanel_start := strings.Index(cur_body, "<div class=\"dayPanel\"") + + // link_start - link_end is to extract the URL + // TODO: make this more elegant to allow multiple of these links + // (maybe use an html parser) + // or at least use extract_attribute() + link_start := daypanel_start + link_start += strings.Index(cur_body[link_start:], "<a href=\"./?module") + 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) + + fmt.Println("!!!") + fmt.Printf("%i %i %i\n", daypanel_start, link_start, link_end) + fmt.Println(link_url) + fmt.Println("!!!") + + resp, _ := client.Get(link_url) + body, _ := io.ReadAll(resp.Body) + + fmt.Println(string(body)) + + form_str := get_form_from_request_body(body) + form_values := parse_form(form_str) + form_values["ctl01$sessionCode"][0] = attendance_code + + //fmt.Println(form_str) + //fmt.Println(form_values) + + resp, _ = client.PostForm(link_url, form_values) + body, _ = io.ReadAll(resp.Body) + + fmt.Println(string(body)) + fmt.Println(resp) + + if strings.Index(string(body), "ctl01_errorMessage") != -1 { + errormsg_start := strings.Index(string(body), "ctl01_errorMessage") + errormsg_start += strings.Index(string(body)[errormsg_start:], "Text\">") + errormsg_start += len("Text\">") + errormsg_end := errormsg_start + errormsg_end += strings.Index(string(body)[errormsg_start:], "<") + fmt.Println(string(body)[errormsg_start:errormsg_end]) + } else if strings.Index(string(body), "ctl02_codeSuccessMessage") != -1 { + fmt.Println("Code successfully recorded") + } +} + func get_form_from_request_body(req_body []byte) string { body_str := string(req_body) form_start := strings.Index(body_str, "<form") diff --git a/main.go b/main.go @@ -14,7 +14,7 @@ func main() { // select_course() course_code := 7329 - launch_aplus(course_code) + launch_aplus(course_code, "A1B2C") // enter_code() }