-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
221 lines (189 loc) · 4.5 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"bufio"
"errors"
"fmt"
"net/url"
"os"
"strings"
"syscall"
"github.com/apex/log"
lcli "github.com/apex/log/handlers/cli"
"github.com/shahinam/cloudac-dl/client"
"github.com/urfave/cli"
"golang.org/x/crypto/ssh/terminal"
)
var version = "1.x-dev"
// CommandLineOptions Command line options.
type CommandLineOptions struct {
userName string
passWord string
saveDir string
resolution string
courseURL string
inputFile string
}
// Init app.
func init() {
log.SetHandler(lcli.Default)
log.SetLevel(log.DebugLevel)
}
func main() {
dir, _ := os.Getwd()
app := cli.NewApp()
app.Name = "cloudac-dl"
app.Version = version
app.Usage = `Downloads the video lectures for the given Cloud Academy course.
Homepage: https://github.com/shahinam/cloudac-dl`
app.Authors = []cli.Author{
{
Name: "Muhammad Inam",
Email: "[email protected]",
},
}
app.Action = func(c *cli.Context) error {
return cli.ShowAppHelp(c)
}
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "user, u",
Usage: "The login email address for your Cloud Academy account.",
Value: "",
},
cli.StringFlag{
Name: "pass, p",
Usage: "The password for your Cloud Academy account.",
Value: "",
},
cli.StringFlag{
Name: "out, o",
Usage: "The directory where the videos are saved.",
Value: dir,
},
cli.StringFlag{
Name: "res, r",
Usage: "The required video resolution. Allowed values are 360, 720, and 1080.",
Value: "720p",
},
cli.StringFlag{
Name: "file, f",
Usage: "Download URLs found in local or external FILE",
Value: "",
},
}
app.Commands = []cli.Command{
{
Name: "course",
Usage: "Download a course.",
Action: func(c *cli.Context) error {
return download(c, "course")
},
},
{
Name: "path",
Aliases: []string{"learning-path"},
Usage: "Download all courses in learning path.",
Action: func(c *cli.Context) error {
return download(c, "path")
},
},
}
_ = app.Run(os.Args)
}
// Read the input file into URL array.
func readInputFile(inputFile string) ([]string, error) {
file, err := os.Open(inputFile)
if err != nil {
return nil, err
}
defer file.Close()
var lines []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
text := strings.Trim(scanner.Text(), " ")
if text != "" || !strings.HasPrefix(text, "#") {
lines = append(lines, scanner.Text())
}
}
return lines, scanner.Err()
}
// Download.
func download(c *cli.Context, op string) error {
args := parseCommandLineArgs(c)
cl, err := getClient(c, args)
if err != nil {
log.Fatal(err.Error())
}
links := []string{}
if args.inputFile != "" {
links, err = readInputFile(args.inputFile)
if err != nil {
return err
}
}
// Append the url if provided.
if args.courseURL != "" {
links = append(links, args.courseURL)
}
co := &client.Course{
CourseURL: args.courseURL,
SaveDir: args.saveDir,
Resolution: args.resolution,
}
for _, link := range links {
co.CourseURL = link
// set an error if invalid op is provided.
err := errors.New("invalid operation")
if op == "course" {
err = cl.DownloadCourse(co)
} else if op == "path" {
err = cl.DownloadLearningPath(co)
}
if err != nil {
log.Error(err.Error())
}
}
return nil
}
// Get client.
func getClient(c *cli.Context, args *CommandLineOptions) (*client.Client, error) {
// Get the client & course.
cl := client.New()
cl.SetUserName(args.userName)
cl.SetPassWord(args.passWord)
// Login.
err := cl.Login()
return cl, err
}
// Parse command line arguments.
func parseCommandLineArgs(c *cli.Context) *CommandLineOptions {
// Command line options.
args := &CommandLineOptions{}
args.userName = c.GlobalString("user")
args.passWord = c.GlobalString("pass")
args.saveDir = c.GlobalString("out")
args.resolution = c.GlobalString("res")
args.inputFile = c.GlobalString("file")
args.courseURL = c.Args().First()
// Validations.
if args.userName == "" {
_ = cli.ShowAppHelp(c)
os.Exit(1)
}
if args.inputFile == "" && args.courseURL == "" {
log.Fatalf("Please provide a URL to download or specify a URL list with --file flag.")
}
if args.courseURL != "" {
_, err := url.ParseRequestURI(args.courseURL)
if err != nil {
log.Fatalf("The provided url %s is invalid.\n", args.courseURL)
}
}
// If password is not provided - get it interactively.
if args.passWord == "" {
fmt.Print("Please enter password: ")
password, _ := terminal.ReadPassword(int(syscall.Stdin))
args.passWord = string(password)
}
return args
}