commit c411ee3447fca4389bff3d228a5a0068204f0dad
parent cc3ab10c6af5dbe11b12c9feb2373e3793fb41ff
Author: Juan Roig <jaroig@outlook.com>
Date: Tue, 31 Oct 2023 02:06:01 -0400
Form parsing done correctly
Diffstat:
| A | go.sum | | | 0 | |
| M | main.go | | | 54 | ++++++++++++++++++++++++++++++++++++++++++++++++++++-- |
2 files changed, 52 insertions(+), 2 deletions(-)
diff --git a/go.sum b/go.sum
diff --git a/main.go b/main.go
@@ -5,6 +5,7 @@ import (
"fmt"
"io"
"net/http"
+ "net/url"
"strings"
)
@@ -49,6 +50,48 @@ func get_course_ids(canvas_courses []CanvasCourse) []uint64 {
return courses
}
+// parseForm 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 := 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]
+}
+
func main() {
client := http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
@@ -59,7 +102,7 @@ func main() {
// https://canvas.instructure.com/api/v1/courses?access_token={}&per_page=100", canvas_token
base_link := "https://floridapolytechnic.instructure.com/api/v1"
- token := "token here"
+ token := "token"
// course selection by user happens below
//favorites := fmt.Sprintf("%s/users/self/favorites/courses?access_token=%s&per_page=100", base_link, token)
@@ -100,8 +143,15 @@ func main() {
form_end := strings.Index(body_str, "</form>") + 7
form_html := body[form_start:form_end]
- fmt.Println(string(form_html))
+ form := string(form_html)
+ fmt.Println(form)
+ form_values := parse_form(form)
+
+ fmt.Println("Form Values:")
+ for key, values := range form_values {
+ fmt.Printf("%s: %s\n", key, values)
+ }
}
/*