6 Commits

Author SHA1 Message Date
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 98 additions and 24 deletions

View File

@@ -21,6 +21,12 @@ go build -o canvasarchiver ./cmd/canvasarchiver
./canvasarchiver
```
Or
```bash
./canvasarchiver -fo
```
For files-only mode.
2. On first run, you'll be prompted to authenticate:
- Visit the provided OAuth URL
- Authorize the application

View File

@@ -1,6 +1,7 @@
package main
import (
"flag"
"fmt"
"net/http"
"os"
@@ -12,6 +13,9 @@ import (
)
func main() {
filesOnly := flag.Bool("fo", false, "Files only mode - download all files to a single directory without module structure")
flag.Parse()
httpClient := &http.Client{}
authenticator := auth.NewAuthenticator(httpClient)
@@ -25,7 +29,7 @@ func main() {
fmt.Print("Enter Course ID: ")
fmt.Scanln(&courseID)
canvasClient := canvas.NewClient(httpClient, accessToken, courseID)
canvasClient := canvas.NewClient(httpClient, accessToken, courseID, *filesOnly)
if err := canvasClient.GetCourseInfo(); err != nil {
fmt.Printf("Error: %v\n", err)
@@ -40,5 +44,7 @@ func main() {
canvasClient.DownloadModules(courseRoot)
if !*filesOnly {
panopto.DownloadMainRecordings(httpClient, accessToken, courseID, courseRoot)
}
}

View File

@@ -21,13 +21,15 @@ type Client struct {
AccessToken string
CourseID string
CourseName string
FilesOnly bool
}
func NewClient(httpClient *http.Client, accessToken, courseID string) *Client {
func NewClient(httpClient *http.Client, accessToken, courseID string, filesOnly bool) *Client {
return &Client{
HTTPClient: httpClient,
AccessToken: accessToken,
CourseID: courseID,
FilesOnly: filesOnly,
}
}
@@ -83,7 +85,10 @@ func (c *Client) DownloadCourseFiles(root string) {
rawFolderPath := folderMap[file.FolderID]
safeFolderPath := utils.SanitizePath(rawFolderPath)
subDir := filepath.Join(root, "Course Files", safeFolderPath)
subDir := root
if !c.FilesOnly {
subDir = filepath.Join(root, "Course Files", safeFolderPath)
}
os.MkdirAll(subDir, 0o755)
path := filepath.Join(subDir, utils.Sanitize(file.DisplayName))
@@ -125,9 +130,15 @@ func (c *Client) DownloadModules(courseRoot string) {
resp.Body.Close()
for _, mod := range modules {
modBaseDir := filepath.Join(courseRoot, "Modules", utils.Sanitize(mod.Name))
modBaseDir := courseRoot
if !c.FilesOnly {
modBaseDir = filepath.Join(courseRoot, "Modules", utils.Sanitize(mod.Name))
}
os.MkdirAll(modBaseDir, 0o755)
if !c.FilesOnly {
fmt.Printf("\n[Module] %s\n", mod.Name)
}
subHeaderStack := []string{}
lastIndent := 0
@@ -135,13 +146,16 @@ func (c *Client) DownloadModules(courseRoot string) {
for _, item := range mod.Items {
targetDir := modBaseDir
if len(subHeaderStack) > 0 {
if len(subHeaderStack) > 0 && !c.FilesOnly {
targetDir = filepath.Join(modBaseDir, filepath.Join(subHeaderStack...))
}
os.MkdirAll(targetDir, 0o755)
switch item.Type {
case "SubHeader":
if c.FilesOnly {
continue
}
currentIndent := item.Indent
if currentIndent <= lastIndent && len(subHeaderStack) > 0 {
levelsToKeep := currentIndent
@@ -162,11 +176,28 @@ func (c *Client) DownloadModules(courseRoot string) {
c.downloadModuleFile(item, targetDir)
case "ExternalTool":
if c.FilesOnly {
continue
}
indent := strings.Repeat(" ", len(subHeaderStack)+1)
fmt.Printf("%s- Found video tool: %s\n", indent, 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":
if c.FilesOnly {
continue
}
c.searchPageForVideos(item, targetDir)
}
}

View File

@@ -34,6 +34,7 @@ type ModuleItem struct {
Type string `json:"type"`
URL string `json:"url"`
HTMLURL string `json:"html_url"`
ExternalURL string `json:"external_url"`
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
isDirectLink := false
if strings.Contains(inputURL, "/api/v1/") {
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 {
isDirectLink = true
launchURL = fmt.Sprintf("%s/api/v1/courses/%s/external_tools/sessionless_launch?id=%s&launch_type=course_navigation",
config.BaseURL, courseID, config.PanoptoID)
}
@@ -70,6 +71,7 @@ func DownloadVideo(httpClient *http.Client, accessToken, courseID, modDir, input
resp.Body.Close()
if launchData.URL == "" {
fmt.Printf(" [!] No launch URL found (Video skipped)\n")
return
}
@@ -84,17 +86,21 @@ func DownloadVideo(httpClient *http.Client, accessToken, courseID, modDir, input
json.NewDecoder(bResp.Body).Decode(&bridgeData)
bResp.Body.Close()
formReq, _ := http.NewRequest("GET", bridgeData.SessionURL, nil)
formReq.Header.Set("User-Agent", config.UserAgent)
formResp, err := httpClient.Do(formReq)
formResp, err := httpClient.Get(bridgeData.SessionURL)
if err != nil {
return
}
formHTML, _ := io.ReadAll(formResp.Body)
formHTMLBytes, _ := io.ReadAll(formResp.Body)
formResp.Body.Close()
formHTML := string(formHTMLBytes)
action := utils.ResolveAction(bridgeData.SessionURL, string(formHTML))
formData := utils.ExtractFormFields(string(formHTML))
if strings.Contains(formHTML, "U hebt geen toegang") || strings.Contains(formHTML, "You do not have access") {
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.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") {
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")
cData := "# Netscape HTTP Cookie File\n"
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)
ytCmd := getYoutubeDLCommand()
cmd := exec.Command(ytCmd,
"--no-playlist",
"--cookies", cookieFile,
"--referer", config.BaseURL+"/",
"-P", modDir,
"-o", utils.Sanitize(title)+".%(ext)s",
finalURL)
targetURL)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {