Compare commits
2 Commits
v1.0.1
...
05ed4dd4ed
| Author | SHA1 | Date | |
|---|---|---|---|
|
05ed4dd4ed
|
|||
|
352315b041
|
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@@ -12,6 +13,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
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{}
|
httpClient := &http.Client{}
|
||||||
|
|
||||||
authenticator := auth.NewAuthenticator(httpClient)
|
authenticator := auth.NewAuthenticator(httpClient)
|
||||||
@@ -25,7 +29,7 @@ func main() {
|
|||||||
fmt.Print("Enter Course ID: ")
|
fmt.Print("Enter Course ID: ")
|
||||||
fmt.Scanln(&courseID)
|
fmt.Scanln(&courseID)
|
||||||
|
|
||||||
canvasClient := canvas.NewClient(httpClient, accessToken, courseID)
|
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)
|
||||||
@@ -40,5 +44,7 @@ func main() {
|
|||||||
|
|
||||||
canvasClient.DownloadModules(courseRoot)
|
canvasClient.DownloadModules(courseRoot)
|
||||||
|
|
||||||
panopto.DownloadMainRecordings(httpClient, accessToken, courseID, courseRoot)
|
if !*filesOnly {
|
||||||
|
panopto.DownloadMainRecordings(httpClient, accessToken, courseID, courseRoot)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,17 +17,21 @@ 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
|
||||||
|
downloadedFiles map[string]bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClient(httpClient *http.Client, accessToken, courseID string) *Client {
|
func NewClient(httpClient *http.Client, accessToken, courseID string, filesOnly bool) *Client {
|
||||||
return &Client{
|
return &Client{
|
||||||
HTTPClient: httpClient,
|
HTTPClient: httpClient,
|
||||||
AccessToken: accessToken,
|
AccessToken: accessToken,
|
||||||
CourseID: courseID,
|
CourseID: courseID,
|
||||||
|
FilesOnly: filesOnly,
|
||||||
|
downloadedFiles: make(map[string]bool),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,10 +84,20 @@ 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 {
|
||||||
|
subDir = filepath.Join(root, "Course Files", safeFolderPath)
|
||||||
|
}
|
||||||
os.MkdirAll(subDir, 0o755)
|
os.MkdirAll(subDir, 0o755)
|
||||||
path := filepath.Join(subDir, utils.Sanitize(file.DisplayName))
|
path := filepath.Join(subDir, utils.Sanitize(file.DisplayName))
|
||||||
|
|
||||||
@@ -125,9 +139,15 @@ func (c *Client) DownloadModules(courseRoot string) {
|
|||||||
resp.Body.Close()
|
resp.Body.Close()
|
||||||
|
|
||||||
for _, mod := range modules {
|
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)
|
os.MkdirAll(modBaseDir, 0o755)
|
||||||
fmt.Printf("\n[Module] %s\n", mod.Name)
|
|
||||||
|
if !c.FilesOnly {
|
||||||
|
fmt.Printf("\n[Module] %s\n", mod.Name)
|
||||||
|
}
|
||||||
|
|
||||||
subHeaderStack := []string{}
|
subHeaderStack := []string{}
|
||||||
lastIndent := 0
|
lastIndent := 0
|
||||||
@@ -135,13 +155,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 +185,17 @@ 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":
|
case "ExternalUrl":
|
||||||
|
if c.FilesOnly {
|
||||||
|
continue
|
||||||
|
}
|
||||||
if strings.Contains(item.ExternalURL, "panopto.eu") {
|
if strings.Contains(item.ExternalURL, "panopto.eu") {
|
||||||
indent := strings.Repeat(" ", len(subHeaderStack)+1)
|
indent := strings.Repeat(" ", len(subHeaderStack)+1)
|
||||||
fmt.Printf("%s- Found direct video link: %s\n", indent, item.Title)
|
fmt.Printf("%s- Found direct video link: %s\n", indent, item.Title)
|
||||||
@@ -175,6 +204,9 @@ func (c *Client) DownloadModules(courseRoot string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case "Page":
|
case "Page":
|
||||||
|
if c.FilesOnly {
|
||||||
|
continue
|
||||||
|
}
|
||||||
c.searchPageForVideos(item, targetDir)
|
c.searchPageForVideos(item, targetDir)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -200,11 +232,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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user