Skip to content

Commit 3106d87

Browse files
committed
metadata: refactor New & MatchBestInfo
1 parent dd1eefa commit 3106d87

File tree

4 files changed

+30
-42
lines changed

4 files changed

+30
-42
lines changed

bundle.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func (a *audioc) skipFolder(path string) bool {
112112
}
113113
} else {
114114
// derive metadata from album folder and see if it matches
115-
m := metadata.New(alb, nil)
115+
m := metadata.New(alb)
116116
if m.Info.ToAlbum() == alb {
117117
return true
118118
}
@@ -144,7 +144,7 @@ func (a *audioc) processArtwork(file string) error {
144144
// files to this location. {Info.title} is currently not used, only disc/track.
145145
func mergeFolderFunc(f string) (int, string) {
146146
// use metadata to obtain info from filename
147-
m := metadata.New(f, nil)
147+
m := metadata.New(f)
148148

149149
disc, _ := strconv.Atoi(regexp.MustCompile(`^\d+`).FindString(m.Info.Disc))
150150
track, _ := strconv.Atoi(regexp.MustCompile(`^\d+`).FindString(m.Info.Track))

file.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@ func (a *audioc) processFile(index int) (*metadata.Metadata, error) {
3030
i.Album = i.MatchCleanAlbum(a.Config.Album)
3131
}
3232

33-
m := metadata.New(a.Files[index], i)
33+
m := metadata.New(a.Files[index])
3434

35-
// call Probe after setting m.Info.Artist
36-
err := m.Probe(a.Ffprobe, filepath.Join(a.Config.Dir, a.Files[index]))
35+
// info from embedded tags within audio file
36+
d, err := a.Ffprobe.GetData(filepath.Join(a.Config.Dir, a.Files[index]))
3737
if err != nil {
3838
return m, err
3939
}
4040

41+
m.Info, m.Match = m.Info.MatchBestInfo(i,
42+
metadata.ProbeTagsToInfo(d.Format.Tags))
43+
4144
// skip if sources match (unless --force)
4245
if m.Match && !a.Config.Force {
4346
m.Resultpath = a.Files[index]
@@ -51,6 +54,7 @@ func (a *audioc) processFile(index int) (*metadata.Metadata, error) {
5154
if a.Config.Collection ||
5255
(len(fpa) > 2 && fpa[0] == m.Info.Artist && fpa[1] == m.Info.Year) {
5356

57+
// override with custom resultpath
5458
m.Resultpath = filepath.Join(m.Info.Artist, m.Info.Year)
5559
}
5660

metadata/metadata.go

+17-34
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,9 @@ type Ffprober interface {
2929
}
3030

3131
// filePath used to derive info
32-
func New(filePath string, i *Info) *Metadata {
33-
// create info if not passed as arg
34-
if i == nil {
35-
i = &Info{}
36-
}
37-
38-
// create new metadata
32+
func New(filePath string) *Metadata {
3933
var file string
40-
m := &Metadata{ Filepath: filePath, Info: i }
34+
m := &Metadata{ Filepath: filePath, Info: &Info{} }
4135

4236
// derive file info if has file extension
4337
if regexp.MustCompile(`\.[A-Za-z0-9]{1,5}$`).FindString(filePath) != "" {
@@ -56,11 +50,11 @@ func New(filePath string, i *Info) *Metadata {
5650
}
5751

5852
// build info from ffprobe.Tags
59-
func probeTagsToInfo(p *ffprobe.Tags) *Info {
53+
func ProbeTagsToInfo(p *ffprobe.Tags) *Info {
6054
i := &Info{ Artist: p.Artist, Disc: p.Disc,
6155
Track: p.Track, Title: p.Title }
6256

63-
i.Album = i.MatchCleanAlbum(p.Album)
57+
i.Album = strings.TrimSpace(i.MatchCleanAlbum(p.Album))
6458
return i
6559
}
6660

@@ -109,24 +103,6 @@ func (m *Metadata) fromFile(s string) {
109103
m.Info.Title = matchAlbumOrTitle(s)
110104
}
111105

112-
func (m *Metadata) Probe(ffp Ffprober, fp string) error {
113-
// info from embedded tags within audio file
114-
d, err := ffp.GetData(fp)
115-
if err != nil {
116-
return err
117-
}
118-
119-
// if artist not yet specified, use ffprobe artist tag
120-
if m.Info.Artist == "" {
121-
m.Info.Artist = d.Format.Tags.Artist
122-
}
123-
124-
// combine info w/ embedded tags
125-
m.Info, m.Match = m.Info.matchProbeInfo(probeTagsToInfo(d.Format.Tags))
126-
127-
return nil
128-
}
129-
130106
func (i *Info) MatchCleanAlbum(s string) string {
131107
s = i.matchDiscOnly(s)
132108
s = i.matchDate(s)
@@ -169,15 +145,25 @@ func (i *Info) ToFile() string {
169145

170146
// compare file & path info against ffprobe.Tags info and combine into best
171147
// return includes boolean if info sources match (no update necessary)
172-
func (i *Info) matchProbeInfo(p *Info) (*Info, bool) {
148+
func (i *Info) MatchBestInfo(c, p *Info) (*Info, bool) {
149+
// set custom artist
150+
if len(c.Artist) > 0 {
151+
i.Artist = c.Artist
152+
}
153+
154+
// set custom album
155+
if len(c.Album) > 0 {
156+
i.Album = c.Album
157+
}
158+
173159
// compare using safeFilename since info is derived from filename
174160
compare := p
175161
compare.Album = safeFilename(compare.Album)
176162
compare.Title = safeFilename(compare.Title)
177163

178164
if *i != *compare {
179165
// info overrides probe for Artist
180-
if p.Artist != i.Artist {
166+
if len(p.Artist) == 0 || len(c.Artist) > 0 {
181167
p.Artist = i.Artist
182168
}
183169

@@ -198,13 +184,10 @@ func (i *Info) matchProbeInfo(p *Info) (*Info, bool) {
198184
p.Track = i.Track
199185
}
200186

201-
// TODO: use album of source that derived the most info
202-
p.Album = strings.TrimSpace(p.Album)
203-
if len(p.Album) < len(i.Album) {
187+
if (len(p.Album) < len(i.Album)) || len(c.Album) > 0 {
204188
p.Album = i.Album
205189
}
206190

207-
// use the longer title
208191
if len(p.Title) < len(i.Title) {
209192
p.Title = i.Title
210193
}

metadata/metadata_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ func TestMatchProbeInfo(t *testing.T) {
7878
}
7979

8080
for x := range tests {
81-
rInfo, match := tests[x].info.matchProbeInfo(probeTagsToInfo(tests[x].tags))
81+
rInfo, match := tests[x].info.MatchBestInfo(&Info{},
82+
ProbeTagsToInfo(tests[x].tags))
8283

8384
if *rInfo != *tests[x].comb {
8485
t.Errorf("Expected %v, got %v", rInfo, tests[x].comb)
@@ -98,7 +99,7 @@ func TestInfoFromFile(t *testing.T) {
9899
}
99100

100101
for x := range tests {
101-
m := New(tests[x][0][0], nil)
102+
m := New(tests[x][0][0])
102103
compare := []string{ m.Info.Year, m.Info.Month, m.Info.Day,
103104
m.Info.Disc, m.Info.Track, m.Info.Title }
104105
if strings.Join(compare, "\n") != strings.Join(tests[x][1], "\n") {
@@ -118,7 +119,7 @@ func TestInfoFromPath(t *testing.T) {
118119
}
119120

120121
for x := range tests {
121-
m := New(tests[x][0][0], nil)
122+
m := New(tests[x][0][0])
122123
compare := []string{ m.Info.Year, m.Info.Month, m.Info.Day, m.Info.Album }
123124
if strings.Join(compare, "\n") != strings.Join(tests[x][1], "\n") {
124125
t.Errorf("Expected %v, got %v", tests[x][1], compare)

0 commit comments

Comments
 (0)