-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch-from-tags.go
186 lines (158 loc) · 4.67 KB
/
batch-from-tags.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
/*
Copyright © 2023 Chris Slamar chris@slamar.com
*/
package cmd
import (
"errors"
"fmt"
"github.com/cslamar/audiobooker/audiobooker"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"io/fs"
"os"
"path/filepath"
"strings"
"time"
)
// batchFromTagsCmd represents the files command
var batchFromTagsCmd = &cobra.Command{
Use: "from-tags",
Short: `Bind audiobook combining title tag of each file as chapter names`,
Long: `Bind audiobook combining title tag of each file as chapter names in the compiled audiobook`,
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Printf("starting batch from tags\n\n")
processStart := time.Now()
var err error
// Parse scoped options
sourceFilesRoot, err := cmd.Flags().GetString("source-files-root")
if err != nil {
return err
}
pathPattern, err := cmd.Flags().GetString("path-pattern")
if err != nil {
return err
}
// slice of directories that contain books
bookDirs := make([]string, 0)
log.Debugln("src files root:", sourceFilesRoot)
err = filepath.WalkDir(sourceFilesRoot, func(path string, d fs.DirEntry, err error) error {
// return if not a directory
if !d.IsDir() {
return nil
}
// scan files in path
dirs, err := os.ReadDir(path)
if err != nil {
return err
}
// if this directory contains directories, skip
for _, dir := range dirs {
if dir.Type().IsDir() {
return nil
}
}
// output the last level directories
log.Debugln("found book directory at:", path)
bookDirs = append(bookDirs, path)
return nil
})
if err != nil {
return err
}
// if no book directories were found, error out
if len(bookDirs) == 0 {
return errors.New("no book directories found in path")
}
// loop through found books
for _, dir := range bookDirs {
// create config struct and parse ENV variables for configs
config := audiobooker.Config{}
defer config.Cleanup()
if err := config.Parse(); err != nil {
return err
}
// watch for early terminations
go watchForTermSignals(&config)
// generate and validate configs
if err := generateBatchOpts(&config, cmd.Flags()); err != nil {
return err
}
// validate full path formatting
var fullPath string
if strings.HasSuffix(sourceFilesRoot, "/") {
fullPath = sourceFilesRoot + pathPattern
} else {
fullPath = sourceFilesRoot + "/" + pathPattern
}
// parse source based on pattern
pathTags, err := audiobooker.ParsePathTags(dir, fullPath)
if err != nil {
return err
}
// get the source files path to current book directory
config.SourceFilesPath = dir
// create book instance and generate metadata from path
book := audiobooker.Book{}
book.ParseFromPattern(pathTags)
// initialize config
if err := config.New(); err != nil {
return err
}
log.Debugln(book)
// compute output filename from metadata and patterns
if err := config.SetOutputFilename(book); err != nil {
return err
}
fmt.Println("book found at:", dir)
for k, v := range pathTags {
log.Printf("%+15s: %s\n", k, v)
}
fmt.Printf("output filepath: %s\n\n", filepath.Join(config.OutputPath, config.OutputFile))
// if dry-run flag is given, output metadata for validation but don't convert
if dryRun {
fmt.Printf("dry-run flag was set, skipping conversion\n\n")
continue
}
log.Debugln("Beginning conversion")
// make output directory paths
if err := os.MkdirAll(config.OutputPath, 0755); err != nil {
return err
}
// pre-transcode files
if err := audiobooker.TranscodeSourceFiles(&config); err != nil {
return err
}
// Parse files to chapters inside Book struct/object
if err := book.ParseToChapters(config); err != nil {
return err
}
log.Debugln(book)
// Generate metadata for book
if err := book.GenerateMetaTemplate(config); err != nil {
return err
}
// combine pre-transcode files
if err := audiobooker.Combine(config); err != nil {
return err
}
// Apply metadata to output file
if err := audiobooker.Bind(config, book); err != nil {
return err
}
log.Debugln("that one is done")
}
fmt.Println("Entire process took:", time.Now().Sub(processStart))
fmt.Println("fin.")
return nil
},
}
func init() {
batchCmd.AddCommand(batchFromTagsCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// filesCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// filesCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}