-
Notifications
You must be signed in to change notification settings - Fork 7
/
script.go
113 lines (107 loc) · 3.08 KB
/
script.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
package main
import (
"flag"
"fmt"
. "github.com/KeluDiao/gotube/api"
"log"
)
func main() {
isDownload := flag.Bool("d", false, "use this flag to download video")
isRetList := flag.Bool("l", false, "use this flag to retrieve video list")
url := flag.String("url", "", "video url")
id := flag.String("id", "", "video id")
search := flag.String("-search", "", "search by key words (specify top k with command -k)")
flag.StringVar(search, "s", "", "search by key words (specify top k with command -k)")
k := flag.Int("k", 1, "return top k results, only valid with key word searching")
rep := flag.String("-videorepository", "", "(optional) repository to store videos")
flag.StringVar(rep, "rep", "", "(optional) repository to store videos")
quality := flag.String("-quality", "", "(optional) video quality. e.g. medium")
flag.StringVar(quality, "q", "", "(optional) video quality. e.g. medium")
extension := flag.String("-extension", "", "(optional) video extension. e.g. video/mp4, video/flv, video/webm")
flag.StringVar(extension, "ext", "", "(optional) video extension. e.g. mp4, flv")
isHelp := flag.Bool("h", false, "help")
flag.Parse()
if *isHelp {
fmt.Println("Usage:")
flag.PrintDefaults()
return
}
invalidCommand := false
if *isDownload && *isRetList {
fmt.Println("You can only either download or retrieve video list.")
invalidCommand = true
} else if !*isDownload && !*isRetList {
fmt.Println("Please choose if you want to download or retrieve video list.")
invalidCommand = true
}
//Find out how many sources are specified
sourceNum := 0
if *url != "" {
sourceNum++
}
if *id != "" {
sourceNum++
}
if *search != "" {
sourceNum++
}
if sourceNum == 0 {
fmt.Println("Please specify one of url, id, and key word(s).")
invalidCommand = true
} else if sourceNum > 1 {
fmt.Println("Please don't specify more than one of url, id, and key word(s).")
invalidCommand = true
}
if invalidCommand {
fmt.Println("Usage:")
flag.PrintDefaults()
return
}
//Get the video list
var vl VideoList
var err error
if *url != "" {
vl, err = GetVideoListFromUrl(*url)
} else if *id != "" {
vl, err = GetVideoListFromId(*id)
} else {
ids, err := GetTopKVideoIds(*search, *k)
if err != nil {
log.Fatal(err)
}
if *isRetList {
fmt.Printf("The top %v results for key words \"%v\" are:\n\n", *k, *search)
}
for _, vid := range ids {
vl, err = GetVideoListFromId(vid)
if err != nil {
log.Fatal(err)
}
Exec(vl, *isDownload, *isRetList, *rep, *quality, *extension)
}
return
}
if err != nil {
log.Fatal(err)
}
Exec(vl, *isDownload, *isRetList, *rep, *quality, *extension)
}
/*
* Choose either downloading or retrieving video list
*/
func Exec(vl VideoList, isDownload, isRetList bool, rep, quality, extension string) {
if isDownload {
fmt.Printf("Downloading %v...\n", vl.Title)
err := vl.Download(rep, quality, extension)
if err != nil {
log.Fatal(err)
}
} else if isRetList {
fmt.Printf("Videos under the name of %v:\n", vl.Title)
err := vl.Filter(quality, extension)
if err != nil {
log.Fatal(err)
}
fmt.Println(vl)
}
}