-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
184 lines (176 loc) · 4.04 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
package main
import (
"errors"
"fmt"
"io"
"io/fs"
"math/rand"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/7fffffff/jsf"
cli "github.com/jawher/mow.cli"
)
func main() {
useAudioPresetExts := false
useVideoPresetExts := true
exts := []string{}
outputPath := ""
roots := []string{}
cfg := &config{
rng: rand.New(jsf.New(time.Now().UnixNano())),
}
app := cli.App("xspf", "print an xspf (VLC) playlist to standard output")
app.LongDesc = `print an xspf (VLC) playlist to standard output
Examples:
# find audio files in the current directory
xspf -a . > playlist.xspf
# find and shuffle video files in dir1 and dir2
xspf -v -s dir1 dir2 > playlist.xspf
# find *.ext1 and *.ext2 files in dir1 and dir2
xspf -e ext1 -e ext2 dir1 dir2 > playlist.xspf`
app.Spec = "(-a | -v | -e=<ext>...)... [-o=<output file>] [OPTIONS] DIRS..."
app.StringsArgPtr(&roots, "DIRS", []string{}, "directories to search")
app.BoolOptPtr(&useAudioPresetExts, "a audio", false, "match a bunch of audio extensions")
app.BoolOptPtr(&useVideoPresetExts, "v video", false, "match a bunch of video extensions")
app.StringsOptPtr(&exts, "e ext", []string{}, "extension to match; can be used multiple times")
app.BoolOptPtr(&cfg.m3u, "m3u", false, "output as m3u")
app.BoolOptPtr(&cfg.mpcpl, "mpcpl", false, "output as mpcpl (Media Player Classic)")
app.BoolOptPtr(&cfg.shuffle, "s shuffle", false, "shuffle the output")
app.StringOptPtr(&outputPath, "o output", "", "output to file instead stdout")
_ = app.BoolOpt("h help", false, "show help with examples")
app.Action = func() {
err := func() error {
if useAudioPresetExts {
exts = append(exts, presetAudioExts()...)
}
if useVideoPresetExts {
exts = append(exts, presetVideoExts()...)
}
cfg.exts = newExtMatcher(exts)
if cfg.exts.Empty() {
app.PrintHelp()
cli.Exit(1)
}
var output io.Writer = os.Stdout
if outputPath != "" {
file, err := os.Create(outputPath)
if err != nil {
return err
}
defer file.Close()
output = file
}
return cfg.WriteAll(output, roots)
}()
if err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
cli.Exit(1)
}
}
err := app.Run(os.Args)
if err != nil {
fmt.Fprintf(os.Stderr, "%v", err)
os.Exit(1)
}
}
type config struct {
exts *extMatcher
rng *rand.Rand
shuffle bool
m3u bool
mpcpl bool
}
func (cfg *config) WriteAll(wr io.Writer, roots []string) error {
allFiles := []string{}
for _, root := range roots {
root = filepath.Clean(root)
err := filepath.Walk(root, func(p string, info fs.FileInfo, err error) error {
if err != nil {
if errors.Is(err, os.ErrPermission) || errors.Is(err, os.ErrNotExist) {
fmt.Fprintln(os.Stderr, err)
return nil
}
return err
}
if info.IsDir() {
return nil
}
if info.Size() < 1 {
return nil
}
ext := strings.ToLower(filepath.Ext(p))
if ext == "" {
return nil
}
if cfg.exts.Match(ext) {
abs, err := filepath.Abs(p)
if err != nil {
return err
}
if cfg.m3u || cfg.mpcpl {
allFiles = append(allFiles, abs)
} else {
// xspf paths need to look like
// /C:/A/B/C.mp3
allFiles = append(allFiles, path.Clean("/"+filepath.ToSlash(abs)))
}
}
return nil
})
if err != nil {
return err
}
}
if cfg.shuffle && cfg.rng != nil {
cfg.rng.Shuffle(len(allFiles), func(i, j int) {
allFiles[i], allFiles[j] = allFiles[j], allFiles[i]
})
}
if cfg.m3u {
m3u := m3uWriter{
wr: wr,
}
err := m3u.Begin()
if err != nil {
return err
}
for _, f := range allFiles {
err = m3u.WriteFile(f)
if err != nil {
return err
}
}
return nil
}
if cfg.mpcpl {
mpc := &mpcplWriter{
wr: wr,
}
err := mpc.Begin()
if err != nil {
return err
}
for _, f := range allFiles {
err = mpc.WriteFile(f)
if err != nil {
return err
}
}
return nil
}
xspf := newXSPFWriter(wr)
err := xspf.Begin()
if err != nil {
return err
}
for _, f := range allFiles {
err = xspf.WriteTrack(f)
if err != nil {
return err
}
}
return xspf.End()
}