diff options
Diffstat (limited to 'aplus.go')
| -rw-r--r-- | aplus.go | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/aplus.go b/aplus.go new file mode 100644 index 0000000..e2ec53b --- /dev/null +++ b/aplus.go | |||
| @@ -0,0 +1,76 @@ | |||
| 1 | package main | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "encoding/json" | ||
| 5 | "fmt" | ||
| 6 | "io" | ||
| 7 | "net/http" | ||
| 8 | "net/url" | ||
| 9 | "strings" | ||
| 10 | ) | ||
| 11 | |||
| 12 | func get_aplus(token string, link string, client http.Client) string { | ||
| 13 | resp, err := client.Get(link) | ||
| 14 | |||
| 15 | if err != nil { | ||
| 16 | fmt.Println("Error performing GET request to initial link.") | ||
| 17 | } | ||
| 18 | defer resp.Body.Close() | ||
| 19 | body, _ := io.ReadAll(resp.Body) | ||
| 20 | |||
| 21 | var aplus Aplus | ||
| 22 | json.Unmarshal(body, &aplus) | ||
| 23 | |||
| 24 | return aplus.URL | ||
| 25 | } | ||
| 26 | |||
| 27 | func get_form_from_request_body(req_body []byte) string { | ||
| 28 | body_str := string(req_body) | ||
| 29 | form_start := strings.Index(body_str, "<form") | ||
| 30 | form_end := strings.Index(body_str, "</form>") + 7 | ||
| 31 | form_html := req_body[form_start:form_end] | ||
| 32 | |||
| 33 | return string(form_html) | ||
| 34 | } | ||
| 35 | |||
| 36 | // parse_form extracts form fields and values from the given HTML form string. | ||
| 37 | func parse_form(form_html string) url.Values { | ||
| 38 | form_values := make(url.Values) | ||
| 39 | inputs := strings.Split(form_html, "<input") | ||
| 40 | |||
| 41 | for _, input := range inputs { | ||
| 42 | // Extract field name and value | ||
| 43 | name := extract_attribute(input, "name") | ||
| 44 | value := extract_attribute(input, "value") | ||
| 45 | |||
| 46 | if name != "" { | ||
| 47 | form_values.Add(name, value) | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | return form_values | ||
| 52 | } | ||
| 53 | |||
| 54 | func extract_attribute(input string, attribute string) string { | ||
| 55 | start := strings.Index(input, attribute+"=\"") | ||
| 56 | if start == -1 { | ||
| 57 | start = strings.Index(input, attribute+"='") | ||
| 58 | } | ||
| 59 | |||
| 60 | if start == -1 { | ||
| 61 | return "" | ||
| 62 | } | ||
| 63 | |||
| 64 | start += len(attribute) + 2 | ||
| 65 | |||
| 66 | end := strings.Index(input[start:], "\"") | ||
| 67 | if end == -1 { | ||
| 68 | end = strings.Index(input[start:], "'") | ||
| 69 | } | ||
| 70 | |||
| 71 | if end == -1 { | ||
| 72 | return "" | ||
| 73 | } | ||
| 74 | |||
| 75 | return input[start : start+end] | ||
| 76 | } | ||
