diff options
Diffstat (limited to 'aplus.go')
| -rw-r--r-- | aplus.go | 58 |
1 files changed, 56 insertions, 2 deletions
| @@ -10,7 +10,7 @@ import ( | |||
| 10 | "strings" | 10 | "strings" |
| 11 | ) | 11 | ) |
| 12 | 12 | ||
| 13 | func launch_aplus(course_code int) { | 13 | func launch_aplus(course_code int, attendance_code string) { |
| 14 | aplus_link := fmt.Sprintf("%s/courses/%d/external_tools/sessionless_launch?id=%d&access_token=%s", | 14 | aplus_link := fmt.Sprintf("%s/courses/%d/external_tools/sessionless_launch?id=%d&access_token=%s", |
| 15 | base_link, course_code, external_tools_code, token) | 15 | base_link, course_code, external_tools_code, token) |
| 16 | aplus := get_aplus(token, aplus_link, client) | 16 | aplus := get_aplus(token, aplus_link, client) |
| @@ -24,7 +24,9 @@ func launch_aplus(course_code int) { | |||
| 24 | resp, _ = client.PostForm("https://floridapoly.aplusattendance.com/canvas", form_values) | 24 | resp, _ = client.PostForm("https://floridapoly.aplusattendance.com/canvas", form_values) |
| 25 | body, _ = io.ReadAll(resp.Body) | 25 | body, _ = io.ReadAll(resp.Body) |
| 26 | 26 | ||
| 27 | fmt.Println(string(body)) | 27 | submit_code(string(body), attendance_code, "https://floridapoly.aplusattendance.com/canvas/student/") |
| 28 | |||
| 29 | // fmt.Println(string(body)) | ||
| 28 | } | 30 | } |
| 29 | 31 | ||
| 30 | func get_aplus(token string, link string, client http.Client) string { | 32 | 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 { | |||
| 38 | return aplus.URL | 40 | return aplus.URL |
| 39 | } | 41 | } |
| 40 | 42 | ||
| 43 | func submit_code(cur_body string, attendance_code string, current_url string) { | ||
| 44 | // the links to submit the code for a class is under the dayPanel div | ||
| 45 | daypanel_start := strings.Index(cur_body, "<div class=\"dayPanel\"") | ||
| 46 | |||
| 47 | // link_start - link_end is to extract the URL | ||
| 48 | // TODO: make this more elegant to allow multiple of these links | ||
| 49 | // (maybe use an html parser) | ||
| 50 | // or at least use extract_attribute() | ||
| 51 | link_start := daypanel_start | ||
| 52 | link_start += strings.Index(cur_body[link_start:], "<a href=\"./?module") | ||
| 53 | link_start += len("<a href=\"") | ||
| 54 | link_end := link_start | ||
| 55 | link_end += strings.Index(cur_body[link_end:], "\"") | ||
| 56 | |||
| 57 | link_url := current_url + cur_body[link_start:link_end] | ||
| 58 | link_url = html.UnescapeString(link_url) | ||
| 59 | |||
| 60 | fmt.Println("!!!") | ||
| 61 | fmt.Printf("%i %i %i\n", daypanel_start, link_start, link_end) | ||
| 62 | fmt.Println(link_url) | ||
| 63 | fmt.Println("!!!") | ||
| 64 | |||
| 65 | resp, _ := client.Get(link_url) | ||
| 66 | body, _ := io.ReadAll(resp.Body) | ||
| 67 | |||
| 68 | fmt.Println(string(body)) | ||
| 69 | |||
| 70 | form_str := get_form_from_request_body(body) | ||
| 71 | form_values := parse_form(form_str) | ||
| 72 | form_values["ctl01$sessionCode"][0] = attendance_code | ||
| 73 | |||
| 74 | //fmt.Println(form_str) | ||
| 75 | //fmt.Println(form_values) | ||
| 76 | |||
| 77 | resp, _ = client.PostForm(link_url, form_values) | ||
| 78 | body, _ = io.ReadAll(resp.Body) | ||
| 79 | |||
| 80 | fmt.Println(string(body)) | ||
| 81 | fmt.Println(resp) | ||
| 82 | |||
| 83 | if strings.Index(string(body), "ctl01_errorMessage") != -1 { | ||
| 84 | errormsg_start := strings.Index(string(body), "ctl01_errorMessage") | ||
| 85 | errormsg_start += strings.Index(string(body)[errormsg_start:], "Text\">") | ||
| 86 | errormsg_start += len("Text\">") | ||
| 87 | errormsg_end := errormsg_start | ||
| 88 | errormsg_end += strings.Index(string(body)[errormsg_start:], "<") | ||
| 89 | fmt.Println(string(body)[errormsg_start:errormsg_end]) | ||
| 90 | } else if strings.Index(string(body), "ctl02_codeSuccessMessage") != -1 { | ||
| 91 | fmt.Println("Code successfully recorded") | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 41 | func get_form_from_request_body(req_body []byte) string { | 95 | func get_form_from_request_body(req_body []byte) string { |
| 42 | body_str := string(req_body) | 96 | body_str := string(req_body) |
| 43 | form_start := strings.Index(body_str, "<form") | 97 | form_start := strings.Index(body_str, "<form") |
