Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
14a71e7dca | ||
| 051b67de51 | |||
|
|
9691ecd7a5 | ||
|
|
b8e6180b35 | ||
|
|
8591ae283e | ||
| 11d9867155 | |||
| 9ffe1283c2 | |||
|
05ed4dd4ed
|
|||
| be63064bee | |||
| cd259e01a3 | |||
|
352315b041
|
15
README.md
15
README.md
@@ -21,18 +21,31 @@ go build -o canvasarchiver ./cmd/canvasarchiver
|
||||
./canvasarchiver
|
||||
```
|
||||
|
||||
Or for files-only mode:
|
||||
```bash
|
||||
./canvasarchiver -fo
|
||||
```
|
||||
|
||||
2. On first run, you'll be prompted to authenticate:
|
||||
- Visit the provided OAuth URL
|
||||
- Authorize the application
|
||||
- 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:
|
||||
- Regular course files (to `Course Files/`)
|
||||
- Module content (to `Modules/`)
|
||||
- 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
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -12,6 +13,11 @@ import (
|
||||
)
|
||||
|
||||
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{}
|
||||
|
||||
authenticator := auth.NewAuthenticator(httpClient)
|
||||
@@ -21,11 +27,31 @@ func main() {
|
||||
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
|
||||
fmt.Print("Enter Course ID: ")
|
||||
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 {
|
||||
fmt.Printf("Error: %v\n", err)
|
||||
@@ -40,5 +66,7 @@ func main() {
|
||||
|
||||
canvasClient.DownloadModules(courseRoot)
|
||||
|
||||
if !filesOnly {
|
||||
panopto.DownloadMainRecordings(httpClient, accessToken, courseID, courseRoot)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,13 +21,19 @@ type Client struct {
|
||||
AccessToken string
|
||||
CourseID 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{
|
||||
HTTPClient: httpClient,
|
||||
AccessToken: accessToken,
|
||||
CourseID: courseID,
|
||||
FilesOnly: filesOnly,
|
||||
ModuleNumbers: moduleNumbers,
|
||||
downloadedFiles: make(map[string]bool),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,6 +53,20 @@ func (c *Client) GetCourseInfo() error {
|
||||
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) {
|
||||
fmt.Println("\n[*] Fetching regular course files...")
|
||||
|
||||
@@ -80,10 +100,24 @@ func (c *Client) DownloadCourseFiles(root string) {
|
||||
fileCount := 0
|
||||
for _, file := range files {
|
||||
|
||||
if c.FilesOnly {
|
||||
if c.downloadedFiles[file.DisplayName] {
|
||||
continue
|
||||
}
|
||||
c.downloadedFiles[file.DisplayName] = true
|
||||
}
|
||||
|
||||
rawFolderPath := folderMap[file.FolderID]
|
||||
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)
|
||||
path := filepath.Join(subDir, utils.Sanitize(file.DisplayName))
|
||||
|
||||
@@ -124,10 +158,21 @@ func (c *Client) DownloadModules(courseRoot string) {
|
||||
json.NewDecoder(resp.Body).Decode(&modules)
|
||||
resp.Body.Close()
|
||||
|
||||
for _, mod := range modules {
|
||||
modBaseDir := filepath.Join(courseRoot, "Modules", utils.Sanitize(mod.Name))
|
||||
for i, mod := range modules {
|
||||
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)
|
||||
fmt.Printf("\n[Module] %s\n", mod.Name)
|
||||
|
||||
if !c.FilesOnly {
|
||||
fmt.Printf("\n[Module] %s\n", modName)
|
||||
}
|
||||
|
||||
subHeaderStack := []string{}
|
||||
lastIndent := 0
|
||||
@@ -135,13 +180,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 +210,17 @@ 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)
|
||||
@@ -175,6 +229,9 @@ func (c *Client) DownloadModules(courseRoot string) {
|
||||
}
|
||||
|
||||
case "Page":
|
||||
if c.FilesOnly {
|
||||
continue
|
||||
}
|
||||
c.searchPageForVideos(item, targetDir)
|
||||
}
|
||||
}
|
||||
@@ -200,11 +257,18 @@ func (c *Client) downloadModuleFile(item models.ModuleItem, dir string) {
|
||||
return
|
||||
}
|
||||
|
||||
if c.FilesOnly {
|
||||
if c.downloadedFiles[fileMeta.DisplayName] {
|
||||
return
|
||||
}
|
||||
c.downloadedFiles[fileMeta.DisplayName] = true
|
||||
}
|
||||
|
||||
ext := filepath.Ext(fileMeta.DisplayName)
|
||||
origBase := strings.TrimSuffix(fileMeta.DisplayName, ext)
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ type TokenResponse struct {
|
||||
}
|
||||
|
||||
type Course struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user