2023-11-07 00:21:48 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2023-11-16 13:44:11 -05:00
|
|
|
"errors"
|
2023-11-07 00:21:48 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2023-11-13 02:08:03 -05:00
|
|
|
"net/http/cookiejar"
|
2023-11-07 00:21:48 -05:00
|
|
|
"os"
|
2023-11-16 13:44:11 -05:00
|
|
|
"strings"
|
2023-11-13 02:08:03 -05:00
|
|
|
|
|
|
|
"golang.org/x/net/publicsuffix"
|
2023-11-07 00:21:48 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func initialize() {
|
2024-01-16 15:23:35 -05:00
|
|
|
instance_url := os.Getenv("CANVAS_INSTANCE")
|
2024-01-17 16:51:00 -05:00
|
|
|
if len(instance_url) == 0 {
|
|
|
|
fmt.Println("The CANVAS_INSTANCE environment variable seems to be empty. Exiting.")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2023-11-16 13:44:11 -05:00
|
|
|
base_link = instance_url + "/api/v1"
|
2023-11-07 00:21:48 -05:00
|
|
|
token = os.Getenv("CANVAS_API_KEY")
|
2024-01-17 16:51:00 -05:00
|
|
|
if len(token) == 0 {
|
|
|
|
fmt.Println("The CANVAS_API_KEY environment variable seems to be empty. Exiting.")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2023-11-13 02:08:03 -05:00
|
|
|
jar, _ := cookiejar.New(&cookiejar.Options{
|
|
|
|
PublicSuffixList: publicsuffix.List,
|
|
|
|
})
|
|
|
|
|
|
|
|
client = http.Client{
|
|
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
2023-11-21 11:07:28 -05:00
|
|
|
if verbose {
|
|
|
|
fmt.Println("Redirected to:", req.URL)
|
|
|
|
fmt.Println(req.Cookies())
|
|
|
|
}
|
2023-11-13 02:08:03 -05:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
Jar: jar,
|
|
|
|
}
|
2023-11-07 00:21:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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.")
|
2023-11-13 02:08:03 -05:00
|
|
|
return nil
|
2023-11-07 00:21:48 -05:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-11-16 13:44:11 -05:00
|
|
|
func init_aplus_toolid(course_id int) error {
|
|
|
|
link := fmt.Sprintf("%s/courses/%d/external_tools?access_token=%s&search_term=aplus&include_parents=true", base_link, course_id, token)
|
2024-01-12 17:22:35 -05:00
|
|
|
|
2023-11-16 13:44:11 -05:00
|
|
|
resp, err := client.Get(link)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, _ := io.ReadAll(resp.Body)
|
|
|
|
|
|
|
|
var external_tools []ExternalTool
|
|
|
|
err = json.Unmarshal(body, &external_tools)
|
|
|
|
if err != nil && !strings.Contains(string(body), "\"unauthorized\"") {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tool := range external_tools {
|
2024-01-12 17:22:35 -05:00
|
|
|
if strings.Contains(tool.URL, ".aplusattendance.com/") {
|
2023-11-16 13:44:11 -05:00
|
|
|
base_aplus_link = tool.URL
|
|
|
|
external_tools_code = tool.ID
|
2023-11-20 13:09:35 -05:00
|
|
|
return nil
|
2023-11-16 13:44:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("Could not find A+ link and tool ID")
|
|
|
|
}
|
|
|
|
|
2024-01-12 17:28:49 -05:00
|
|
|
func list_all_courses(onlyreturn bool) []CanvasCourse {
|
2024-01-11 11:33:48 -05:00
|
|
|
all_courses := fmt.Sprintf("%s/courses?access_token=%s&enrollment_state=active&per_page=100", base_link, token)
|
2023-11-14 04:23:18 -05:00
|
|
|
|
|
|
|
canvas_courses := get_courses(token, all_courses, client)
|
2023-11-07 00:21:48 -05:00
|
|
|
|
2024-01-11 18:36:22 -05:00
|
|
|
if !onlyreturn {
|
|
|
|
for _, course := range canvas_courses {
|
|
|
|
fmt.Printf("%d, %s\n", course.ID%10000, course.Name)
|
|
|
|
}
|
|
|
|
|
2024-01-12 17:28:49 -05:00
|
|
|
return nil
|
2023-11-07 00:21:48 -05:00
|
|
|
}
|
2024-01-11 18:36:22 -05:00
|
|
|
|
|
|
|
return canvas_courses
|
2023-11-07 00:21:48 -05:00
|
|
|
}
|
|
|
|
|
2023-11-14 04:23:18 -05:00
|
|
|
func list_favorite_courses() {
|
|
|
|
|
2023-11-07 00:21:48 -05:00
|
|
|
// course selection by user happens below
|
|
|
|
favorites := fmt.Sprintf("%s/users/self/favorites/courses?access_token=%s&per_page=100", base_link, token)
|
2023-11-14 04:23:18 -05:00
|
|
|
|
|
|
|
canvas_courses := get_courses(token, favorites, client)
|
|
|
|
|
|
|
|
for _, course := range canvas_courses {
|
2023-11-15 13:30:45 -05:00
|
|
|
fmt.Printf("%d, %s\n", course.ID%10000, course.Name)
|
2023-11-14 04:23:18 -05:00
|
|
|
}
|
2023-11-07 00:21:48 -05:00
|
|
|
}
|