mirror of
https://codeberg.org/flpolyaplus/aplus.git
synced 2024-11-21 17:00:30 -05:00
reorganize functions to aplus-related and canvas-related.
This commit is contained in:
parent
4af67b0363
commit
89c2e2b38e
76
aplus.go
Normal file
76
aplus.go
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func get_aplus(token string, link string, client http.Client) string {
|
||||||
|
resp, err := client.Get(link)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error performing GET request to initial link.")
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
body, _ := io.ReadAll(resp.Body)
|
||||||
|
|
||||||
|
var aplus Aplus
|
||||||
|
json.Unmarshal(body, &aplus)
|
||||||
|
|
||||||
|
return aplus.URL
|
||||||
|
}
|
||||||
|
|
||||||
|
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 := 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]
|
||||||
|
}
|
57
canvas.go
Normal file
57
canvas.go
Normal file
@ -0,0 +1,57 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func initialize() {
|
||||||
|
base_link = "https://floridapolytechnic.instructure.com/api/v1"
|
||||||
|
token = os.Getenv("CANVAS_API_KEY")
|
||||||
|
}
|
||||||
|
|
||||||
|
func get_courses(token string, link string, client http.Client) []CanvasCourse {
|
||||||
|
resp, err := client.Get(link)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error performing GET request to get courses.")
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
body, _ := io.ReadAll(resp.Body)
|
||||||
|
|
||||||
|
var canvas_courses []CanvasCourse
|
||||||
|
err2 := json.Unmarshal(body, &canvas_courses)
|
||||||
|
if err2 != nil {
|
||||||
|
fmt.Println(err2)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return canvas_courses
|
||||||
|
}
|
||||||
|
|
||||||
|
// Need to change this to grab both ID and name, return as pairs.
|
||||||
|
func get_course_id_name_pair(canvas_courses []CanvasCourse) []uint64 {
|
||||||
|
var courses []uint64
|
||||||
|
|
||||||
|
for _, course := range canvas_courses {
|
||||||
|
courses = append(courses, course.ID%10000)
|
||||||
|
}
|
||||||
|
return courses
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
func select_course() int {
|
||||||
|
// course selection by user happens below
|
||||||
|
favorites := fmt.Sprintf("%s/users/self/favorites/courses?access_token=%s&per_page=100", base_link, token)
|
||||||
|
all_courses := fmt.Sprintf("%s/courses?access_token=%s&per_page=100", base_link, token)
|
||||||
|
var canvas_courses []CanvasCourse
|
||||||
|
canvas_courses = get_courses(token, favorites, client)
|
||||||
|
var course_ids []uint64
|
||||||
|
course_ids = get_course_ids(canvas_courses)
|
||||||
|
//selected := 1 // course_ids[selected]
|
||||||
|
// course selection by user happens above
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
*/
|
128
funcs.go
128
funcs.go
@ -1,128 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
/*
|
|
||||||
func get_courses(token string, link string, client http.Client) []CanvasCourse {
|
|
||||||
resp, err := client.Get(link)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error performing GET request to get courses.")
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error trying to read html body for courses.")
|
|
||||||
}
|
|
||||||
|
|
||||||
var canvas_courses []CanvasCourse
|
|
||||||
err2 := json.Unmarshal(body, &canvas_courses)
|
|
||||||
if err2 != nil {
|
|
||||||
fmt.Println(err2)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return canvas_courses
|
|
||||||
}
|
|
||||||
|
|
||||||
// Need to change this to grab both ID and name, return as pairs.
|
|
||||||
func get_course_id_name_pair(canvas_courses []CanvasCourse) []uint64 {
|
|
||||||
var courses []uint64
|
|
||||||
|
|
||||||
for _, course := range canvas_courses {
|
|
||||||
courses = append(courses, course.ID%10000)
|
|
||||||
}
|
|
||||||
return courses
|
|
||||||
}
|
|
||||||
|
|
||||||
func select_course() int {
|
|
||||||
// course selection by user happens below
|
|
||||||
//favorites := fmt.Sprintf("%s/users/self/favorites/courses?access_token=%s&per_page=100", base_link, token)
|
|
||||||
//all_courses := fmt.Sprintf("%s/courses?access_token=%s&per_page=100", base_link,token)
|
|
||||||
// var canvas_courses []CanvasCourse
|
|
||||||
//canvas_courses = get_courses(token, favorites, client)
|
|
||||||
//var course_ids []uint64
|
|
||||||
//course_ids = get_course_ids(canvas_courses)
|
|
||||||
//selected := 1 // course_ids[selected]
|
|
||||||
// course selection by user happens above
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
func get_aplus(token string, link string, client http.Client) string {
|
|
||||||
resp, err := client.Get(link)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error performing GET request to initial link.")
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error reading body of initial aplus html response " +
|
|
||||||
"which is supposed to contain a one-time link for getting the form.")
|
|
||||||
}
|
|
||||||
|
|
||||||
var aplus Aplus
|
|
||||||
json.Unmarshal(body, &aplus)
|
|
||||||
|
|
||||||
return aplus.URL
|
|
||||||
}
|
|
||||||
|
|
||||||
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 := 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]
|
|
||||||
}
|
|
17
main.go
17
main.go
@ -4,9 +4,11 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var base_link string
|
||||||
|
var token string
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
client := http.Client{
|
client := http.Client{
|
||||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||||
@ -15,8 +17,7 @@ func main() {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
base_link := "https://floridapolytechnic.instructure.com/api/v1"
|
initialize()
|
||||||
token := os.Getenv("CANVAS_API_KEY")
|
|
||||||
|
|
||||||
// select_course()
|
// select_course()
|
||||||
|
|
||||||
@ -30,11 +31,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, _ := io.ReadAll(resp.Body)
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Error reading body of initial aplus html response.")
|
|
||||||
}
|
|
||||||
|
|
||||||
form_str := get_form_from_request_body(body)
|
form_str := get_form_from_request_body(body)
|
||||||
fmt.Println(form_str)
|
fmt.Println(form_str)
|
||||||
@ -62,8 +59,8 @@ func main() {
|
|||||||
fmt.Println(req)
|
fmt.Println(req)
|
||||||
|
|
||||||
// we need to submit the above post request correctly in order to submit this request.
|
// we need to submit the above post request correctly in order to submit this request.
|
||||||
//resp, err = client.Get("https://floridapoly.aplusattendance.com/canvas/student/?canvasCourse=7329&doStudentAuth=true")
|
//resp, _ = client.Get("https://floridapoly.aplusattendance.com/canvas/student/?canvasCourse=7329&doStudentAuth=true")
|
||||||
//body, err = io.ReadAll(req.Body)
|
//body, _ = io.ReadAll(resp.Body)
|
||||||
|
|
||||||
//fmt.Println(string(body))
|
//fmt.Println(string(body))
|
||||||
//fmt.Println(req)
|
//fmt.Println(req)
|
||||||
|
Loading…
Reference in New Issue
Block a user