14 Commits

Author SHA1 Message Date
joren
14a71e7dca Update Readme 2026-03-11 20:12:08 +01:00
051b67de51 Merge pull request 'impl-me' (#4) from impl-me into main
Reviewed-on: #4
2026-03-11 20:10:55 +01:00
joren
9691ecd7a5 Fix course files 2026-03-11 20:06:46 +01:00
joren
b8e6180b35 add numbering 2026-03-11 19:56:37 +01:00
joren
8591ae283e Add support to download all the users courses 2026-03-11 19:36:54 +01:00
11d9867155 Merge pull request 'Prevent dupes in fo mode' (#3) from files-only into main
Reviewed-on: #3
2026-03-11 17:52:41 +01:00
9ffe1283c2 Merge branch 'main' into files-only 2026-03-11 17:52:31 +01:00
05ed4dd4ed Prevent dupes in fo mode 2026-03-11 17:51:34 +01:00
be63064bee Update README.md 2026-03-11 17:40:48 +01:00
cd259e01a3 Merge pull request 'Add files only mode' (#2) from files-only into main
Reviewed-on: #2
2026-03-11 17:38:56 +01:00
352315b041 Add files only mode 2026-03-11 17:37:18 +01:00
c9fa079893 Merge pull request 'Add support for hyperlinks' (#1) from hyperlink into main
Reviewed-on: #1
2026-02-13 20:58:15 +01:00
6cc25602dc Merge branch 'main' into hyperlink 2026-02-13 20:58:06 +01:00
fbf75c88b3 Add support for hyperlinks 2026-02-13 20:42:53 +01:00
5 changed files with 179 additions and 34 deletions

View File

@@ -21,18 +21,31 @@ go build -o canvasarchiver ./cmd/canvasarchiver
./canvasarchiver ./canvasarchiver
``` ```
Or for files-only mode:
```bash
./canvasarchiver -fo
```
2. On first run, you'll be prompted to authenticate: 2. On first run, you'll be prompted to authenticate:
- Visit the provided OAuth URL - Visit the provided OAuth URL
- Authorize the application - Authorize the application
- Copy the authorization code back to the terminal - Copy the authorization code back to the terminal
3. Enter your Course ID when prompted 3. Enter your Course ID when prompted (or use `-me` to download all enrolled courses)
4. The tool will download: 4. The tool will download:
- Regular course files (to `Course Files/`) - Regular course files (to `Course Files/`)
- Module content (to `Modules/`) - Module content (to `Modules/`)
- Panopto recordings (to `Recordings/`) - Panopto recordings (to `Recordings/`)
### Flags
| Flag | Description |
|------|-------------|
| `-fo` | Files only mode - download all files to a single directory without module structure |
| `-me` | Download all enrolled courses |
| `-n` | Prefix modules with order numbers `[1]`, `[2]`, etc. |
## Configuration ## Configuration

View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"net/http" "net/http"
"os" "os"
@@ -12,6 +13,11 @@ import (
) )
func main() { func main() {
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")
moduleNumbers := flag.Bool("n", false, "Prefix modules with order numbers [1], [2], etc.")
flag.Parse()
httpClient := &http.Client{} httpClient := &http.Client{}
authenticator := auth.NewAuthenticator(httpClient) authenticator := auth.NewAuthenticator(httpClient)
@@ -21,11 +27,31 @@ func main() {
return return
} }
if *me {
canvasClient := canvas.NewClient(httpClient, accessToken, "", *filesOnly, *moduleNumbers)
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, *moduleNumbers)
}
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) downloadCourse(httpClient, accessToken, courseID, *filesOnly, *moduleNumbers)
}
func downloadCourse(httpClient *http.Client, accessToken, courseID string, filesOnly, moduleNumbers bool) {
canvasClient := canvas.NewClient(httpClient, accessToken, courseID, filesOnly, moduleNumbers)
if err := canvasClient.GetCourseInfo(); err != nil { if err := canvasClient.GetCourseInfo(); err != nil {
fmt.Printf("Error: %v\n", err) fmt.Printf("Error: %v\n", err)
@@ -40,5 +66,7 @@ func main() {
canvasClient.DownloadModules(courseRoot) canvasClient.DownloadModules(courseRoot)
panopto.DownloadMainRecordings(httpClient, accessToken, courseID, courseRoot) if !filesOnly {
panopto.DownloadMainRecordings(httpClient, accessToken, courseID, courseRoot)
}
} }

View File

@@ -17,17 +17,23 @@ import (
) )
type Client struct { type Client struct {
HTTPClient *http.Client HTTPClient *http.Client
AccessToken string AccessToken string
CourseID string CourseID string
CourseName string CourseName string
FilesOnly bool
ModuleNumbers bool
downloadedFiles map[string]bool
} }
func NewClient(httpClient *http.Client, accessToken, courseID string) *Client { func NewClient(httpClient *http.Client, accessToken, courseID string, filesOnly, moduleNumbers bool) *Client {
return &Client{ return &Client{
HTTPClient: httpClient, HTTPClient: httpClient,
AccessToken: accessToken, AccessToken: accessToken,
CourseID: courseID, CourseID: courseID,
FilesOnly: filesOnly,
ModuleNumbers: moduleNumbers,
downloadedFiles: make(map[string]bool),
} }
} }
@@ -47,6 +53,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...")
@@ -80,10 +100,24 @@ func (c *Client) DownloadCourseFiles(root string) {
fileCount := 0 fileCount := 0
for _, file := range files { for _, file := range files {
if c.FilesOnly {
if c.downloadedFiles[file.DisplayName] {
continue
}
c.downloadedFiles[file.DisplayName] = true
}
rawFolderPath := folderMap[file.FolderID] rawFolderPath := folderMap[file.FolderID]
safeFolderPath := utils.SanitizePath(rawFolderPath) safeFolderPath := utils.SanitizePath(rawFolderPath)
subDir := filepath.Join(root, "Course Files", safeFolderPath) subDir := root
if !c.FilesOnly {
if safeFolderPath != "" && strings.ToLower(safeFolderPath) != "course files" {
subDir = filepath.Join(root, "Course Files", safeFolderPath)
} else {
subDir = filepath.Join(root, "Course Files")
}
}
os.MkdirAll(subDir, 0o755) os.MkdirAll(subDir, 0o755)
path := filepath.Join(subDir, utils.Sanitize(file.DisplayName)) path := filepath.Join(subDir, utils.Sanitize(file.DisplayName))
@@ -124,10 +158,21 @@ func (c *Client) DownloadModules(courseRoot string) {
json.NewDecoder(resp.Body).Decode(&modules) json.NewDecoder(resp.Body).Decode(&modules)
resp.Body.Close() resp.Body.Close()
for _, mod := range modules { for i, mod := range modules {
modBaseDir := filepath.Join(courseRoot, "Modules", utils.Sanitize(mod.Name)) modName := mod.Name
if c.ModuleNumbers {
modName = fmt.Sprintf("[%d] %s", i+1, mod.Name)
}
modBaseDir := courseRoot
if !c.FilesOnly {
modBaseDir = filepath.Join(courseRoot, "Modules", utils.Sanitize(modName))
}
os.MkdirAll(modBaseDir, 0o755) os.MkdirAll(modBaseDir, 0o755)
fmt.Printf("\n[Module] %s\n", mod.Name)
if !c.FilesOnly {
fmt.Printf("\n[Module] %s\n", modName)
}
subHeaderStack := []string{} subHeaderStack := []string{}
lastIndent := 0 lastIndent := 0
@@ -135,13 +180,16 @@ func (c *Client) DownloadModules(courseRoot string) {
for _, item := range mod.Items { for _, item := range mod.Items {
targetDir := modBaseDir targetDir := modBaseDir
if len(subHeaderStack) > 0 { if len(subHeaderStack) > 0 && !c.FilesOnly {
targetDir = filepath.Join(modBaseDir, filepath.Join(subHeaderStack...)) targetDir = filepath.Join(modBaseDir, filepath.Join(subHeaderStack...))
} }
os.MkdirAll(targetDir, 0o755) os.MkdirAll(targetDir, 0o755)
switch item.Type { switch item.Type {
case "SubHeader": case "SubHeader":
if c.FilesOnly {
continue
}
currentIndent := item.Indent currentIndent := item.Indent
if currentIndent <= lastIndent && len(subHeaderStack) > 0 { if currentIndent <= lastIndent && len(subHeaderStack) > 0 {
levelsToKeep := currentIndent levelsToKeep := currentIndent
@@ -162,11 +210,28 @@ func (c *Client) DownloadModules(courseRoot string) {
c.downloadModuleFile(item, targetDir) c.downloadModuleFile(item, targetDir)
case "ExternalTool": case "ExternalTool":
if c.FilesOnly {
continue
}
indent := strings.Repeat(" ", len(subHeaderStack)+1) indent := strings.Repeat(" ", len(subHeaderStack)+1)
fmt.Printf("%s- Found video tool: %s\n", indent, item.Title) fmt.Printf("%s- Found video tool: %s\n", indent, item.Title)
panopto.DownloadVideo(c.HTTPClient, c.AccessToken, c.CourseID, targetDir, item.URL, item.Title) panopto.DownloadVideo(c.HTTPClient, c.AccessToken, c.CourseID, targetDir, item.URL, item.Title)
case "ExternalUrl":
if c.FilesOnly {
continue
}
if strings.Contains(item.ExternalURL, "panopto.eu") {
indent := strings.Repeat(" ", len(subHeaderStack)+1)
fmt.Printf("%s- Found direct video link: %s\n", indent, item.Title)
panopto.DownloadVideo(c.HTTPClient, c.AccessToken, c.CourseID, targetDir, item.ExternalURL, item.Title)
}
case "Page": case "Page":
if c.FilesOnly {
continue
}
c.searchPageForVideos(item, targetDir) c.searchPageForVideos(item, targetDir)
} }
} }
@@ -192,11 +257,18 @@ func (c *Client) downloadModuleFile(item models.ModuleItem, dir string) {
return return
} }
if c.FilesOnly {
if c.downloadedFiles[fileMeta.DisplayName] {
return
}
c.downloadedFiles[fileMeta.DisplayName] = true
}
ext := filepath.Ext(fileMeta.DisplayName) ext := filepath.Ext(fileMeta.DisplayName)
origBase := strings.TrimSuffix(fileMeta.DisplayName, ext) origBase := strings.TrimSuffix(fileMeta.DisplayName, ext)
fileName := fileMeta.DisplayName fileName := fileMeta.DisplayName
if !strings.EqualFold(origBase, item.Title) && item.Title != "" { if !c.FilesOnly && !strings.EqualFold(origBase, item.Title) && item.Title != "" {
fileName = fmt.Sprintf("%s (%s)%s", origBase, item.Title, ext) fileName = fmt.Sprintf("%s (%s)%s", origBase, item.Title, ext)
} }

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"`
} }
@@ -30,10 +31,11 @@ type Module struct {
} }
type ModuleItem struct { type ModuleItem struct {
Title string `json:"title"` Title string `json:"title"`
Type string `json:"type"` Type string `json:"type"`
URL string `json:"url"` URL string `json:"url"`
HTMLURL string `json:"html_url"` HTMLURL string `json:"html_url"`
ContentID int `json:"content_id"` ExternalURL string `json:"external_url"`
Indent int `json:"indent"` ContentID int `json:"content_id"`
Indent int `json:"indent"`
} }

View File

@@ -45,12 +45,13 @@ func DownloadVideo(httpClient *http.Client, accessToken, courseID, modDir, input
} }
var launchURL string var launchURL string
isDirectLink := false
if strings.Contains(inputURL, "/api/v1/") { if strings.Contains(inputURL, "/api/v1/") {
launchURL = inputURL launchURL = inputURL
} else if strings.Contains(inputURL, "panopto.eu") {
launchURL = fmt.Sprintf("%s/api/v1/courses/%s/external_tools/sessionless_launch?url=%s",
config.BaseURL, courseID, url.QueryEscape(inputURL))
} else { } else {
isDirectLink = true
launchURL = fmt.Sprintf("%s/api/v1/courses/%s/external_tools/sessionless_launch?id=%s&launch_type=course_navigation", launchURL = fmt.Sprintf("%s/api/v1/courses/%s/external_tools/sessionless_launch?id=%s&launch_type=course_navigation",
config.BaseURL, courseID, config.PanoptoID) config.BaseURL, courseID, config.PanoptoID)
} }
@@ -70,6 +71,7 @@ func DownloadVideo(httpClient *http.Client, accessToken, courseID, modDir, input
resp.Body.Close() resp.Body.Close()
if launchData.URL == "" { if launchData.URL == "" {
fmt.Printf(" [!] No launch URL found (Video skipped)\n")
return return
} }
@@ -84,17 +86,21 @@ func DownloadVideo(httpClient *http.Client, accessToken, courseID, modDir, input
json.NewDecoder(bResp.Body).Decode(&bridgeData) json.NewDecoder(bResp.Body).Decode(&bridgeData)
bResp.Body.Close() bResp.Body.Close()
formReq, _ := http.NewRequest("GET", bridgeData.SessionURL, nil) formResp, err := httpClient.Get(bridgeData.SessionURL)
formReq.Header.Set("User-Agent", config.UserAgent)
formResp, err := httpClient.Do(formReq)
if err != nil { if err != nil {
return return
} }
formHTML, _ := io.ReadAll(formResp.Body) formHTMLBytes, _ := io.ReadAll(formResp.Body)
formResp.Body.Close() formResp.Body.Close()
formHTML := string(formHTMLBytes)
action := utils.ResolveAction(bridgeData.SessionURL, string(formHTML)) if strings.Contains(formHTML, "U hebt geen toegang") || strings.Contains(formHTML, "You do not have access") {
formData := utils.ExtractFormFields(string(formHTML)) fmt.Printf(" [!] Access denied by Panopto (U hebt geen toegang). Skipping.\n")
return
}
action := utils.ResolveAction(bridgeData.SessionURL, formHTML)
formData := utils.ExtractFormFields(formHTML)
pReq, _ := http.NewRequest("POST", action, strings.NewReader(formData.Encode())) pReq, _ := http.NewRequest("POST", action, strings.NewReader(formData.Encode()))
pReq.Header.Set("Content-Type", "application/x-www-form-urlencoded") pReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")
@@ -150,7 +156,31 @@ func DownloadVideo(httpClient *http.Client, accessToken, courseID, modDir, input
} }
} }
// This is for making sure yt-dlp does not auto-start downloading all videos, when access to a hyperlink is denied
if finalURL != "" && !strings.Contains(finalURL, "NonFatalError") { if finalURL != "" && !strings.Contains(finalURL, "NonFatalError") {
targetURL := finalURL
if isDirectLink {
targetURL = inputURL
checkReq, _ := http.NewRequest("GET", targetURL, nil)
checkReq.Header.Set("User-Agent", config.UserAgent)
checkResp, err := panoptoClient.Do(checkReq)
if err == nil {
checkResp.Body.Close()
if checkResp.StatusCode == http.StatusFound || checkResp.StatusCode == http.StatusSeeOther {
loc, _ := checkResp.Location()
if loc != nil && (strings.Contains(loc.String(), "Login.aspx") || strings.Contains(loc.String(), "Auth")) {
fmt.Printf(" [!] Video inaccessible (redirects to Login). Skipping to prevent mass download.\n")
return
}
}
}
}
cookieFile := filepath.Join(modDir, ".cookies_temp.txt") cookieFile := filepath.Join(modDir, ".cookies_temp.txt")
cData := "# Netscape HTTP Cookie File\n" cData := "# Netscape HTTP Cookie File\n"
panoptoDomain, _ := url.Parse("https://vub.cloud.panopto.eu") panoptoDomain, _ := url.Parse("https://vub.cloud.panopto.eu")
@@ -162,14 +192,14 @@ func DownloadVideo(httpClient *http.Client, accessToken, courseID, modDir, input
fmt.Printf(" [*] Downloading video: %s\n", title) fmt.Printf(" [*] Downloading video: %s\n", title)
ytCmd := getYoutubeDLCommand() ytCmd := getYoutubeDLCommand()
cmd := exec.Command(ytCmd, cmd := exec.Command(ytCmd,
"--no-playlist", "--no-playlist",
"--cookies", cookieFile, "--cookies", cookieFile,
"--referer", config.BaseURL+"/", "--referer", config.BaseURL+"/",
"-P", modDir, "-P", modDir,
"-o", utils.Sanitize(title)+".%(ext)s", "-o", utils.Sanitize(title)+".%(ext)s",
finalURL) targetURL)
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {