Add support to download all the users courses

This commit is contained in:
joren
2026-03-11 19:36:54 +01:00
parent 11d9867155
commit 8591ae283e
3 changed files with 38 additions and 2 deletions

View File

@@ -14,6 +14,7 @@ import (
func main() { func main() {
filesOnly := flag.Bool("fo", false, "Files only mode - download all files to a single directory without module structure") filesOnly := flag.Bool("fo", false, "Files only mode - download all files to a single directory without module structure")
me := flag.Bool("me", false, "Download all enrolled courses")
flag.Parse() flag.Parse()
httpClient := &http.Client{} httpClient := &http.Client{}
@@ -25,11 +26,31 @@ func main() {
return return
} }
if *me {
canvasClient := canvas.NewClient(httpClient, accessToken, "", *filesOnly)
courses, err := canvasClient.GetEnrolledCourses()
if err != nil {
fmt.Printf("Error fetching courses: %v\n", err)
return
}
fmt.Printf("[+] Found %d enrolled courses\n", len(courses))
for _, course := range courses {
fmt.Printf(" -> Downloading: %s (ID: %d)\n", course.Name, course.ID)
downloadCourse(httpClient, accessToken, fmt.Sprintf("%d", course.ID), *filesOnly)
}
return
}
var courseID string var courseID string
fmt.Print("Enter Course ID: ") fmt.Print("Enter Course ID: ")
fmt.Scanln(&courseID) fmt.Scanln(&courseID)
canvasClient := canvas.NewClient(httpClient, accessToken, courseID, *filesOnly) downloadCourse(httpClient, accessToken, courseID, *filesOnly)
}
func downloadCourse(httpClient *http.Client, accessToken, courseID string, filesOnly bool) {
canvasClient := canvas.NewClient(httpClient, accessToken, courseID, filesOnly)
if err := canvasClient.GetCourseInfo(); err != nil { if err := canvasClient.GetCourseInfo(); err != nil {
fmt.Printf("Error: %v\n", err) fmt.Printf("Error: %v\n", err)
@@ -44,7 +65,7 @@ func main() {
canvasClient.DownloadModules(courseRoot) canvasClient.DownloadModules(courseRoot)
if !*filesOnly { if !filesOnly {
panopto.DownloadMainRecordings(httpClient, accessToken, courseID, courseRoot) panopto.DownloadMainRecordings(httpClient, accessToken, courseID, courseRoot)
} }
} }

View File

@@ -51,6 +51,20 @@ func (c *Client) GetCourseInfo() error {
return nil return nil
} }
func (c *Client) GetEnrolledCourses() ([]models.Course, error) {
req, _ := http.NewRequest("GET", fmt.Sprintf("%s/api/v1/courses?enrollment_state=active&per_page=100", config.BaseURL), nil)
req.Header.Set("Authorization", "Bearer "+c.AccessToken)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var courses []models.Course
json.NewDecoder(resp.Body).Decode(&courses)
return courses, nil
}
func (c *Client) DownloadCourseFiles(root string) { func (c *Client) DownloadCourseFiles(root string) {
fmt.Println("\n[*] Fetching regular course files...") fmt.Println("\n[*] Fetching regular course files...")

View File

@@ -10,6 +10,7 @@ type TokenResponse struct {
} }
type Course struct { type Course struct {
ID int `json:"id"`
Name string `json:"name"` Name string `json:"name"`
} }