add numbering

This commit is contained in:
joren
2026-03-11 19:56:37 +01:00
parent 8591ae283e
commit b8e6180b35
2 changed files with 17 additions and 9 deletions

View File

@@ -15,6 +15,7 @@ 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") 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{}
@@ -27,7 +28,7 @@ func main() {
} }
if *me { if *me {
canvasClient := canvas.NewClient(httpClient, accessToken, "", *filesOnly) canvasClient := canvas.NewClient(httpClient, accessToken, "", *filesOnly, *moduleNumbers)
courses, err := canvasClient.GetEnrolledCourses() courses, err := canvasClient.GetEnrolledCourses()
if err != nil { if err != nil {
fmt.Printf("Error fetching courses: %v\n", err) fmt.Printf("Error fetching courses: %v\n", err)
@@ -37,7 +38,7 @@ func main() {
fmt.Printf("[+] Found %d enrolled courses\n", len(courses)) fmt.Printf("[+] Found %d enrolled courses\n", len(courses))
for _, course := range courses { for _, course := range courses {
fmt.Printf(" -> Downloading: %s (ID: %d)\n", course.Name, course.ID) fmt.Printf(" -> Downloading: %s (ID: %d)\n", course.Name, course.ID)
downloadCourse(httpClient, accessToken, fmt.Sprintf("%d", course.ID), *filesOnly) downloadCourse(httpClient, accessToken, fmt.Sprintf("%d", course.ID), *filesOnly, *moduleNumbers)
} }
return return
} }
@@ -46,11 +47,11 @@ func main() {
fmt.Print("Enter Course ID: ") fmt.Print("Enter Course ID: ")
fmt.Scanln(&courseID) fmt.Scanln(&courseID)
downloadCourse(httpClient, accessToken, courseID, *filesOnly) downloadCourse(httpClient, accessToken, courseID, *filesOnly, *moduleNumbers)
} }
func downloadCourse(httpClient *http.Client, accessToken, courseID string, filesOnly bool) { func downloadCourse(httpClient *http.Client, accessToken, courseID string, filesOnly, moduleNumbers bool) {
canvasClient := canvas.NewClient(httpClient, accessToken, courseID, filesOnly) 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)

View File

@@ -22,15 +22,17 @@ type Client struct {
CourseID string CourseID string
CourseName string CourseName string
FilesOnly bool FilesOnly bool
ModuleNumbers bool
downloadedFiles map[string]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), downloadedFiles: make(map[string]bool),
} }
} }
@@ -152,15 +154,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{}