-
Notifications
You must be signed in to change notification settings - Fork 5
/
options.go
220 lines (180 loc) · 5.35 KB
/
options.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
package main
import (
"flag"
"fmt"
"math/rand"
"os"
"runtime/debug"
"strings"
"rsc.io/getopt"
)
type optionalString struct {
*string
}
func (o *optionalString) Set(s string) error {
if o.string == nil {
o.string = new(string)
}
*o.string = s
return nil
}
func (o *optionalString) String() string {
if o.string == nil {
return "unset"
}
return *o.string
}
var (
forceOutput bool
forceOutputDefault = false
usernameURL bool
usernameURLDefault = false
archiveMode bool
archiveModeDefault = false
hideConsole bool
hideConsoleDefault = false
groupSelect string
groupSelectDefault = "best"
groupList bool
groupListDefault = false
showVersion bool
showVersionDefault = false
accessTokenPlatform string
accessTokenPlatformDefault = "web"
accessTokenPlayerType string
accessTokenPlayerTypeDefault = "site"
accessTokenPlayerBackend optionalString
accessTokenOAuth optionalString
accessTokenDeviceID optionalString
)
func init() {
flag.BoolVar(&forceOutput, "f", forceOutputDefault, "Force output to standard output even if TTY is detected")
flag.BoolVar(&usernameURL, "u", usernameURLDefault, "Treat USERNAME as a URL")
flag.BoolVar(&archiveMode, "a", archiveModeDefault, "Start downloading from the oldest segment rather than the newest")
flag.StringVar(&groupSelect, "g", groupSelectDefault, "Select specified playlist group\n\t\"best\" will select the best available group")
flag.BoolVar(&groupList, "G", groupListDefault, "List available playlist groups and exit")
flag.BoolVar(&showVersion, "v", showVersionDefault, "Show version information and exit")
getopt.Aliases(
"f", "force-output",
"u", "url",
"a", "archive",
"g", "group",
"G", "list-groups",
"v", "version",
)
flag.StringVar(&accessTokenPlatform, "access-token-platform", accessTokenPlatformDefault, "The platform to send when acquiring an access token")
flag.StringVar(&accessTokenPlayerType, "access-token-player-type", accessTokenPlayerTypeDefault, "The player type to send when acquiring an access token")
flag.Var(&accessTokenPlayerBackend, "access-token-player-backend", "The player backend to send when acquiring an access token (optional)")
flag.Var(&accessTokenOAuth, "access-token-oauth", "OAuth token to send when acquiring an access token (optional)")
flag.Var(&accessTokenDeviceID, "access-token-device-id", "Device ID to send when acquiring an access token (optional)\n\tIf no device ID is specified, one will be generated randomly")
}
func printVersion() {
bi, ok := debug.ReadBuildInfo()
if !ok {
stdErr.Println("failed to read build info")
return
}
var versionString string
versionString += fmt.Sprintf("twitchpipe %s", bi.Main.Version)
if bi.Main.Version == "(devel)" {
var rev string
for _, s := range bi.Settings {
if s.Key == "vcs.revision" {
rev = s.Value
break
}
}
if rev != "" {
revLen := 7
if len(rev) < revLen {
revLen = len(rev)
}
versionString += fmt.Sprintf(" %s", rev[:revLen])
}
}
stdErr.Println(versionString)
}
func printUsage() {
stdErr.Println("Usage: twitchpipe [OPTIONS...] <USERNAME> [COMMAND...]")
stdErr.Println()
stdErr.Println("If COMMAND is specified, it will be executed and stream data will be \nwritten to its standard input.")
stdErr.Println("Otherwise, stream data will be written to standard output.")
stdErr.Println()
stdErr.Println("Options:")
getopt.PrintDefaults()
}
func findBest(playlists []playlistInfo) playlistInfo {
var best playlistInfo
var highBitrate int
for _, p := range playlists {
if p.Group == "chunked" {
return p
}
if p.Bandwidth > highBitrate {
highBitrate = p.Bandwidth
best = p
}
}
return best
}
func printGroups(playlists []playlistInfo) {
columns := []*struct {
title string
length int
content []string
fn func(p playlistInfo) string
}{
{"Group", 0, nil, func(p playlistInfo) string { return p.Group }},
{"Name", 0, nil, func(p playlistInfo) string { return p.Name }},
{"Resolution", 0, nil, func(p playlistInfo) string { return fmt.Sprintf("%dx%d", p.Width, p.Height) }},
{"Codec", 0, nil, func(p playlistInfo) string {
var cs string
for _, c := range strings.Split(p.Codec, ",") {
cs += strings.SplitN(c, ".", 2)[0] + "+"
}
if cs != "" {
cs = cs[:len(cs)-1]
}
return cs
}},
{"Bitrate", 0, nil, func(p playlistInfo) string { return fmt.Sprintf("%dk", p.Bandwidth/1024) }},
}
for _, c := range columns {
c.length = len(c.title)
}
for _, p := range playlists {
for _, c := range columns {
content := c.fn(p)
c.content = append(c.content, content)
if len(content) > c.length {
c.length = len(content)
}
}
}
for _, c := range columns {
fmt.Fprint(os.Stderr, c.title)
if c.length-len(c.title) > 0 {
fmt.Fprint(os.Stderr, strings.Repeat(" ", c.length-len(c.title)))
}
fmt.Fprint(os.Stderr, " ")
}
fmt.Fprintln(os.Stderr)
best := findBest(playlists)
for i := range playlists {
for _, c := range columns {
content := c.content[i]
fmt.Fprint(os.Stderr, content)
if c.length-len(content) > 0 {
fmt.Fprint(os.Stderr, strings.Repeat(" ", c.length-len(content)))
}
fmt.Fprint(os.Stderr, " ")
}
if playlists[i].Group == best.Group {
fmt.Fprint(os.Stderr, "(best)")
}
fmt.Fprintln(os.Stderr)
}
}
func randDeviceID() string {
return fmt.Sprintf("%16x", rand.Uint64())
}