forked from gophergala/aeris
/
main.go
216 lines (154 loc) · 3.76 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
package main
import (
"flag"
"fmt"
"io"
"os"
"github.com/gophergala/aeris/download"
"github.com/gophergala/aeris/info"
)
var (
inputItag int
)
func main() {
flag.IntVar(&inputItag, "itag", -1, "itag of the stream to download")
flag.Usage = func() {
printUsage()
}
flag.Parse()
if flag.NArg() == 0 {
printUsage()
os.Exit(0)
}
cmd := flag.Arg(0)
switch cmd {
case "get":
if flag.NArg() < 2 {
printUsageCommand("get")
os.Exit(1)
}
id := flag.Arg(1)
i := info.NewInfo(id)
err := i.Fetch()
if err != nil {
fmt.Println("there was an error fetching info for the video", id)
os.Exit(1)
}
var downloadStream *info.Stream
if inputItag != -1 {
// see if we can satisfy the user
for _, stream := range i.Streams() {
if stream.Format.Itag == inputItag {
downloadStream = stream
break
}
}
if downloadStream == nil {
fmt.Printf("%s doesn't have a stream with format with itag %d. Picking default stream (the best one available).\n", id, inputItag)
}
}
// pick the best stream by default
if downloadStream == nil {
downloadStream = i.Streams()[0]
}
var output io.WriteCloser
if flag.NArg() > 2 {
if flag.Arg(2) == "-" {
output = os.Stdout
} else {
fmt.Println("writing download result to:", flag.Arg(2))
output, err = os.Create(flag.Arg(2))
if err != nil {
fmt.Println("failed to create file")
os.Exit(1)
}
}
} else {
ext, err := downloadStream.Format.Extension()
if err != nil {
// no extension, I don't have all possibilities listed in (*Format).Extension()
ext = ""
}
fmt.Println("writing download result to:", i.Id+ext)
output, err = os.Create(i.Id + ext)
if err != nil {
fmt.Println("failed to create file")
os.Exit(1)
}
}
download.Download(i, downloadStream, output)
output.Close()
case "info":
if flag.NArg() < 2 {
printUsageCommand("info")
os.Exit(1)
}
id := flag.Arg(1)
showVideoInfo(id)
case "help":
if flag.NArg() == 2 {
printUsageCommand(flag.Arg(1))
} else {
printUsage()
}
default:
printUsage()
}
}
func showVideoInfo(id string) {
i := info.NewInfo(id)
fmt.Println("fetching video info")
err := i.Fetch()
if err != nil {
fmt.Println("there was an error while fetching info for", id)
}
fmt.Println("available itags:")
fmt.Println("")
for _, stream := range i.Streams() {
fmt.Printf(" [%d] %s - %s\n", stream.Format.Itag, stream.Format.Container, stream.Format.Video.Resolution)
}
}
func printHeader() {
fmt.Println("")
fmt.Println(" aeris - (c) Gijsbrecht Hermans 2015")
fmt.Println("")
}
func printSubHeader() {
fmt.Println(" Easily download videos in various formats from YouTube")
fmt.Println("")
}
func printUsage() {
printHeader()
printSubHeader()
fmt.Println("Usage:")
fmt.Println("")
fmt.Println(" aeris [flags] <command> [arguments...]")
fmt.Println("")
fmt.Println("A List of possible commands:")
fmt.Println("")
fmt.Println(" get download a video from YouTube")
fmt.Println(" info fetch technical info about a video from YouTube")
fmt.Println(" help help with aeris")
fmt.Println("")
fmt.Println("To view information about the usage of these commands execute")
fmt.Println("")
fmt.Println(" aeris help <command>")
}
func printUsageCommand(cmd string) {
printHeader()
printSubHeader()
fmt.Println("Usage:")
fmt.Println("")
switch cmd {
case "get":
fmt.Println(" aeris get [flags] <video-id> [target]")
fmt.Println("")
fmt.Println(" -itag itag identifying a YouTube stream to download")
case "info":
fmt.Println(" aeris info <video-id>")
case "help":
fmt.Println(" woah! help-ception!")
default:
fmt.Println("command", cmd, "doesn't exist")
}
}