8 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
4 changed files with 91 additions and 20 deletions

View File

@@ -21,24 +21,31 @@ go build -o canvasarchiver ./cmd/canvasarchiver
./canvasarchiver ./canvasarchiver
``` ```
Or Or for files-only mode:
```bash ```bash
./canvasarchiver -fo ./canvasarchiver -fo
``` ```
For files-only mode.
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

@@ -14,6 +14,8 @@ 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")
moduleNumbers := flag.Bool("n", false, "Prefix modules with order numbers [1], [2], etc.")
flag.Parse() flag.Parse()
httpClient := &http.Client{} httpClient := &http.Client{}
@@ -25,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, *filesOnly) 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)
@@ -44,7 +66,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

@@ -22,14 +22,18 @@ type Client struct {
CourseID string CourseID string
CourseName string CourseName string
FilesOnly bool FilesOnly bool
ModuleNumbers bool
downloadedFiles map[string]bool
} }
func NewClient(httpClient *http.Client, accessToken, courseID string, filesOnly bool) *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, FilesOnly: filesOnly,
ModuleNumbers: moduleNumbers,
downloadedFiles: make(map[string]bool),
} }
} }
@@ -49,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...")
@@ -82,12 +100,23 @@ 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 := root subDir := root
if !c.FilesOnly { if !c.FilesOnly {
if safeFolderPath != "" && strings.ToLower(safeFolderPath) != "course files" {
subDir = filepath.Join(root, "Course Files", safeFolderPath) 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))
@@ -129,15 +158,20 @@ 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 {
modName := mod.Name
if c.ModuleNumbers {
modName = fmt.Sprintf("[%d] %s", i+1, mod.Name)
}
modBaseDir := courseRoot modBaseDir := courseRoot
if !c.FilesOnly { if !c.FilesOnly {
modBaseDir = filepath.Join(courseRoot, "Modules", utils.Sanitize(mod.Name)) modBaseDir = filepath.Join(courseRoot, "Modules", utils.Sanitize(modName))
} }
os.MkdirAll(modBaseDir, 0o755) os.MkdirAll(modBaseDir, 0o755)
if !c.FilesOnly { if !c.FilesOnly {
fmt.Printf("\n[Module] %s\n", mod.Name) fmt.Printf("\n[Module] %s\n", modName)
} }
subHeaderStack := []string{} subHeaderStack := []string{}
@@ -223,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"`
} }