mirror of
https://codeberg.org/flpolyaplus/aplus.git
synced 2024-11-21 17:00:30 -05:00
vin
7b613b030d
It is very messy, but later using golang's HTML parser should help make it more readable.
145 lines
3.8 KiB
Go
145 lines
3.8 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"html"
|
|
"io"
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
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)
|
|
|
|
resp, _ := client.Get(aplus)
|
|
body, _ := io.ReadAll(resp.Body)
|
|
|
|
form_str := get_form_from_request_body(body)
|
|
form_values := parse_form(form_str)
|
|
|
|
resp, _ = client.PostForm("https://floridapoly.aplusattendance.com/canvas", form_values)
|
|
body, _ = io.ReadAll(resp.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 {
|
|
resp, _ := client.Get(link)
|
|
|
|
body, _ := io.ReadAll(resp.Body)
|
|
|
|
var aplus Aplus
|
|
json.Unmarshal(body, &aplus)
|
|
|
|
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")
|
|
form_end := strings.Index(body_str, "</form>") + 7
|
|
form_html := req_body[form_start:form_end]
|
|
|
|
return string(form_html)
|
|
}
|
|
|
|
// parse_form extracts form fields and values from the given HTML form string.
|
|
func parse_form(form_html string) url.Values {
|
|
form_values := make(url.Values)
|
|
inputs := strings.Split(form_html, "<input")
|
|
|
|
for _, input := range inputs {
|
|
// Extract field name and value
|
|
name := extract_attribute(input, "name")
|
|
value := html.UnescapeString(extract_attribute(input, "value"))
|
|
|
|
if name != "" {
|
|
form_values.Add(name, value)
|
|
}
|
|
}
|
|
|
|
return form_values
|
|
}
|
|
|
|
func extract_attribute(input string, attribute string) string {
|
|
start := strings.Index(input, attribute+"=\"")
|
|
if start == -1 {
|
|
start = strings.Index(input, attribute+"='")
|
|
}
|
|
|
|
if start == -1 {
|
|
return ""
|
|
}
|
|
|
|
start += len(attribute) + 2
|
|
|
|
end := strings.Index(input[start:], "\"")
|
|
if end == -1 {
|
|
end = strings.Index(input[start:], "'")
|
|
}
|
|
|
|
if end == -1 {
|
|
return ""
|
|
}
|
|
|
|
return input[start : start+end]
|
|
}
|