-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
125 lines (101 loc) · 2.16 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
package main
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/codegangsta/cli"
"github.com/poying/necourse/necourse"
"github.com/tj/go-spin"
)
var progName = filepath.Base(os.Args[0])
func main() {
app := cli.NewApp()
app.Name = progName
app.Version = Version
cli.AppHelpTemplate = appHelpTemplate
app.Action = action
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "concurrent,c",
Value: 5,
Usage: "同時下載數",
},
cli.StringFlag{
Name: "out,o",
Value: ".",
Usage: "檔案存放位置",
},
cli.BoolFlag{
Name: "hd",
Usage: "高畫質",
},
}
app.Run(os.Args)
}
func action(c *cli.Context) {
args := c.Args()
if len(args) < 1 {
cli.ShowAppHelp(c)
return
}
defer func() {
if err := recover(); err != nil {
fmt.Println("Error: ", err)
}
}()
outputDir, err := filepath.Abs(c.String("out"))
check(err)
course, err := necourse.Get(args[0])
check(err)
downloader := NewDownloader(uint(c.Int("concurrent")))
task := downloader.Task(course)
quality := necourse.SD
if c.Bool("hd") {
quality = necourse.HD
}
downloader.Download(task, &Options{
Quality: quality,
OutputDir: outputDir,
})
progress(course, task)
task.Wait()
}
func progress(course necourse.Course, task *Task) {
ticker := time.NewTicker(time.Millisecond * 100)
spinner := spin.New()
go func() {
for range ticker.C {
tick(course, task, spinner)
}
}()
}
func tick(course necourse.Course, task *Task, spinner *spin.Spinner) {
spinFram := spinner.Next()
iter := task.Status.Iter()
fmt.Print("\033[0;0H\033[0J\n")
fmt.Printf(" \033[36m%s\033[m\n\n", course.Title())
lines := 3
for iter.HasNext() {
video, status := iter.Next()
lines += 1
if status.Done() {
if status.Failed() {
fmt.Printf(" \033[31m✗\033[m %s (%s)\n", video.Title(), status.Error())
} else {
fmt.Printf(" \033[32m✓\033[m %s\n", video.Title())
}
continue
}
if status.Started() {
fmt.Printf(" \033[36m%s\033[m %s \033[90m(%d bytes)\033[m\n", spinFram, video.Title(), status.Progress())
continue
}
fmt.Printf(" \033[90m%s\033[m\n", video.Title())
}
}
func check(err error) {
if err != nil {
panic(err)
}
}