-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
128 lines (110 loc) · 2.98 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
package main
import (
"bytes"
"fmt"
"github.com/dhowden/tag"
"github.com/jessevdk/go-flags"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
)
var opts struct {
Noop bool `short:"n" long:"noop" description:"Run, but do not rename. Useful for testing when combined with verbose"`
Verbose bool `short:"v" long:"verbose" description:"Show verbose debug information"`
Directory string `short:"d" long:"directory" description:"Base directory containing the audio files" required:"true"`
Recursive bool `short:"r" long:"recursive" description:"Recursively target subdirectories"`
}
func main() {
_, err := flags.ParseArgs(&opts, os.Args)
if err != nil {
fmt.Println("Could not parse arguments")
os.Exit(1)
}
err = Run(opts.Directory)
if err != nil {
fmt.Println("Could not read directory")
os.Exit(1)
}
}
func Run(directory string) error {
files, err := ioutil.ReadDir(directory)
if err != nil {
return err
}
for _, file := range files {
filePath := filepath.Join(directory, file.Name())
if file.IsDir() {
if opts.Recursive {
Run(filePath)
}
continue
}
fileBytes, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Printf("Warning: cannot file read %s\n", filePath)
}
reader := bytes.NewReader(fileBytes)
metadata, err := tag.ReadFrom(reader)
if err != nil {
// Assume file is not an audio file
continue
}
title := metadata.Title()
if title == "" {
if opts.Verbose {
fmt.Printf("Could not rename file %s. No title\n", filePath)
}
continue
}
extension := filepath.Ext(filePath)
title = escapeChars(title)
title = title + extension
newPath := filepath.Join(directory, title)
if opts.Verbose {
fmt.Printf("Renaming \"%s\" to \"%s\"\n", filePath, newPath)
}
if opts.Noop {
continue
}
err = os.Rename(filePath, newPath)
if err != nil {
fmt.Printf("Warning: could not rename %s\n!", filePath)
fmt.Println(err.Error())
}
}
return nil
}
func escapeChars(filename string) string {
switch runtime.GOOS {
default:
fallthrough
case "windows":
return escapeWindowsCharacters(filename)
case "linux":
return escapeLinuxChars(filename)
case "darwin":
return escapeWindowsCharacters(filename)
}
}
func escapeWindowsCharacters(filename string) string {
filename = strings.ReplaceAll(filename, "\\", "-")
filename = strings.ReplaceAll(filename, "/", "-")
filename = strings.ReplaceAll(filename, ":", " -")
filename = strings.ReplaceAll(filename, "*", "-")
filename = strings.ReplaceAll(filename, "?", "-")
filename = strings.ReplaceAll(filename, "\"", "-")
filename = strings.ReplaceAll(filename, "<", "")
filename = strings.ReplaceAll(filename, ">", "")
filename = strings.ReplaceAll(filename, "|", "-")
return filename
}
func escapeLinuxChars(filename string) string {
return strings.ReplaceAll(filename, "/", " ")
}
func escapeMacChars(filename string) string {
filename = strings.ReplaceAll(filename, ":", " -")
filename = strings.ReplaceAll(filename, "/", " ")
return filename
}