-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (60 loc) · 2.45 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"log"
c "github.com/Astrak00/AGDownloader/courses"
download "github.com/Astrak00/AGDownloader/download"
"github.com/Astrak00/AGDownloader/files"
prog_args "github.com/Astrak00/AGDownloader/prog_args"
token "github.com/Astrak00/AGDownloader/token"
types "github.com/Astrak00/AGDownloader/types"
u "github.com/Astrak00/AGDownloader/user"
webui "github.com/Astrak00/AGDownloader/webUI"
"github.com/fatih/color"
)
func main() {
// Parse the flags to get the language, user token, the path to save the downloaded files, maxGoroutines and courses list to download
arguments := prog_args.ParseCLIArgs()
// Attribution of the program creator
color.Cyan("Program created by Astrak00 to download files from Aula Global at UC3M\n")
// In case the user has not provided a token though the cli, we try to obtain it from a file or ask the user for it
if arguments.UserToken == "" {
arguments.UserToken = token.ObtainToken()
}
// If there are missing arguments, we prompt the user for them
if !arguments.CheckAllAsigned() {
arguments = prog_args.PromptMissingArgs(arguments)
}
// Obtain the user information by logging in with the token
user, err := u.GetUserInfo(arguments.UserToken)
for err != nil {
user, err = u.GetUserInfo(arguments.UserToken)
log.Default().Printf("Error getting user info: %v\nTrying again", err)
}
// Obtain the courses the user is enrolled in
courses, err := c.GetCourses(arguments.UserToken, user.UserID, arguments.Language)
if err != nil {
log.Fatalf("Error getting courses: %v\n", err)
}
var coursesList []types.Course
if arguments.WebUI {
coursesList = webui.ShowCourseWeb(courses)
} else {
coursesList = c.SelectCoursesInteractive(arguments.Language, arguments.CoursesList, courses)
}
// Create an interactive list so the user can select the courses to download
// Create a channel to store the files and another for the errors that may occur when listing all the resources to download
filesStoreChan := make(chan types.FileStore, len(courses)*100)
errChan := make(chan error, len(courses))
// List all the resources to downloaded and send them to the channel
files.ListAllResources(coursesList, arguments.UserToken, arguments.DirPath, errChan, filesStoreChan)
close(errChan)
close(filesStoreChan)
for err := range errChan {
if err != nil {
fmt.Println("Error:", err)
}
}
// Download all the files in the channel
download.DownloadFiles(filesStoreChan, arguments.MaxGoroutines, courses)
}