Skip to content

Commit be1fd63

Browse files
committed
metadata: refactor cont
1 parent 3106d87 commit be1fd63

File tree

3 files changed

+71
-84
lines changed

3 files changed

+71
-84
lines changed

file.go

+6-12
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,18 @@ import (
1212
"github.com/jamlib/audioc/metadata"
1313
)
1414

15-
func (a *audioc) processFile(index int) (*metadata.Metadata, error) {
16-
i := &metadata.Info{}
17-
18-
// if --artist mode, artist is set from flag
19-
if a.Config.Artist != "" {
20-
i.Artist = a.Config.Artist
21-
}
15+
func (a *audioc) InfoFromConfig(index int) *metadata.Info {
16+
i := &metadata.Info{ Artist: a.Config.Artist, Album: a.Config.Album }
2217

2318
// if --collection mode, artist set from parent folder name
2419
if a.Config.Collection {
2520
i.Artist = strings.Split(a.Files[index], fsutil.PathSep)[0]
2621
}
2722

28-
// if --album mode, album is set from flag
29-
if a.Config.Album != "" {
30-
i.Album = i.MatchCleanAlbum(a.Config.Album)
31-
}
23+
return i
24+
}
3225

26+
func (a *audioc) processFile(index int) (*metadata.Metadata, error) {
3327
m := metadata.New(a.Files[index])
3428

3529
// info from embedded tags within audio file
@@ -38,7 +32,7 @@ func (a *audioc) processFile(index int) (*metadata.Metadata, error) {
3832
return m, err
3933
}
4034

41-
m.Info, m.Match = m.Info.MatchBestInfo(i,
35+
m.Info, m.Match = m.MatchBestInfo(a.InfoFromConfig(index),
4236
metadata.ProbeTagsToInfo(d.Format.Tags))
4337

4438
// skip if sources match (unless --force)

metadata/metadata.go

+58-66
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ type Info struct {
2323
Disc, Track, Title string
2424
}
2525

26-
// Ffprober within context of metadata only needs to implement GetData
27-
type Ffprober interface {
28-
GetData(filePath string) (*ffprobe.Data, error)
29-
}
30-
3126
// filePath used to derive info
3227
func New(filePath string) *Metadata {
3328
var file string
@@ -51,11 +46,63 @@ func New(filePath string) *Metadata {
5146

5247
// build info from ffprobe.Tags
5348
func ProbeTagsToInfo(p *ffprobe.Tags) *Info {
54-
i := &Info{ Artist: p.Artist, Disc: p.Disc,
55-
Track: p.Track, Title: p.Title }
49+
return &Info{ Artist: p.Artist, Album: p.Album, Disc: p.Disc, Track: p.Track,
50+
Title: p.Title }
51+
}
52+
53+
// compare file & path info against ffprobe.Tags info and combine into best
54+
// return includes boolean if info sources match (no update necessary)
55+
func (m *Metadata) MatchBestInfo(c, p *Info) (*Info, bool) {
56+
// set custom artist
57+
if len(c.Artist) > 0 {
58+
m.Info.Artist = c.Artist
59+
}
60+
61+
// set custom album
62+
if len(c.Album) > 0 {
63+
m.Info.Album = c.matchCleanAlbum(c.Album)
64+
}
5665

57-
i.Album = strings.TrimSpace(i.MatchCleanAlbum(p.Album))
58-
return i
66+
// compare using safeFilename since info is derived from filename
67+
compare := p
68+
compare.Album = safeFilename(compare.Album)
69+
compare.Title = safeFilename(compare.Title)
70+
71+
// pull info from album
72+
p.Album = p.matchCleanAlbum(p.Album)
73+
74+
if *m.Info != *compare {
75+
if len(m.Info.Artist) == 0 {
76+
m.Info.Artist = p.Artist
77+
}
78+
if len(m.Info.Year) == 0 {
79+
m.Info.Year = p.Year
80+
}
81+
if len(m.Info.Month) == 0 {
82+
m.Info.Month = p.Month
83+
}
84+
if len(m.Info.Day) == 0 {
85+
m.Info.Day = p.Day
86+
}
87+
if len(m.Info.Disc) == 0 {
88+
m.Info.Disc = regexp.MustCompile(`^\d+`).FindString(p.Disc)
89+
}
90+
if len(m.Info.Track) == 0 {
91+
m.Info.Track = regexp.MustCompile(`^\d+`).FindString(p.Track)
92+
}
93+
// take longer album if custom album not set
94+
if len(m.Info.Album) == 0 || (len(c.Album) == 0 && len(p.Album) > len(m.Info.Album)) {
95+
m.Info.Album = p.Album
96+
}
97+
// take longer title
98+
if len(m.Info.Title) < len(p.Title) {
99+
m.Info.Title = p.Title
100+
}
101+
102+
return m.Info, false
103+
}
104+
105+
return m.Info, true
59106
}
60107

61108
// derive info album info from nested folder path
@@ -66,7 +113,7 @@ func (m *Metadata) fromPath(p string) {
66113
// start inner-most folder, work out
67114
foundAlbum := false
68115
for x := len(sa)-1; x >= 0; x-- {
69-
sa[x] = m.Info.MatchCleanAlbum(sa[x])
116+
sa[x] = m.Info.matchCleanAlbum(sa[x])
70117

71118
if sa[x] != "" {
72119
// only overwrite album if not yet set
@@ -103,7 +150,7 @@ func (m *Metadata) fromFile(s string) {
103150
m.Info.Title = matchAlbumOrTitle(s)
104151
}
105152

106-
func (i *Info) MatchCleanAlbum(s string) string {
153+
func (i *Info) matchCleanAlbum(s string) string {
107154
s = i.matchDiscOnly(s)
108155
s = i.matchDate(s)
109156
s = i.matchYearOnly(s)
@@ -143,61 +190,6 @@ func (i *Info) ToFile() string {
143190
return out + safeFilename(i.Title)
144191
}
145192

146-
// compare file & path info against ffprobe.Tags info and combine into best
147-
// return includes boolean if info sources match (no update necessary)
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-
159-
// compare using safeFilename since info is derived from filename
160-
compare := p
161-
compare.Album = safeFilename(compare.Album)
162-
compare.Title = safeFilename(compare.Title)
163-
164-
if *i != *compare {
165-
// info overrides probe for Artist
166-
if len(p.Artist) == 0 || len(c.Artist) > 0 {
167-
p.Artist = i.Artist
168-
}
169-
170-
// update Year, Month, Day, Disc, Track if not set
171-
if len(p.Year) == 0 {
172-
p.Year = i.Year
173-
}
174-
if len(p.Month) == 0 {
175-
p.Month = i.Month
176-
}
177-
if len(p.Day) == 0 {
178-
p.Day = i.Day
179-
}
180-
if len(p.Disc) == 0 || regexp.MustCompile(`^\d+`).FindString(p.Disc) != p.Disc {
181-
p.Disc = i.Disc
182-
}
183-
if len(p.Track) == 0 {
184-
p.Track = i.Track
185-
}
186-
187-
if (len(p.Album) < len(i.Album)) || len(c.Album) > 0 {
188-
p.Album = i.Album
189-
}
190-
191-
if len(p.Title) < len(i.Title) {
192-
p.Title = i.Title
193-
}
194-
195-
return p, false
196-
}
197-
198-
return i, true
199-
}
200-
201193
// converts roman numeral to int; only needs to support up to 5
202194
var romanNumeralMap = map[string]string{
203195
"I": "1", "II": "2", "III": "3", "IV": "4", "V": "5",

metadata/metadata_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,35 @@ func TestToFile(t *testing.T) {
5151

5252
func TestMatchProbeInfo(t *testing.T) {
5353
tests := []struct {
54-
info, comb *Info
54+
m *Metadata
55+
comb *Info
5556
tags *ffprobe.Tags
5657
match bool
5758
}{
58-
{ info: &Info{ Album: "Kean College After Midnight", Title: "After Midnight" },
59+
{ m: &Metadata{Info: &Info{ Album: "Kean College After Midnight", Title: "After Midnight" }},
5960
tags: &ffprobe.Tags{ Album: "Something Else" },
6061
comb: &Info{ Album: "Kean College After Midnight", Title: "After Midnight" },
6162
match: false,
6263
},{
63-
info: &Info{ Album: "Kean College After Midnight", Year: "1980" },
64+
m: &Metadata{Info: &Info{ Album: "Kean College After Midnight", Year: "1980" }},
6465
tags: &ffprobe.Tags{ Album: "1980 Kean College After Midnight" },
6566
comb: &Info{ Album: "Kean College After Midnight", Year: "1980" },
6667
match: true,
6768
},{
68-
info: &Info{ Album: "Kean College After Midnight", Year: "1980" },
69+
m: &Metadata{Info: &Info{ Album: "Kean College After Midnight", Year: "1980" }},
6970
tags: &ffprobe.Tags{ Album: "1980.02.28 Kean College After Midnight" },
7071
comb: &Info{ Album: "Kean College After Midnight", Year: "1980", Month: "02", Day: "28" },
7172
match: false,
7273
},{
73-
info: &Info{ Disc: "1" },
74+
m: &Metadata{Info: &Info{ Disc: "1" }},
7475
tags: &ffprobe.Tags{ Disc: "1/2" },
7576
comb: &Info{ Disc: "1" },
7677
match: false,
7778
},
7879
}
7980

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

8485
if *rInfo != *tests[x].comb {

0 commit comments

Comments
 (0)